Unity Shader learning (1) Understanding the basic structure of unity shader

Recently, I plan to learn Unity’s Shader carefully, and I will still use it in future projects. Although it is very comfortable to use other people’s shader, it is someone else’s, no matter how easy it is to use, and I can’t understand it myself, so I still have to understand it myself. Today, let's get acquainted with the structure of the shader and my simple understanding of the rendering of the unity shader.

1. Simple understanding of computer rendering process

First of all, you must learn shader, and you must understand the computer rendering process. Here I recommend everyone to Baidu. The following reference:
Unity technical art
computer rendering process
Simple understanding of computer rendering process
Conceptually, we generally divide the computer rendering process into several stages :
1. Application stage: Mainly the data such as lighting and models are processed by the CPU to form the primitive data of points, lines and surfaces.
2. Geometry stage: processing point, line and surface information.
3. Rasterization stage: adding texture, color, etc.
(Note: The above stage is to simplify the process, and the actual operation is more complicated)
insert image description here

2. Understand the structure of Unity Shader

Open Unity and create a shader. You can see that the default shader uses the standard pbr shader rendering method.
insert image description here
Let’s create another shader and choose one by right-clicking "Shader". Here we choose the unlit Unlit Shader to create Well, it is shown as follows:
insert image description here
Double-click to open the shader. Here I use VScode to write the Shader. The Shader of Unity uses the shaderlab language. The underlying interface is encapsulated and can be called directly. It is compatible with all platforms.

Shader "Unlit/shader1"
{
    
    
    Properties
    {
    
    
        _MainTex ("Texture", 2D) = "white" {
    
    }
    }
    SubShader
    {
    
    
        Tags {
    
     "RenderType"="Opaque" }
        LOD 100

        Pass
        {
    
    
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"

            struct appdata
            {
    
    
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
    
    
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            v2f vert (appdata v)
            {
    
    
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
    
    
                // sample the texture
                fixed4 col = tex2D(_MainTex, i.uv);
                // apply fog
                UNITY_APPLY_FOG(i.fogCoord, col);
                return col;
            }
            ENDCG
        }
    }
}

The top one is the Shader naming operation, no need to manage

Shader "Unlit/shader1"

Then there is the Properties block, here are the pre-defined parameter variables, which can be adjusted through the Unity panel

  Properties
    {
    
    
        _MainTex ("Texture", 2D) = "white" {
    
    }
    }

insert image description here
The main form of definition is
_Name ("Display Name", Type) = Default Value
Type type also includes:
Color color
Int integer
Float floating point number
Vector four-dimensional number
2D texture
3D texture
Cube cube texture

SubShader is similar to the rendering list, which can provide a variety of image quality options for rendering.
Tags: Set special rendering methods, such as rendering for transparent objects and translucent objects.
LOD: Set the detail control of rendering. The processing is based on device performance. You can leave it alone

  SubShader
    {
    
    
        Tags {
    
     "RenderType"="Opaque" }
        LOD 100

Pass block, all rendering is performed here, starting from CGPROGRAM and ending with ENDCG
Generally, there can be multiple Pass

      Pass
        {
    
    
        CGPROGRAM
        ENDCG

		}

#pragma vertex/fragment name are the names of the vertex shader and fragment shader respectively. After
defining the vertex shader and fragment shader, you need to deal with it. See
the comments in the code for details.

     CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            //引入UnityCG
            #include "UnityCG.cginc"
			
            struct appdata
            {
    
    
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
    
    
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
			//几何阶段的顶点着色器处理,需要输入一个应用阶段的数据appdata,即点线面等信息
			//输入之后处理,返回一个片元着色器要是用到的数据v2f
            v2f vert (appdata v)
            {
    
    
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }
			//光栅化阶段,输入一个需要光栅化处理的结构体v2f,返回颜色信息
            fixed4 frag (v2f i) : SV_Target
            {
    
    
                // sample the texture
                fixed4 col = tex2D(_MainTex, i.uv);
                // apply fog
                UNITY_APPLY_FOG(i.fogCoord, col);
                return col;
            }
            ENDCG

The above stages can be summed up as follows:
vert processes the point information
frag processes the point information and outputs the color.
Let’s not understand the specific details. Let’s write our own code in the next section

Guess you like

Origin blog.csdn.net/qq_14942529/article/details/125403389