Shader "VrchatHoldem/Standard Fade Depth"
{
    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"
            "RenderType" = "TransparentCutout"
            "IgnoreProjector" = "True"
        }
        LOD 200

        Blend Off
        ZWrite On
        AlphaToMask On

        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);
            clip(_Color.a - Bayer8(screenPixel));

            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"
}
