"Unity Shaders" Learning Note--surfaceshader (i) Understanding structure
Reprint Please specify source: http://www.cnblogs.com/-867259206/p/5595747.html
This series of articles was written using Unity5.3.
Before writing the code:
Of course, if unity wasn't installed, you wouldn't learn unity shaders, would it?
Before reading this series of articles you need to have some programming concepts.
In VS, Unity shaders does not have syntax highlighting and smart hints, and the VS party can refer to this article to make the code highlighted, shaderlabvs
or NShader
a plugin such as download or the like to highlight the code.
This is the basic knowledge of the unity shaders for small white, and if you have a foundation or you are a great God, then these articles are not for you.
Due to the limitation of the author's level, there may be some fallacy in the text, and I implore to point out.
Now we're going to officially start writing unity shaders.
1. 新建Unity Project。2. 在Assets文件夹下新建三个文件夹:Materials、Shaders、Textures。3. 在Shaders文件夹下右键,Create-Shader-Standard Surface Shader,命名为MySurfaceShader。4. 在Materials文件夹下新建Material,命名为MySurfaceShader,将它的Shader改为新建的Shader。5. 新建一个Cube,将新建的材质赋给它。
If you are using a version below 5.3, your right-click menu will not have so many shader, just create it. The shader code you create directly with standard surface shader is a little more streamlined than standard surface shader, and you'll understand when you hear the explanation.
There are four kinds of Shader in the right-click menu, namely Standard Surface Shader, unlit Shader, Imageeffect Shader, Compute Shader.
unlit Shader and Imageeffect Shader are all vertex&fragment shader,compute Shader in a previous article.
Let's open the Mysurfaceshader and have a look.
Shader"Custom/mysurfaceshader"{Properties {_color ("Color", Color) = (1,1,1,1) _maintex ("Albedo (RGB)", 2D) ="White"{} _glossiness ("Smoothness",Range(0,1)) = 0.5 _metallic ("Metallic",Range(0,1)) = 0.0} subshader {Tags {"Rendertype"="Opaque"} LOD Cgprogram//Physically based standard lighting model, and enable Shadows on all light types#pragma surface surf Standard fullforwardshadows//Use Shader Model 3.0 target, to get nicer looking lighting#pragma target 3.0 sampler2d _maintex; structInput{FLOAT2 Uv_maintex; }; Half _glossiness; Half _metallic; Fixed4 _color; void Surf (Input inch, InOut surfaceoutputstandard o) {//Albedo comes from a texture tinted by colorFixed4 C = tex2d (_maintex,inch. Uv_maintex) * _COLOR; O.albedo = C.rgb;//metallic and smoothness come from slider variablesO.metallic = _metallic; o.smoothness = _glossiness; O.Alpha= C.A; } ENDCG} FallBack"Diffuse"}
-
-
-
Note that there is no semicolon.
This is an introduction to the properties of the Unity website. The properties types supported by the
Unity are as follows:
Color :颜色 Inspector面板里会有一个选取颜色的pickerRange :某范围内的浮点数 Inspector面板里会出现一个Slider2D :2D纹理Rect :创建非2次方边长的纹理Cube :创建Cube Map,也是一种纹理Float :浮点数,和Range的区别就是Range是一个范围内的数,Float是单个数Vector:向量 定义一个四元素的容器
Subshader is a child shader. tags{} is a label. LOD is the abbreviation for level of detail, which is discussed later.
Cgprogram and ENDCG inside are CG programs.
#pragma surface surf Standard fullforwardshadows
Surface description is surface shader, which can be replaced by vertex or fragment,surf is a cosmetic function, and there is a surf function underneath the code snippet. Standard Fullforwardshadows is a light model function.
#pragma target 3.0
Sets the version of the compilation target shader model.
The thing in the back surf function is the point.
On the top of the surf function, define the variables in the properties with the same name again. There is also an input struct, which is the entry structure.
Let's see what the surf function does.
Define a 4-yuan fixed-point number c,tex2d is a texture sampling function, the first parameter is a texture, the second parameter is the UV coordinate, and the return value of the function is multiplied by the color. o This variable is the variable that the shader will output, which is the variable that stores the effect our eyes will see. The following statement means assigning each value to a different value in O. RGB is the RGB color, albedo is the reflection value, Metalic is a metal value, smoothness is a smooth value, alpha is a transparent channel. In fact, good English will be able to understand the meaning of these variables. (Forgive my translation)
If the version is below 5.3, the type of the O variable is surfaceoutput, the two types mean the same, but different structures.
In the case of the standard fullforwardshadows lighting model, the Surfaceoutputstandard is applied to the application, and if it is a Lambert illumination model (other versions of Unity's default lighting model), the surfaceoutput is appropriate.
This is the basic structure of a surface shader program.
In the next article I will describe in detail the types of CG and the two structures of input and surfaceoutput.
"Unity Shaders" Learning Note--surfaceshader (i) Understanding structure