[Unity shaders] Use cginclude to modularize your shader-create a cginclude file storage Lighting Model

Source: Internet
Author: User

This series mainly references the book "Unity shaders and effects cookbook" (thanks to the author of the original book) and adds a bit of personal understanding or expansion.

Here are all the illustrations in this book. Here is the code and resources required for this book (you can also download them from the official website ).

========================================================== ===================================================== =====



Preface


It is good to know the built-in cginclude file, but what if we want to create our own cginclude file to store the illumination model and helper functions?


The good news is that we can indeed create our own cginclude file. The bad news is that we need to know a little bit about the code syntax. Okay, let's get started!



Preparations


The good news is that this preparation work is finally a little different... The bad message is that I can no longer copy and paste it...

  1. First, create a new example file, such as mycginclude.txt.
  2. Then, change the file suffix to. cginc. Of course, the operating system will usually give you some warning information, saying that this file will become unavailable, but believe me, we are available.
  3. Import the new. cginc file to our unity Project (note that in my project, it is located in a new folder named cgincludes ). After compilation, we can see that unity has compiled the file as a cginclude file. As shown below:

Now, we are ready to create custom cginclude code. Double-click the cginclude file and open it in monodevelop ~

Implementation
Open the cginclude file and enter the following code.
  1. First, use the following preprocessing command to start our cginclude file. These statements are similar to # pragma and # include. Here, we want to define a new code set, as long as our surface shader contains this file in its compilation instructions, the code can be executed. Enter the following code at the beginning of the cginclude file:
    #ifndef MY_CG_INCLUDE#define MY_CG_INCLUDE

  2. Then, make sure that # ifndef or # ifdef has a # endif to end the definition check. Just like an if statement, two curly braces are required. Enter the following code in the # define command:
    #endif

  3. Next, we can fill in the remaining parts. Enter the following code:
    // Custom Build-in Variablesfixed4 _MyColor;// Lighting modelsinline fixed4 LightingHalfLambert (SurfaceOutput s, fixed3 lightDir, fixed atten) {fixed diff = max (0, dot (s.Normal, lightDir));diff = (diff + 0.5) * 0.5;fixed4 c;c.rgb = s.Albedo * _LightColor0.rgb * ((diff * _MyColor.rgb) * atten * 2);c.a = s.Alpha;return c;}

  4. The following is the complete mycginlcude. cginc file:
    #ifndef MY_CG_INCLUDE#define MY_CG_INCLUDE// Custom Build-in Variablesfixed4 _MyColor;// Lighting modelsinline fixed4 LightingHalfLambert (SurfaceOutput s, fixed3 lightDir, fixed atten) {fixed diff = max (0, dot (s.Normal, lightDir));diff = (diff + 0.5) * 0.5;fixed4 c;c.rgb = s.Albedo * _LightColor0.rgb * ((diff * _MyColor.rgb) * atten * 2);c.a = s.Alpha;return c;}#endif

    The above is equivalent to a header file, but it still takes some other steps to make full use of it. We need to tell the current shader that we want to use our own files and code.

  5. Returns the shader used in the previous section. We need to include our own cginclude file in the block, just as we need to add header file reference at the beginning of C ++. At the same time, our shader used the built-in Lambert illumination model, but now we want to use a custom half Lambert illumination model. Because we already include the cginclude file, we can directly specify this model in the # pragma command:
    CGPROGRAM#include "../CgIncludes/MyCgInclude.cginc"#pragma surface surf HalfLambert

    Explanation: The path of the. cginc file to the shader must be specified. That is to say, if it and shader are placed in the same folder, you can directly write the name. But in my project, shader is placed under the shaders folder, while. cginc is placed under the cgincludes folder, so the above method is required.

  6. Finally, do you still remember to declare a _ mycolor variable in the cginclude file? We also need to add this attribute in the shader properties:
    Properties {_MainTex ("Base (RGB)", 2D) = "white" {}_DesatValue ("Desaturate", Range(0, 1)) = 0.5_MyColor ("My Color", Color) = (1, 1, 1, 1)}


Finally, unity is returned. If a compilation error occurs and the. cginc file cannot be found, there is a problem with your location. You can change it by referring to the above explanation.
The final result is as follows. Note that unity has used our new half Lambert illumination model (which is used to highlight the brightness of the backlight) and added a new sample color. The left side is the previous result, and the right side is the result.
 


Explanation
When writing a shader, we can use the # include preprocessing command to include other code sets like using the header file in C ++. This tells unity that we want the current shader to use the code contained in these files. We actually include CG code snippets in the corresponding position.
Once we declare the # include command, unity can find the file in the project, and then unity will find the defined code snippets in the file. That is, we use the # ifndef command and # ifndef command. When we declare the # ifndef command, we are telling unity that if this name is not defined, we will use this name to define something! In this section, we want to go to # define my_cg_include. Therefore, if unity does not find a definition named my_cg_include, it will create it when compiling the cginclude file. # Ifndef tells unity that this definition is over here. You don't need to find the following one!
Now you see how powerful the custom cginclude file is (similar to the header file in C ++), we can use it to store all the custom lighting models, to reduce code duplication. Other benefits, such as flexibility, can be derived from the C ++ header file ~


[Unity shaders] Use cginclude to modularize your shader-create a cginclude file storage Lighting Model

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.