Sort out the basic structure of Unity Shader (learning)

0x00 written in front

 

I have been reading The Book of Shaders before, why did I start writing Unity Shader? On the one hand, because the book is not yet finished, I have read the latest chapter of the book when writing this article; on the other hand, some practice is also needed to test and consolidate the knowledge learned. The environment provided by the Unity engine happens to be a good medium.

 

This article does not have a complete and runnable Shader code, but simply sorts out the basic structure of the Unity Shader to pave the way for future learning.

 

0x01 basic frame

 

The basic structure of a Unity Shader is as follows:

Shader "Path/ShaderName"{
   
           Properties    {
   
   
    }
        SubShader    {
   
           [Tags]        [RenderSetup]        Pass        {
   
               Name "PassName"            [Tags]            [RenderSetup]                                }            }        Fallback "OtherShaderName" }

 

0x02 name

 

Path/ShaderName determines the location of the Shader on the selection panel, much like the full path of a file. For example, Shader "Path0/Path1/CustomShader" would be located at Path0 → Path1 → CustomShader:

 

 

0x03 attribute

 

The properties defined in the Properties brackets will be displayed on the material panel and can be adjusted easily.

Properties{
   
       Name("Display Name", Type) = DefaultValue    }

 

Defining a property is just like defining a variable in C, but with a slightly different format. In the above code, Name is the name of the attribute, Display Name is the name displayed on the material panel, Type is the type of the attribute, and DefaultValue is the default value of the attribute. ps: Name often starts with an underscore, such as _PropertiesName.

 

Some commonly used properties are listed below:

 

number

_Number0("Number 0", Range(-2.0, 2.0)) = 0.0_Number1("Number 1", Float) = 0.618_Number2("Number 2", Int) = 3

 

Among them, Range will display a slider on the panel, and its two parameters represent the minimum and maximum values ​​that can be slid in turn. Float is a floating point number and Int is an integer.

 

 

colors and vectors

_Color("Color", Color) = (1, 1, 1, 1)_Vector("Vector", Vector) = (1, 1, 1, 1)

 

The four values ​​of Color represent RGBA respectively, and the modification button and the suction button will be displayed on the panel. Vector Vector is a four-dimensional vector, displayed as XYZW four grids on the panel.

 

 

texture

_2DTexture("2D Texture", 2D) = "defaulttexture" {}_Cube("Cube", Cube) = "defaulttexture" {}_3DTexture("3D Texture", 3D) = "defaulttexture" {}

 

2D means a 2D texture map, Cube means a cube map, and 3D means a 3D texture map. They both have Tiling and Offset.

 

 

 The properties defined in Properties can be used in the code block of SubShader, just define variables with the same name and matching type as these properties in the corresponding places  . The types in SubShader are slightly different from those in Properties. The corresponding matching relationship between the two is listed below:

 

  • Color and Vector can correspond to float4, half4, fixed4

  • Range and Float can correspond to float, half, fixed

  • 2D corresponds to sampler2D

  • Cube corresponds to samplerCUBE

  • 3D corresponds to sampler3D

 

In the SubShader code, some properties may not be defined in Properties, both of which can be dynamically modified by C# scripts at runtime (for example, use the method Material.SetFloat to modify floating-point properties). The difference between the two is that the properties defined in Properties will be saved after modification, but the properties not in Properties will not.

 

0x04 SubShader

 

Each Unity Shader can contain multiple SubShaders. When Unity displays a Mesh, it will find the first one that can run from these SubShaders to run. Because different hardware supports different shaders, here can be understood as

 

0x05 Tags

 

Tags are kv structures used to specify how and when Shader renders objects.

 

Tags { "TagName1" = "Value1" "TagName2" = "Value2" }

 

A detailed description of Tags can be found in: ShaderLab: SubShader Tags.

 

0X06 RenderSetup

 

RenderSetup is used to set the rendering state of the graphics card. For example, setting the culling mode.

 

Cull Back | Front | Off

 

For detailed settings of the rendering state, please refer to: ShaderLab: Pass.

 

0x07 Pass

 

The RenderSetup specified in SubShader will be applied to all Passes, and we can also specify it separately in Pass. The Tags in the Pass are different from those in the SubShader  , and are mainly used to control the ambient light and vertex lighting in the Pass. For details, see: ShaderLab: Pass Tags.

Pass{
   
       Name "PassName"     [Tags]    [RenderSetup]        }

 

After specifying a name for the Pass with Name, the Pass can be reused in other places. Among them, the Pass name needs to be capitalized. The usage is as follows:

 

UsePass "ShaderName/PASSNAME"

 

There can be multiple Passes in a SubShader, and these Passes will be executed sequentially.

 

0x08 Fallback

 

If all SubShaders are unavailable, the shader specified by Fallback will be tried. like:

 

Fallback "Diffuse"

 

0X09 Summary

 

This article simply sorts out the basic structure of Unity Shader, because the understanding is not deep, some of the content can only be roughly mentioned. These contents will be further studied in the future when necessary.

 

Finally, let's review the basic structure of Unity Shader:

 

  • Each Shader needs to define a name, the format is:

    Shader "Path/ShaderName" {}。

  • In the brackets of Shader, if necessary, we can define the properties of the material in the Properties semantic block.

  • Each Shader contains multiple sub-shaders SubShader.

    Multiple SubShaders are used because different hardware supports different shaders.

    When the instance is running, the engine will help us find the first SubShader that can run.

    When all SubShaders cannot run, use the Shader specified by Fallback.

  • Each SubShader contains multiple Passes, and each Pass represents a rendering process.

    Because some effects require several renderings to be displayed, multiple passes need to be used at this time.

  • Both SubShader and Pass can set the rendering state through some semantics (Tags and RenderType), and Pass can also pass through:

    Name "PassName" to specify the name of the Pass.

 

References:

 

  • [1] Shaders Overview

  • [2] Essentials of getting started with Unity Shader

Unity3D Game Development Essence Tutorial Dry Goods Recommended Search

shader

Guess you like

Origin blog.csdn.net/qq_15559109/article/details/110522027