In OpenGL, support for vertex shaders and fragment shaders is required for proper rendering, and each frame in the vertex shader is processed once for each vertex of the object in the scene.
If you use OpenGL yourself, you need to read the model data in C + + code, passing vertex coordinates, vertex colors, and UV coordinates to the vertex shader.
So in a vertex shader, you can modify the vertex data.
Build a test scenario and import the FBX model with the book.
In Unity3d, the FBX or other model is imported into the editor, and a diffuse material is created by default. There is no vertex function in this material, that is to say, the vertex data cannot be processed, so the model that comes in is gray and no color.
New material Vertexcolor.mat, new Vertexcolor.shader.
The following adds the Vert () function (vertex function) to Shader to get the vertex color in the model.
1. Declare the vertex parameter and tell unity that our shader contains a vertex function
pragma surface surf Lambert vertex:vert
2. Write vertex function vert ()
void Vert (inout appdata_full v,out Input o) {};
The purpose of writing vertex functions is to get the vertex color, pass it to the surf () function, the vertex color is stored in the structure of Appdata_full, and then out of input, the default input is not the variable that stores the vertex color, we need to add up.
struct Input {float2 uv_maintex;float4 vertexcolor;};
Then add the vert () vertex function.
void Vert (inout appdata_full v,out Input o) {unity_initialize_output (input,o); O.vertexcolor=v.color;}
The definition of appdata_full can be found in the Unitycg.cginc file
struct Appdata_full { float4 vertex:position; FLOAT4 tangent:tangent; FLOAT3 Normal:normal; FLOAT4 texcoord:texcoord0; FLOAT4 Texcoord1:texcoord1; FIXED4 color:color; #if defined (shader_api_xbox360) half4 Texcoord2:texcoord2;half4 texcoord3:texcoord3;half4 Texcoor D4:texcoord4;half4 texcoord5:texcoord5; #endif};
You can see that the appdata_full contains vertex coordinates, tangent, normals, Uvs, UV1, vertex colors.transfer from http://www.thisisgame.com.cn Http://blog.csdn.net/huutu
The following two versions of Normals are also available, which can be selected according to requirements.
struct Appdata_base { float4 vertex:position; FLOAT3 Normal:normal; Float4 texcoord:texcoord0;}; struct Appdata_tan { float4 vertex:position; FLOAT4 tangent:tangent; FLOAT3 Normal:normal; Float4 texcoord:texcoord0;};
3. Use the vertex color as the output in the surf () function.
void Surf (Input in, InOut surfaceoutput o) {o.albedo=in.vertexcolor;}
Full shader code
Shader "Cookbookshaders/chapt7-1/vertexcolor" {Properties {_maintex ("Base (RGB)", 2D) = "White" {}}subshader {Tags {"Ren Dertype "=" Opaque "}lod 200cgprogram#pragma surface surf Lambert vertex:vertsampler2d _maintex;struct input{float2 uv_ Maintex;float4 Vertexcolor;}; void Vert (inout appdata_full v,out Input o) {unity_initialize_output (input,o); o.vertexcolor=v.color;} void Surf (Input in, InOut surfaceoutput o) {o.albedo=in.vertexcolor;} ENDCG} FallBack "Diffuse"}
Assign the material to the model to see the effect as follows
Test Project Download:
Http://pan.baidu.com/s/1pL3pkmv
Unity Shaders and Effects Cookbook (7-1) accessing vertex colors in surface Shader