// __multiversion__
// This signals the loading code to prepend either #version 100 or #version 300 es as apropriate.

// To use centroid sampling we need to have version 300 es shaders, which requires changing:
// attribute to in
// varying to out when in vertex shaders or in when in fragment shaders
// defining an out vec4 FragColor and replacing uses of gl_FragColor with FragColor
// texture2D to texture
#if __VERSION__ >= 300

#ifdef MSAA_FRAMEBUFFER_ENABLED
#define _centroid centroid
#else
#define _centroid
#endif

// version 300 code
_centroid in vec2 uv;

#define varying in
#define texture2D texture
out vec4 FragColor;
#define gl_FragColor FragColor

#else

// version 100 code
varying vec2 uv;

#endif

uniform sampler2D TEXTURE_0;
uniform sampler2D TEXTURE_1;
uniform vec4 OVERLAY_COLOR;

#ifdef ENABLE_VERTEX_TINT_MASK
uniform vec4 CHANGE_COLOR;
#endif

#ifdef GLINT
uniform vec4 GLINT_COLOR;
#endif

varying vec4 color;

#ifdef ENABLE_FOG
varying vec4 fogColor;
#endif

#ifdef GLINT
	varying vec2 layer1UV;
	varying vec2 layer2UV;

	vec4 glintBlend(vec4 dest, vec4 source) {
		return vec4(source.rgb * source.rgb, 0.0) + dest;
	}
#endif

void main()
{
	vec4 diffuse = texture2D( TEXTURE_0,  uv );
	vec3 Itens_Color = vec3(1.0,1.0,1.2);

#ifdef ALPHA_TEST
#ifdef ENABLE_VERTEX_TINT_MASK
	if(diffuse.a <= 0.0)
#else
	if(diffuse.a < 0.5)
#endif
	 	discard;
#endif

#ifdef ENABLE_VERTEX_TINT_MASK
	diffuse.rgb = mix(diffuse.rgb, diffuse.rgb*color.rgb, diffuse.a);
	if (color.a > 0.0) {
		diffuse.a = (diffuse.a > 0.0) ? 1.0 : 0.0;
	}
#endif

#ifdef GLINT
	vec4 layer1 = texture2D(TEXTURE_1, fract(layer1UV)).rgbr * GLINT_COLOR;
	vec4 layer2 = texture2D(TEXTURE_1, fract(layer2UV)).rgbr * GLINT_COLOR;
	vec4 glint = (layer1 + layer2);

	#ifdef INVENTORY
		diffuse.rgb = glint.rgb;
	#else
		diffuse.rgb = glintBlend(diffuse, glint).rgb;
	#endif
#endif

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

diffuse.rgb *= Itens_Color;

#ifdef ENABLE_FOG
	//apply fog
	diffuse.rgb = mix( diffuse.rgb, fogColor.rgb, fogColor.a );
#endif

#ifdef ENABLE_VERTEX_TINT_MASK
	gl_FragColor = diffuse;
#else
	gl_FragColor = diffuse * color;
#endif
}
