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

#include "fragmentVersionCentroid.h"
#include "uniformPerFrameConstants.h"

#if __VERSION__ >= 300
	#ifndef BYPASS_PIXEL_SHADER
		#if defined(TEXEL_AA) && defined(TEXEL_AA_FEATURE)
			_centroid in highp vec2 uv0;
			_centroid in highp vec2 uv1;
		#else
			_centroid in vec2 uv0;
			_centroid in vec2 uv1;
		#endif
	#endif
#else
	#ifndef BYPASS_PIXEL_SHADER
		varying vec2 uv0;
		varying vec2 uv1;
	#endif
#endif

varying vec4 color;
varying highp vec3 wpos;
varying highp vec4 posi;

#ifdef FOG
varying vec4 fogColor;
#endif

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

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

#include "dect.h"




void main()
{
#ifdef BYPASS_PIXEL_SHADER
	gl_FragColor = vec4(0, 0, 0, 0);
	return;
#else 

#if USE_TEXEL_AA
	vec4 diffuse = texture2D_AA(TEXTURE_0, uv0);
#else
	vec4 diffuse = texture2D(TEXTURE_0, uv0);
	highp vec4 tex = texture2D(TEXTURE_0, uv0);
	highp vec4 NC = texture2D(TEXTURE_1, uv1);
#endif
	
    highp vec4 tep = FOG_COLOR;
	highp vec4 temp = texture2D(TEXTURE_1, vec2(0.0, 1.0));
    highp float chuva = Rain(FOG_CONTROL);
    highp float dia = pow(max(min(tep.r * 1.5, 1.0), 0.0), 1.2);
    highp float diat = pow(max(min(temp.r * 1.5, 1.0), 0.0), 1.2);
    highp float noite =  mix(0.0, 2.5, pow(max(min(1.0 - temp.r * 1.5, 1.0), 0.0), 1.2));
    
    highp vec3 DC = mix(vec3(1.4,1.4,1.4),vec3(FOG_COLOR.rgb) + pow(uv1.x, 3.0),chuva);
    highp float fog_flag = 0.0;
    highp vec3 SC = vec3(0.768,0.758,0.858)+uv1.x*vec3(0.318,0.318,0.17);
    highp vec3 La = vec3(0.7,0.1,-0.3);
    highp vec3 Lb = vec3(0.7,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, pisc);
    highp float shdY = 0.87;
    highp float shdX = color.g;
    
#ifdef SEASONS_FAR
	diffuse.a = 1.0;
#endif

#if USE_ALPHA_TEST
	#ifdef ALPHA_TO_COVERAGE
	#define ALPHA_THRESHOLD 0.05
	#else
	#define ALPHA_THRESHOLD 0.5
	#endif
	if(diffuse.a < ALPHA_THRESHOLD)
		discard;
#endif
	
vec4 inColor = color;

#if defined(BLEND)
	diffuse.a *= inColor.a;
#endif

#if !defined(ALWAYS_LIT)
	NC.rgb = mix(mix(NC.rgb,vec3(1.0,1.0,1.0),uv1.y),NC.rgb *max(NC.rgb, vec3(0.07,0.0,0.88)),noite);
	diffuse *= NC;
#endif

#ifndef SEASONS
	#if !USE_ALPHA_TEST && !defined(BLEND)
		diffuse.a = inColor.a;
	#endif
	
	diffuse.rgb *= inColor.rgb;
#else
	vec2 uv = inColor.xy;
	diffuse.rgb *= mix(vec3(1.0,1.0,1.0), texture2D( TEXTURE_2, uv).rgb*2.0, inColor.b);
	diffuse.rgb *= inColor.aaa;
	diffuse.a = 1.0;
#endif

if(End(FOG_COLOR)){
    
    diffuse.rgb *= vec3(0.511,0.362,0.985);
    diffuse.rgb += diffuse.rgb * vec3(0.511,0.362,0.985)*1.2 * pow(uv1.x, 2.0);
   
}else{
//nether
if(Nether(FOG_CONTROL)){//nether
    //nether do fumaça
    diffuse.rgb += mix(vec3(0.0,0.0,0.0), mix(La * 2.5, La * 6.0, pow(uv1.x,0.8)), clamp(FOG_COLOR.x - 0.1, 0.0, 1.0));
    //nether gases de azul
    diffuse.rgb *= mix(diffuse.rgb ,vec3(-0.2,-0.2,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(uv1.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(uv1.x,3.0)), clamp(FOG_CONTROL.x - FOG_COLOR.y, 0.0, 1.0));
    
}else{

if(Underwater(FOG_CONTROL)){
diffuse.r *= 0.3;
}else{

    if(color.a==0.0){
        diffuse.rgb *= 1.25;
        shdY = 0.8749;
        shdX = color.g*1.3;
    }
    if(color.b > color.r*2.0){
        shdX = 1.0;
        shdY = 0.9158;
    }

    #ifdef FOG
    fog_flag = 0.5;
    #endif
    diffuse.rgb *= DC;

    if(((uv1.y<shdY-0.000)||(shdX<0.630))&&fog_flag==0.0)diffuse.rgb *= SC;
    
    if(((uv1.y<shdY-0.001)||(shdX<0.625))&&fog_flag==0.0)diffuse.rgb *= SC;
    
    if(((uv1.y<shdY-0.002)||(shdX<0.620))&&fog_flag==0.0)diffuse.rgb *= SC;
    
    if(((uv1.y<shdY-0.003)||(shdX<0.615))&&fog_flag==0.0)diffuse.rgb *= SC;
    
    

diffuse.rgb += (diffuse.rgb*(L*1.5-0.5)*pow(uv1.x,3.0)*1.5)*mix(mix(1.0,1.0-uv1.y,diat),1.0,chuva);

}


}}

#ifdef FOG
	diffuse.rgb = mix( diffuse.rgb, fogColor.rgb, fogColor.a );
#endif

	gl_FragColor = diffuse;
	
#endif // BYPASS_PIXEL_SHADER
}
