Hi all,
I’m trying to wrap my head around the use of stencil buffer by doing a simple blob shadow with it but I’m not getting the expected result.
It seems simple in theory, but I’m unable to get the back facing polygons to clear the stencil buffer. Here’s my attempt using a single shader pass. Any ideas? Thanks!
Shader code
Shader "Custom/Stencil Shadow" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
}
SubShader {
Tags { "RenderType"="Opaque" "Queue"="Geometry-1"}
Pass {
// ZWrite Off
Cull Off
Lighting Off
Blend DstColor OneMinusSrcAlpha
Stencil {
Ref 0
ReadMask 1
WriteMask 1
CompFront Equal // Front
PassFront IncrSat
// FailFront Keep
// ZFailFront Keep
CompBack Greater // Back
// PassBack Keep
// FailBack Keep
ZFailBack IncrSat
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
struct appdata {
float4 vertex : POSITION;
};
struct v2f {
float4 pos : SV_POSITION;
};
v2f vert(appdata v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
return o;
}
half4 frag(v2f i) : SV_Target {
return half4(0,0,0,0.5);
}
ENDCG
}
}
FallBack "Diffuse"
}