"Unity Shaders" uses Cginclude to make your shader modular--unity built-in cginclude files

Source: Internet
Author: User

This series is the main reference to Unity Shaders and Effects Cookbook (thanks to the original book author), at the same time will add a little personal understanding or expansion.

Here are all the illustrations of this book.

Here is the code and resources required for this book (and of course you can download it from the website).

========================================== Cutting Line ==========================================



write in front


La La La, another new chapter opened ... Why do you speak cginclude? What is CG? Woo, according to my understanding is CG is shader language inside of cross-platform language. It is well known that the GLSL language works on the OpenGL interface, while the HLSL language works on the DirectX interface. The CG language is able to use these two different APIs without having to consider platform issues. There are, of course, very many platform-related performance issues.

Unity agrees to embed the CG snippet in the shader and then compile it into OpenGL, DirectX, Flash, and so on. So that our shader work on different platforms. The use of cginclude allows us to reuse the code to achieve shader modularity.


In fact, we've previously unconsciously used a series of unity built-in cginclude to write surface Shader.

Do you remember Lambert, Blingphony light function? This is a CG fragment that was written in advance for us using unity.

Understand and write our own cginclude files. Helps us to change shader faster and more easily.

Before writing your own files, let's start by learning what built-in lighting models, functions, and state variables are provided to us by unity.


Surface Shader is the pride of unity, which saves us a lot of code, which is very important because it does a lot of work for us behind the scenes. We were able to find this code under Editor/data/cgincludes (Mac is content/cgincludes). Some of these files are responsible for shadows and lighting. Some are auxiliary functions, and some are responsible for platform dependencies. Assuming without them, our shader will become more laborious to write.


You can find some of the information that unity provides in this link.


The following is a formal understanding of the built-in cginclude files by using the auxiliary functions of the polygons in the Unitycg.cginc file.



preparatory work


    1. Create a new scene and a sphere, adding a parallel light.
    2. Create a new shader and material that can be named Helperfunctionshader.
    3. Assign shader to material and give material to the sphere.
    4. Finally, open the Unitycg.cginc file. So that we can see the specific implementations of these auxiliary functions.

Implement

  1. Add the following new properties to the properties block. We need a texture and a control de-color slider:
    Properties {_maintex ("Base (RGB)", 2D) = "White" {}_desatvalue ("Desaturate", Range (0, 1)) = 0.5}

  2. Add a reference to the new attribute above in Cgprogram:
    Sampler2d _maintex;fixed _desatvalue;

    Note : The range of the fixed type is-2.0 to +2.0. Accuracy is 1/256, more than the lost Oh!

  3. Finally, change the surf function. It uses a function--lerp and luminance that we have not seen before. The luminance function is defined inside the unitycg.cginc. And Lerp is a function of CG.
    void Surf (Input in, InOut surfaceoutput o) {half4 c = tex2d (_maintex, In.uv_maintex); O. Albedo = Lerp (C.rgb, Luminance (C.RGB), _desatvalue), O. Alpha = C.A;}

Finally, by changing the size of the Color slider bar. You will be able to get the following effects (from left to right, respectively 0.0,0.5,1.0):


explain
we use the built-in helper function luminance () to get a de-color effect at high speed, or to achieve shader grayscale. All of this is due to the Unitycg.cginc file's own initiative for our shader including the relevant code.
assuming you search for the Unitycg.cginc file, you can find examples such as the following code:
Converts color to luminance (grayscale) inline fixed luminance (fixed3 c) {return dot (c, fixed3 (0.22, 0.707, 0.071));}

Since unity compiles the code on its own initiative, we are able to use these functions directly in shader.


There is also a function lerp (), which is a function of CG. The official website has a specific function description.


If you are careful, you can find a file--lighting.cginc (not much code) in the Cginclude directory. This file includes all of us on similar #pragma surface surf Lambert The lighting model used in this declaration.






"Unity Shaders" uses Cginclude to make your shader modular--unity built-in cginclude files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.