#version 330 core

uniform vec3 iResolution;
uniform float iTime;
uniform float uAlpha;

out vec4 FragColor;

// helper: stable tanh for vec4
vec4 tanh4(vec4 x) {
    vec4 e = exp(clamp(2.0 * x, vec4(-20.0), vec4(20.0)));
    return (e - vec4(1.0)) / (e + vec4(1.0));
}

void mainImage(out vec4 o, vec2 u)
{
    float s = 0.3;
    float i = 0.0;
    float n;
    
    vec3 r = iResolution;
    vec3 p = vec3(0.0);
    
    for(u = (u - r.xy / 2.0) / r.y - s; i++ < 32.0 && ++s > 0.001;)
    {
        for(p += vec3(u * s, s), s = p.y, n = 0.01; n < 1.0; n += n)
        {
            s += abs(dot(sin(p.z + iTime + p / n), r / r)) * n * 0.1;
        }
    }
    
    // preserve the bright center while avoiding the singularity that causes a dark dot / tinted halo
    float d = max(length(u - vec2(0.1)), 0.08);
    o = tanh4(i * vec4(5.0, 2.0, 1.0, 1.0) / d / 500.0);
    o.a = 1.0;
}

void main() {
    mainImage(FragColor, gl_FragCoord.xy);
    FragColor.a *= uAlpha;
}
