#version 330 core
// Purpose: Lens flare package fragment shader; renders procedural source dots, ghosts, halos, and streaks.

uniform vec3 uColor;
uniform float uIntensity;
uniform int uKind;

in vec2 vLocal;

out vec4 oColor;

float saturate(float v)
{
    return clamp(v, 0.0, 1.0);
}

void main()
{
    float r = length(vLocal);
    float mask = smoothstep(1.0, 0.0, r);
    float alpha = 0.0;

    if (uKind == 0) {
        float core = exp(-r * r * 3.8);
        float glow = exp(-r * r * 0.72) * 0.42;
        float ring = exp(-pow((r - 0.58) * 7.0, 2.0)) * 0.18;
        alpha = (core + glow + ring) * mask;
    } else if (uKind == 1) {
        float disc = exp(-r * r * 2.2) * 0.48;
        float ring = exp(-pow((r - 0.62) * 8.5, 2.0)) * 0.48;
        alpha = (disc + ring) * mask;
    } else if (uKind == 2) {
        float halo = exp(-pow((r - 0.73) * 5.2, 2.0)) * 0.58;
        halo += exp(-r * r * 0.5) * 0.15;
        alpha = halo * mask;
    } else {
        float horizontal = exp(-abs(vLocal.y) * 18.0) * smoothstep(1.0, 0.0, abs(vLocal.x));
        float vertical = exp(-abs(vLocal.x) * 32.0) * smoothstep(0.55, 0.0, abs(vLocal.y));
        alpha = horizontal + vertical * 0.22;
    }

    alpha = saturate(alpha * uIntensity);
    if (alpha <= 0.001) {
        discard;
    }

    oColor = vec4(uColor * alpha, alpha);
}
