Unity | Shader basics (Episode 3: Case <Intervening in material color>)

Table of contents

1. Introduction to this section

1 Review of the previous episode

2 Introduction to this section

2. Introduction to theory

 3. Cases of intervening in material color

1 Get location information

2 Working with colors

4. All codes in this section

5. Preview of the next episode


1. Introduction to this section

1 Review of the previous episode

In the last episode, we learned that the syntax format of shader

2 Introduction to this section

In this episode, we learn about the second simplest shader case, a simple intervention in shader color.

2. Introduction to theory

1 Get location information

Unity | Shader basics (what is a shader)_unity shader-CSDN blog

In this article, we can see that the one that initially determines the position is called the vertex shader, because the color can be applied only after the position is determined.

Conclusion: We can intervene in the position of coloring in the vertex shader

2. Change color ideas

The last step of coloring is in the fragment shader (also called pixel shader). We can no matter how it runs in the middle. If we change the color at the end, the displayed color will definitely change.

 3. Cases of intervening in material color

Semantics used in this case:

:POSITION Get the vertex coordinates of the model

:SV_POSITION Screen coordinates output to the pixel shader

:SV_TARGET The output value is directly used for rendering

1 Get location information

a. As mentioned above, we need to obtain the vertex position information of the model in the vertex shader

Introduce the vertex position information code:

Shader "Custom/001"
{
SubShader
    {
        pass
        {
            CGPROGRAM
            //引入vertex //起名叫vert
            #pragma vertex vert

            ENDCG
        }
    }
}

b. Get the model vertex position

According to the semantic explanation above, we already know how to get the model vertices.

c.Coordinate conversion

The vertex position of the model must be in world coordinates. Our screen may not be able to display the full range. Our shader only needs to manage what can be seen on the screen, so we need to know the position information in screen coordinates.

Conclusion: We need to convert the vertex position in world coordinates to a position in screen coordinates

Method: Unity has already done it for me, just use it directly

The code for converting world coordinates to screen coordinates is as follows:

//最后会得到,模型的屏幕坐标
UnityObjectToClipPos(这里输入模型的顶点世界坐标)

d. Output the converted coordinates to the screen coordinates of the pixel shader

Summary of the above function codes:

Shader "Custom/001"
{
SubShader
    {
        pass
        {
            CGPROGRAM
            //引入vertex //起名叫vert
            #pragma vertex vert
            
                        //引入模型顶点坐标    //return的值直接给到片元着色器的屏幕坐标
            float4 vert(float4 v :POSITION):SV_POSITION
            {
            //返回处理过的坐标数据
            return UnityObjectToClipPos(v);
            }
            ENDCG
        }
    }
}
2 Working with colors

a.Introduce fragment shader information code

//引入fragment //起名叫frag
#pragma fragment frag

b. Modify color code

Because the fragment shader outputs color directly, we assume that we want to output a white color

//白色的写法(原因略)
fixed4(1,1,1,1)

Fragment shader outputs white code

//片元着色器方法  //直接输出渲染
float4 frag():SV_TARGET
{
    //输出白色
   return float4(1,1,1,1);
}

Remark:

If the data returned here are all in 0-1, by default 0 is black and 1 is white.

If it is in 0-255, the default 0 is black and 255 is white.

4. All codes in this section

Shader "Custom/001"
{
SubShader
    {
        pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            float4 vert(float4 v :POSITION):SV_POSITION
            {
            return UnityObjectToClipPos(v);
            }

            float4 frag():SV_TARGET
            {
            return float4(1,1,1,1);
            }

            ENDCG
        }
    }
}

5. Preview of the next episode

The next episode will explain shader syntax and use structures to obtain data.

Guess you like

Origin blog.csdn.net/weixin_49427945/article/details/134973329