const float PI = 3.14159265;
vec3 palette(in float t) {

    vec3 a = vec3(.860, .560, .680);
    vec3 b = vec3(.337, .206, .448);
    vec3 c = vec3(.536, .124, .536);
    vec3 d = vec3(-.393, -.393, .107);
    return a + b * cos(6.28318 * (c * t + d));
}

vec2 rotate(vec2 v, float a) {
	float sinA = sin(a);
	float cosA = cos(a);
	return vec2(v.x * cosA - v.y * sinA, v.y * cosA + v.x * sinA);
}

float square(vec2 uv, float d) {
	return max(abs(uv.x), abs(uv.y)) - d;
}

float smootheststep(float edge0, float edge1, float x)
{
    x = clamp((x - edge0)/(edge1 - edge0), 0.0, 1.0) * 3.14159265;
    return 0.5 - (cos(x) * 0.5);
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {

  	vec2 uv = fragCoord.xy / iResolution.xy;
	uv = uv * 2.0 - 1.0;
	uv.x *= iResolution.x / iResolution.y;
	uv *= 1.5;
    float blurAmount = -0.005 * 1080.0 / iResolution.y;

	float period = 2.0;
	float time = iTime / period;
	time =mod(time, 1.0);
	time = smootheststep(0.0, 1.0, time);

    // Original uv, without tiling.
    vec2 uv0 = uv;
    vec3 finalColor = vec3(0.);

    for(float i = 0.0; i < 3.0; i++) {


        float d = length(uv) + 9. * exp(-length(uv0));

        vec3 col = palette(length(uv0) + i * .4 + iTime * .4);

        d = sin(fract(d * 2. + iTime) / 8.);
        d = abs(d);
        float n = float(i);
	float size = 1.0 - n / 18.0;
float rotateAmount = (cos(iTime)*n * 0.5 + 0.25) * PI * 2.0;

		fragColor = vec4(0.0, 0.0, 0.0, 1.0);

        d = pow(0.05 / d, 1.1);
		 finalColor= mix(finalColor, vec3(1.0), smoothstep(0.0, blurAmount, square(rotate(uv, -rotateAmount * time), size)));
         float blackOffset = mix(1.0 / 4.0, 1.0 / 2.0, n / 9.0) / 9.0;
		finalColor= mix(	finalColor, vec3(0.0), smoothstep(0.0, blurAmount, square(rotate(uv, -(rotateAmount + PI / 2.0) * time), size - blackOffset)));
        finalColor+= col * d * fract(sin(palette(max(abs(uv.x), abs(uv.y) * uv0.x))) * 1.9);
    }


    fragColor = vec4(finalColor, 1);

}
