当在编写Cg或者HLSL着色器程序的时候,我们需要使用语义(Semantic)来指明输入输出变量的“意图”。
例如下面的这段Shader代码:
Shader "Custom/VF" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
Pass {
Tags { "LightMode" = "ForwardBase" }
CGPROGRAM
#pragma multi_compile_fwdbase
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
struct a2v {
float4 vertex : POSITION;
fixed3 normal : NORMAL;
fixed4 texcoord : TEXCOORD0;
fixed4 color : COLOR;
};
struct v2f {
float4 pos : POSITION;
float2 uv : TEXCOORD0;
};
v2f vert(a2v v) {
v2f o;
//Transform the vertex to projection space
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
//Get the UV coordinates
o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
return o;
}
float4 frag(v2f i) : COLOR {
fixed4 texColor = tex2D(_MainTex, i.uv);
return texColor;
}
ENDCG
}
}
FallBack "Diffuse"
}
其中a2v结构里变量后面用冒号隔开的就是语义。
a2v作为vert函数的输入,它里面所标识的语义用来告诉Unity我们需要什么样的数据。
此外,我们看到frag方法名的后面也添加了COLOR语义,这是告诉Unity,这个方法输出一个色彩值,然后Unity就可以把这个色彩值写入GPU。
你可能会注意到vert的输出frag的