// Highlight for the selected proxy chips of PhysicalRaisePile. Used by two materials on every proxy chip:
//   ChipHighlightFill    - _OutlineWidth 0, Cull Back,  queue 3000: translucent tinted body.
//   ChipHighlightOutline - _OutlineWidth >0, Cull Front, queue 3001: solid shell grown outwards from the chip.
// Both write the same stencil bit and only draw where it is still clear, so across all the chips in the stack the
// body is blended once, the outline is drawn once and only around the union of the chips (one silhouette, no seams
// between chips). Because the outline queue is higher, every chip's body is in the stencil before any outline draws.
Shader "VrchatHoldem/ChipHighlight"
{
    Properties
    {
        [HDR]_Color ("Color", Color) = (1,1,0,0.25)
        _OutlineWidth ("Outline Width (metres, view plane)", Float) = 0
        _DepthTolerance ("Depth Tolerance", Float) = 0.002
        [Enum(UnityEngine.Rendering.CullMode)]_Cull ("Cull", Float) = 2
        [HideInInspector]_DepthTest ("Depth Test", Float) = 4
    }

    SubShader
    {
        // DisableBatching: the vertex shader works in the chip's object space (radial/axial outline direction). Dynamic
        // batching bakes several chips into one world-space mesh, which sent the outline in the wrong direction as soon
        // as more than one proxy chip was lit.
        Tags { "Queue"="Transparent" "RenderType"="Transparent" "IgnoreProjector"="True" "DisableBatching"="True" }
        LOD 100

        Pass
        {
            Name "Highlight"
            Cull [_Cull]
            ZWrite Off
            ZTest [_DepthTest]
            Blend SrcAlpha OneMinusSrcAlpha

            Stencil
            {
                Ref 32
                ReadMask 32
                WriteMask 32
                Comp NotEqual
                Pass Replace
            }

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

            fixed4 _Color;
            float _OutlineWidth;
            float _DepthTolerance;

            struct appdata
            {
                float4 vertex : POSITION;
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
                UNITY_VERTEX_OUTPUT_STEREO
            };

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

                // Grow the silhouette by _OutlineWidth (metres) in the plane facing the camera. The direction is the
                // vertex's way out of the prism (radially for the rim, along the axis for the caps) projected into
                // view space and normalised, so the outline is the same width all the way round whatever the viewing
                // angle. Pushing along the 3D direction instead made it thicker where the chip's cap faces the camera
                // (the top chip) and thinner where neighbouring chips hide the axial part.
                float3 p = v.vertex.xyz;
                float radial = length(p.xy);
                float2 outwards = radial > 1e-6 ? p.xy / radial : float2(0, 0);
                float3 outDir = float3(outwards, sign(p.z));
                float2 viewDir = mul((float3x3)UNITY_MATRIX_MV, outDir).xy;
                float viewDirLength = length(viewDir);
                viewDir = viewDirLength > 1e-5 ? viewDir / viewDirLength : float2(0, 0);

                float4 vertex = float4(p, 1);
                float4 viewPosition = mul(UNITY_MATRIX_MV, vertex);
                viewPosition.xy += viewDir * _OutlineWidth;

                // The proxy sits exactly on the real chip, so test at a slightly farther virtual depth to avoid
                // z-fighting while keeping the on-screen position.
                float4 clipPosition = mul(UNITY_MATRIX_P, viewPosition);
                viewPosition.z -= _DepthTolerance;
                float4 shiftedClipPosition = mul(UNITY_MATRIX_P, viewPosition);
                clipPosition.z = shiftedClipPosition.z * clipPosition.w / shiftedClipPosition.w;

                o.vertex = clipPosition;
                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
                return _Color;
            }
            ENDCG
        }
    }
}
