#version 330

uniform float uTime;
uniform vec2 uRes;
uniform float uSpeed;
uniform float uVol;

uniform vec2 uRes;


float time;
vec3  pos2;

vec3 rotate(vec3 r, float v){ return vec3(r.x*cos(v)+r.z*sin(v),r.y,r.z*cos(v)-r.x*sin(v));}
vec3 sqr(vec3 a){return a*a;}
float terrain(vec3 pos, float myTime){   
    vec3 o = pos;
    //pos.y -= sin(pos.z*0.01)*10.0;
    time += (o.x)*0.00001 ;//+ sin(pos.z*0.00001);
    //time -= (o.y)*0.000005 ;
    pos.y += (1.0 + 0.1*sin(pos.z*0.01))*100.0;
    pos2.xz = pos.xz = mod(pos.xz,3200.0)-1600.0;
    pos2.xz = pos.xz = mod(pos.xz,1600.0)-800.0;
	pos2.xz = pos.xz = mod(pos.xz,800.0)-400.0;
    pos2.xz = pos.xz = mod(pos.xz,400.0)-200.0;
	pos.xyz = rotate(pos.xyz,pos.z*0.003+pos.x*0.001+time*0.1);
	pos.xzy  = abs(pos.xyz)-50.0 + sin(myTime*0.3+1.3)*50.0;
	return 	60.0 + sin(pos.x*0.12+myTime)*sin(pos.y*0.14+myTime)*sin(pos.z*0.11+myTime)*5.0 - length(pos);
}

out vec4 fragColor;

void main( void ){


    time = uTime*1.0;

    vec2 uv = fragCoord.xy/(iResolution.xx*0.5)-vec2(1.0+cos(time*0.3)*0.4,0.9);
//  float music = texture2D( iChannel3, uv ).r;
//  float musicspace = music*22.0;
    vec3 ray    = normalize(vec3(uv.x,uv.y,0.7));
    vec3 campos = vec3(cos(time*0.32),sin(time*0.5)*20.0-300.0,cos(time*0.1)*500.0);
    vec3 pos    = campos;
    pos.z -= uTime*100.0;
    float test  = 0.0;
    float test1 = 0.0;
    float test2 = 0.0;
    float test3 = 0.0;
    float test4 = 0.0;
    float trans = 0.0;
    float transScale = 90.0/60.0;
   
    //pos.z += sqrt(music)*100.0;//*(abs(sin(uTime*0.1))+0.5);
//-music*-122.0;
	for(int i=0;i<90;i++){
        vec3 p0 = pos;
        p0.y -= sin(pos.z*0.01)*10.0;
       // p0.y -= sin(p0.z*0.01)*180.0;
        float myTime = time + p0.x*0.01;
        float wastime = time;
        p0.y *= (1.0 + 0.75*sin(p0.z*0.2)*uTime*0.0001);//*(1.0 + 0.15*sin(music));
       // p0.z += music*100.0;
        float t0 = terrain(p0, myTime + music);
        time = wastime;
        vec3 pos2 = p0;pos2.y*=3.63;pos2.xz *= 22.0;

        float t1 = terrain(pos2 + rotate(vec3(25.0,75,-35.0), uTime*1.0), myTime);
     //   t1+=music;
		vec3 pos3 = pos;pos3.y*=5.1163;pos3.xz *= .1222;
        float t2 = terrain(pos3 + rotate(vec3(225.0,275,-35.0+sin(pos3.z)*5.0), music*1.0+uTime*0.2+100.0), time + pos3.x*0.01);
        
        vec3 pos4 = p0;pos4.y*= 1.20;pos4.xz /= 22.0;// 0.1;//22.0;
        float t3 = terrain(pos4 + rotate(vec3(25.0,15.0,00.0), -uTime*2.5),-music+ myTime);
        
   
        test = max(t0,t1);
        test = min(test,-t2);
        test = max(test,t3);
  
        test1 = t0;
        test2 = t1;
        test3 = t2;
        test4 = t3;
        
       // test *= (0.5 + (music*music)*0.5);
       // max(test, test/musicspace);
        //if(test >50.0){transScale = float(i)/60.0;break;}
      //  test = max(test,-0.01);
        pos += ray*clamp((test*0.2131353125),-50.0,-2.0);
        trans += smoothstep(0.0,4.0,abs(test));
    }
	trans /= transScale;

    vec3 shells = texture2D( iChannel2, uv ).rgb;
    vec3 cat = texture2D( iChannel0, uv ).rgb;
    vec3 roots = texture2D( iChannel1, uv ).rgb;
    vec3 barf = texture2D( iChannel1, vec2(0.5, pos.z*0.00001) ).rgb;
    //test *= (0.5 + (music*music*music*music*music)*0.5);
    // test *= (0.5 + (music*music)*0.5);
    // test1 *= (0.5 + (music*music)*0.5);
    //  test4 *= (0.5 + (music*music)*0.5);
	float c1 = smoothstep(-0.2,7.0,test2*0.2);
	float c2 = smoothstep(-0.0,70.0,test1);
	float c3 = smoothstep(70.0,45.0,trans);
    float c4 = smoothstep(-0.0,50.0,test4);
    //vec3(0.93,0.9,0.2)
  //  vec3 catcolor = cat;
    float dd = (mod(uTime+94.0+music*10.0,100.0)-0.0)*0.01;
    vec3 maincolor = mix(vec3(0.51,0.73,1.1), vec3(0.711,0.723,0.3), clamp(dd*dd,0.0,1.0));
    vec3 col =  c2*max(vec3(1.37,0.2,0.1),shells) + sqr((c3*maincolor+(c3*barf))*1.5) + c1*roots*vec3(5.9*music,0.2,0.0)+ sqrt(c4)*vec3(0.13,2.0*music,0.0) +roots*0.2;
	fragColor = vec4(col-dot(uv,uv)*0.4,1.0);
}
