
// Uniform variables for texturing
uniform sampler2D 	aaa_tex2d[4];

// Dimension of textures : 0, 1, 2, 3 dimension, -1 if unused per Tex Unit
uniform	int			aaa_tex_dim[4];

// Size of Texture for each texture unit
uniform vec2		aaa_tex_0_size;

// 0 = time
// 1 = mouse X
// 2 = mouse Y
uniform float		aaa_pu_float[6];


mat2 RotateMat(float angle)
{
	float si = sin(angle);
	float co = cos(angle);
	return mat2(co, si, -si, co);
}

vec3 Colour(in float h) 
{
	h = fract(h);
	return clamp((abs(fract(h + vec3(3.0, 2.0, 1.0) / 3.0) * 6.0-3.0) - 1.0), 0.0 , 1.0);
}

void main(void)
{	
	float time = aaa_pu_float[0];
	// Rough panning...
	vec2 pixel = (gl_FragCoord.xy - aaa_tex_0_size.xy*.5)/aaa_tex_0_size.xy 
							+ vec2(0.0,.1-smoothstep(9.0, 12.0, time)*.35
							+ smoothstep(18.0, 20.0, time)*.15);
	vec3 col;
	float n;
	float inc = (smoothstep(17.35, 18.5, time)-smoothstep(18.5, 21.0, time)) * (time-16.0) * 0.1;
	mat2 rotMat =  RotateMat(inc);
	for (int i = 1; i < 50; i++)
	{
		pixel = pixel * rotMat;
		float depth = 40.0+float(i) + smoothstep(18.0, 21.0, time)*65.;
		vec2 uv = pixel * depth/210.0;
		
		// Shifting the pan to the text near the end...
		// And shifts to the right again for the last line of text at 23:00!
		col = texture2D( aaa_tex2d[0], fract(uv+vec2(.5 + smoothstep(20.0, 21.0, time)*.11
												     + smoothstep(23.0, 23.5, time)*.04
												  , .7-smoothstep(20.0, 21.0, time)*.2))).rgb;
		col = mix(col, col * Colour((float(i)+aaa_pu_float[0])*.52), smoothstep(18.5, 21.5, time));

		if ((1.0-(col.y*col.y)) < float(i+1) / 50.0)
		{
			break;
		}

	}
	// Some contrast...
	col = min(col*col*1.5, 1.0);
	
	// Fade to red evil face...
	float gr = smoothstep(17., 16., time) + smoothstep(18.5, 21.0, time);
	float bl = smoothstep(17., 15., time) + smoothstep(18.5, 21.0, time);
	col = col * vec3(1.0, gr, bl);
	
	// Cut off the messy end...
	col *= smoothstep(29.5, 28.2, time);
	gl_FragColor = vec4(col, 1.0);
}