Stencil buffer or depth mask with shader graph

Hi all,

Wondering if it is possible to create a depthmask shader in URP’s shader graph?

For example I have a tree model and a floor plane.
I would like the floor plane to intersect the tree trunk, obscuring everything that is below the floor, revealing the skydome behind it.
Then, I would like another model, say a pond shape to reinstate the buffer, like a portal.

In this screenshot I have the tree selected, and the cap for the pond shape that reveals the pond model that extends below the floor

With custom shaders in build in renderer it is trivial. You just need a depthmask shader for the floor, and set that model to a lower render Queue value than the other objects.

SubShader {
// Render the mask after regular geometry, but before masked geometry and
// transparent things.

Tags {"Queue" = "Geometry+10" }

// Don't draw in the RGBA channels; just the depth buffer

ColorMask 0
ZWrite On

// Do nothing specific in the pass:

Stencil
{
//Ref [_Stencil]
Comp equal
Pass Zero
}

Pass {}
}
}

Then another shader to put on the pond cap that cuts through it again with an even lower queue value.

Shader "Unlit/WriteStencil"
{
Properties
{
_Stencil ("Stencil ID", int) = 0
}
SubShader
{
Tags { "RenderType"="Opaque" }
ColorMask 0
ZWrite off

Stencil
{
Ref [_Stencil]
Comp always
Pass replace
}

I have to use URP shader graph as I am trying to convert a game to Apple Vision Pro which only uses shader graph and the built in shaders, but the control you have of the depth buffer seems much more limited in URP. Am I wasting my time even looking at this?

2 Likes

Same problem here. I got this shader script, but I don’t know how to convert “Blend Zero One” to shader graph.
Please someone tell me there’s a way.

Shader "Custom/CameraCutoutURP"
{
    SubShader
    {
        Tags { "Queue" = "Geometry-10" }
    
        Pass
        {
            Blend Zero One // Turn off color
        
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            float4 vert(float4 vertex : POSITION) : SV_POSITION
            {
                return UnityObjectToClipPos(vertex);
            }

            half4 frag() : SV_Target
            {
                return half4(1, 1, 1, 1); // Color doesn't matter
            }
            ENDCG
        }
    }
}

I have the same issue, anyone figured that out?

I’m just tagging along because I’d love it if unity would support stencil buffer operations in shader graph. It’s wild that it’s not already supported.

2 Likes

You can use Renderer Feature to make it work.

Can you elaborate please.

1 Like

If you need to use stencil, you can use render objects (or something like that) renderer feature, where you can take (selected) material or shader (I think) and override it’s behaviour, including how it interacts with stencil buffer.

I am curious if this is in Shader Graph as well; I haven’t found it.

It seems like overkill to me to use Render Objects.

For now, I think I will use the Shader Graph feature that lets you output the resulting shader to HLSL. Then I’ll modify the code to do what I need.