Depth Mask per object?

Let’s imagine I have 4 cubes in the scene. Cube A, B, C, and D.
Cube B is inside of cube A
Cube C is inside of cube B
Cube D is inside of cube C

If we look at it hierarchically we would get something like this:
Cube A
----Cube B
--------Cube C
------------Cube D

Please refer to the attached images to understand the setup for the issue. The left image is what you’re looking for. You can also think of these as Russian babushkas, it’s the same principle.

Now, what would I like to do with this? I would like to have an option to hide parts of one cube to reveal all the others. Basically, cut away a part of the red cube (A) to reveal the blue cube (B) underneath and so on and so on until we can cut away all cubes to reveal what they contain. Sorry if I explained this poorly.
These meshes don’t actually need to be cut, they simply need to be hidden.
I’ve tried using depth masks for this and it somewhat works (

). I can hide one cube with a certain queue but when I try to hide others with separate depth masks they end up hiding the cubes underneath.

Don’t really have the code for this, since it’s just something that I’m doing manually in the inspector at the moment.

Have you checked stencil buffers? I’m not sure if they would work for your specific case but check them if you haven’t yet.

Without watching your whole video it seems like stencil buffers are exactly what you need. You have 255 stencil bits and you can render a stencil mask with any arbitrary geometry and test against it however you want (equal, greater than, less than, etc.) in any other shader. If you control the render order and what values you are setting/testing in the stencil buffer, you have a lot of flexibility to cut holes in geometry.

I’ve gotten pretty close to this by following some documentation. Here’s how it currently looks like:
5117927--505346--CurrentState.PNG

I have a red ball which should represent a single cube I mentioned above. I also have this ‘empty’ ball on the right which should be my mask. The right ball will change the material of the red ball to the green on any place they intersect, I don’t want that. Instead, I would like the right ball to ‘hide’ the left ball, so instead of having green in the place where they intersect, we would have nothing. Here are my current shaders:

Shader "Custom/Green" {
    Properties{
        [IntRange] _StencilRef("Stencil Reference Value", Range(0,255)) = 0
    }
    SubShader{
        Tags { "RenderType" = "Opaque" "Queue" = "Geometry+1"}
        Pass {
            Stencil {
                Ref [_StencilRef]
                Comp equal
                Pass keep
            }

            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
        }
    }
}
Shader "Custom/Red" {
    Properties{
        [IntRange] _StencilRef("Stencil Reference Value", Range(0,255)) = 0
    }
    SubShader{
        Tags { "RenderType" = "Opaque" "Queue" = "Geometry"}
        Pass {
            Stencil {
                Ref[_StencilRef]
                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
        }
    }
}

Thanks!