赞
踩
Shader "Unlit/shadow" { Properties { _MainTex ("Texture", 2D) = "white" {} } SubShader { Tags { "RenderType"="Opaque" } LOD 100 Pass { Tags{"LightMode" = "ForwardBase"} CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile_fog #pragma multi_compile_fwdbase // 非常重要,不然没法正确计算阴影 #include "UnityCG.cginc" #include "Lighting.cginc" #include "AutoLight.cginc" struct appdata { float4 vertex : POSITION; float3 normal : NORMAL; float2 uv : TEXCOORD0; }; struct v2f { float2 uv : TEXCOORD0; float4 vertex_world : TEXCOORD1; float3 normal : NORMAL; float4 pos : SV_POSITION; SHADOW_COORDS(2) // 2 即 TEXCOORD2 }; sampler2D _MainTex; float4 _MainTex_ST; v2f vert (appdata v) { v2f o; o.vertex_world = v.vertex; o.pos = UnityObjectToClipPos(v.vertex); o.uv = TRANSFORM_TEX(v.uv, _MainTex); o.normal = v.normal; TRANSFER_SHADOW(o) // 填充阴影坐标,根据平台自动调整 return o; } fixed4 frag (v2f i) : SV_Target { fixed4 col = tex2D(_MainTex, i.uv); fixed shadow = SHADOW_ATTENUATION(i); // 根据平台自己得到相应值 float4 lightColor = _LightColor0; float3 lightDir = WorldSpaceLightDir(i.vertex_world).xyz; return col * lightColor * saturate(dot(i.normal, normalize(lightDir))) * shadow; // shadow 和 最终颜色乘上就可 } ENDCG } } FallBack "Specular" }
效果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。