細節慢慢補充,有幾個須要注意的地方,必需要有接收投影的pass也就是Name是ShadowCollector的,必須添加#pragma multi_compile_fwdbase,物體的着色器必須有Tag 裏面的 {"LightMode" = "ForwardBase"}。處理投影要在接收投影的物體shader裏。app
隨後是三個宏。spa
SHADOW_COORDS這個宏後面的參數是指第幾個通道,不要和其餘的出現衝突,也就是要改變投影的顏色話必需要佔用一個通道。code
在頂點處理器裏調用TRANSFER_SHADOW。blog
而後像素着色器調用SHADOW_ATTENUATION,返回的就是這個像素是否存在陰影中。get
可改變投影顏色的範例以下。it
Shader "Example/ShadowColor" { Properties { _MainColor("MainColor",color) = (0.5,0.5,0.5,1) _ShadowColor("ShadowColor",color) = (1,1,0,1) } SubShader { Tags {"RenderType"="Opaque"} Pass { Tags {"LightMode" = "ForwardBase"} CGPROGRAM #pragma multi_compile_fwdbase #include "UnityCG.cginc" #include "Lighting.cginc" #include "AutoLight.cginc" #pragma vertex vertexMain #pragma fragment fragmentMain fixed4 _MainColor; fixed4 _ShadowColor; struct VertexToFragment { float4 pos : SV_POSITION; half4 uv : TEXCOORD0; SHADOW_COORDS(1) }; VertexToFragment vertexMain (appdata_full v) { VertexToFragment result = (VertexToFragment)0; result.pos = mul(UNITY_MATRIX_MVP, v.vertex); result.uv.xy = v.texcoord.xy; TRANSFER_SHADOW(result) return result; } fixed4 fragmentMain (VertexToFragment i) : COLOR0 { float attenuation = SHADOW_ATTENUATION(i); return lerp(_MainColor*_ShadowColor,_MainColor,attenuation); } ENDCG } Pass { Name "ShadowCollector" Tags { "LightMode" = "ShadowCollector" } Fog {Mode Off} ZWrite On ZTest LEqual CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile_shadowcollector #define SHADOW_COLLECTOR_PASS #include "UnityCG.cginc" struct appdata { float4 vertex : POSITION;}; struct v2f { V2F_SHADOW_COLLECTOR;}; v2f vert (appdata v) { v2f o; TRANSFER_SHADOW_COLLECTOR(o) return o; } fixed4 frag (v2f i) : SV_Target { SHADOW_COLLECTOR_FRAGMENT(i)} ENDCG } } }