Shader "VrchatHoldem/CardOutlineAlpha"
{
    Properties
    {
        _MainTex ("Card Texture", 2D) = "white" {}
        [HDR]_OutlineColor ("Outline Color", Color) = (1,1,0,1)
        _Cutoff ("Alpha Cutoff", Range(0,1)) = 0.5
        _Alpha ("Fade Alpha (animated)", Range(0,1)) = 1
        _DepthTolerance ("Depth Tolerance", Float) = 0
        [HideInInspector]_DepthTest ("Depth Test", Float) = 4
    }

    SubShader
    {
        Tags { "Queue"="AlphaTest" "RenderType"="TransparentCutout" }
        LOD 100

        Pass
        {
            Name "Outline"
            Cull Back
            ZWrite Off
            ZTest [_DepthTest]

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma multi_compile_instancing
            #include "UnityCG.cginc"

            sampler2D _MainTex;
            fixed4 _OutlineColor;
            fixed _Cutoff;
            fixed _Alpha;
            float _DepthTolerance;

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
                float2 uv : TEXCOORD0;
                UNITY_VERTEX_OUTPUT_STEREO
            };

            v2f vert(appdata v)
            {
                v2f o;
                UNITY_SETUP_INSTANCE_ID(v);
                UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);

                float4 clipPosition = UnityObjectToClipPos(v.vertex);
                float4 viewPosition = mul(UNITY_MATRIX_MV, v.vertex);
                viewPosition.z -= _DepthTolerance;
                float4 shiftedClipPosition = mul(UNITY_MATRIX_P, viewPosition);

                // Keep the original screen position while testing at a virtual depth
                // farther from the camera.
                clipPosition.z = shiftedClipPosition.z * clipPosition.w / shiftedClipPosition.w;

                o.vertex = clipPosition;
                o.uv = v.uv;
                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
                float alpha = tex2D(_MainTex, i.uv).a * _Alpha;
                clip(alpha - _Cutoff);
                return _OutlineColor;
            }
            ENDCG
        }
    }
}
