Unity Shader structure
I. Introduction
If Unity ShderLab is not often written, it is always in a vague state and easy to forget. Here is a list of the grammatical structure of ShaderLab to better help us learn
2. Skeleton
Shader "Unlit/NewUnlitShader" // 名字
{
Properties //属性
{
}
SubShader
{
Tags {
"RenderType"="Opaque" }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
//a2v struct a2v结构体
//v2f struct v2f结构体
//vert func 顶点函数
//frag func 片元函数
ENDCG
}
//可以多个Pass,同个SubPass里的Pass是 &(与)的关系,即会一起起作用
Pass{
}
Pass{
}
...
}
//可以多个SubShader,SubShader之间是 ||(或)的关系,即最终只会选择一个起作用
SubShader {
}
SubShader {
}
...
//当所有的SubShader都不符合目标平台时,会保底运行Fallback指定的shader
Fallback "OtherShaderName"
}
Three, Properties attribute
Format
【Attribute】_Name(“DisplayName”, PropertyType) = DefaultValue
Property introduction
- 【Attribute】: It can be added or not. After adding it, the display on the Inspector panel can be changed.
- Such as [Toggle]: the original type of float can be changed into a switch
- Such as [NoScaleOffset]: 2D type will generate Tilling and Offset options by default in Inspector. Adding this can cancel the display of these two
- _Name: The variable name used for code identification. Generally names start with '_'
- DisplayName: The variable name used for the Inspector panel display
- PropertyType: property type
There are many property types, the following will illustrate - DefaultValue: default value
According to different PropertyType attributes, there will be different default values
The following list explains the declarations corresponding to various PropertyTypes
PropertyType | statement |
---|---|
Int | _Int(“Int”,Int) = 1 |
Float | _Float(“Float”,Float) = 1 |
Range(min,max) | _Range(“Range”,Range(0,1)) = 0 |
Color | _Color(“Color”,Color) = (0,0,0,0) |
Vector | _Vector(“Vector”,Vector) = (0,0,0,0) |
2d | _MainTex(“MainTex”,2D) = “” {} |
3D | _3D(“3D”,3D) = “white”{} |
Cube | _Cube(“Cube”,Cube) = “”{} |
Declaring properties in Properties is mainly for modifying and viewing in the Inspector panel. But if you want to actually use the attribute in the code, you have to declare it again in the Pass code block.
The type declared in Properties is called the ShaderLab attribute type; the type declared again in the Pass semantic block is called the CG variable type. Here is a list of their correspondence
ShaderLab variable type | CG variable type |
---|---|
Int | float,half,fixed |
Float | float,half,fixed |
Range(min,max) | float,half,fixed |
Color,Vector | float4,half4,fixed4 |
2d | sampler2D |
3D | sampler3D |
Cube | samplerCube |
4. Template code
Finally, attach a whole template code to better browse and grasp the overall structure
Shader "Unilt/CustomUniltShader"
{
Properties
{
_MainTex("MainTex",2D) = "white"{
}
}
SubShader
{
Tags{
"RenderType"="Opaque" }
pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
struct a2v
{
float4 pos : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
};
v2f vert(a2v v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.pos);
o.uv = TRANSFORM_TEX(v.uv,_MainTex);
return o;
}
fixed4 frag(v2f o) : SV_TARGET
{
fixed4 col = tex2D(_MainTex,o.uv);
return col;
}
ENDCG
}
//可以继续增加Pass
//pass{}
//pass{}
}
/**
继续指定SubShader
SubShader
{
}
...
**/
Fallback "Unlit/UnlistShader" //注释掉该行,或者替换成你的Fallback shader
}