Unity Shader vertex-fragment shader (1)

1. Unity Shader Foundation

1. Create and use Shader Shader
in Unity is generally used for two purposes:

  • Assigned to a material for physical rendering;
  • Assigned to the script for image processing, such as Post Processing;
    2. How to write the Shader of the Unity rendering pipeline
  • Vertex and Fragment Shader;
  • Surface Shader;
  • Fixed Function Shader (Fixed Function Shader, which is gradually being abandoned);

2. Basics of CG grammar

1. Compilation instructions

Commonly used compilation instructions for CG:
insert image description here

(1) Compilation target level

The CG code is compiled into different Shader Models, and it must be adapted to the corresponding GPU platform, otherwise there will be some Shaders that cannot run on the GPU.
The level at which the compilation target is declared #pragma target namecan also use #pragma require featuredirectives to directly declare a specific feature.

#pragma target 3.5  //目标等级3.5
#pragma require geometry tessellation  //需要几何体细分功能

(2) Rendering platform

Unity has cross-platform features, it supports many rendering APIs, such as Direct3D, Opengl. By default, Unity will compile a Shader program for all supported platforms, but you need to specify to compile some platforms or not to compile some platforms.
insert image description here
Example of use:

#pragma only_renderers d3d11  //目标只编译Direct3D 11/12 平台
#pragma exclude_renderers glcore //不编译 OpenGL 3.x/4.x

2. Shader function

A basic Shader example:

Shader "Custom/Simplest  Shader"
{
    SubShader
    {
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            void vert (in float4 vertex : POSITION,
                    out float4 position : SV_POSITION)
            {
                position = UnityObjectToClipPos(vertex);
            }

            void frag (in float4 vertex : SV_POSITION,
                    out fixed4 color : SV_TARGET)
            {
                color = fixed4(1, 0, 0, 1);
            }
            ENDCG
        }
    }
}

In Shader, vertex-fragment shaders are mainly implemented through vertex functions and fragment functions.

(1) Function without return value

The vertex functions and fragment functions in the above Shader use functions with no return value, and the variables are output through the out keyword.
The syntax structure is as follows:

 void name(in 参数,out 参数)
 {
 	//函数体
 }

void: the function starts with void, indicating that the return value is empty;
name: defines the name of the function;
in: input parameter, the syntax is: in+data type+name, a function can have multiple inputs, and the keyword in can be omitted.
out: output parameter, the syntax is: out+data type+name, a function can have multiple outputs.

(2) Functions with return values

The syntax structure is as follows:

 type name(in 参数)
 {
 	//函数体
 	return 返回值;
 }

3. Semantics

When writing shader functions in CG language, both the input parameters and output parameters of the function need to be filled with a semantic (Semantic) to represent the data information they want to pass.
Semantics can perform a lot of tedious operations, enabling users to avoid communicating directly with the GPU under the hood.
Key words separated by colons after parameters and in all uppercase are semantics.

(1) The input semantics of the vertex shader

Vertex data is passed to the vertex function in the form of input parameters, and each input parameter needs to be filled with a semantic to represent the passed data.
insert image description here

Note:
When the vertex information contains less elements than the elements required by the vertex shader input, the missing part will be filled with 0, and the w component will be filled with 1; for
example: the input semantics TEXCOOED0 is declared as float4 type, then the vertex shader The final data obtained is ( x , y , 0 , 1 ) (x,y,0,1)(x,and ,0,1)

(2) Vertex shader output and fragment shader input semantics

In the entire rendering pipeline, the most important part of the vertex shader is the need to output the coordinates of the vertex in the clipping space , so that the GPU can know the rasterized position and depth value of the vertex on the screen . In the vertex function this output parameter value needs to be filled with SV_POSITION semantics of type float4.
The output values ​​produced by the vertex shader will be interpolated in the triangle convenience stage and finally fed into the fragment shader as pixel values.
In other words, 顶点着色器的输出就是片元着色器的输入.
insert image description here
The fragment shader will automatically obtain the clip space vertex coordinates output by the vertex shader, so the SV_POSITION input by the fragment function can be omitted.

Note:
Unlike the input semantics of vertex functions, TEXCOORDn no longer specifies the UV coordinates of the model, nor does COLORn specify the vertex color. They are more widely used and can be used to declare any compliant data.

(3) Fragment shader output semantics

The fragment shader usually only outputs a fixed4 type of color information, the output value is stored in the Render Target, and the output parameters are filled with SV_TARGET semantics.

Guess you like

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