您可以使用“顶点修改器”函数来修改顶点着色器中的传入顶点数据。这种做法适合程序化动画和沿法线挤出等操作。表面着色器编译指令 vertex:functionName 将被用于此目的,其中的一个函数会采用 inout appdata_full 参数。
以下着色器会让顶点沿法线按材质所指定的量移动:
Shader "Example/Normal Extrusion" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_Amount ("Extrusion Amount", Range(-1,1)) = 0.5
}
SubShader {
Tags { "RenderType" = "Opaque" }
CGPROGRAM
#pragma surface surf Lambert vertex:vert
struct Input {
float2 uv_MainTex;
};
float _Amount;
void vert (inout appdata_full v) {
v.vertex.xyz += v.normal * _Amount;
}
sampler2D _MainTex;
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
}
ENDCG
}
Fallback "Diffuse"
}