Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


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

Important
The Built-In Render Pipeline is deprecated and will be made obsolete in a future release.
It remains supported, including bug fixes and maintenance, through the full Unity 6.7 LTS lifecycle.
Use prebuilt HLSL vertex input structures from
UnityCG.cginc
to simplify vertex shader declarations in the Built-In Render Pipeline.
To use a prebuilt structure, follow these steps:
  1. Add
    #include "UnityCG.cginc"
    at the top of your
    HLSLPROGRAM
    . This imports the
    UnityCG.cginc
    file, which contains the prebuilt structures.
  2. When you declare your vertex shader function, use one of the prebuilt structures as the input parameter type. For example, to use
    appdata_base
    , use
    v2f vert (appdata_base vertex_data)
    .
The prebuilt structures in
UnityCG.cginc
are:
  • appdata_base
    : Declares
    vertex
    for position,
    normal
    , and
    texcoord
    .
  • appdata_tan
    : Declares
    vertex
    for position,
    tangent
    ,
    normal
    , and
    texcoord
    .
  • appdata_full
    : Declares
    vertex
    for position,
    tangent
    ,
    normal
    ,
    color
    ,
    texcoord
    ,
    texcoord1
    ,
    texcoord2
    , and
    texcoord3
    .
  • appdata_img
    : Declares
    vertex
    for position, and
    texcoord
    .

Example

The following shader uses
appdata_base
, and colors the mesh based on its normals.
Shader "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 } } }

Additional resources