/*licenced under love, peace and happyness ✌️ */


#ifdef GL_ES
precision mediump float;
#endif

 const float PI = 3.14159;

uniform float time;
uniform vec2 mouse;
uniform vec2 resolution;

#define time iTime
#define resolution iResolution

vec2 ouv;


 //generic rotation formula
vec2 rot(vec2 uv,float a){
	return vec2(uv.x*cos(a)-uv.y*sin(a),uv.y*cos(a)+uv.x*sin(a));
}



vec3 returnGrain(vec2 _uv, float amount){

	 float x = (_uv.x + 4.0 ) * (_uv.y + 4.0 ) * (time * 10.0);
	 vec4 grain = vec4(mod((mod(x, 13.0) + 1.0) * (mod(x, 123.0) + 1.0), 0.01)-0.005) *  amount;
	 return grain.xyz;

}


float rand(vec2 n) {
    return fract(sin(dot(n, vec2(12.9898, 4.1414))) * 43758.5453);
}

float noise(vec2 p){
    vec2 ip = floor(p);
    vec2 u = fract(p);
    u = u*u*(3.0-2.0*u);

    float res = mix(
        mix(rand(ip),rand(ip+vec2(1.0,0.0)),u.x),
        mix(rand(ip+vec2(0.0,1.0)),rand(ip+vec2(1.0,1.0)),u.x),u.y);
    return res*res;
}

const mat2 mtx = mat2( 0.80,  0.60, -0.60,  0.80 );

float fbm( vec2 p )
{
    float f = 0.0;

     vec4 cc = texture(iChannel0,ouv );

	for(int i=0;i<11; i++){




	vec2 p1 = vec2(p.x*.4*float(i),p.y*.4*float(i));

	p1.x *=	noise(p1*.5+time*.1)*.4;
	p1.y *=	noise(p*.5-time*.1)*.4;


    f -=  noise(vec2(cc.r,cc.b ))*.019;
    f +=  noise(p1);



	}


    return f;
}

float pattern( in vec2 p )
{
	return fbm( p + fbm( p*2.3 + fbm( p*.33 ) ) );
}



vec3 hsv2rgb( in vec3 c )
{
    vec3 rgb = clamp( abs(mod(c.x*6.0+vec3(0.0,4.0,2.0),6.0)-3.0)-1.0, 0.0, 1.0 );
    return c.z * mix( vec3(1.0), rgb, c.y);
}


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

	float _x = mouse.x;

	vec2 uv = gl_FragCoord.xy/resolution.y;



	uv.x += resolution.y/resolution.x;


    ouv = gl_FragCoord.xy/resolution.xy;




	uv = rot(uv,time*.03);

	float shade = pattern(uv);


    vec2 uv_osc = vec2(
    sin(uv.x*PI*1.+PI)*.5+.5,
    sin(uv.y*PI*1.+PI)*.5+.5
    );


     vec4 cc = texture(iChannel0,uv_osc );


   // shade += sin(fbm( vec2(cc.r,cc.b))  )*12.;



	vec3 col = vec3(
		sin(shade*.91+ time*.01)*1.75+.5 ,
		cos(shade*3.+ time*.13)*.75+.5 ,
		cos(shade*13.+ time*.13)*.5+.6
			//shade*.7+.4
	);

	col = hsv2rgb(col);

	col += returnGrain(uv,9.);

	fragColor = vec4( col, 1.0 );

}




