uniform sampler2D texture0;
uniform sampler2D texture1;
//uniform sampler2D texture2;

void main()
{
	//Simple passthrough, outputs interpolated vertex colours.
	vec4 textureColour = texture2D(texture0, gl_TexCoord[0].xy);
	vec4 patternValue = texture2D (texture1, gl_TexCoord[0].xy * 0.1);
	
	float backingValue = textureColour.r - textureColour.b;
	
	
	patternValue.xyz = mix (patternValue.xyz, vec3(0.5,0.5,0.5), backingValue);
		
	patternValue.xyz *= gl_Color.xyz;
	//Note: small numbers added as mad hax to ensure that black colours come out middle grey when patterns are turned off, instead of causing a divide by zero.
	float intensity = (patternValue.x + patternValue.y + patternValue.z + 0.0005) / (gl_Color.x + gl_Color.y + gl_Color.z + 0.001);
	intensity = intensity * 0.5 + 0.15;
	
	float centreDistance = length(gl_TexCoord[0].xy - vec2(0.5,0.5));
	float distanceAround = textureColour.g;
	float labelValue = textureColour.b;
	
	
	
	//float timeIntensity = clamp(((gl_Color.a - distanceAround)*50.0) + 0.5, 0.0, 1.0);
	
	float tap1 = clamp((gl_Color.a - texture2D(texture0, gl_TexCoord[0].xy - vec2(-0.005, -0.01)).g) * 100.0, 0.0, 1.0);
	float tap2 = clamp((gl_Color.a - texture2D(texture0, gl_TexCoord[0].xy - vec2( 0.005,  0.01)).g) * 100.0, 0.0, 1.0);
	float tap3 = clamp((gl_Color.a - texture2D(texture0, gl_TexCoord[0].xy - vec2(-0.01,  0.005)).g) * 100.0, 0.0, 1.0);
	float tap4 = clamp((gl_Color.a - texture2D(texture0, gl_TexCoord[0].xy - vec2( 0.01, -0.005)).g) * 100.0, 0.0, 1.0);
	
	float timeIntensity = (tap1 + tap2 + tap3 + tap4) / 4.0;
	
	gl_FragColor.xyz = vec3(intensity + 0.4 * timeIntensity);
	//gl_FragColor.xyz = vec3(textureColour.xyz)	
	gl_FragColor.xyz = mix (gl_FragColor.xyz, gl_Color.xyz, labelValue);
	
	gl_FragColor.a = clamp((0.5 - centreDistance) * 50.0, 0.0, 1.0);
}