uniform sampler2D iChannel0;
uniform sampler2D tex0;
uniform sampler2D tex1;
uniform vec2 iResolution;
uniform float iGlobalTime;
vec2 iMouse = vec2(sin(iGlobalTime)*1000.,sin(iGlobalTime*1.3563453)*1000.);
#define AA 1
const int MBS = 3;
float r(vec2 c){return fract(sin(dot(c,vec2(12.9898,78.233)))*43758.5453);}
float xor (float x,float y, inout float z){
	x*=100.;
    y*=100.;
    x=mod(x,209.);
    y=mod(y,209.);
    float v=iGlobalTime*12.0;
    for (int i=0;i<11;i++){
    	float p=pow(2.,float(i));
        float myxor=0.;
        if (x>p){
        	myxor+=2.;
            x-=p;
        }
        if (y>p){
        	myxor-=2.;
            y-=p;
        }
        if(myxor < 1.0) v-=p;     
    }
    z = mod(v, 50.0);
    return v/30.0;
}

vec3 map (float v){
	float r=v*6.;
    r = mod(r,3.)/1.05;
    float g=v*12.;
    g = mod(g,4.);
    float b=v*1.;
    b = mod(b,1.);
    return 0.6*vec3(r,g*0.4,b);
}
/*
vec3 bg(vec2 uv){
 	vec3 acc = vec3(0.0);
    float time = iGlobalTime-0.018;
    for(int i = 0; i < MBS; ++i){
        float arc = 2.*uv.x-0.25+(time+0.15*(0.5+0.5*sin(12.774*time)));
        float r = 0.5/length(uv);
        float cap = float(r<16.);
        r+=time*3.4;
        float z;
        acc += map(xor(r*1.0*cap,arc, z));
        time += 0.0072;
    }
    return 0.85*acc+0.04*r(uv+time);
}
*/
vec3 sample(vec2 uv){
 	vec3 acc = vec3(0);
    float time = iGlobalTime-0.018;
    for(int i = 0; i < MBS; ++i){
        //uv += time;
        float t = uv.y;
        uv.y /= max(0.01, sin(1.5*3.141*uv.x));
        uv.x /= max(0.01, sin(1.75*3.141*t));
        float arc = atan(uv.x, uv.y);
        arc -= time;
        
        
        float r = 0.5/length(uv);
        float cap = float(r<10.);
        r+=time*1.3;
        float z;
        float val = xor(r*1.0*cap,arc, z);
        //if(z < 25.0) acc = bg(uv*.3);
        //	else acc += map(val);
        acc += map(val);
        
        time += 0.0036;
    }
    return 0.3+0.7*acc/float(MBS)+0.04*r(uv+mod(time,50.0));
}

void mainImage(out vec4 fragColor, in vec2 fragCoord){
	vec2 uv = (fragCoord.xy / iResolution.xy)*1.9;
    uv.y /= iResolution.x/iResolution.y;
    uv -= vec2(0.75,0.535);
    vec3 ms = vec3(0.0);
    vec2 hp = vec2(0.6)/iResolution.xy;
#if AA
    ms += 0.25*sample(uv);
    ms += 0.25*sample(uv+vec2(hp.x,0));
    ms += 0.25*sample(uv+vec2(hp.y,0));
    ms += 0.25*sample(uv+hp);
#else
    ms += sample(uv);
#endif
    fragColor = vec4(ms,0);
}

void main() {
    vec4 fragColor = vec4( 0.);
    vec2 fragCoord = gl_FragCoord.xy;
    mainImage( fragColor, fragCoord);
    gl_FragColor = fragColor;
}