#ifndef SHOULDER_PANEL_DITHER_INCLUDED
#define SHOULDER_PANEL_DITHER_INCLUDED

// Distance fade shared by the shoulder panel background and text. The fade is measured from the camera to the
// renderer's pivot on the horizontal plane, so every pixel of a panel fades together, and it is applied as an
// ordered (Bayer 4x4) dither: pixels are clipped rather than blended, which needs no sorting and costs no CPU.
float _FadeNear;
float _FadeFar;

float ShoulderPanelFade()
{
    float3 pivot = float3(unity_ObjectToWorld[0].w, unity_ObjectToWorld[1].w, unity_ObjectToWorld[2].w);
    float dist = length(pivot.xz - _WorldSpaceCameraPos.xz);
    return saturate((_FadeFar - dist) / max(_FadeFar - _FadeNear, 0.0001));
}

void ShoulderPanelDitherClip(float4 screenPosition)
{
    static const float bayer[16] = {
         0.0,  8.0,  2.0, 10.0,
        12.0,  4.0, 14.0,  6.0,
         3.0, 11.0,  1.0,  9.0,
        15.0,  7.0, 13.0,  5.0 };
    uint2 cell = (uint2)floor(screenPosition.xy) % 4;
    clip(ShoulderPanelFade() - (bayer[cell.y * 4 + cell.x] + 0.5) / 16.0);
}

#endif
