

//Made with love by Peleg Gefen <3


//#define Time_And_Zoom //Allows you to use the horizontal mouse axis to "peek into the future" to see the full evolution of the wave.



//#define Grass_Like //Animates the cubes in a cool low poly growing grass thing.

#ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
#else
precision mediump float;
#endif


vec2 rot (vec2 p,float a)
{
    float c = cos(a);
    float s = sin(a);
    return p*mat2(c,s,-s,c);
}



float hexDist(vec2 p) {
    p = abs(p);
    //distance to the diagonal line
    float c = dot(p, normalize(vec2(1., 1.73)));

    // distance to the vertical line
    c = max(c, p.x);
    c += sin(iTime + 4000.) *5. +5.;
    return c;
  }

vec4 hexCoords(vec2 uv) {
    vec2 r = vec2(1., 1.73);
    vec2 h = r * 0.5;
    vec2 a = mod(uv, r) - h;
    vec2 b = mod(uv - h, r) - h;

    vec2 gv;
    if(length(a) < length(b))
      gv = a;
    else
      gv = b;

    float y = .5 - hexDist(gv);
    float x = atan(gv.x, gv.y);
    vec2 id = uv - gv;
    return vec4(x, y, id.x, id.y);

}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
	float iTime = iTime * .25;
	iTime += 800.;
	vec2 uv = (fragCoord.xy-.5*iResolution.xy)
	/iResolution.y;


     vec4 col = vec4(0.);
     vec2 uv1 = uv;
     //uv *= 3.;
     uv -= 2.;
     uv *= (sin(iTime * .75)*1.5+1.5) + 3.;
     uv += vec2(iTime *	 .01);

     #ifdef Time_And_Zoom
     iTime += iMouse.x * 10.;
     uv *= iMouse.y * .007;
     #else
     uv -= (iMouse.xy / iResolution.xy) * 2. ;
     #endif

     uv += rot(uv , (cos(iTime)*.5+.5));



     vec4 uvid = hexCoords(uv * 2.);

     float t = smoothstep(.5,.5
 	 	,uvid.y
 	 	* sin(( length(uvid.zw))
 	 	 * iTime *0.1)*.5+.5);


	col = vec4(
    t * tan(iTime * .5)*.5+.5 * sin(iTime * 2.5)*.5+.5
    , t*cos(iTime * .25)*.5+.5* sin(iTime * 5.)*.5+.5
    , t * sin(iTime * .1275)*.5+.5 * sin(iTime * 10.)*.5+.5
    ,1.);




	//lit face
	col += vec4(smoothstep(.99,.991,uvid.x));


	//shading
	col += vec4(smoothstep(-1.,-1.,uvid.x)) * .4;


 	 //hexagons shrinking and expanding, wave form
 	 col *= vec4(smoothstep(.000001,.00001
 	 	,uvid.y

        #ifdef Grass_Like
 	 	* uvid.x
        #endif
        * sin(( length(uvid.zw))
 	 	 * iTime *.01)*.5+.5));


	fragColor = vec4( col);
}
