Cg=c for graphics C-language superset for computer graphics programming
Prerequisite Knowledge Points:
1.CG code must be used
Cgprogram
。。。
ENDCG, surround it.
2. The main function name of the vertex shader and fragment shader can be arbitrary, but it needs to be #pragma vert and #pragma fragment and fully match the main function name, shader will find the portal
3.FLOAT4 is a compressed array, float4 vert and float vert[4] are different in strict sense, although all are stored 4 float, but float4 as a vector type do point multiplication, internal product processing and other high-speed
4. Semantics: Variables in addition to variable names and data types, but also in: after the declaration of its semantics
Example one: RGB cube
Examples illustrate:
Create a shader for a cube (cube), in the Cartesian coordinate system of the cube, the value range of three coordinates x, Y, Z to {0,0,0} to {1,1,1}, and the Rgba color Red,green,blue,alpha value range is exactly {0,0,0,0} to {1,1,1,1}, the Alpha is written dead to 1, then r,g,b with X, Y, Z to establish one by one mappings, you can get a 6 polygons to display all the RGB color cube:
The detailed code and its meanings are as follows:
Shader "Custom/rgbcube" {subshader {Pass {cgprogram#pragma vertex vert//Vertex shader entry function declaration #pragma fragment Frag//Fragment Shader entry function declaration/ /vertex output struct struct Vertexoutput {//DECLARE member POS of struct, type float type of 4-element vector, semantics for sv_position,col; Float4 pos:sv_position;float4 Col: texcoord0;};/ /Vertex shader entry function vert, matching with pragma first declaration, return type is just defined vertex output struct vertexoutput vert (float4 vertexpos:position) {vertexoutput output; /There is no need for the structkeyword//vertex shader to write data to the output structure body. Output.pos = Mul (UNITY_MATRIX_MVP, Vertexpos),//mul is the vertex transform function, UNITY_MATRIX_MVP is the built-in matrix of UNITY, and Vertexpos is the function's form/ The purpose of this line of code is to convert the Vertexpos (this example, the vertex vector of the cube object) to a vertex transformation based on Unity's built-in matrix Output.col = Vertexpos + FLOAT4 (0.5, 0.5, 0.5, 0.0);// This line of code is the key to the implementation of the RGB cube//vertexpos of the domain for the problem of the x, Y, Z ternary group minus 0.5 composition of the range//But here the accepted type is FLOAT4, it is visible that the quaternion should be meaningless constant 1//meaning is the value of the Vertexpos of the domain { -0.5,-0.5,-0.5,1} to {0.5,0.5,0.5,1}//The vector addition of +{0.5,0.5,0.5,0} for this range of value to get RGB (a constant 1) of all the color interval return output;//the output structure returned, Go to the next step (simple to understand for the fragment shader)//ps: A more careful link with vertex transformations--vertex coloring------the rasterization geometry element//--> fragment is shaded--and slightly}//fragment shader entry function Frag, Matches the pragma second declaration, the return type is FLOAT4 semantics for color,//here except that the color has no other output, soNo output structure FLOAT4 frag (vertexoutput input): COLOR//The function's shape is the output structure of the vertex shader, no semantics//reason is that the fragment shader is in the next segment of the vertex shader, and the parameters are passed in this order {// Because the col attribute is already computed in the vertex shader, it is returned directly to the next link//The next link is what is not discussed here return input.col;} endcg}}//assumes that the above Subshader render failure is rolled back using Diffusefallback "diffuse"}
Add:
Just wrote it out, why Vertexpos's range is { -0,5,-0.5,-0.5,1} to {0.5,0.5,0.5,1} instead of {0,0,0,1} to {1,1,1,1}?
Since the origin of our Cartesian coordinate system is not at the vertex but in the geometry center of the cube, its range is { -0,5,-0.5,-0.5,1} to {0.5,0.5,0.5,1},over
If my blog is helpful to you or if you have any questions, please add Chongqing u3d QQ Group I will give you answer: 68994667, also can add group with us to exchange technology
Interpreting CG in Unity Shader series of first knowledge CG