#define s(v) smoothstep(15./iResolution.y,0.,v) // AA
#define ro(a) (mat2(cos(a),sin(a),-sin(a),cos(a))) // 2d rotation matrix
float hash21(vec2 p) {return fract(fract(dot(p,vec2(5.15123,2.1235)))*41.3151);}

float cir (vec2 p, vec2 c, float r, float w) {
    return abs(length(p-c)-r)- w;
    }

float squ(vec2 p, vec2 b, float w) {
    vec2 d = abs(p)-b;
    return abs(length(max(d,0.0)) + min(max(d.x,d.y),0.0)) - w;
}

float tri(vec2 p, float w){
    const float k = sqrt(3.0);
    p.x = abs(p.x) - 1.0;
    p.y = p.y + 1.0/k;
    if( p.x+k*p.y>0.0 ) p = vec2(p.x-k*p.y,-k*p.x-p.y)/2.0;
    p.x -= clamp( p.x, -2.0, 0.0 );
    return abs(-length(p)*sign(p.y))-w;
}

float fbm(vec2 p){
    return texture(iChannel0, p*.001).x*.533 +
           texture(iChannel0, p*.01).x*.267 +
           texture(iChannel0, p*.3).x*.133 +
           texture(iChannel0, p*.5).x*.067;
}

float watercolor (vec2 p) {
       p*=5.;
       vec2 q = vec2(0.);
       q.x = fbm(p);
       q.y = fbm( p + vec2(1.0));
       vec2 r = vec2(0.);
       r.x = fbm( p + 1.0*q + vec2(1.7,9.2));
       r.y = fbm( p + 1.0*q + vec2(8.3,2.8));
       float f = fbm(p+r);
       return clamp(f,0.,1.);
}

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{

    vec2 uv = (fragCoord - iResolution.xy * .5) / iResolution.y;

    uv *= ro(iTime * .1);
    uv += vec2(iTime * .1);

    uv *= 2.;

    vec2 offs = (vec2(fbm(uv*16.), fbm(uv*16. + .35)) - .5) * .07;

    vec2 grid = floor(uv);
    vec2 fl = uv - grid;
    float id = hash21(sin(cos(grid) * 4.));
    float lvl = 1.;

    for (int i = 0; i < 4; i++)
        if (id * 10. >= 1.) {
            vec2 t = fl;
            grid = floor(fl * 2.);
            fl = t * 2. - grid;
            id = hash21(grid + cos(id) * sin(grid));
            lvl++;
        }

    fl -= vec2(.5);
    fl += offs;

    vec3 col = vec3(.9);

    col = mix(col, col*.7, texture(iChannel0, uv*5.).x*.5);
    col += texture(iChannel0, (uv+vec2(.125,.382))*5.).x*.2;
    col = mix(col, vec3(.95,.95,1.0)*col, texture(iChannel0, uv*.02).x);

    float tem = 0.;

    if (id < .05)
        tem = s(cir(fl, vec2(.0), .25, .1));
    else if (id < .1)
        tem = s(squ(fl*ro(id*100.), vec2(.25), .1));
    else
        tem = s(tri(fl*ro(id*100.)*3., .3));

    col = mix(col, mix(col, vec3(hash21(grid*2.*id), hash21(grid*3.*id), hash21(grid*4.*id)), watercolor(uv)), tem);

    fragColor = vec4(col,1.0);
}
