URP Stencil using code

Hello, I have a handful of shaders that needs to be used in the Stencil Buffer, but I don’t want to use a Layer to mask them using the Custom SRP.

The block of code I’m using for the Mask is the following

Shader "Alien_Spawn_Mask"
{
    Properties
    {
       
    }
    SubShader
    {
        Tags
        {
            "RenderType" = "Opaque"
            "RenderPipeline" = "UniversalPipeline"
            "Queue" = "Geometry"
        }

        Pass
        {
            Blend Zero One
            ZWrite Off
           
            Stencil
            {
                Ref 100
                Comp Always
                Pass Replace
                Fail Keep
            }
        }
    }
}

And the object that I want to mask is using this code

            Pass
            {

                Name "StandardLit"
               

                Blend[_SrcBlend][_DstBlend]
                ZWrite[_ZWrite]
                Cull[_Cull]

                Stencil
            {
                Ref 100
                Comp Equal
                Pass Keep
                Fail Keep
            }

                HLSLPROGRAM

The result is that the mask is not showing, as intended, but the object that I want to mask is showing entirely without masking.

Is there any way to solve it?

1 Like

Hard to say without more info, but here’s some guesses as to what may be going wrong.

  • make sure the stencil is being drawn first (render queue, can set in material)
  • maybe you want ztest always on the stencil mask?