Unity surfaceshader Start Programming
at the beginning of 14, I had set myself the three goals to be achieved this year. One of them is to learn to write their own shader and to invest in practical project applications. Now, a blink of an eye calendar has turned to June, and his study on shader, but also stay in the three days of fishing two days fragmented learning state, his heart is very anxious. Usually the task is many, squeezed out of their own study time is one aspect.
But more important. Or because he didn't make a steady plan. To supervise the content of learning by using all the time that can be used. On the contrary, I have done too much.
So. Continue to take the form of blog posts to urge their own study, the shader learn about the drip all recorded. and develop their own learning schedule, in the specified time node, must complete the content of the plan. Taking this step is a beginning--unity shaderlab basic Learning record.
Unity uses its own defined shaderlab to organize the content of shader. and compiled for different platforms. In Unity's own defined Shaderlab, the surfaceshader structure encapsulates a very complex shader that can handle different lighting and shading, while still working under Unity's two render paths (forward and deferred). The basic structure of surfaceshader, as seen below:
Shader "Custom/newshader" {
Properties {
Some properties, the shader data interface
}
Subshader {
Subshader packaged a rendering scheme. This is our main position when we write shader.
}
Subshader {
Subshader can have more than one. This is to allow us to write the shader adapted to different graphics cards
}
FallBack "Diffuse"
}
All the attributes that we can define in the properties block, such as the following:
Properties {
_texture ("Texture", 2D) = property in the form of "white" {}//Picture (texture)
_color ("Color", color) = (1, 1, 1, 1)//Color properties
_cube ("3D Map", Cube) = "White" {}//3d texture, 6 images required
_vector ("Vector4", vector) = (1, 1, 1, 1)//4 elements of a vector
_float ("float", float) = 1.0//floating-point number
_range ("Range", Range ( -10,10)) = 1.0//limit range of floating-point numbers
}
In the structure provided by Unity's Shaderlab, the logical code portion of the shader. are located in the GLSLPROGRAM-ENDGLSL section of Subshader (GLSL language). or the CGPROGRAM-ENDCG section (CG/HLSL language), we are able to choose different shader languages to write.
The attributes defined in the properties block above must be declared again in the logical code block (that is, the GLSLPROGRAM-ENDGLSL or CGPROGRAM-ENDCG section).
The way they are declared in CG code is as follows:
Cgprogram
Sampler2d _texture;
FLOAT4 _color;
Samplercube _cube;
FLOAT4 _vector;
float _float;
float _range;
... ...
Endcg
It is important to note that the name of the variable declared in the CG code block must be consistent with the name of the variable in the properties block. Otherwise, an error is indicated.
In summary, the complete code for a surfaceshader is:
Shader "Custom/newshader" {
Properties {
_maintex ("Texture", 2D) = property in the form of "white" {}//Picture (texture)
}
Subshader {
Tags {"Rendertype" = "Opaque"}
LOD 200
Cgprogram
#pragma surface surf Lambert
Sampler2d _maintex;
struct Input {
FLOAT2 Uv_maintex;
};
void Surf (Input in, Inoutsurfaceoutput o) {
Half4 C = tex2d (_maintex, In.uv_maintex);
O.albedo = C.rgb;
O.alpha = C.A;
}
Endcg
}
FallBack "Diffuse"
}
This is also the creation of the shader default template in unity.
About this default shader some content templates, I record the next one will be analyzed again. Here's the first post:)
Unity Surfaceshader Start programming