Shadowcaster Fragment Shader Discard
Hello all –
I'm currently working through some bugs with one of my shaders. This shader draws meshes through windows in space. If the window is located behind the object, I don't want the mesh to render.
My first render in my entire renderpass renders the Z values of all of the portals into a depth buffer. I think sample that depth buffer to determine if the fragment being rendered is located in front or behind the portal depth.
Unfortunately, this seems to cull the entire shadow instead of just the area covered by the portal. I know this same logic works in a non-shadow shader, so I am sure that I am just not knowledgeable in this subject.
Here is the shader. Thank you for your help! // Pass to render object as a shadow caster Pass { Name "ShadowCaster" Tags { "LightMode" = "ShadowCaster" }
Fog {Mode Off}
ZWrite On ZTest LEqual Cull Off
Offset 0, 0
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_shadowcaster
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
#include "AutoLight.cginc"
sampler2D _TempDepthTex;
struct v2f {
V2F_SHADOW_CASTER;
float4 screenPos : TEXCOORD0;
float4 posWorld : TEXCOORD2;
float2 depth : TEXCOORD1;
};
v2f vert( appdata_base v )
{
v2f o;
TRANSFER_SHADOW_CASTER(o);
o.posWorld = mul(UNITY_MATRIX_MVP, v.vertex);
o.depth = o.posWorld.zw;
o.screenPos = ComputeScreenPos(o.posWorld);
return o;
}
float4 frag( v2f i ) : COLOR
{
i.screenPos.xy /= i.screenPos.w;
float4 portalDepthXy = tex2D(_TempDepthTex, i.screenPos.xy);
float portalDepth = portalDepthXy.x;
float newDepth = i.depth.x / i.depth.y;
if (newDepth > portalDepth)
{
discard;
}
SHADOW_CASTER_FRAGMENT(i)
}
ENDCG
} //Pass
Did you ever solve this? I think I'm hitting a similar issue.
I think, I got a similar problem; did you already find a solution?
Your answer
![](https://koobas.hobune.stream/wayback/20220612033918im_/https://answers.unity.com/themes/thub/images/avi.jpg)