#version 100
uniform sampler2D TEXTURE_0;
uniform sampler2D TEXTURE_1;

varying vec4 light;
varying vec4 fogColor;

#ifndef COLOR_BASED
	varying vec2 uv;
#endif
#ifdef USE_OVERLAY
	varying vec4 overlayColor;
#endif

#ifdef USE_COLOR_MASK
	varying vec4 changeColor;
#endif


#ifdef USE_EMISSIVE
#define NEEDS_DISCARD(C)	(C.a + C.r + C.g + C.b == 0.0)
#else
#define NEEDS_DISCARD(C)	(C.a < 0.5)
#endif

void main()
{
	vec4 color = vec4(1.0);
#ifndef COLOR_BASED
	color = texture2D( TEXTURE_0, uv );

	#ifdef ALPHA_TEST
		if(NEEDS_DISCARD(color))
		 	discard;
	#endif
#endif

#ifdef USE_COLOR_MASK
	color.rgb = mix( color, color*changeColor, color.a).rgb;
	color.a = 1.0;
#endif

#ifdef USE_OVERLAY
	//use either the diffuse or the OVERLAY_COLOR
	color.rgb = mix(color, overlayColor, overlayColor.a).rgb;
#endif

#ifdef USE_EMISSIVE
	//make glowy stuff
	color *= mix(vec4(1.0), light, color.a );
#else
	color *= light;
#endif

	//apply fog
	color.rgb = mix( color.rgb, fogColor.rgb, fogColor.a );

	//WARNING do not refactor this 
	gl_FragColor = color;
}
