Unity Shader - Introduction to UnityCG.cginc


UnityCG.cginc is Unity's built-in Shader include file. It is the include file with the largest amount of code and the largest file in Unity.
Many built-in helper functions and data structures are declared in UnityCG.cginc, which can avoid a lot of repetitive coding work.

First, the vertex shader input structure

Unity declares a large number of structures in the include file, among which the UnityCG.cginc file contains the following structures:

//appdata 基础结构体
struct appdata_base {
    float4 vertex : POSITION;
    float3 normal : NORMAL;
    float4 texcoord : TEXCOORD0;
    UNITY_VERTEX_INPUT_INSTANCE_ID
};
//appdata 切向量结构体
struct appdata_tan {
    float4 vertex : POSITION;
    float4 tangent : TANGENT;
    float3 normal : NORMAL;
    float4 texcoord : TEXCOORD0;
    UNITY_VERTEX_INPUT_INSTANCE_ID
};
//appdata 完整结构体
struct appdata_full {
    float4 vertex : POSITION;
    float4 tangent : TANGENT;
    float3 normal : NORMAL;
    float4 texcoord : TEXCOORD0;
    float4 texcoord1 : TEXCOORD1;
    float4 texcoord2 : TEXCOORD2;
    float4 texcoord3 : TEXCOORD3;
    fixed4 color : COLOR;
    UNITY_VERTEX_INPUT_INSTANCE_ID
};
// appdata 图像特效结构体
struct appdata_img
{
    float4 vertex : POSITION;
    half2 texcoord : TEXCOORD0;
    UNITY_VERTEX_INPUT_INSTANCE_ID
};
// v2f 图像特效结构体
struct v2f_img
{
    float4 pos : SV_POSITION;
    half2 uv : TEXCOORD0;
    UNITY_VERTEX_INPUT_INSTANCE_ID
    UNITY_VERTEX_OUTPUT_STEREO
};

The last line of each structure is a macro, which can be ignored temporarily.

The information contained in all structures can be summarized as follows:
insert image description here

Second, the vertex transformation function

A detailed summary of vertex transformation functions is as follows:

function illustrate
float4 UnityObjectToClipPos(float3 pos) Transform vertices from model space to homogeneous clip space , equivalent to mul(UNITY_MATRIX_MVP,float4(pos,1.0))
float3 UnityObjectToViewPos(float3 pos) Transforms vertices from model space to camera space , equivalent to mul(UNITY_MATRIX_MV,float4(pos,1.0)).xyz. When the input is of type float4, Unity will automatically reload to type float3
float3 UnityWorldToViewPos(float3 pos) Transform a vertex from world space to camera space , equivalent to mul(UNITY_MATRIX_V,float4(pos,1.0)).xyz
float4 UnityWorldToClipPos(float3 pos) Transform vertices from world space to homogeneous clip space , equivalent to mul(UNITY_MATRIX_VP,float4(pos,1.0))
float4 UnityViewtToClipPos(float3 pos) Transform vertices from camera space to homogeneous clip space , equivalent to mul(UNITY_MATRIX_P,float4(pos,1.0))

3. Vector transformation function

function illustrate
float3 UnityObjectToWorldDir(float3 v) Transform vectors from model space to world space , normalized
float3 UnityWorldToObjectDir(float3 v) Convert vectors from world space to model space , normalized
float3 UnityObjectToWorldNormal(float3 v) Convert normals from model space to world space , already normalized

4. Lighting auxiliary function

UnityCG.cginc defines some calculation vertices pointing to the direction vector of the light. The commonly used functions are summarized as follows (the following functions are only applicable to the forward rendering path (ForwardBase or ForwardAdd Pass type)):

function illustrate
float3 UnityWorldSpaceLightDir(in float3 v) Input world space vertex coordinates, return the vector in world space pointing from the vertex to the light , not normalized
float3 ObjSpaceLightDir(in float4 v) Input model space vertex coordinates, return a vector in model space pointing from the vertex to the light , not normalized
float3 Shader4PointLights(...) Enter a series of required variables, return the lighting information of 4 point lights, use this function to calculate per-vertex lighting in forward rendering

5. View vector function

Calculate the direction vector of the vertex pointing to the camera, also known as the viewing angle vector. The commonly used functions are as follows:

function illustrate
float3 UnityWorldSpaceViewDir(float3 v) Input vertices in world space, return the vector in world space pointing from the vertex to the camera , not normalized
float3 ObjSpaceViewDir(float4 v) Input model space vertices, return a vector in model space pointing from the vertex to the camera , not normalized

6. Other auxiliary functions and macros

function illustrate
TRANSFORM_TEX(tex,name) Macro definition, input UV coordinates and texture name to get the texture coordinates of the map
fixed3 UnpackNormal(fixed packedNormal) Map the normal vector from [0,1] to [-1,1]
half Luminance(half3 rgb) Convert color data to grayscale data
float4 ComputeScreenPos(float4 pos) Enter the clip space vertex coordinates to get the screen space texture coordinates, which are used for screen space texture mapping
float4 ComputeGrabScreenPos(float4 pos) Enter the clip space vertex coordinates to get the texture coordinates of the sampled GrabPass

Seven, macro introduction

Macros need to be defined before they can be used, by replacing a string with an identifier. When using it, you only need to enter the identifier. Unity will automatically replace the identifier with a string when compiling, so you can simply understand the macro as the replacement of the string.

The syntax of a macro is:

# define name string; 
  • #define: indicates the instruction defined by the macro;
  • name: macro name, you can directly enter the name for use later;
  • string: The content to replace the macro name with when compiling, which can be numbers, expressions, functions, etc.

Guess you like

Origin blog.csdn.net/qq_40120946/article/details/122225952