Shader "VrchatHoldem/CardMaskVolume"
{
    Properties
    {
        // One stencil bit per shell, living in the high nibble so we
        // never collide with common avatar stencil values (bits 0-3).
        //
        //   Shell0 (outer)  -> 16  (0x10, bit 4)
        //   Shell1          -> 32  (0x20, bit 5)
        //   Shell2          -> 64  (0x40, bit 6)
        //   Shell3 (inner)  -> 128 (0x80, bit 7)
        //
        // Note: stencil IncrWrap/DecrWrap always carry at bit 0, so an
        // offset wrap-counter is impossible; hence one bit per shell.
        _StencilRef ("Stencil Bit", Float) = 16
    }

    SubShader
    {
        Tags
        {
            // Must render AFTER the card depth prepass,
            // but BEFORE the card colour/fade passes.
            "Queue" = "AlphaTest"
            "RenderType" = "Opaque"
        }


        // ============================================================
        // PASS 1 - SET
        //
        // Renders only the BACK (far) walls. When a far wall ends up
        // BEHIND the card pixel (z-fail), the pixel is inside this
        // shell, so we set this shell's bit.
        // ============================================================
        Pass
        {
            Name "CardMaskVolumeSet"

            ColorMask 0
            ZWrite Off

            // Back faces only (far walls, seen from inside too).
            Cull Front

            // Compare against the card depth written by the prepass.
            ZTest LEqual

            Stencil
            {
                Ref [_StencilRef]
                ReadMask 240
                WriteMask [_StencilRef]

                Comp Always

                PassFront Keep
                PassBack  Keep

                FailFront Keep
                FailBack  Keep

                ZFailFront Keep
                ZFailBack  Replace
            }

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma target 3.0

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
            };

            v2f vert(appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                // ColorMask 0 means this is never actually written.
                return 0;
            }

            ENDCG
        }


        // ============================================================
        // PASS 2 - CLEAR
        //
        // Renders only the FRONT (near) walls. When a near wall ends up
        // BEHIND the card pixel (z-fail), the pixel is NOT inside this
        // shell, so we clear this shell's bit.
        //
        // Runs after SET, so the near-side case (both walls behind the
        // card) is set then immediately cleared back to 0.
        // ============================================================
        Pass
        {
            Name "CardMaskVolumeClear"

            ColorMask 0
            ZWrite Off

            // Front faces only (near walls; behind the camera when the
            // camera is itself inside a shell).
            Cull Back

            ZTest LEqual

            Stencil
            {
                Ref 0
                ReadMask 240
                WriteMask [_StencilRef]

                Comp Always

                PassFront Keep
                PassBack  Keep

                FailFront Keep
                FailBack  Keep

                ZFailFront Replace
                ZFailBack  Keep
            }

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma target 3.0

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
            };

            v2f vert(appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                return 0;
            }

            ENDCG
        }
    }

    FallBack Off
}
