#version 330 core

in vec2 vUv;
in float vAlpha;

out vec4 FragColor;

uniform sampler2D uFontTexture;
uniform float uAlpha;
uniform int uRenderMode;
uniform vec2 uTextureTexelSize;
uniform vec3 uGlowColor;
uniform float uGlowIntensity;
uniform float uGlowRadius;

void main()
{
    vec4 color = texture(uFontTexture, vUv);
    if (uRenderMode == 1)
    {
        vec2 texel = uTextureTexelSize * max(uGlowRadius, 0.0);
        float baseAlpha = color.a;
        float glow = 0.0;
        glow += texture(uFontTexture, vUv + vec2(-texel.x, 0.0)).a;
        glow += texture(uFontTexture, vUv + vec2(texel.x, 0.0)).a;
        glow += texture(uFontTexture, vUv + vec2(0.0, -texel.y)).a;
        glow += texture(uFontTexture, vUv + vec2(0.0, texel.y)).a;
        glow += texture(uFontTexture, vUv + vec2(-texel.x, -texel.y)).a * 0.8;
        glow += texture(uFontTexture, vUv + vec2(-texel.x, texel.y)).a * 0.8;
        glow += texture(uFontTexture, vUv + vec2(texel.x, -texel.y)).a * 0.8;
        glow += texture(uFontTexture, vUv + vec2(texel.x, texel.y)).a * 0.8;
        glow = max(0.0, glow / 7.2 - baseAlpha * 0.35);

        float glowAlpha = glow * uAlpha * vAlpha * uGlowIntensity;
        if (glowAlpha <= 0.001)
            discard;

        FragColor = vec4(uGlowColor, glowAlpha);
        return;
    }

    color.a *= uAlpha * vAlpha;

    if (color.a <= 0.001)
        discard;

    FragColor = color;
}
