Shader "VrchatHoldem/Standard Fade Stencil Outside"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo", 2D) = "white" {}
        _Cutoff ("Alpha Cutoff", Range(0,1)) = 0.5
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
    }

    SubShader
    {
        Tags
        {
            "Queue" = "AlphaTest+10"
            "RenderType" = "TransparentCutout"
            "IgnoreProjector" = "True"
        }

        LOD 200

        Blend Off

        // Depth was established by the depth proxy.
        ZWrite Off
        ZTest Equal

        AlphaToMask On

        // ------------------------------------------------------------
        // bits 4-7 = mask volume shell flags
        //
        // 0:
        // pixel is outside ALL mask volumes.
        // ------------------------------------------------------------
        Stencil
        {
            Ref 0
            ReadMask 240
            WriteMask 0
            Comp Equal
        }

        CGPROGRAM
        #pragma surface surf Standard fullforwardshadows keepalpha addshadow
        #pragma target 3.0

        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;
            float4 screenPos;
        };

        half _Cutoff;
        half _Glossiness;
        half _Metallic;
        fixed4 _Color;


        float Bayer2(float2 pixel)
        {
            float2 parity = fmod(floor(pixel), 2.0);

            return parity.x * 2.0
                 + parity.y * 3.0
                 - parity.x * parity.y * 4.0;
        }


        float Bayer8(float2 pixel)
        {
            float rank = Bayer2(pixel) * 16.0;
            rank += Bayer2(floor(pixel * 0.5)) * 4.0;
            rank += Bayer2(floor(pixel * 0.25));

            return (rank + 0.5) / 64.0;
        }


        void surf(Input IN, inout SurfaceOutputStandard o)
        {
            fixed4 textureColor =
                tex2D(_MainTex, IN.uv_MainTex);

            float2 screenUV =
                IN.screenPos.xy / IN.screenPos.w;

            float2 screenPixel =
                floor(screenUV * _ScreenParams.xy);


            // Existing whole-card fade via _Color.a.
            clip(
                _Color.a -
                Bayer8(screenPixel)
            );


            // Existing alpha-cutout AA.
            half alphaWidth =
                max(
                    fwidth(textureColor.a),
                    1.0h / 255.0h
                );

            half edgeCoverage =
                saturate(
                    (textureColor.a - _Cutoff)
                    / alphaWidth
                    + 0.5h
                );

            clip(
                edgeCoverage
                - 1.0h / 255.0h
            );


            o.Albedo =
                textureColor.rgb * _Color.rgb;

            o.Metallic =
                _Metallic;

            o.Smoothness =
                _Glossiness;

            o.Alpha =
                edgeCoverage;
        }

        ENDCG
    }

    FallBack "Standard"
}