This series focuses on Unity Shaders and Effects Cookbook (thanks to the author of the book), plus a bit of personal understanding or outreach.
Here are all the illustrations of this book. Here is the code and resources required for this book (you can also download it from the website, of course).
========================================== Split Line ==========================================
This time the work is very simple, we are mainly to understand the unity shaders basic work principle.
get ready to work create a new unity Project in the assets directory with a new folder: Shaders creates a new folder under the Assets directory: Materials
implementation under the Shaders folder, right-click to create a new shader, named Basicdiffuse, and open it; Under the Materials folder, make a material, And using the Basicdiffuse.shader we created earlier, create a sphere and drag the material you created earlier onto it to see the effect; You can also add a texture to material.
After explaining the opening of the Basicdiffuse.shader, we can see that unity has actually written a lot of code for us:
Shader "Custom/basicdiffuse" {
Properties {
_maintex ("Base (RGB)", 2D) = "White" {}
}
subshader {
Tags {"Rendertype" = "Opaque"}
LOD
cgprogram
#pragma surface surf Lambert sampler2d _maintex
;
struct Input {
float2 uv_maintex;
};
void Surf (Input in, InOut surfaceoutput o) {
Half4 c = tex2d (_maintex, In.uv_maintex);
O.albedo = C.rgb;
O.alpha = C.A;
}
ENDCG
}
FallBack "diffuse"
}
The first line shows the path of the shader in Unity, which appears in the drop-down list when you select a material shader, and can be changed at any time, and the last line indicates that when this shader fails to run in the current environment, Unity's own diffuse Shader will be called by default, and the rest will be covered in the following chapters.
Unity uses CG to achieve this, it encapsulates these implementation details, provides us with a component-based method of writing shader, such as adjusting the image of the UV coordinates, matrix conversion, etc. it helps you do it. Previously, we needed to rewrite the code for some basic functionality to create a shader from scratch. As your experience becomes richer, you will naturally want to understand how unity handles the work of the image Processing Unit (GPU). If you want to know how unity calls CG, you can see it in Unity's installation directory unity4\editor\data\cgincludes. In some of the sections that follow.