#version 330 core

in vec2 vUV;
out vec4 FragColor;

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

void mainImage(out vec4 o, vec2 u) {
    float i,a,d,s,t = iTime;
    vec3 p = iResolution;
    u = (u + u - p.xy) / p.y;
    // initialize accumulators
    o = vec4(0.0);
    i = 0.0;
    s = 0.0;
    d = 0.0;

    for (o *= i; i++ < 64.; d += (s = 0.05 + abs(s) * 0.2), o += 1.0 / s)
        for (p = vec3(u * d, d + t),
             s = 8.0 - length(p.xy * sin(p.y * 0.25)),
             a = 0.05; a < 2.0; a += a)
            p += cos(t + p.yzx) * 0.03,
            s -= abs(dot(sin(0.5 * t + 0.1 * p.z + p * a * 12.0), 0.05 + p - p)) / a;

    o = tanh(o * o / 3e6 / dot(u, u));
}

void main()
{
    vec2 uv = vUV * iResolution.xy;
    vec4 outCol;
    mainImage(outCol, uv);
    outCol.a *= uAlpha;
    FragColor = outCol;
}