
varying vec2 texcoord;
varying vec2 texcoordmul;

uniform sampler2D seedTex;
uniform float time;

float saw(float a) {
//	return abs(mod(a, 2.0)-1.0);
//	return 0.5+0.5*cos(a*3.14159265358979);

	return mix(abs(mod(a, 2.0)-1.0), 0.5+0.25*cos(a*3.14159265358979), 0.5*sin(time*0.25)+0.5);
}

float saw2(float a) {
	return clamp(abs(mod(a, 2.0)-1.0), 0.25, 0.75);
}

float saw3(float a) {
	a = mod(a, 1.0);
	a = min(a*10.0, (1-a)*1.1);
	return a;
}

float saw4(float a) {
	if (a < -0.1) return a+0.1;
	if (a > 0.1) return a-0.1;
	return 0.0;
}

float square(float a) {
	if (mod(a, 1.0) < 0.5) return 0.0;
	return 1.0;
}

void main() {
	//vec2 texcoord = gl_TexCoord[0].xy;
/*
	vec4 col = texture2D(seedTex, texcoord);
	float h = (col.r-0.5)*sin(time) + (col.g-0.5)*cos(time);
	h += 0.25*((col.b-0.5)*sin(time*3.0) + (col.a-0.5)*cos(time*3.0));
/*	vec4 col2 = texture2D(seedTex, texcoordmul);
	h += 0.050*((col2.r-0.5)*sin(time) + (col2.g-0.5)*cos(time));
	h += 0.025*((col2.b-0.5)*sin(time*3.0) + (col2.a-0.5)*cos(time*3.0));#/
	gl_FragColor.r = 0.5+0.35*h;*/

	float v1,v2,v3,v4;

	vec2 texmul = texcoord*32.0;
	v1 = saw(saw2(texmul.x+time)+saw2(texmul.y+time));
	texmul = texcoord*48.0;
	vec2 texmul2 = vec2(texcoord.x*28.0-texcoord.y*6.0, texcoord.y*28.0+texcoord.x*6.0);
	v2 = saw2(saw2(texmul2.x+time*0.5)+saw2(texmul2.y+time*0.5));

	float val = v1+v2;

/*	texmul = texcoord*16.0;
	v3 = saw3(texmul.x+time*0.11)+saw3(-texmul.x-time*0.13);
	v4 = saw3(texmul.y+time*0.23)+saw3(-texmul.y-time*0.21);
	val = (v3+v4)*0.25;*/

/*	texmul = texcoord*48.0;
	val = saw(saw(texmul.x+time*0.95)+saw(texmul.y+time*0.95));*/

	texmul = vec2(abs(texcoord.x-0.5), abs(texcoord.y-0.5))*32.0;

	float yp = saw(time*0.25+saw2(time*0.35+texmul.x*0.25+texmul.y*0.34));
	val = yp+saw4(val-yp);

//	v3 = (square(texmul.x+time*0.75)+square(texmul.y+time*0.75))*0.3;


	texmul = texcoord*32.0;
	v1 = (saw(texmul.x+time*0.95)+saw(texmul.y+time*0.95));
	texmul = vec2(texcoord.x*28.0-texcoord.y*6.0, texcoord.y*28.0+texcoord.x*6.0);
	v2 = (saw(texmul.x+time*0.95)+saw(texmul.y+time*0.95));
	texmul = vec2(texcoord.x*16.0-texcoord.y*26.0, texcoord.y*16.0+texcoord.x*26.0);
	v3 = (saw(texmul.x+time*0.95)+saw(texmul.y+time*0.95));

//	val = v1+v2+v3-min(min(v1,v2),v3)-max(max(v1,v2),v3);
	val = max(min(max(v1,v2), v3), min(v1,v2));
//	val *= 0.5;
	val -= 0.5;



	vec4 col = vec4(val);

	gl_FragColor = col;
}
