Unity3D rendering important programming - shader programming.

Unity3D rendering important programming - shader programming.

Date: 2017-11-09
Views: 71

As is known to all, all rendering work of Unity3D is inseparable from the Shader, namely Shader. It is actually a small program that is responsible for combining the input Mesh in a specified manner with the input map or color, and then the output. The drawing unit can draw the image to the screen based on this output. The input map or color, plus the corresponding Shader, and the specific parameter Settings for Shader, will be packaged and stored together, resulting in a Material. After that, we can render the material to the appropriate renderer. In the end, Shader is a program that specifies the points and colors that can be read by the input color map and the output renderer.

1. Type of Shader.

Shader generally falls into three categories:

1. Surface Shader -- Surface Shader.

Contains illumination calculations, 3D scenes.

2. Vertex and Fragment Shader -- Vertex/Fragment Shader.

Does not include lighting operation, 2D scene, special effects.

3. Fixed Function Shader -- Fixed Function Shader.

Is not recommended


Vertex/fragment shader.

2. Basic structure of Shader.

Open the newly created Shader with a text editor:



The attribute Properties {}

Example: _Name(" Display Name ", type) = defaultValue[{options}]

_Name: variable name.

Display Name: Display Name.

Type: there are several types.


 DefaultValue: the defaultValue corresponds to the type.


Options: optional

2D/Rect/Cube -- the TexGen mode of OpenGL, at least {}


Define a Color with a default value of translucent blue (" MainColor ", Color) = (0, 0, 1, 0.5)

Define a default value of pure white Texture Main Texture _MainTex (" Main Texture ", 2D) = "white" {}

SubShader SubShader.

1. Tags

Tags {"TagName1" = "Value1" "TagName2" = "Value2"}

Role: control the rendering engine "when" and "how" to render the subshader content?

Render type:

Tags {" RenderType "=" Opaque "} // render an Opaque object with output.

Tags {" RenderType "=" Transparent "// render outputs Transparent objects.

Render order queue:

Tags {" Queue "=" Transparent "// specify the order Queue to render Transparent objects.

2. Detail level LOD.

LOD: the Level of Detail

The larger the number, the higher the detail, the greater the consumption.

"LOD 100" is a very low value that is the same as the LOD value of the VertexLit (vertex light) Shader.


Tag CG program

The implementation language is Microsoft's HLSL language (most of which is the same as NVIDIA's CG language)

4, # pragma

Role: indicates that the compilation corresponds to the Shader function.

The #pragma vertex vert // function is named vertex Shader of vert.

#pragma fragment frag // function name fragment Shader named frag.

5, sampler2D

Sampler2D is a data container interface that is bound to the texture, which is the type of 2D textures in CG/HLSL.

There are also sampler1D, sampler3D, samplerCUBE and other formats.

Note: because the CG program needs to access the variables of the properties (properties), the variables in the CG program must be declared with the same name as the previous variable.

6, the include

Role: import Unity contains files for Shader predefined variables and functions.

#include: "UnityCG. Cginc" // import Unity universal CG predefined files.

7. Appdata and v2f.

Appdata: data entered into Vertex Shader.

V2f: output from Vertex Shader, input to Fragment Shader data.

"Float4" defines a four-dimensional floating-point vector with a floating-point precision of 32-bit.

": POSITION" is the "semantics" of the Shader parameter, representing the spatial coordinates of Vertex.

"SV_POSITION" describes the pixel location of Vertex.

8. Vert and frag.

UnityObjectToClipPos() // convert the object space of Vertex to the clipping space of the camera (screen 2D coordinate)

"Fixed4" defines a four-dimensional floating-point vector with a floating-point precision of 11-bit and range from -2.0 to +2.0.

"SV_Target" describes pixel pixel to Color.

"Return fixed4(1,0,0,1)" indicates that the final frag returns a pure red pixel.