#version 330 core

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

out vec4 FragColor;

void mainImage(out vec4 O, vec2 I )
{ 
   float t = iTime * 0.2;
   vec3  r = iResolution;
   mat2 R = mat2(cos(t + 0.0), -sin(t + 0.0), sin(t + 0.0), cos(t + 0.0));
   O = vec4(0.0);
   float d=0.0,s=0.0;
   for(float i = 1.0; i < 99.0; i+= 1.0)
     {
        vec3 p = vec3( (I+I - r.xy) / r.y * d * R , d - 7.0);
        p.yz *= R;
        float turbulence = sin(dot(cos(p + t).yzx , sin(p + t) ) / 0.16);
        float cubeSDF = max(abs(p.x), max(abs(p.y), abs(p.z))) - 5.5;
        float distField = max(turbulence, cubeSDF) - i / 80.0;
        s = 0.01 + 0.1 * abs(distField);
        O += max(sin(vec4(3,2,1,1) * i * 0.4) / s , -length(p * p));
        d += s;
     }
     O = tanh(O * O / 4e5);
}

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