vec3 palette(float t) {
    vec3 a = vec3(1.5, 0.5, 0.5);
    vec3 b = vec3(1.5, 0.5, 1.5);
    vec3 c = vec3(1.0, 1.0, 1.0);
    vec3 d = vec3(1.263, 0.416, 0.557);

    return a * b*cos(6.28318 * (c*t+d));
}
#define PI 3.14159265359

float random(float x)
{
    return fract(439029.0*sin(x));
}

float random(vec2 uv)
{
    return fract(439029.0*sin(dot(uv, vec2(85.3876, 9.38532))));
}


vec3 hsv2rgb( in vec3 c ){
    vec3 rgb = clamp(abs(mod(c.x*6.0+vec3(0.0,4.0,2.0),
                             6.0)-3.0)-1.0,
                     0.0,
                     1.0 );
    rgb = rgb*rgb*(3.0-2.0*rgb);
    return c.z * mix(vec3(1.0), rgb, c.y);
}
float sd_box( in vec2 p, in vec2 b )
{
    vec2 d = abs(p)-b;
    return length(max(d,0.0)) + min(max(d.x,d.y),0.0);
}
vec2 rotate(vec2 v, float a) {
	float s = sin(a);
	float c = cos(a);
	mat2 m = mat2(c, s, -s, c);
	return m * v;
}


void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    vec2 uv = (fragCoord * 2.0 - iResolution.xy) / iResolution.y;
    vec2 uv0 = uv;
    vec3 finalColor = vec3(0.0);
     float angle = atan(-uv.y, -uv.x) + PI;
    float radius = length(uv);


    float rayNum = 50.0; // Number of radial rays from center
    float rayPadding = 1.0; // In terms of ray size (so 0.5 is half and 1.0 is equal to the size of a ray)
    float pulseSize = 0.05;
    float pulseSpeed = 0.7;
    float pulseWarp = 0.3;

   const float w = 0.002;
    float col = 0.;
    for (float i = .1; i < 10.; i += .1) {
        float s = sin(iTime + i * cos(iTime * .1)) * .15 + .15 + i * .001;
        uv = rotate(uv, iTime * (.005 + sin(iTime * 0.2) * 0.002) + i * .001);
        col = max(col, min(smoothstep(-w, w, sd_box(uv, vec2(s))), smoothstep(w, -w, sd_box(uv, vec2(s)))));
    }
    float sectionSize = 2.0*PI/rayNum;
    float sectionNum = floor(angle/sectionSize); // Labels each section with a number
    float raySize = 2.0*PI/(rayNum*(1.0 + rayPadding));
    float rayUv = mod(angle, sectionSize);

    float rays = min( pow((raySize/2.0)/abs(rayUv - sectionSize/2.0), 3.0), 1.0 );

    pulseSpeed += 2.0*random(sectionNum);
    float pulseIndex = floor((pow(radius, pulseWarp) - 0.11*pulseSpeed*+iTime)/pulseSize)*pulseSize;
    float pulses = step(0.085, random(pulseIndex + 0.374*sectionNum));

    rays *= pulses;
    uv*=mat2(cos(iTime),sin(iTime),-sin(iTime),cos(iTime));
    for(float i = 0.0; i < 6.0; i++){
        uv = fract(uv * (2.1 - cos(iTime))) - 0.5;

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

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

        d = sin(d * 8. + iTime)/8.;
        d = abs(d);
        d = pow(0.01 / d, 1.2);

        finalColor += col * d;
    }

    fragColor = vec4(finalColor, 1.0);
}
