uniform sampler2D texture0;
uniform sampler2D texture1; //Particle texture
varying vec2 worldPos;
uniform vec4 particleOffsets1;
uniform vec4 particleOffsets2;
uniform float glowFactor;

void main()
{

	//vec4 textureColour = texture2D(texture0, gl_TexCoord[0].xy);
	vec4 particleTexture = max (
							max(
							texture2D(texture1, worldPos * 0.09 + particleOffsets1.xy),
							texture2D(texture1, worldPos * 0.09 + particleOffsets1.zw)),
							max(
							texture2D(texture1, worldPos * 0.09 + particleOffsets2.xy),
							texture2D(texture1, worldPos * 0.09 + particleOffsets2.zw)));
	
	
	float distanceFromCentre = (length(gl_TexCoord[0].xy - vec2(0.5, 0.5)) * 0.25);
	float distanceFromCentreNoised = distanceFromCentre + 0.01 * particleTexture.b;
	distanceFromCentre += 0.01;
	
	float mainColour = clamp((0.001 / (distanceFromCentreNoised * distanceFromCentreNoised) - 0.005) * glowFactor, 0.0, 1.0);
	float otherColours = (0.00002 / (distanceFromCentreNoised * distanceFromCentreNoised * distanceFromCentreNoised * 2.5) - 0.005) * pow(glowFactor, 1.5);
	
	float killValue = clamp((1.0 - ((-distanceFromCentre / 0.03) + 1.0) * 10.0),0.0,1.0);
	
	otherColours *= killValue;
	
	mainColour *= (particleTexture.r * pow(mainColour, 0.3) * 0.3) + 1.0;
	
	vec3 outColour = mix(gl_Color.rgb, vec3(1.0, 1.0, 1.0), otherColours) * mainColour;

	gl_FragColor = vec4(outColour * gl_Color.a, gl_Color.a);
	
	//gl_FragColor = vec4(1, 1, 1, otherColours);
}