Unity shader学习之Alpha Test的阴影
Alpha Test的阴影,
shader如下:
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)' Shader "Custom/Alpha Test Shadow" { Properties { _Color ("Main Color", Color) = (1,1,1,1) // 必需 _MainTex("Main Texture", 2D) = "white" {} _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5 // 必需 } SubShader { Tags { "Queue" = "AlphaTest" "RenderType" = "TransparentCutout" } Pass { Tags { "LightMode" = "ForwardBase" } Cull Off // 背面 CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile_fwdbase #include "UnityCG.cginc" #include "Lighting.cginc" #include "AutoLight.cginc" float4 _Color; sampler2D _MainTex; float _Cutoff; struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; float3 normal : NORMAL; }; struct v2f { float4 pos : SV_POSITION; float2 uv : TEXCOORD0; float3 worldNormal : TEXCOORD1; float3 worldPos : TEXCOORD2; SHADOW_COORDS(3) }; v2f vert(appdata v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.uv = v.uv; o.worldNormal = UnityObjectToWorldNormal(v.normal); o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz; TRANSFER_SHADOW(o); return o; } fixed4 frag(v2f i) : SV_TARGET { fixed4 albedo = tex2D(_MainTex, i.uv) * _Color; clip(albedo.a - _Cutoff); fixed3 ambient = albedo.rgb * UNITY_LIGHTMODEL_AMBIENT.rgb; float3 worldLight = UnityWorldSpaceLightDir(i.worldPos); fixed3 diff = albedo.rgb * _LightColor0.rgb * max(0, dot(i.worldNormal, worldLight)); UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos); fixed3 col = ambient + diff * atten; return fixed4(col, 1); } ENDCG } } Fallback "Transparent/Cutout/VertexLit" // 必需,阴影投射pass }
效果如下: