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

#include "fragmentVersionCentroid.h"
#include "uniformShaderConstants.h"

#include "uniformShaderConstants.h"
#include "util.h"
#include "uniformPerFrameConstants.h"

LAYOUT_BINDING(0) uniform sampler2D TEXTURE_0;

#if __VERSION__ >= 300
_centroid varying vec4 uv;
#else
varying vec4 uv;
#endif

#ifdef ENABLE_FOG
varying vec4 fogColor;
#endif

#ifdef ENABLE_LIGHT
varying vec4 light;
#endif

#ifndef DISABLE_TINTING
varying vec4 color;
#endif

varying highp vec4 ligc;


#include "dect.h"

void main()
{
	vec4 diffuse = texture2D(TEXTURE_0, uv.xy);
	vec4 base = texture2D(TEXTURE_0, uv.zw);

#ifndef DISABLE_TINTING
	base.a = mix(diffuse.r * diffuse.a, diffuse.a, color.a);
	base.rgb *= color.rgb;
#endif

highp vec4 lig = vec4(0.0,0.0,0.0,0.0);

#ifdef ENABLE_LIGHT
	base.rgb *= light.rgb;
	lig = light;
#endif

    highp vec4 tep = FOG_COLOR;
    highp float chuva = Rain(FOG_CONTROL);
    highp float dia = mix(pow(max(min(tep.r * 1.5+0.3, 1.0), 0.0), 1.2),1.0,chuva);
	
	highp vec3 Lal = vec3(0.5,0.2,-0.3);
    highp vec3 Lb = vec3(0.5,0.2,-0.3) * 0.5;
    highp float pisc = 0.2 * cos(TIME * 0.3 * cos(0.3 * TIME));
    highp vec3 Ll = mix(Lal, Lb, mix(pisc,0.0,mix(0.0,1.,pow(ligc.x,1.6))));
    
	highp vec3 La = vec3(0.5,0.1,-0.3);
	highp vec3 Nc = max(base.rgb,vec3(0.0,0.0,1.5));
	highp vec3 Cn = mix( Nc*Nc, Ll* 1.8,pow(ligc.x,1.5));
	highp vec3 Cc = mix( lig.rgb, vec3(1.35,1.34,1.31)*1.2, pow( lig.g,0.5));
	highp vec3 chd = mix(vec3(1.0,1.0,1.0),  mix(vec3(0.5,0.5,0.5) *  lig.rgb, Ll* 1.8, pow(ligc.x,10.0)), chuva);

if(ligc.b <= 0.99){
if(End(FOG_COLOR)){
    base.rgb *= vec3(0.511,0.362,0.985);
    base.rgb += mix(vec3(0.0,0.0,0.0), base.rgb * vec3(0.511,0.362,0.985) * 1.2 , pow(ligc.x, 2.0));
}else{
if(Nether(FOG_CONTROL)){//nether
	//nether do fumaça
	base.rgb += mix(vec3(0.0,0.0,0.0),  mix(La * 2.5, La * 6.0, pow(ligc.x,0.8)), clamp(FOG_COLOR.x - 0.1, 0.0, 1.0));
	//nether gases de azul
	base.rgb *= mix(base.rgb, vec3(-0.2,-0.2,4.0), clamp(FOG_COLOR.y - (FOG_COLOR.x - 0.1), 0.0, 1.0));
	base.rgb += mix(vec3(0.0,0.0,0.0) ,(vec3(0.0,0.0,base.b) * 15.0) * pow(ligc.x,5.0), clamp(FOG_COLOR.y - (FOG_COLOR.x - 0.1), 0.0, 1.0));
	base.rgb += mix(base.rgb, mix(-vec3(0.0,0.1,0.1) * 1.5, La * 2.0, pow(ligc.x,3.0)), clamp(FOG_CONTROL.x - FOG_COLOR.y, 0.0, 1.0));
}else{
if(Underwater(FOG_CONTROL)){
	base.r *= 0.3;
	base.rgb *= mix(mix(vec3(0.2,0.2,0.3), vec3(0.0,0.5,1.0), pow(lig.g,1.0)), base.rgb, pow(ligc.x,2.0));
}else{

if(FOG_COLOR.g > 0.0 && FOG_COLOR.g < 0.1){
base.rgb *= mix(Cn, chd, chuva);
}else{
base.rgb *= mix(mix(vec3(1.0,1.0,1.0), Cc * lig.g, dia),chd,chuva);
if(ligc.r < 0.91) {
base.rgb *= mix(vec3(0.7,0.7,0.7), vec3(1.0,1.0,1.0), chuva);
}
}}}}}

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

#ifdef UI_ENTITY
	base.a *= HUD_OPACITY;
#endif
	gl_FragColor = base;
}