Passing vertex information to a vertex program
CG/HLSL vertex programs, vertex information must be passed through the structure. Several commonly used vertex structures are defined in the Unitycg.cginc file. And in most cases it's enough to use them.
- Appdata_base: The position of the vertex, normal, and a texture coordinate.
- Appdata_tan: The position of the vertex, tangent, normal, and a texture coordinate.
- Appdata_full: Vertex position, tangent, normal, four texture coordinates and color.
The mesh of this material color is based on normals and only uses the Appdata_base vertex program input:
Shader "Vertexinputsimple" { Subshader { Pass { cgprogram #pragma vertex vert #pragma fragment Frag #include "unitycg.cginc" struct v2f { float4 pos:sv_position; Fixed4 color:color; }; v2f Vert (Appdata_base v) { v2f o; O.pos = Mul (UNITY_MATRIX_MVP, V.vertex); O.COLOR.XYZ = v.normal * 0.5 + 0.5; O.COLOR.W = 1.0; return o; } Fixed4 Frag (v2f i): sv_target {return i.color;} ENDCG }}}
If you want to access different vertex information, you need to declare your own vertex structure, or add input parameters to the vertex shader. Vertex information is considered CG/HLSL semantics and must be followed by the following rules:
Semantic:
- POSITION: Vertex coordinates, typical float4.
- Normal: normal vertex, typical float3.
- TEXCOORD0: is the first UV coordinate, a typical FLOAT2,FLOAT3
- Texcoord1~texcoord3 is the fourth UV coordinate of the second road.
- TANGENT: is a tangent vector (for normal mapping), a typical FLOAT4
- Color: A colour that is available for each vertex, a typical float
Visualize Uvs
The following shader example uses vertex coordinates and the first vertex shader to enter texture coordinates, which are defined in the structure AppData. This is useful for debugging UV coordinates on a grid. UV coordinates are visualized as red and green colors (RG), and the coordinates of more than 0~1 range have an additional blue color applied.
Shader "Debug/uv 1" {subshader { Pass { Fog {Mode Off} cgprogram #pragma vertex vert #pragma fragmen T Frag //Vertex input:position, UV struct AppData { float4 vertex:position; FLOAT4 texcoord:texcoord0; }; struct V2F { float4 pos:sv_position; FLOAT4 uv:texcoord0; }; v2f Vert (AppData v) { v2f o; O.pos = Mul (UNITY_MATRIX_MVP, V.vertex); O.UV = FLOAT4 (v.texcoord.xy, 0, 0); return o; } Half4 Frag (v2f i): sv_target { Half4 c = frac (I.UV); if (Any (saturate (I.UV)-I.UV)) c.b = 0.5; return c; } ENDCG }}}
Visualize vertex colors
This shader uses the vertex position and the color of each vertex as the vertex shader input (defined in structure AppData).
Shader "Debug/vertex Color" {subshader { Pass { Fog {Mode Off} cgprogram #pragma Vertex vert #pragm A fragment Frag //Vertex input:position, color struct AppData { float4 vertex:position; Fixed4 color:color; }; struct V2F { float4 pos:sv_position; Fixed4 color:color; }; v2f Vert (AppData v) { v2f o; O.pos = Mul (UNITY_MATRIX_MVP, V.vertex); O.color = Fixed4 (1,0.8,0.7,1); return o; } Fixed4 Frag (v2f i): sv_target {return i.color;} ENDCG }}}
Debug color material applied to a model illumination into color
Visualize Normals
Use vertex positions and normals as input to the vertex shader (defined in structure AppData). Normal x, Y, Z components are visualized as r,g,b colors. Because the value of the normals is at 1. .1 in the range,
Shader "Debug/normals" {subshader { Pass { Fog {Mode Off} cgprogram #pragma vertex vert #pragma fra Gment frag //Vertex input:position, normal struct AppData { float4 vertex:position; FLOAT3 normal:normal; }; struct V2F { float4 pos:sv_position; Fixed4 color:color; }; v2f Vert (AppData v) { v2f o; O.pos = Mul (UNITY_MATRIX_MVP, V.vertex); O.COLOR.XYZ = v.normal * 0.5 + 0.5; O.COLOR.W = 1.0; return o; } Fixed4 Frag (v2f i): sv_target {return i.color;} ENDCG }}}
The debug normal material is applied to a model. You can see the model hard shadow edge.
(Ignoring the Unknown object .......) Model of Ngui)
Visualize Tangent and Vice Normals
Tangent and sub-normal vectors are used for normal maps. In unity only tangent vectors are stored in vertices, and the secondary normals originate from Normals and tangents.
Use vertex positions and normals as input to the vertex shader (defined in structure AppData). The x, Y, z components of the tangent are visualized as r,g,b colors.
Tangent
Shader "Debug/tangents" {subshader { Pass { Fog {Mode Off} cgprogram #pragma vertex vert #pragma fr Agment frag //Vertex input:position, tangent struct AppData { float4 vertex:position; FLOAT4 tangent:tangent; }; struct V2F { float4 pos:sv_position; Fixed4 color:color; }; v2f Vert (AppData v) { v2f o; O.pos = Mul (UNITY_MATRIX_MVP, V.vertex); O.color = v.tangent * 0.5 + 0.5; return o; } Fixed4 Frag (v2f i): sv_target {return i.color;} ENDCG }}}
(Ignoring the previous unknown object .......) Model of Ngui)
Secondary normals
It uses vertex positions, normals, and tangent vertex inputs. Calculates the Vice normals from Normals and tangents. Like normal normals or tangents.
Shader "Debug/binormals" {subshader { Pass { Fog {Mode Off} cgprogram #pragma vertex vert #pragma f Ragment frag //Vertex input:position, normal, tangent struct AppData { float4 vertex:position; FLOAT3 Normal:normal; FLOAT4 tangent:tangent; }; struct V2F { float4 pos:sv_position; FLOAT4 color:color; }; v2f Vert (AppData v) { v2f o; O.pos = Mul (UNITY_MATRIX_MVP, V.vertex); Calculate Binormal Float3 binormal = Cross (v.normal, v.tangent.xyz) * V.TANGENT.W; O.COLOR.XYZ = binormal * 0.5 + 0.5; O.COLOR.W = 1.0; return o; } Fixed4 Frag (v2f i): sv_target {return i.color;} ENDCG }}}
Provide vertex data for vertex programs