#version 330

in vec2 fragTexCoord;
in vec4 fragColor;

uniform sampler2D texture0;
uniform float radius;
uniform float alpha;
uniform float texelW;

out vec4 finalColor;

void main() {
    vec4 color;
    if (radius < 0.5) {
        color = texture(texture0, fragTexCoord);
    } else {
        color = vec4(0.0);
        float total = 0.0;
        int spread = min(int(radius * 3.0), 48);
        for (int i = -spread; i <= spread; i++) {
            float w = exp(-0.5 * float(i * i) / (radius * radius));
            color += texture(texture0, fragTexCoord + vec2(float(i) * texelW, 0.0)) * w;
            total += w;
        }
        color /= total;
    }
    // Render texture stores premultiplied alpha; un-premultiply so BLEND_ALPHA composites correctly.
    if (color.a > 0.001)
        finalColor = vec4(color.rgb / color.a, color.a * alpha);
    else
        finalColor = vec4(0.0);
}
