Hi! I am trying to build a shader that will only cast a shadow but not render the object itself. There are various attempts in Unity Answers, the forums and various spots on the web, but nobody actually seems to have achieved this effect. The closest I got so far is this: Code (CSharp): Shader "Custom/Shadow Only" { SubShader { Pass { Tags { "LightMode" = "ShadowCaster" } CGPROGRAM #pragma vertex vert #pragma fragment frag float4 vert(float4 v:POSITION) : SV_POSITION { return mul (UNITY_MATRIX_MVP, v); //return float4(0.0,0.0,0.0,1.0); } fixed4 frag() : COLOR { //discard; return fixed4(0.0,0.0,0.0,1.0); } ENDCG } } } As you will notice, it obscures the shadow of any object behind it and itself. For some reason, there's also a strange moiré pattern on some of its faces:
It's work properly only with deferred rendering, at least in U5b. As far i remember in 4.6 it's work fine. Code (csharp): Shader "Custom/ShadowOnly" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} } SubShader { Pass { Name "ShadowCaster" Tags { "LightMode" = "ShadowCaster" } Fog {Mode Off} ZWrite On ZTest LEqual Cull Off Offset 1, 1 CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile_shadowcaster #include "UnityCG.cginc" struct v2f { V2F_SHADOW_CASTER; }; v2f vert( appdata_base v ) { v2f o; TRANSFER_SHADOW_CASTER(o) return o; } float4 frag( v2f i ) : SV_Target { SHADOW_CASTER_FRAGMENT(i) } ENDCG } } } add Well is some with what inside answers link.
Thank you. In deferred mode, my attempt seems to work, too. I'll go with mine for now but keep this bookmarked in case I run into trouble. If anyone has a suggestion for a method that works in both forward and deferred rendering, I'm still interested
4.5.4f1 lol they have changed the way shadows work under the hood in unity 5.0 though, Ill have a play with this at work when we try the beta of 5.0 in a week or so
Not sure where I found it but just disable receive shadows on the object and try this: Code (CSharp): Shader "Transparent/InvisibleShadowCaster" { Subshader { UsePass "VertexLit/SHADOWCOLLECTOR" UsePass "VertexLit/SHADOWCASTER" } Fallback off }