Shader "Custom/AlphaTestShader"{
Properties{
_MainTex("MainTex",2D)="white"{}
_Cutoff("AlphaScale",Range(0,1))=0.5
}
SubShader{
Tags{
//渲染顺序2450
"Quene"="AlphaTest"
//忽略投影
"IgroneProjector"="True"
//透明度裁切渲染模式
"RenderType"="TransparentCutout"
}
Pass{
Tags{
//前向渲染
"LightMode"="ForwardBase"
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
//2D图形采样
sampler2D _MainTex;
//2D图形 缩放+平移变量
float4 _MainTex_ST;
//裁切透明度
fixed _Cutoff;
struct a2v{
//模型空间的顶点位置
float4 vertex:POSITION;
//模型空间的法线方向
float3 normal:NORMAL;
//原始UV
float4 texcoord:TEXCOORD0;
};
struct v2f{
//屏幕裁切空间位置
float4 pos:SV_POSITION;
//世界法线,没有归一化
float3 worldNormal:TEXCOORD0;
//世界坐标位置,没有归一化
float3 worldPos:TEXCOORD1;
//材质的UV属性
float2 uv:TEXCOORD2;
};
v2f vert(a2v v){
v2f o;
//将顶点从模型空间转裁切空间
o.pos=UnityObjectToClipPos(v.vertex);
//将法线从模型空间转到世界空间
o.worldNormal=UnityObjectToWorldNormal(v.normal);
//将顶点从模型空间转倒世界空间
o.worldPos=mul(unity_ObjectToWorld,v.vertex).xyz;
//获取UV
o.uv=TRANSFORM_TEX(v.texcoord,_MainTex);
return o;
}
fixed4 frag(v2f i):SV_TARGET{
//获取材质的颜色
fixed4 texColor=tex2D(_MainTex,i.uv);
//材质本身的颜色
fixed3 albedo=texColor.rgb;
//alphaTest 将clip <0的直接过滤掉
clip(texColor.a-_Cutoff);
//环境光*材质颜色
fixed3 ambient=UNITY_LIGHTMODEL_AMBIENT.xyz*albedo;
//归一化的世界法线
fixed3 worldNormal=normalize(i.worldNormal);
//归一化的光照,输入世界坐标位置
fixed3 worldLight=normalize(UnityWorldSpaceLightDir(i.worldPos));
//漫反射=灯光颜色*材质颜色*灯光在法线上的投影
fixed3 diffuse=_LightColor0.rgb*albedo*saturate(dot(worldNormal,worldLight));
//漫反射+自发光,透明度是1
return fixed4(diffuse+ambient,1);
}
ENDCG
}
}
Fallback "Transparent/Cutout/VertexLit"
}