#version 330 core

in vec2 vUV;
out vec4 outColor;

uniform vec3 iResolution; // x=width, y=height, z=1
uniform float iTime;

mat2 rotate2D(float t) {
    return mat2(cos(t), sin(t), -sin(t), cos(t));
}

void main() {
    vec2 r = iResolution.xy;
    vec2 fragCoord = vUV * iResolution.xy;
    vec2 n = vec2(0.0);
    vec2 N = vec2(0.0);
    vec2 q;
    vec2 p = (fragCoord * 2.0 - r) / r.y;
    float S = 5.0;
    float a = 0.0;
    float t = iTime;

    for (int k = 0; k < 30; k++) {
        float j = float(k);
        mat2 m = rotate2D(5.0);
        p *= m;
        n *= m;
        q = p * S + j + n + t * 4.0 + sin(t * 4.0) * 0.8;
        a += dot(cos(q) / S, r / r);
        n += q = sin(q);
        N += q / (S + 20.0);
        S *= 1.2;
    }

    // small ambient lift so background is a bit clearer
    vec4 o = vec4(0.03);
    o += 0.1 - a * 0.1;
    o.r *= 5.0;
    o += min(0.7, 0.001 / length(N));
    // slightly reduce vignette/darkening to keep background clearer
    o -= o * dot(p, p) * 0.6;

    outColor = o;
}
