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
... Why cginclude? What is CG? Well, in my understanding, CG is a cross-platform language in the Shader Language. As we all know, the glsl language works on the OpenGL interface, while the HLSL language works on the DirectX interface, and the Cg language can use these two different APIs, you do not need to consider Platform issues. Of course, there are many performance problems related to the platform. Unity allows CG code snippets to be embedded in the shader and then compiled into OpenGL, DirectX, Flash, and so on, so that our shader can work on different platforms. The use of cginclude allows us to reuse code and implement the shader modularization.
In fact, we have used a series of built-in cginclude of unity to write surface shader without knowing it. Do you still remember Lambert and blingphony lighting functions? This is the CG clip written for us in advance using unity. Understanding and writing our own cginclude file helps us to modify shader faster and more conveniently. Before writing our own files, we should first learn which built-in lighting models, functions, and state variables Unity provides for us.
Surface shader is the pride of unity, which saves us a lot of code. The important reason is that it has done a lot of work behind the scenes. We can find the code in Editor/data/cgincludes (content/cgincludes in MAC. Some of these files are responsible for shadow and illumination, some are auxiliary functions, and some are responsible for platform dependencies. Without them, it would be even harder to write our shader.
You can find some information provided by unity in this link.
The following uses the auxiliary functions in the unitycg. cginc file to formally understand the built-in cginclude file.
Preparations
- Create a new scene and a sphere, and add a parallel light.
- Create a new shader and material, which can be named helperfunctionshader.
- Assign shader to material and assign material to sphere.
- Finally, open the unitycg. cginc file so that we can view the specific implementation of these auxiliary functions.
Implementation
- Add the following new properties in the properties block. We need a texture and a color control slider:
Properties {_MainTex ("Base (RGB)", 2D) = "white" {}_DesatValue ("Desaturate", Range(0, 1)) = 0.5}
- Add a reference to the new property in cgprogram:
sampler2D _MainTex;fixed _DesatValue;
Note:: The fixed type can only be used for floating point values ranging from 0 to 1 ~
- Finally, modify the surf function. It uses a function we have never seen before-lerp and luminance. the luminance function is defined in 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 de-Color slider, you can get the following effect (from left to right de-color values correspond to 0.0, 0.5, 1.0 respectively ):
Explanation
By using the built-in auxiliary function luminance (), we can quickly get a de-color effect, or achieve the grayscale of shader. These are because the unitycg. cginc file automatically contains the relevant code for our shader.
If you search for the unitycg. cginc file, you can find 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 will automatically compile the code, we can use these functions directly in the shader.
Another function, lerp (), is a function of CG, which is described in detail on the official website.
If you are careful, you can find another file named lighting. cginc in the cginclude folder (not much code ). This file contains all the lighting models we use in statements like # pragma surface surf Lambert.
[Unity shaders] Use cginclude to modularize your shader-built-in unity cginclude File