最近有點(diǎn)忙,所以好久都沒寫東西了,但Surface Shader入門系列還少個(gè)重要的部分沒介紹——頂點(diǎn)變換。還是先看下代碼:
Shader "Demo/Diffuse Vertex"{
Properties{
_MainTex("MainTex", 2D) = ""{}
_IsSphere("IsSphere", int) = 1
}
SubShader{
Tags{"RenderType"="Opaque"}
LOD 200
CGPROGRAM
#pragma surface surf Lambert vertex:vert
sampler2D _MainTex;
bool _IsSphere;
struct Input {
float2 uv_MainTex;
};
void vert(inout appdata_full v) {
if (!_IsSphere) {
v.vertex.y = 0;
}
}
void surf(Input IN, inout SurfaceOutput o) {
half4 c = tex2D(_MainTex, IN.uv_MainTex);
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
直接說明與之前介紹代碼不同的部分:
#pragma surface surf Lambert vertex:vert
vertex:vert 聲明一個(gè)自定義的vert函數(shù)來改變頂點(diǎn)。
void vert(inout appdata_full v) {
if (!_IsSphere) {
v.vertex.y = 0;
}
}
將當(dāng)前頂點(diǎn)的本地坐標(biāo)的Y值賦予傳入的變量值。
效果圖

圖7-1 左圖_IsSphere值為1,右為0
后記
Surface Shader入門系列暫時(shí)到此為止,接下來如果有時(shí)間會(huì)側(cè)重片段著色器和相關(guān)渲染知識(shí)的介紹。小伙伴有任何問題都可留言或私聊,共同學(xué)習(xí)進(jìn)步。