
varying vec4 Position;
varying vec4 Normal;
varying vec2 UVs;
varying vec4 Color;
uniform float ttime;
uniform float _dat_x;

varying float fc0; 
uniform float fog;
uniform float PosY;
uniform float beat2;

uniform float Slider0;
uniform float Slider1;

uniform float ttimebeat;

vec2 hash(vec2 p) {
	p = vec2(dot(p, vec2(127.1, 311.7)), dot(p, vec2(269.5, 183.3)));
	return -1.0 + 2.0*fract(sin(p)*43758.5453123);
}

float noise(in vec2 p) {
	const float K1 = 0.366025404; // (sqrt(3)-1)/2;
	const float K2 = 0.211324865; // (3-sqrt(3))/6;

	vec2 i = floor(p + (p.x + p.y)*K1);
	vec2 a = p - i + (i.x + i.y)*K2;
	vec2 o = (a.x>a.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0); //vec2 of = 0.5 + 0.5*vec2(sign(a.x-a.y), sign(a.y-a.x));
	vec2 b = a - o + K2;
	vec2 c = a - 1.0 + 2.0*K2;
	vec3 h = max(0.5 - vec3(dot(a, a), dot(b, b), dot(c, c)), 0.0);
	vec3 n = h*h*h*h*vec3(dot(a, hash(i + 0.0)), dot(b, hash(i + o)), dot(c, hash(i + 1.0)));

	return dot(n, vec3(70.0));
}


mat2 RotationMatrix(float f) {

	mat2 Temp;
	Temp[0][0] = cos(f);
	Temp[1][0] = -sin(f);
	Temp[0][1] = sin(f);
	Temp[1][1] = cos(f);
	return Temp;
};


