Hole shader

Hey, I have a shader which is responsible for cutting holes in a mesh. This combined with an URP render object with the following settings. I have an issue where in some cases it shows through the object. Is it possible to prevent this, or maybe only make it work in a direction? Positive/Negative Z.

Name “Shell”
Event: AfterRenderingOpaques
Overrides Stencil: value equal to the one of the shader.
Compare function: Not equal
Pass: Keep
Fail: Keep
Z Fail: Keep

Shader "Custom/StencilMask"
{
    Properties
    {
        [IntRange] _StencilID ("Stencil ID", Range(0, 255)) = 0
    }

    SubShader
    {
        Tags { "RenderType"="Opaque" "Queue"="Geometry-1" "RenderPipeline" = "UniversalPipeline"}

        Pass
        {
            Blend Zero One
            ZWrite Off

            Stencil
            {
                Ref [_StencilID]
                Comp Always
                Pass Replace
            }
        }
    }
}

I use this with 2 plates as a model. With one having the face pointing up, other down.

It seems to work good, but possible too good. It is also showing from the sides.

9805236--1407894--upload_2024-4-30_14-52-17.png

It seems fine if I make it longer, but not with more complex meshes.

Managed to solve it with some help. Thank you all! Don’t know how to delete this post.

I am very curious of how did you solve this problem

It’s quite complex and not perfect, but I ended up using many, many URP render effects effecting specific layers, split the model into multiple parts and made sure parts are only rendered in specific ways. There are some assets on the asset store which do simple versions of this, but what I required was a bit too complex.

In the example above, you’d have to manually create the mesh and faces/triangle winding order to get this to work. I would create one mesh facing forward, but having no face on the back, then another mesh facing backwards, but not having faces on the front. Create single face circles and place them, same logic but backwards at the right locations, then make sure they are only rendered by one of the meshes you want them to be able to see through. It’s a bit hard to explain.