#version 330 core

in vec2 vUV;
in vec2 vLocal;

out vec4 FragColor;

uniform sampler2D uMask;
uniform vec4 uFillColor;
uniform vec4 uGlowColor;
uniform float uAlpha;
uniform float uGlow;
uniform float uEdge;
uniform float uTime;
uniform float uReveal;
uniform float uScanSpeed;
uniform vec2 uTexelSize;

void main()
{
    float mask = texture(uMask, vUV).a;

    float neighbor = 0.0;
    neighbor = max(neighbor, texture(uMask, vUV + vec2(uTexelSize.x, 0.0)).a);
    neighbor = max(neighbor, texture(uMask, vUV - vec2(uTexelSize.x, 0.0)).a);
    neighbor = max(neighbor, texture(uMask, vUV + vec2(0.0, uTexelSize.y)).a);
    neighbor = max(neighbor, texture(uMask, vUV - vec2(0.0, uTexelSize.y)).a);

    float edge = max(0.0, neighbor - mask);
    float reveal = 1.0 - smoothstep(uReveal, uReveal + 0.08, vLocal.x);
    float scan = 0.82 + 0.18 * sin((vLocal.y * 90.0) + uTime * uScanSpeed);

    vec3 rgb = uFillColor.rgb * scan;
    float alpha = mask * uFillColor.a;

    rgb += uGlowColor.rgb * edge * uGlow;
    alpha += edge * uGlowColor.a * uGlow * uEdge;
    alpha *= reveal * uAlpha;

    if (alpha <= 0.001)
        discard;

    FragColor = vec4(rgb, clamp(alpha, 0.0, 1.0));
}
