Use built-in HLSL structures in the Built-In Render Pipeline
Use prebuilt HLSL vertex input structures from UnityCG.cginc to simplify vertex shader declarations in the Built-In Render Pipeline.
Read time 2 minutesLast updated a month ago
Use prebuilt HLSL vertex input structures from to simplify vertex shader declarations in the Built-In Render Pipeline.
UnityCG.cgincTo use a prebuilt structure, follow these steps:
- Add at the top of your
#include "UnityCG.cginc". This imports theHLSLPROGRAMfile, which contains the prebuilt structures.UnityCG.cginc - When you declare your vertex shader function, use one of the prebuilt structures as the input parameter type. For example, to use , use
appdata_base.v2f vert (appdata_base vertex_data)
The prebuilt structures in are:
UnityCG.cginc- : Declares
appdata_basefor position,vertex, andnormal.texcoord - : Declares
appdata_tanfor position,vertex,tangent, andnormal.texcoord - : Declares
appdata_fullfor position,vertex,tangent,normal,color,texcoord,texcoord1, andtexcoord2.texcoord3 - : Declares
appdata_imgfor position, andvertex.texcoord
Example
The following shader uses , and colors the mesh based on its normals.
appdata_baseShader "VertexInputSimple" { SubShader { Pass { HLSLPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" // Declare the structure for data passed from the vertex shader to the fragment shader. struct v2f { float4 position : SV_POSITION; half4 color : COLOR; }; // Use the built-in appdata_base structure for the vertex shader. v2f vert (appdata_base v) { v2f output_data; // Use the position from appdata_base. output_data.position = UnityObjectToClipPos(v.vertex); // Use the normal from appdata_base. output_data.color.xyz = v.normal * 0.5 + 0.5; output_data.color.w = 1.0; return output_data; } half4 frag (v2f i) : SV_Target { return i.color; } ENDHLSL } } }