I’ve recently got the hang of Stencils, and use a lot of unlit materials to practice writing them. I have 2 stencil masks I’m using to learn - and they seem to work just fine on regular unlit shaders. It’s a simple hole cutting shader with a front culled mesh drawn inside of it.
The result is the following:
2 Cylinders; 1 used as a mask to cut out any geometry above the cylinder, the other being the actual fill shader for the parts of the cylinder not cut. Simple stuff.
The code for the shaders;
Hole Cutout:
Shader "Stencils/HoleCutout" {
SubShader {
Tags { "RenderPipeline"="HDRenderPipeline" "RenderType"="Opaque" "Queue"="Geometry+1"}
ColorMask 0
ZWrite Off
Stencil {
Ref 66
Comp always
Pass replace
}
//Basic Colour
CGINCLUDE
#include "UnityCG.cginc"
struct appdata {
float4 vertex : POSITION;
};
struct v2f {
float4 pos : SV_POSITION;
};
v2f vert(appdata v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
return o;
}
half4 frag(v2f i) : SV_Target {
return half4(1,0,0,1);
}
ENDCG
Pass {
Cull Front
ZTest Less
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
ENDCG
}
Pass {
Cull Back
ZTest Greater
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
ENDCG
}
}
}
Hole Fill:
Shader "Stencils/Hole" {
SubShader {
Tags { "RenderType"="Opaque" "Queue"="Geometry+2"}
Pass {
ColorMask RGB
Cull Front
ZTest Always
Stencil {
Ref 66
Comp notequal
}
CGPROGRAM
#include "UnityCG.cginc"
#pragma surf Lambert
#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 = UnityObjectToClipPos(v.vertex);
return o;
}
half4 frag(v2f i) : SV_Target {
return half4(0,0,1,1);
}
ENDCG
}
}
}
Why is it whenever I add the stencil code to a HDRP Lit Shader, I get the following result?
And whenever I move the camera around:

As another important note; without Alpha-clipping the non-culled part of the geometry also fails to draw. I’m assuming alpha-clip is necessary to cull parts the mesh - and all would be good, but I can’t for the life of me figure out why any kind of transparent mask (with ZDepth on) seems to produce this result.
This isn’t just limited to this shader but any other shader I’ve tried to produce which masks (culls) part of a mesh so I can draw other meshes over it has the exact same result.
Does HDRP Lit even work with transparent masks?
Just in case, code inside HDRP Lit Hole:
Pass
{
Name "GBuffer"
Tags { "LightMode"="GBuffer" }
Cull Front
ZTest Always
//ZWrite On
Stencil {
Ref 66
Comp notequal
}
ColorMask [_LightLayersMaskBuffer4] 4
ColorMask [_LightLayersMaskBuffer5] 5
HLSLPROGRAM

