
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*0.2 + gl_Vertex.y*0.0);
	mat2 M2 = RotationMatrix(-ttime*0.2 + 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;
	//Position.xyz *= 1 + _dat_x*0.15;
	vec3 Normal = gl_Normal.xyz;
	float beat = sin(2 * 3.14159*ttimebeat * 85 / 60.0);
	beat = pow(clamp(beat, 0.0, 1.0), 4.0);

	float beatm = sin(2 * 3.14159*ttimebeat * 0.5 * 85 / 60.0-0.2*fog);
	beatm = pow(clamp(beatm, 0.0, 1.0), 6.0);

	float ft = 2.5*noise(Position.xy*10.24 + vec2(beat2*0.1+0.0*ttime));
	ft = clamp(ft, 0.0, 0.99);
	
	
	ft = pow(ft, 13.0)*0.05*1.3*(ttime+5)*1.1;
	ft = pow(ft, 5.0);

	float ft2 = 2.0*noise(Position.xy*2.33 + vec2(0.0*ttime));
	if (ft2 >- 1.2) ft = 0.0;
	
	ft *= 0.02+beatm;
	float ftt = 110.4;
	//ftt = 0.2-beatm*0.2;
	ftt = 0.2;
	ftt += clamp(0.8*(ttime-1), 0.0, 0.4);
	ftt -= beatm*0.4;
	//Position.xyz *= 1 - 1*beatm;
	//ftt = 0.5+0.31*length(Position.xz);

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

	vec3 Dir = Normal.xyz*-1.5+vec3 (0.01);
	Dir = vec3(10,0,0)*ttime;

		


	//Dir = vec3(100.0*beat2, 0, 0);
	//ft = 0.0;
	Dir = vec3(100.0,0,0);
	ftt = 0.5;
	//-_dat_x*0.05;
	if (fog < 0.5)
		ftt = 0.6;
	//-_dat_x*0.05;

	//+_dat_x;
	Dir = vec3(0.0, 0.0, 5.0);
	ftt = 100.0;
	if (Slider0>0.1)
		ftt = 0.5+5.0*Slider0;

	//ftt *= 1-beatm;
	//Color += vec4(0.3);
	//Position.y = 0;
	//Normal.xz = Normal.xz;
	//Position.xyz += (5.0*normalize(Position.xyz) - Position.xyz)*0.5;
	//*(clamp(0.1*ttime, 0.0, 1.0));
	//Position.xy = M*Position.xy;
	Position.xyz += (vec3(ivec3((Position.xyz + Dir*0.1*ttime+vec3 (0.0))*ftt)) / ftt - Dir*0.1*ttime-Position.xyz)*1.0;
	//Position.xy = M2*Position.xy;

	//Normal.x = 0;
	//Normal.xy = abs(Normal.xy);
	//0;
	Position.xyz += Normal.xyz*5.5*ft;
	gl_Position = gl_ModelViewProjectionMatrix  *Position;
	
	//noise(Position.xy)*100.0;
	
	Normal.xyz=gl_Normal.xyz;
	//	Position.xyzw= gl_Vertex;
	float dott = 0.5+abs(0.5*dot (Normal,vec3(1.0, 0.0, 0.0)));
	dott = pow(dott, 1.0)*1;
	if (Slider1 > 0.5)
		Color += vec4(1000.3)*(1 - clamp ( 1000.0*length(Color.xyz),0.0,1.0));
	//Color.xyz *= 2.0*vec3(dott);
	//Color.xyz *= 1 + beatm*2.0;
	//*vec3(dott);
	
	Color *= dott;
	Color *= 1 + beatm;
	
	
	//Color.xyz *= 0.5 + 100.0*beatm;
	//Color.xyz *= 1.0 - clamp(0.05*length(Position.xyz), 0.0, 0.8);
//	Color.xyz *= 1.0-float(fog)*0.2;
	//Normal.xyz*0.0;



}
