#define TAU 6.28318530718
#define MAX_ITER 5

varying vec2 vUv;
uniform float uTime;
uniform vec3 uOuterColor;

void main() {
    float time = uTime * 0.25;
    vec2 uv = vUv;

    vec2 p = mod(uv * TAU, TAU) - 250.0;
    vec2 i = vec2(p);

    float c = 1.0;
    float inten = 0.005;

    for (int n = 0; n < MAX_ITER; n++) {
        float t = time * (1.0 - (3.5 / float(n + 1)));
        i = p + vec2(
            cos(t - i.x) + sin(t + i.y),
            sin(t - i.y) + cos(t + i.x)
        );
        c += 1.0 / length(vec2(
            p.x / (sin(i.x + t) / inten),
            p.y / (cos(i.y + t) / inten)
        ));
    }

    c /= float(MAX_ITER);
    c = 1.17 - pow(c, 1.4);

    vec3 fractalCol = vec3(pow(abs(c), 8.0));
    fractalCol = clamp(fractalCol + vec3(0.0, 0.35, 0.5), 0.0, 1.0);

    vec3 darkColor = uOuterColor * 0.4;
    vec3 col = mix(darkColor, uOuterColor, fractalCol.r);

    float verticalFade = 1.0 - vUv.y;

    gl_FragColor = vec4(col * verticalFade, verticalFade);
}
