Unity Shaders and Effects Cookbook (7-1) Access vertices in Surface Shader

In OpenGL, the support of vertex shader and fragment shader is needed for correct rendering. In vertex shader, each frame is for each object in the scene All vertices are processed once.

If you use OpenGL yourself, you need to read the model data in the C++ code, and transfer the vertex coordinates, vertex colors, and UV coordinates to the vertex shader.

So in the vertex shader, the vertex data can be modified.

Set up the test scene and import the FBX model attached to the book.

In Unity3d, import FBX or other models into the editor, and a Diffuse material will be created by default. There is no vertex function in this material, which means that vertex data cannot be processed, so the imported model is gray and has no color.

New material VertexColor.mat, new VertexColor.shader.

Next, add the vert() function (vertex function) to the Shader to get the vertex color in the model.

1. Declare the vertex parameter to tell Unity that our shader contains a vertex function

pragma surface surf Lambert vertex:vert

2、编写顶点函数vert()

void vert(inout appdata_full v,out Input o)< br />{

};

The purpose of writing a vertex function is to get the vertex color and pass it to the surf () function. The vertex color is stored in the appdata_full structure, Then pass it out from Input. By default, there is no variable to store vertex color in Input, we need to add it.

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;
}

在UnityCG.cginc 文件中可以找到appdata_full Definition of

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 texcoord4: TEXCOORD4;
half4 texcoord5: TEXCOORD5;
#endif
};

You can see that the appdata_full contains vertices Coordinate, tangent, normal, UV, UV1, vertex color. Reproduced from http://www.thisisgame.com.cn http://blog.csdn.net/ huutu

At the same time, the normal has the following two versions, 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 output in the surf() function.

void surf (Input IN, inout SurfaceOutput o) 
{
o.Albedo=IN.vertexColor;
}< /pre>

Complete Shader code

Shader "CookBookShaders/Chapt7-1/VertexColor" 
{
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader
{
Tags {"RenderType"="Opaque" }
LOD 200

CGPROGRAM
#pragma surface surf Lambert vertex:vert

sampler2D _MainTex;

struct Input< br /> {
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)
{< br /> o.Albedo=IN.vertexColor;
}
ENDCG
}
FallBack "Diffuse"
}

The material is assigned to the model, and the effect is as follows

Download the test project:

http://pan. baidu.com/s/1pL3pkmv

In OpenGL, the support of vertex shader and fragment shader is required to perform correct rendering. In vertex shader , Each frame must be processed once for each vertex of the object in the scene.

If you use OpenGL yourself, you need to read the model data in the C++ code, and transfer the vertex coordinates, vertex colors, and UV coordinates to the vertex shader.

So in the vertex shader, the vertex data can be modified.

Set up the test scene and import the FBX model attached to the book.

In Unity3d, import FBX or other models into the editor, and a Diffuse material will be created by default. There is no vertex function in this material, which means that vertex data cannot be processed, so the imported model is gray and has no color.

New material VertexColor.mat, new VertexColor.shader.

Next, add the vert() function (vertex function) to the Shader to get the vertex color in the model.

1. Declare the vertex parameter to tell Unity that our shader contains a vertex function

pragma surface surf Lambert vertex:vert

2、编写顶点函数vert()

void vert(inout appdata_full v,out Input o)< br />{

};

The purpose of writing a vertex function is to get the vertex color and pass it to the surf () function. The vertex color is stored in the appdata_full structure, Then pass it out from Input. By default, there is no variable to store vertex color in Input, we need to add it.

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;
}

在UnityCG.cginc 文件中可以找到appdata_full Definition of

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 texcoord4: TEXCOORD4;
half4 texcoord5: TEXCOORD5;
#endif
};

You can see that the appdata_full contains vertices Coordinate, tangent, normal, UV, UV1, vertex color. Reproduced from http://www.thisisgame.com.cn http://blog.csdn.net/ huutu

At the same time, the normal has the following two versions, 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 output in the surf() function.

void surf (Input IN, inout SurfaceOutput o) 
{
o.Albedo=IN.vertexColor;
}< /pre>

Complete Shader code

Shader "CookBookShaders/Chapt7-1/VertexColor" 
{
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader
{
Tags {"RenderType"="Opaque" }
LOD 200

CGPROGRAM
#pragma surface surf Lambert vertex:vert

sampler2D _MainTex;

struct Input< br /> {
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)
{< br /> o.Albedo=IN.vertexColor;
}
ENDCG
}
FallBack "Diffuse"
}

The material is assigned to the model, and the effect is as follows

Download the test project:

http://pan.baidu. com/s/1pL3pkmv

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 1517 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.