/*originals  https://www.shadertoy.com/view/MdXSzS https://www.shadertoy.com/view/wdtczM  https://www.shadertoy.com/view/MdfBz7 https://www.shadertoy.com/view/XsjBRt */



#define M_PI 3.1415926535897932384626433832795
#define TWO_PI 6.283185307179586
float random(vec2 co)
{
    highp float a = 12.9898;
    highp float b = 78.233;
    highp float c = 43758.5453;
    highp float dt= dot(co.xy ,vec2(a,b));
    highp float sn= mod(dt,3.14);
    return fract(sin(sn) * c);
}
// radom number in 2d
float hash(vec2 p) {
  return fract(sin(dot(p,vec2(12.9898,78.2333)))*43758.5453123);
}

// noise in 2d
float noise(vec2 p) {
    vec2 i = floor(p);
    vec2 f = fract(p);
    vec2 u = f*f*(3.0-2.0*f);
    return mix( mix( hash( i + vec2(0.0,0.0) ), hash( i + vec2(1.0,0.0) ), u.x),
                mix( hash( i + vec2(0.0,1.0) ), hash( i + vec2(1.0,1.0) ), u.x), u.y);
}

// fractal noise in 2d
float fbm ( vec2 p ) {
    const mat2 m = mat2(0.8,0.6,-0.6,0.8);
    float f = 0.0;
    f += 0.5000*noise ( p ); p*=m*2.02;
    f += 0.2500*noise ( p ); p*=m*2.04;
    f += 0.1250*noise ( p ); p*=m*2.03;
    f += 0.0650*noise ( p ); p*=m*2.01;

    // normalize f;
    f /= 0.9375;
    return f;
}

vec3 pal(float domain, vec3 frequency, vec3 phase) {
  return vec3(0.5) + vec3(0.5) * cos(TWO_PI*(frequency*domain+phase));
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
	vec2 uv = (fragCoord.xy / iResolution.xy) - .5;
	float t = iTime * .1 + ((.25 + .05 * sin(iTime * .1))/(length(uv.xy) + 2.07)) * 2.2;
	float si = sin(t);
    vec4 o = fragColor;
    vec2 F = fragCoord;
	float co = cos(t);
	mat2 ma = mat2(co, si, -si, co);
float c6= noise(uv.xy);
	float v1, v2, v3;
	v1 = v2 = v3 = 0.0;
	 vec2 p = (-0.5 + (fragCoord.xy / iResolution.xy)) * vec2(4.0);
  // aspect ratio
  p.x *= iResolution.x / iResolution.y;



    vec2 R = iResolution.xy;
    o-=o;
    for(float d,t = iTime*.1, i = 0. ; i > -1.; i -= .06 )
    {   d = fract( i -3.*t );
        vec4 c = vec4( ( F - R *.5 ) / R.y *d ,i,0 ) * 28.;
        for (int j=0 ; j++ <27; )
            c.xzyw = abs( c / dot(c,c)
                    -vec4( 7.-.2*sin(t) , 6.3 , .7 , 1.-cos(t/.8))/7.);
       o += c * c.yzww  * (d-d*d)  / vec4(3.1,3.,.5,.5);
    }


  // angle and radius to center 0,0
  float a = atan( p.y, abs(p.x) );
  float r = length(p);

  // space distortion
  p += vec2(fbm(vec2(a*2.+iTime*.1,r*.4-iTime*.3)))*5.0;
  // divide the space into cells and get cell index to seed the palette
  float cidx = (floor(p.x+2.0) + (floor(p.y+2.0)*4.0)) / 16.0;
  // color is from palette with cell index
  vec3 color = pal(fbm(p*.5), vec3(1.0), vec3(0.4+cidx,0.2+cidx,0.0));

  // draw a grid for the cells
  color *= smoothstep(0.49,0.44, abs(fract(p.x)-0.5));
  color *= smoothstep(0.49,0.44, abs(fract(p.y)-0.5));

	float s = 0.0;
	for (int i = 0; i < 90; i++)
	{
		vec3 p = s * vec3(uv, 0.0);
        float c5 = random(p.xy);
		float c6= noise(p.xy);
       p.x+=cos(iTime*0.5);
         p.y+=sin(iTime*0.5);
		p += vec3(.22, .3, s - 1.5 - sin(iTime * .13) * .1)+c6*color*c5;
		for (int i = 0; i < 8; i++)	p = abs(p) / dot(p,p) - 0.659;
		v1 += dot(p,p) * .0015 * (1.8 + sin(length(uv.xy * 13.0) + .5  - iTime * .2));
		v2 += dot(p,p) * .0013 * (1.5 + sin(length(uv.xy * 14.5) + 1.2 - iTime * .3));
		v3 += length(p.xy*10.) * .0003;
		s  += .035;
	}

    vec4 outColor = vec4(0.0);
	float time = iTime * 0.1;
    vec2 uvNorm = fragCoord.xy / iResolution.xy;

    uv /= vec2(iResolution.y / iResolution.x, 1.);
	   float c5 = random(uv.xy);



    float grid = smoothstep((sin(length(uv.y-0.5)*(800.*length(uv.y+0.5))) * sin(length(uv.x+0.5)*(800.*length(uv.x-0.5)))), 0.0, 1.0);
    outColor.rgb += (outColor.rgb * vec3(grid) * 0.6);



	float len = length(uv);
	v1 *= smoothstep(.7, .0, len)+c6;
	v2 *= smoothstep(.10, .3, len);
	v3 *= smoothstep(.9, .0, len);

	vec3 col = vec3( v3 * (1.5 + sin(iTime * .2) * .4),
					(v1 + v3) * .3,
					 v2) + smoothstep(0.2, .0, len) * .85 + smoothstep(.0, .6, v3) * .3;

	fragColor=vec4(min(pow(abs(col), vec3(1.2)), 1.0), 1.0);
    fragColor+= outColor;
    fragColor+=o;
}
