I have a simple shader (Red and Blue) which renders the green geometry where the mesh mask (green) intersects with the helmet (red) geometry. I’m trying to port this shader code to the HDRP Lit material but I have had complications.
Basic Shader (Works fine)
Stencil code put on a HDRP Lit Shader in GBuffer Pass:
Red Shader:
Shader "Stencils/Red" {
SubShader {
Tags { "RenderType"="Opaque" "Queue"="Geometry"}
Pass {
Stencil {
Ref 254
Comp always
Pass replace
}
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 = UnityObjectToClipPos(v.vertex);
return o;
}
half4 frag(v2f i) : SV_Target {
return half4(1,0,0,1);
}
ENDCG
}
}
}
Green Shader:
Shader "Stencils/Green" {
SubShader {
Tags { "RenderType"="Opaque" "Queue"="Geometry+1"}
Pass {
Stencil {
Ref 254
Comp equal
Pass keep
ZFail decrWrap
}
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 = UnityObjectToClipPos(v.vertex);
return o;
}
half4 frag(v2f i) : SV_Target {
return half4(0,1,0,1);
}
ENDCG
}
}
}
The GBuffer Pass on my Red HDRP Lit Shader:
Pass
{
Name "GBuffer"
Tags { "LightMode"="GBuffer" }
Cull [_CullMode]
ZTest [_ZTestGBuffer]
Stencil
{
Ref 254
Comp always
Pass replace
}
ColorMask [_LightLayersMaskBuffer4] 4
ColorMask [_LightLayersMaskBuffer5] 5
HLSLPROGRAM
The GBuffer Pass on my Green HDRP Lit Shader:
Pass
{
Name "GBuffer"
Tags { "LightMode"="GBuffer" }
Cull [_CullMode]
ZTest LEqual
Stencil {
Ref 254
Comp equal
Pass keep
ZFail decrWrap
}
ColorMask [_LightLayersMaskBuffer4] 4
ColorMask [_LightLayersMaskBuffer5] 5
HLSLPROGRAM
Now the very very strange thing is when I put the Red Shaders stencil code in the HDRP Lit version, it seems to have no problem picking up the original Unlit Green shader, but will not pick up the Lit version:
But for some reason the Green HDRP Lit version seems to not be rendered at all? Why would one stencil work and not the other? I don’t understand where I’ve gone wrong.