void main () 
{

	
	mat2 M=RotationMatrix(ttime*10.0+gl_Vertex.y*0.0);


	UVs = vec2(gl_MultiTexCoord0);
	Color = gl_Color;
	//gl_Position= ftransform ();
	Position.xyzw = gl_Vertex;
	//Position.xz = M*Position.xz;
	//Position.xz = M*Position.xz;
	vec4 OriPosition = Position;
	Position.xyz *= 1 + _dat_x*0.15;
	vec3 Normal = gl_Normal.xyz;

	float ft = 2.0*noise(Position.xy*0.4 + vec2(0.3*ttime));
	ft = clamp(ft, 0.0, 1.0);
	ft = pow(ft, 10.0)*0.05;
	
	float ft2 = 2.0*noise(Position.xy*1.2 + vec2(0.1*ttime));
	ft2 = clamp(ft2, 0.0, 1.0);
	ft2 = pow(ft2, 5.0)*0.05;

	
	
	//ft = clamp(ft, 0.0, 1.0)*0.03;
	fc0 = ft;

	vec3 Dir = Normal.xyz*-1.5+vec3 (0.01);
	
	Dir = vec3(0.1);
	float fsy = 1.3;
	if (ttime > 1.0)
		fsy = 1.6;

	if (ttime > 3.0)
		fsy = 2.06;
	if (ttime > 4.0)
		fsy = 1.2;
	if (ttime > 5.0)
		fsy = 2.1;
	if (ttime > 7.0) {
		fsy = 1.0;
	 Dir = Normal.xyz*-1.5 + vec3(0.01);

	}

	if (ttime > 8.5) {
		//fsy = 0.95 + (ttime - 8.5)*0.2;
		Dir = Normal.xyz*-0.05 + vec3(-0.01);

	}
	if (ttime > 15.0) {

			fsy = 1.6 + float(int(ttime - 15))*0.0;
			if (fog > 3)
				fsy = 2.3;
		//if (ttime>17.0)			fsy = 2.86 + 0.0*fog;

		//+clamp((ttime - 20.0), 0.0, 1000000.0)*0.053;
		//Dir = Normal.xyz*0.00020*sin(ttime)*clamp (ttime-18,0,1)*0.0 + vec3(-0.23*0.0, 0, 0)*clamp (ttime-18,0.0,1.0);
		//*0 * sin(0.1*ttime);

	}


	float beat = sin(2 * 3.14159*ttimebeat * 85.0 / 60.0);
	beat = pow(clamp (beat,0.0,1.0), 4.0);




	if (ttime > 25.0) {
		//fsy = 0.5+_dat_x*1.1+beat*3.0;
		//+clamp((ttime - 20.0), 0.0, 1000000.0)*0.053;
		Dir = Normal.xyz*0.00+ vec3(3.00+beat2*0.0, -0, 0);
		//*clamp(ttime - 18, 0.0, 1.0);
		//*0 * sin(0.1*ttime);

	}


	//fsy *= 1+ beat*0.1;

	if (ttime > 100.0) {
		fsy = 100.0;
		//+clamp((ttime - 20.0), 0.0, 1000000.0)*0.053;
		Dir = Normal.xyz*0.00 + vec3(-0.00, 0, 0);
		//*clamp(ttime - 18, 0.0, 1.0);
		//*0 * sin(0.1*ttime);

	}

	//fsy = 3.03;
	//Dir = Normal.xyz*0.00 + vec3(0.00, 0, 0);
//	Dir = vec3(2,0,0.0)*ttime;
	if (ttime > 5.0) {
		Dir = -vec3(beat2)*0.5;

		
	//	if (_dat_x<0.2)
		if (ttime > 15.0){
			Dir *= 0.0;
			//if (fog<3)
			Dir = -vec3(beat2*0.7, 0, 0);
		}

		//Dir = vec3(2, 0, 0.0)*ttime;
		//fsy = 4.0;
		//fsy *= 1.2;
	}

	//Dir = vec3(beat2)*0.5;
	float ftt = fsy + ft * 0 + length(Position.xz)*0.0;

//	ftt = 2.0;

	if (ttime<30)
	if (ttime > 15.0){

	//	Position.xyz = normalize(Position.xyz)*(0.8+ 0 * beat);
//	if (fog<2)		Position.xyz = normalize(Position.xyz)*(0.8 + 1 * beat);
	}
//	Position.x *= 1 - beat*1.0;



	if (ttime > 35.0)
		if (ttime<44.0)
	{
		ftt =2.525;
		//Position.x*= 1 - beat*0.9;

		vec3 NewPos = normalize(Position.xyz)*(0.9 + (ttime - 35.0)*0.01 + 0 * beat);

		Position.xyz += (NewPos - Position.xyz)*clamp (0.5*(ttime-35.0),0.0,1.0);

		//Position.xyz += Normal.xyz*5.5*ft*0.1;
		//Color.x += (beat*0.0001);
		//ftt *= 1 - beat*0.3;
		}

		if (ttime > 44.0)
		if (ttime <53.0)
		{
			ftt = 112.2-(ttime-47)*50;
			//Position.x*= 1 - beat*0.9;

			vec3 NewPos = normalize(Position.xyz)*(0.9 + (ttime - 44.0)*0.01 + 0 * beat);

			Position.xyz += (NewPos - Position.xyz)*clamp(0.2*(ttime - 44.0), 0.0, 1.0);
			//ftt *= 1 - beat*0.3;
			//Position.xyz += Normal.xyz*5.5*ft*0.1;
			//Color.x += (beat*0.0001);
		}

		if (ttime > 53.0)
		if (ttime <168.0)
		{
			ftt = 1.5;

			if (int(ttime) % 2 == 0)
				ftt = 1.7;
				//- (ttime - 53) * 50;
			//Position.x*= 1 - beat*0.9;

			vec3 NewPos = 1.0*normalize(Position.xyz)*(0.9 + (ttime - 53.0)*0.01 + 0 * beat);
			

			Position.xyz += (NewPos - Position.xyz)*clamp(0.3 + 0.15*(ttime - 53.0), 0.0, 0.8);


			//ftt *= 1 - beat*0.3;
			//if (_dat_x>0.5)			Position.xyz -= Normal.xyz*5.5*ft*3.1*beat;
			//Color.x += (beat*10.0001);
		}


		ftt += 2.0*Slider0;


		if (Slider0 > 0.99)
			ftt = 10000.0;

	if (ttime>15.0) Position.z *= 1 - beat*0.19;
	
	vec3 NewPos2 = normalize(Position.yzx)*0.9;
	Position.xyz += (NewPos2 - Position.xyz) * Slider1;

	Position.xyz = vec3(ivec3((Position.xyz + Dir*0.1)*ftt)) / ftt - Dir*0.1;
	
	//Position.y = 0;
	Normal.xz = Normal.xz;
	if (Slider0 > 0.99)
		Position.xyz = OriPosition.xyz;

	if (ft2 > 0.001)		Color.xyz *= 10000.0;
	//1 + 10000.0*ft2;
	Color *= 1.0+beat*100;
	//Position.xy = M*Position.xy;
	//Position.xyz += gl_Normal.xyz*-0.08;


	gl_Position = gl_ModelViewProjectionMatrix  *Position;
	
	//noise(Position.xy)*100.0;
	
	Normal.xyz=gl_Normal.xyz;
	//	Position.xyzw= gl_Vertex;
	//	Color += vec4(1000,0,0,1)*(1 - clamp(1000.0*length(Color.xyz), 0.0, 1.0));


//	Color.xyz *= 1.0-float(fog)*0.2;
	//Normal.xyz*0.0;



}
