// followed tutorials https://youtu.be/rvDo9LvfoVE
//https://youtu.be/dhuigO4A7RY

#define NUM_LAYERS 5.

mat2 Rot(float a){
    float s=sin(a), c=cos(a);
    return mat2(c,-s,s,c);

}
//creating different shapes
//responsive to audio
float Star(vec2 uv, float flare){
    float d = dot(uv.x,uv.y/2.);//distance to the center
    float m = .005/d;//more light like than smoothstep(.1,.05,d);
   // uv*=Rot(3.1415/4.);
    float rays= max(0.,1.-abs(uv.x*uv.y*10.));
   //m =flare*rays;


   m=smoothstep(.025,.05,d/8.);
  // m=1.-smoothstep(.08,.05,d);

    return m;


}

float Hash21(vec2 p){
    p = fract(p*vec2(123.34,456.21));
    p += dot(p, p+45.32);
    return fract(p.x*p.y*p.x*p.y/atan(p.x,p.y));


}

vec3 StarLayer(vec2 uv) {
	vec3 col = vec3(0);

    vec2 gv = fract(uv)-.5;
    vec2 id = floor(uv);

    // should add <=
    for(int y=-1;y<=1;y++) {
    	for(int x=-1;x<=1;x++) {
            vec2 offs = vec2(x, y);

    		float n = Hash21(id+offs); // random between 0 and 1
            float size = fract(n*345.32);

    		float star = Star(gv-offs-vec2(n, fract(n*34.))+.5, smoothstep(.6, .5, size)*.6);

            vec3 color = sin(vec3(.5, .1, .9)*fract(n*520.)*1238.2)*.5+.5;
            color = color*vec3(1,.2,.8+size)+vec3(.2, .2, .1)*2.;

            star+= sin(iTime*2.+n*6.2831)*.5+1.;
            col =star*size*color;
        }
    }
    return col;
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    vec2 uv = (fragCoord-.5*iResolution.xy)/iResolution.y;
    vec2 M = (iMouse.xy-.5*iResolution.xy)/iResolution.y;

    float t = iTime*.03;
       float fft  = texelFetch( iChannel0, ivec2(0.5,0.2), 0 ).x;



    vec3 col = vec3(0);

    for (float i =0.; i <1.;i+=1./NUM_LAYERS){

        float depth = fract(i+t);
        //if mouse is engaged
      float scale = mix (mix(10.,100.,length(uv-M)),.8,depth);
        // if mouse is not engaged

       scale -= mix (mix(10.,50.,length(uv-1.-mix(-2.,2.,fft*max(uv.y,uv.x)))),.2,depth);

       //scale *= mix (mix(1.,5.,length(min(uv.y,uv.x)*.5+.5+fft)),.5,depth);

       // scale = mix (mix(10.,100.,length(uv-M)),.8,depth);
        float fade = depth*(1.-smoothstep(.5, .8, depth));
        col += StarLayer(tan(t)-uv*scale+i*20.*M)*fade-Hash21(uv);

    }

     uv*=Rot(t);
     uv.x+=max(t*.0001,.8);
    uv.y-=max(t*.0001,.5);



        col = pow(col, vec3(.85));	// gamma correction


    //if(gv.x>.48|| gv.y>.48) col.r=1.;//coloring the grid


    fragColor = vec4(col,1.0);
}
