uniform sampler2D tex;

uniform vec2 center;
uniform float radius;
uniform float angle;
uniform float time;
uniform float speed;

vec4 variations = vec4(0.1,0.4,0.6,0.9);
vec2 centerV[4];

float modtime = mod(time, 6.283185307);
float percent = modtime/6.283185307;

vec2 singleTriangle(vec2 coords)
{
	float magnification = 1.4;
	float mtime = mod(time, 6.283185307);
	float d = angle * mtime * speed;
	float c = cos(d);
	float s = sin(d);
	float r = radius * (mtime*mtime*0.1*speed+1.);
	mat2 rot = mat2(c, -s, s, c);
	vec2 dist = rot*(coords-center)+vec2(radius/2.,-radius/3.);
	float distlength = length(dist);

	if (dist.y<dist.x/1.3 && r > dist.x && dist.y-0.1 > -r)
		coords = center + (coords-center) / magnification;

	return coords;
}

vec2 multipleTriangles(vec2 coords)
{
	for (int i=0; i<4; i++)
	{
		float magnification = 2.4 + variations[i];

		float mtime = mod(time + variations[i]*6.283185307, 6.283185307);

		magnification = mix(1.,magnification, ((0.5-abs(mtime/6.283185307 - 0.5)) * 2.) );

		float d = angle * mtime * speed * variations[i];
		float c = cos(d);
		float s = sin(d);

		vec2 centerV = center;
		centerV.x *=  variations[i];

		float r = radius; // * (mtime*mtime*0.1*speed+1.) * variations[i];

		mat2 rot = mat2(c, -s, s, c);
		vec2 dist = rot*(coords-centerV)+vec2(radius/2.,-radius/3.);
		float distlength = length(dist);

		if (dist.y<dist.x/1.3 && r > dist.x && dist.y-0.1 > -r)
			coords = center + (coords-center) / magnification;
	}

	return coords;

}

void main()
{

	vec2 coords = gl_TexCoord[0].xy;
	gl_FragColor = gl_Color * mix( texture2D(tex, multipleTriangles(gl_TexCoord[0].xy)), 
								texture2D(tex, gl_TexCoord[0].xy), percent*0.5+0.5);

//	gl_FragColor = gl_Color * texture2D(tex, singleTriangle(gl_TexCoord[0].xy));
}
