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

varying float fc0;

uniform float PosY;
uniform float Active;

uniform float Slider0;
uniform float Slider1;



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);
	UVs = vec2(gl_MultiTexCoord0);
	Color = gl_Color;
	//gl_Position= ftransform ();
	Position.xyzw = gl_Vertex;
	vec3 Normal = gl_Normal.xyz;

	float ft = 1.1*noise(Position.xy*100.02 + vec2(1 + 0.0*ttime));
	float ft2 = 1.0*noise(Position.xz*2.0 + vec2( 0.13+0.0*ttime));
	ft = clamp(ft, 0.0, 1.0);
	ft2 = clamp(ft2, 0.0, 1.0);
	ft2 = pow(1.4*ft2, 30.0)*0.1;
	ft = pow(ft, 10.0)*1.5;
	//ft = pow(ft, 10.0);
	//ft += 1.0;
	//ft = clamp(ft, 0.0, 1.0)*0.03;
	//if (Position.y < 0.575) ft = 0.0;

	fc0 = ft;

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


	float ftt = 20;
	if (_dat_x < 0.001) ftt = 1000.0;
	else
		ftt = 120.0;

	

	float PPosY = clamp (Position.z   +0.1*(ttime-4),0.0,1.0)*10.0;


	ftt = 2.0+PPosY*PPosY;
	if (_dat_x > 0.001) {
		float posy = clamp(PPosY - 14.0, 0.0, 100.0);
		ftt = 226.0 +posy*posy;

	}

	ftt *= 1 - 0.95*beat;


	float frand = fract(sin(fog)*43758.5453123);

	Position2 = Position;
	ftt = 1.7-0.3*fog;
	ftt = 0.7+frand+fog*0.3;
	
	if (fog > 6)
		ftt = 3.6;


	ftt *= clamp(0.4*ttime, 0.0, 1.0);
	ftt *= 1-clamp(0.4*(ttime-18), 0.0, 1.0);
	//+0.2*sin(ttime*1000.0);
	//.2*fog;

//ftt *= PosY*0.1;
	//if (Active>0.5) 
	{
		//Color *= 0.2 + 100000.0*ft;

		ft *= 1.0-0.0*clamp(ttime - 18, 0.0, 1.0);
		Position2 = Normal.xyzz*10.0;
		//if (fog<6)		Position.xyz -= (Normal.xyz*vec3 (0,0,1))*21.5*ft;
		//*beat;
	//	Position.xy = M*Position.xy;
//		Position.xyz = vec3(ivec3(Position.xyz*ftt + vec3(0, -0.0, -0))) / ftt;
		vec3 Dir = vec3(0.3, 130.0, -2.0)*0.1*ttime;
		//+vec3(fog*0.3, 0, fog*2.0);
		 //Dir = -0.6*vec3(Position.x, 0.0, Position.z);
		 //if (int(fog) % 2==0)			 Dir = vec3(3, 3, 0);
		 
		Dir += vec3(frand);

		 if (fog > 6)
			 Dir = vec3(1.2, 0, -0.2*ttime);
		
		 
		 //Dir = 0.3*vec3(Position.x, 0.0, Position.z);
		 Dir *= clamp(0.4*ttime, 0.0, 1.0);
		 Position.xyz = vec3(ivec3((Position.xyz + Dir)*ftt)) / ftt - Dir;

		//Position.xyz += Normal.xyz*0.1;
		Position.y *= 0.0;

	}
	
	//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.xyz = Normal.xyz*0.0;



}
