// Code by Flopine

// Thanks to wsmind, leon, XT95, lsdlive, lamogui,
// Coyhot, Alkama,YX, NuSan and slerpy for teaching me

// Thanks LJ for giving me the spark :3

// Thanks to the Cookie Collective, which build a cozy and safe environment for me
// and other to sprout :)  https://twitter.com/CookieDemoparty

// Shader made for Everyday ATI challenge

#define TAU 6.2831853071
#define dt mod(iTime-TAU/8.,TAU)
#define scale 0.08

#define AAstep(thre, val) smoothstep(-.7,.7,(val-thre)/min(.001,fwidth(val-thre)))
#define square(u,s) AAstep(s, max(abs(u.x),abs(u.y)))
#define circle(u,s) AAstep(s, length(u))


float piece (vec2 uv)
{
   	vec2 offsetX = vec2 (scale,0.);
    vec2 offsetXY = vec2 (scale*0.2, scale);
    return square(uv, scale)
        * circle(uv-offsetX,scale*0.4)
        * circle(uv-offsetXY, scale*0.4)
        +(1.-circle(uv+offsetX,scale*0.4))
        +(1.-circle(uv+offsetXY,scale*0.4));
}

float pattern (vec2 uv)
{return piece(uv)*piece(vec2(-uv.x,-uv.y)+vec2(-scale*2.,scale*2.));}

float grid (vec2 uv)
{
    uv = mod(uv,scale*4.)-scale*2.;
    float d = 1.;
    for (int i=-1; i<=1; i++)
    {
        for(int j=-1; j<=1;j++)
        {
            vec2 offset = vec2(float(i),float(j));
            d *= pattern(uv+offset*(scale*4.));
        }
    }
    return clamp(d,0.,1.);
}

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    vec2 uv = (fragCoord/iResolution.xy);
    uv.x *= iResolution.x/iResolution.y;
    vec2 centered_uv = (2.*fragCoord-iResolution.xy)/iResolution.y;

 	vec3 col = mix(vec3(1.,0.,.5),vec3(.95),grid(uv));

    float mask = piece(centered_uv*sin(length(centered_uv*0.7)-dt)*0.2);
    col = mix(col, 1.-col, clamp(mask,0.,1.));
    fragColor = vec4(sqrt(col),1.0);
}