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

#include "fragmentVersionCentroidUV.h"

#include "uniformEntityConstants.h"
#include "uniformShaderConstants.h"

LAYOUT_BINDING(0) uniform sampler2D TEXTURE_0;
LAYOUT_BINDING(1) uniform sampler2D TEXTURE_1;

#ifdef EFFECTS_OFFSET
#include "uniformEffectsConstants.h"
#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

#include "uniformPerFrameConstants.h"
#include "dect.h"

void main()
{
#ifdef EFFECTS_OFFSET
	vec4 diffuse = texture2D( TEXTURE_0,  uv + EFFECT_UV_OFFSET);
#else
	vec4 diffuse = texture2D( TEXTURE_0,  uv );
#endif

#ifdef MULTI_COLOR_TINT
	// Texture is a mask for tinting with two colors
	vec2 colorMask = diffuse.rg;

	// Apply the base color tint
	diffuse.rgb = colorMask.rrr * color.rgb;

	// Apply the secondary color mask and tint so long as its grayscale value is not 0
	diffuse.rgb = mix(diffuse, colorMask.gggg * CHANGE_COLOR, ceil(colorMask.g)).rgb;
#endif

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

#if defined(ENABLE_VERTEX_TINT_MASK) && !defined(MULTI_COLOR_TINT)
	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; // This line is needed for horse armour icon and dyed leather to work properly
	}
#endif
float uvv = 0.0;
#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);
	glint.rgb *= color.a;
uvv = layer1UV.x;
	#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

#ifdef ENABLE_VERTEX_TINT_MASK

#ifdef ENABLE_CURRENT_ALPHA_MULTIPLY
	diffuse = diffuse * vec4(1.0, 1.0, 1.0, HUD_OPACITY);
#endif

#elif !defined(MULTI_COLOR_TINT)
	diffuse = diffuse * color;
#endif

    highp vec4 temp = texture2D(TEXTURE_1, vec2(0.0,1.0) );
    highp float dia = max(0.0, min( 1.0,(temp.r - 0.5) * 2.0));
    highp float chuva = Rain(FOG_CONTROL);
    highp float dss = mix(mix( 0.0, 2.5, clamp((1.-max(0.0, min( 1.0, (temp.r - 0.48) * 2.0))*(1.5+chuva))*2.5, .0, 1.)*clamp((temp.r -.15)*1.0,0.,1.)),0.5,mix(0.0,5.0,pow(max(min(1.0-temp.r*1.5,1.0),0.0),1.2)));
    highp vec3 La = vec3(0.5,0.1,-0.3);
    highp vec3 Lb = vec3(0.5,0.1,-0.3) * 0.5;
    highp float pisc = 0.2 * cos(TIME * 0.3 * cos(0.3 * TIME));
	highp vec3 L = mix(La, Lb, mix(pisc,0.0,mix(0.0,1.,pow(color.x,9.0))));
    highp vec3 Cc = mix(diffuse.rgb*vec3(0.5,0.5,0.5)*1.2, diffuse.rgb + diffuse.rgb  * (L * 1.5 -0.5),pow(color.x,9.0));
    highp vec3 Cd = mix(mix(diffuse.rgb*0.3, diffuse.rgb*vec3(1.4,1.4,1.4),pow(color.x,9.0)),Cc,chuva);
    highp vec3 Cn =  mix(diffuse.rgb*max(vec3(0.3,0.3,0.3), mix(vec3(0.0,0.0,1.0),vec3(0.0,0.0,0.0),chuva)), diffuse.rgb + diffuse.rgb  * (L * 1.5 -0.5),pow(color.x,9.0));
              

if(End(FOG_COLOR)){
    diffuse.rgb *= vec3(0.511,0.362,0.985);
    diffuse.rgb += mix(vec3(0.0,0.0,0.0), diffuse.rgb * vec3(0.511,0.362,0.985) * 1.2 , pow(color.x, 2.0));
}else{
if(Nether(FOG_CONTROL)){
	vec3 La = vec3(0.5,0.1,-0.3);
	//nether do fumaça
	diffuse.rgb += mix(vec3(0.0,0.0,0.0), mix(La, La * 5.0, pow(color.x, 0.8)), clamp(FOG_COLOR.x - 0.1, 0.0, 1.0));
	//nether gases de azul
	diffuse.rgb *= mix(diffuse.rgb, vec3(0.1,0.1,4.0), clamp(FOG_COLOR.y - (FOG_COLOR.x - 0.1), 0.0, 1.0));
	diffuse.rgb += mix(vec3(0.0,0.0,0.0) ,(vec3(0.0,0.0,diffuse.b) * 15.0) * pow(color.x, 5.0), clamp(FOG_COLOR.y - (FOG_COLOR.x - 0.1), 0.0, 1.0));
    diffuse.rgb += mix(diffuse.rgb, mix(-vec3(0.0,0.1,0.1) * 3.5, La * 5.0, pow(color.x, 3.0)), clamp(FOG_CONTROL.x - FOG_COLOR.y, 0.0, 1.0));
}else{
if(Underwater(FOG_CONTROL)){
    diffuse.r *= 0.3;
	diffuse.rgb *=1.5;
	diffuse.rgb = mix(diffuse.rgb * vec3(0.0, 0.5, 0.9) * 0.6, diffuse.rgb * vec3(0.0, 1.0, 1.0), pow(color.x, 5.0));
}else{
	
	
	diffuse.rgb = mix(Cn, Cd,dia);
	//diffuse.rgb *= mix(mix(vec3(0.3,0.3,0.3), L, pow(color.x, 9.0)), mix(vec3(0.3,0.3,0.3), vec3(1.3,1.3,1.3), pow(color.y, 10.0)), dia);
    //diffuse.rgb = mix(diffuse.rgb, diffuse.rgb * vec3(3.5,3.0,2.0), dss);
}}}


	// Fog needs to be applied after the color tinting.
#ifdef ENABLE_FOG
	//apply fog
	diffuse.rgb = mix(diffuse.rgb, fogColor.rgb, fogColor.a);
#endif

	gl_FragColor = diffuse;
}
