Shader learning seven, UnityCG.cginc

Keywords: Unity

Common shader files: e: \ other \ install \ unity \ 2018.3.13f1 \ unity \ editor \ data \ cgincludes
My installation path is that without CGIncludes, you may have to go to the official website to download the corresponding version of shaders

Common documents are as follows:
UnityCG.cginc: contains the most commonly used help functions, macros, structures, etc
UnityShaderVariables.cginc: automatically included when compiling UnityShader. It contains many built-in global variables, such as unity? Matrix? MVP, etc
Lighting.cginc: contains various built-in lighting models, which will be included automatically if you write SurfaceShader
HLSLSupport.cginc: automatically included when compiling UnityShader. Many macros and definitions for cross platform compilation are declared

Some commonly used structures in UnityCG.cginc

Appdata? Base: can be used for the input of vertex shader, including vertex position, vertex normal, and the first set of texture coordinates
Appdata Tan: can be used for vertex shader input, including vertex position, vertex tangent, vertex normal, the first set of texture coordinates
appdata_full: can be used for input of vertex shader, including vertex position, vertex tangent, vertex normal, four (or more) sets of texture coordinates
Appdata? Img: can be used for the input of vertex shader, including vertex position, the first set of texture coordinates
V2F ﹤ img: can be used for the output of vertex shaders, including the position in the clipping space, texture coordinates

struct appdata_base {
    float4 vertex : POSITION;
    float3 normal : NORMAL;
    float4 texcoord : TEXCOORD0;
    UNITY_VERTEX_INPUT_INSTANCE_ID
};

struct appdata_tan {
    float4 vertex : POSITION;
    float4 tangent : TANGENT;
    float3 normal : NORMAL;
    float4 texcoord : TEXCOORD0;
    UNITY_VERTEX_INPUT_INSTANCE_ID
};

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

struct appdata_img
{
    float4 vertex : POSITION;
    half2 texcoord : TEXCOORD0;
    UNITY_VERTEX_INPUT_INSTANCE_ID
};

struct v2f_img
{
    float4 pos : SV_POSITION;
    half2 uv : TEXCOORD0;
    UNITY_VERTEX_INPUT_INSTANCE_ID
    UNITY_VERTEX_OUTPUT_STEREO
};
Some common help functions in UnityCG.cginc
// Computes world space view direction, from object space position
inline float3 UnityWorldSpaceViewDir( in float3 worldPos )
{
    return _WorldSpaceCameraPos.xyz - worldPos;
}

// Computes world space view direction, from object space position
// *Legacy* Please use UnityWorldSpaceViewDir instead
//Call the above function, input the vertex position in model space, and return the observation direction from the point in world space to the camera
inline float3 WorldSpaceViewDir( in float4 localPos )
{
    float3 worldPos = mul(unity_ObjectToWorld, localPos).xyz;
    return UnityWorldSpaceViewDir(worldPos);
}
// Enter the position of a vertex in model space, and return the viewing direction from the point to the camera in model space
inline float3 ObjSpaceViewDir( in float4 v )
{
    float3 objSpaceCameraPos = mul(unity_WorldToObject, float4(_WorldSpaceCameraPos.xyz, 1)).xyz;
    return objSpaceCameraPos - v.xyz;
}
// Computes world space light direction, from world space position
inline float3 UnityWorldSpaceLightDir( in float3 worldPos )
{
    #ifndef USING_LIGHT_MULTI_COMPILE
        return _WorldSpaceLightPos0.xyz - worldPos * _WorldSpaceLightPos0.w;
    #else
        #ifndef USING_DIRECTIONAL_LIGHT
        return _WorldSpaceLightPos0.xyz - worldPos;
        #else
        return _WorldSpaceLightPos0.xyz;
        #endif
    #endif
}
// Computes world space light direction, from object space position
// *Legacy* Please use UnityWorldSpaceLightDir instead
//Only available in forward rendering. Enter a vertex position in model space to return the lighting direction from that point to the light source in world space. Not normalized
inline float3 WorldSpaceLightDir( in float4 localPos )
{
    float3 worldPos = mul(unity_ObjectToWorld, localPos).xyz;
    return UnityWorldSpaceLightDir(worldPos);
}
// Computes object space light direction
//Only available in forward rendering. Enter a vertex position in model space to return the lighting direction from that point to the light source in model space. Not normalized
inline float3 ObjSpaceLightDir( in float4 v )
{
    float3 objSpaceLightPos = mul(unity_WorldToObject, _WorldSpaceLightPos0).xyz;
    #ifndef USING_LIGHT_MULTI_COMPILE
        return objSpaceLightPos.xyz - v.xyz * _WorldSpaceLightPos0.w;
    #else
        #ifndef USING_DIRECTIONAL_LIGHT
        return objSpaceLightPos.xyz - v.xyz;
        #else
        return objSpaceLightPos.xyz;
        #endif
    #endif
}
// Transforms normal from object to world space
//Convert normal direction from model space to world space
inline float3 UnityObjectToWorldNormal( in float3 norm )
{
#ifdef UNITY_ASSUME_UNIFORM_SCALING
    return UnityObjectToWorldDir(norm);
#else
    // mul(IT_M, norm) => mul(norm, I_M) => {dot(norm, I_M.col0), dot(norm, I_M.col1), dot(norm, I_M.col2)}
    return normalize(mul(norm, (float3x3)unity_WorldToObject));
#endif
}

// Transforms direction from object to world space
//Transform direction vector from model space to world space
inline float3 UnityObjectToWorldDir( in float3 dir )
{
    return normalize(mul((float3x3)unity_ObjectToWorld, dir));
}
// Transforms direction from world to object space
// Transform direction vector from world space to model space
inline float3 UnityWorldToObjectDir( in float3 dir )
{
    return normalize(mul((float3x3)unity_WorldToObject, dir));
}
Common semantics of Unity when transferring model data to vertex shaders from application phase
semantics describe
POSITION Vertex position in model space, usually of type float4
NORMAL Vertex normals, usually of type float3
TANGENT Vertex tangent, usually of type float4
TEXCOORDn, such as TEXCOORD0, TEXCOORD1 Texture coordinates of this vertex, TEXCOORD0 represents the first set of texture coordinates, and so on. Usually float2 or float4
COLOR Vertex color, usually of type fixed4 or float4
Common semantics of Unity when transferring data from vertex shaders to slice shaders
semantics describe
SV_POSITION The vertex coordinates in the clipping space. The structure must contain a variable decorated with this semantics
COLORn, such as: COLOR0, COLOR1, etc Usually used to output the first set of vertex colors, but not required
TEXCOORD0~TEXCOORD7 Usually used to output texture coordinates, but not required

The book says:
SV POSITION is the system numerical semantics introduced in DirectX10. On most platforms, it is equivalent to POSITION semantics. However, on some platforms (such as Sony PS4), SV POSITION must be used to modify the output of vertex shaders. Otherwise, shaders cannot work normally. In order to make shaders have better cross platform performance, it is better to use the semantics at the beginning of SV to modify.

The common semantics of Unity in chip shader output
semantics describe
SV_Target The output value Jianghui is stored in the render target

But now it's like this

Published 16 original articles, won praise 2, visited 1043
Private letter follow

Posted by darkol on Tue, 14 Jan 2020 01:50:38 -0800