Unity | Shader basics (Episode 6: Syntax

Table of contents

1. Introduction to this section

1 Review of the previous episode

2 Introduction to this section

2. Grammatical structure

1 Review

2 theoretical knowledge

3 How to write declarations in Shader

4 Properties and SubShader are not a family after all

3. Resources used in fragment shaders

4. Code implementation

5. All codes

6. Introduction to the next episode


Related Reading

Unity - Manual: Writing Surface Shaders


1. Introduction to this section

1 Review of the previous episode

In the last episode, I made a small colored ball.

2 Introduction to this section

How to add external color resource Color to control the color of the sphere. In similar code, the public Color can be seen outside (as shown in Figure 1) and the color can be changed (as shown in Figure 2).

Figure 1 Add Color
Figure 2 Changing material color externally

2. Grammatical structure

1 Review

The grammatical structure code learned before is as follows:

Shader "Custom/001"    //shader地址
{
SubShader    //干预着色器
    {
        pass    //通道
        {
            CGPROGRAM    //开始CG语言
            #pragma vertex vert      //引用顶点着色器
            #pragma fragment frag    //引用片元着色器

            #include"UnityCG.cginc"    //引用unity写好的一些内容

            ENDCG    //结束CG语言
        }
    }
}

2 theoretical knowledge

Our external resources are usually put in before intervening in the shader, so the location is inside the address and outside the SubShader.

Learning English:

Property Property

Properties Property (plural)------Use this in the code

code show as below:

Shader "Custom/001"
{
//财产
Properties
     {
     }
SubShader
    {
        pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include"UnityCG.cginc"

            ENDCG
        }
    }
}

3 How to write declarations in Shader

In C# syntax, the code we declare is as follows:

//公开 //类型是Color        //赋值为(1,1,1,1)
public Color color = new Color(1,1,1,1);
            //起名是color

shder itself is not an ordinary script, so it requires two names when naming it.

  • The name displayed externally (as shown in Figure 3), for example, I named it Color
Figure 3 Name displayed externally
  • The name for internal use, for example, I named it _Color
  • Code to declare name in shader
Properties
     {
     //内部使用的名称  //声明的数据类型
    _Color("Color",Color)=(1,1,1,1)
         //外部使用的名称   //赋值(1,1,1,1)
     }

4 Properties and SubShader are not a family after all

Although we have declared Color in Properties, this only means that this information exists in this shader script, and it does not mean that we can use it directly.

  • When we use it, we still need to re-declare it in SubShader (to indicate that you borrowed it)
  • The data types of external resources in the shader are different. It does not recognize the Color type. The commonly used ones it recognizes are float3, float4, etc. Color is essentially a float4.
  • SubShader takes over the data code
SubShader
    {
        pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include"UnityCG.cginc"
            
            //重新声明_Color
            float4 _Color ;

            ENDCG
        }
    }

3. Resources used in fragment shaders

At this time we can make a ball, the color of this ball will show its color according to the color changed outside. Changing the color is done directly in the fragment shader.

4. Code implementation

            float4 frag():SV_TARGET
            {
                    //直接将接到的数据输出
            return _Color;
            }

5. All codes

They are all based on the code that was added and subtracted from the previous tutorial.

Shader "Custom/001"
{
Properties
     {
     //内部使用的名称  //声明的数据类型
    _Color("Color",Color)=(1,1,1,1)
         //外部使用的名称   //赋值(1,1,1,1)
     }
SubShader
    {
        pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include"UnityCG.cginc"

            float4 _Color ;

            appdata_base vert(appdata_base v)
            {
            v.vertex =UnityObjectToClipPos(v.vertex);
            return v;
            }

            float4 frag():SV_TARGET
            {
            return _Color;
            }
            ENDCG
        }
    }
}

6. Introduction to the next episode

This episode talks about how to add external resources and change the current material through external resources.

The next episode will talk about a case study on how to add external image resources.

Guess you like

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