#version 330 core

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

out vec4 FragColor;

#define R(p,a,r) mix(a*dot(p,a), p, cos(r)) + sin(r) * cross(p,a)
#define H(h) (cos((h)*6.3 + vec3(0.0, 23.0, 21.0)) * 0.5 + 0.5)

void mainImage(out vec4 O, vec2 C)
{
    O = vec4(0.0);
    vec3 p;
    vec3 r = iResolution;
    vec3 d = normalize(vec3((C - 0.5 * r.xy) / r.y, 1.0));
    float i = 0.0, g = 0.0, e = 0.0, s = 0.0, a = 0.0;
    for (i = 1.0; i < 99.0; i += 1.0) {
        p = d * g;
        p.z += iTime * 0.2;
        p = R(p, vec3(1.0), 1.2);
        p = mod(p, 2.0) - 1.0;
        // preserved odd line from original
        p.xy = vec2(dot(p.xy, p.xy), length(p.xy) - 1.0);
        s = 3.0;
        for (int j = 0; j < 5; j++) {
            p = vec3(10.0, 2.0, 1.0) - abs(p - vec3(10.0, 5.0, 1.0));
            s *= e = 12.0 / clamp(dot(p, p), 0.2, 8.0);
            p = abs(p) * e;
        }
        e = min(length(p.xz), p.y) / s + 0.001;
        g += e;
        a = cos(i * i / 80.0);
        O.xyz += mix(vec3(1.0), H(log(s) * 0.3), 0.5) * a * a / e * 6e-5;
    }
    O = pow(O, vec4(4.0));
}

void main()
{

    vec4 col = vec4(0.0);
    mainImage(col, gl_FragCoord.xy);
    col.a = uAlpha;
    FragColor = col;

}
