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

#include "fragmentVersionCentroid.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 vec3 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"
#include "inform/option.txt"


 //noise value 3D 
highp float hash(highp vec3 p)
 {
    p  = fract(p * 0.3183099 + 0.1);
	p *= 17.0;
    return fract(p.x * p.y * p.z * (p.x + p.y + p.z));
}

highp float noise( in highp vec3 x )
{
    highp vec3 i = floor(x);
    highp vec3 f = fract(x);
    f = f * f * (3.0 - 2.0 * f);
    return mix(mix(mix(hash(i + vec3(0.0,0.0,0.0)), hash(i + vec3(1.0,0.0,0.0)), f.x), mix(hash(i + vec3(0.0,1.0,0.0)), hash(i + vec3(1.0,1.0,0.0)), f.x), f.y), mix(mix(hash(i + vec3(0.0,0.0,1.0)), hash(i + vec3(1.0,0.0,1.0)), f.x), mix(hash(i + vec3(0.0,1.0,1.0)), hash(i + vec3(1.0,1.0,1.0)), f.x), f.y), f.z);
}

highp float WaterPos(highp vec3 v, highp vec3 w, highp vec4 col, float wt)
{
    highp float wap = (length(v)*0.2) / length(v) / length(w);
    if(FOG_CONTROL.x < 0.001){}else{if(wt == 1.0){}else{wap = 0.0;}}
	return wap;
}

highp float onda(highp vec3 X)
{
    X.x *= 0.5;
    X.z *= 0.916;
    highp float D = abs(sin(noise(vec3(X.x + TIME * 0.524, X.x + X.y + TIME * 0.5, X.x - X.z - TIME * 1.444))));
    return mix(0.592, 1.568, D);
}

highp vec3 WaterMap(highp vec3 pos) {
    highp float H = onda(pos - vec3(1.0,0.0,0.0)) - onda(pos);
    return  1.0 - vec3(-H,1.0,1.0);
}

highp vec3 light(highp vec3 di, highp vec3 d, highp vec3 L, highp vec2 u1, highp float dia, highp float ch, highp float dss) {
	highp vec3 col = d;
	col +=  L * 1.5-0.5+col*smoothstep(0.75,1.0,pow(u1.x,1.));
	highp float ti = mix(0.0,mix(0.0,mix(1.0,0.0,ch),dia),u1.y*1.1);
	highp float temp = mix(0.0,1.5,pow(u1.x,3.0));
	temp = mix(temp,0.0,ti);
	col = mix(di,col,temp);
return col;
}

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);
    
#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 float dsol = 0.5 - abs(0.5 - (1.0 - max(0.0, min(1.0, (temp.r - 0.46) * 2.0))));
             dsol = mix(dsol,0.0,chuva);
    highp float dss = mix(mix( 0.0, 2.5, clamp((1.0 - max(0.0, min( 1.0, (temp.r - 0.48) * 2.0)) * (1.5 + chuva)) * 2.5, 0.0, 1.0) * clamp((temp.r - 0.15) * 1.0, 0.0, 1.0)), 0.5, mix(0.0, 5.0, pow(max(min(1.0 - temp.r * 1.5, 1.0), 0.0), 1.2)));
//normal
    highp vec3 N = -normalize(cross(dFdx(posi.xyz), dFdy(posi.xyz)));
    highp float mx = 0.0;float nx = 0.0;float mz = 0.0;float nz = 0.0;float my = 0.0;float ny = 0.0;float mxy = 0.0;float nxy = 0.0;float mzy = 0.0;float nzy = 0.0;float myx = 0.0;float nyx = 0.0;float myz = 0.0;float nyz = 0.0;float mxyt = 0.0;float nxyt = 0.0;
//sombra bias
    highp float SC = mix(mix(0.8, 0.4, diat), 1.0, chuva);
    SC = mix(SC, 0.7, mix(0.0,4.0,dsol));
    highp float biasY = 0.88;
    highp float biasX = 0.67;
    highp float shdX = color.g;
    highp float shdY = uv1.y;
//tempo cor
	highp vec3 DC = vec3(1.4,1.4,1.4)*1.2;
    highp vec3 CC = vec3(FOG_COLOR.rgb);
//luz
    highp vec3 La = 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 L = mix(La, Lb, mix(pisc * (1.0 - 1.0 * diat * uv1.y),0.0,mix(0.0,1.,pow(uv1.x,1.4))));
    highp vec3 AGL = vec3(0.5,0.5,0.5);
    
//-X
if(-N.x > 0.1){if((-N.y > 0.1)||(N.y > 0.1)){}else{mx = 1.0;}}
//+X
if(N.x > 0.1){if((-N.y > 0.1)||(N.y > 0.1)){}else{nx = 1.0;}}
//-Z
if(-N.z > 0.1){if((-N.y > 0.1)||(N.y > 0.1)){}else{mz = 1.0;}}
//+Z
if(N.z > 0.1){if((-N.y > 0.1)||(N.y > 0.1)){}else{nz = 1.0;}}
//+Y
if(-N.y > 0.1){if((-N.x > 0.1)||(N.x > 0.1)||(-N.z > 0.1)||(N.z > 0.1)){}else{my = 1.0;}}
//-Y
if(N.y > 0.1){if((-N.x > 0.1)||(N.x > 0.1)||(-N.z > 0.1)||(N.z > 0.1)){}else{ny = 1.0;}}
//+XY
if((-N.x > 0.1) && (-N.y > 0.1)){if((N.z < 0.1)||(N.y > 0.1)){mxy = 1.0;}}
//-XY
if((N.x > 0.1) && (-N.y > 0.1)){if((-N.z > 0.1)||(N.y > 0.1)){}else{nxy = 1.0;}}
//+ZY
if((-N.z > 0.1) && (-N.y > 0.1)){if((N.x < 0.1)||(N.y > 0.1)){mzy = 1.0;}}
//-ZY
if((N.z > 0.1) && (-N.y > 0.1)){if((-N.x < 0.1)||(N.y > 0.1)){nzy = 1.0;}}
//+YX
if((N.y > 0.1) && (-N.x > 0.1)){if((N.z > 0.1)||(N.x > 0.1)){}else{myx = 1.0;}}
//-YX
if((N.y > 0.1) && (N.x > 0.1)){if((-N.z > 0.1)||(-N.x > 0.1)){}else{nyx = 1.0;}}
//+YZ
if((N.y > 0.1) && (-N.z > 0.1)){if((N.z > 0.1)||(-N.x > 0.1)){}else{myz = 1.0;}}
//-YZ
if((N.y > 0.1) && (N.z > 0.1)){if((-N.z > 0.1)||(N.x > 0.1)){}else{nyz = 1.0;}}
//+XYT
if((-N.x > 0.1) && (-N.y > 0.1)){if((N.z < 0.1)||(N.y > 0.1)){}else{mxyt = 1.0;}}
//-XYT
if((N.x > 0.1) && (-N.y > 0.1)){if((-N.z > 0.1)||(N.y > 0.1)){nxyt = 1.0;}}
highp vec3 mN = vec3(mx,my,mz);
highp vec3 nN = vec3(nx,ny,nz);
highp vec3 mNy = vec3(mxy,0.0,mzy);
highp vec3 nNy = vec3(nxy,0.0,nzy);
highp vec3 mYN = vec3(myx,0.0,myz);
highp vec3 nYN = vec3(nyx,0.0,nyz);
highp vec2 mnxyt = vec2(mxyt,nxyt);
//false
highp float wt = 0.0;
highp float BL = 0.0;
//water false
#ifdef BLEND
if(color.b > color.r){ wt = 1.0; }
    BL = 1.0;
#endif

#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)
	highp vec2 uu = uv1;
           uu.x -= uv1.x;
    highp vec4 NC = min(texture2D(TEXTURE_1, uu),mix(mix(vec4(0.0,0.0,0.0,color.a), vec4(1.0,1.0,1.0,color.a),pow(uv1.y,2.0)),mix(vec4(0.0,0.0,0.0,color.a),vec4(0.5,0.5,0.5,color.a),pow(uv1.y,2.0)),chuva));
    highp vec4 NCn = mix(mix(NC*0.3,max(texture2D(TEXTURE_1, uu),vec4(0.0,0.0,1.2,color.a)),pow(uv1.y,2.0)),NC*0.3,chuva);
              NCn *= NCn;
#endif

#ifndef SEASONS
	#if !USE_ALPHA_TEST && !defined(BLEND)
		diffuse.a = inColor.a;
	#endif
	
	 if(Underwater(FOG_CONTROL)){}else{
		diffuse.rgb = mix(tex.rgb, diffuse.rgb, noite);
		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

//ender
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 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{
//arvores
if(color.a == 0.0){
    diffuse.rgb *= 1.15;
    shdX = color.g * 1.3;
    shdY = uv1.y*1.3;
}
if(color.b > color.r*2.0){
    shdX = 1.0;
    biasY = 0.9158;
}

//ref water
    highp float Fimwater = 0.0;
    #ifdef WATER_REAL
    Fimwater = abs(asin(WaterPos(wpos.xzy, WaterMap(posi.xzy * 5.0), color, wt)));
    #else
    Fimwater = abs(asin(WaterPos(wpos.xzy, diffuse.rgb, color, wt)));
    #endif
    highp float vs = 1.2;
    highp vec3 Ns = vec3(0.0,0.0,0.0);
    Ns = normalize(vec3(wpos.x,wpos.y * vs,wpos.z)) * vs;
    highp vec3 ctemp =  mix(mix(mix(mix( vec3(0.001,0.018,0.105), vec3(0.1,0.4,0.7) * 0.5 * FOG_COLOR.rgb, diat), color.rgb, noite), FOG_COLOR.rgb * 0.5, dss),FOG_COLOR.rgb,chuva);
               ctemp = mix(diffuse.rgb,ctemp,BL);
    highp float Wdi = mix(mix(mix(1.0, 0.3, diat),0.6,mix(0.0,4.0,dsol)), 1.0, length(-wpos) / FAR_CHUNKS_DISTANCE * 1.1);
    highp float posc = length(wpos * 0.009);

    highp vec4 w;
    w = mix(vec4(ctemp,Wdi), mix(mix(vec4(1.0,1.0,1.0,1.0), vec4(1.0,1.0,1.0,1.0), diat), vec4(ctemp,1.0), smoothstep(-0.584, 2.0, length(wpos.xzy) / FAR_CHUNKS_DISTANCE * 1.1)), Fimwater);
//sombra gram
#ifdef ALPHA_TEST 

if(color.a == 0.0){}else{
shdX = min(1.0, N.z-N.y);
}
#endif



if(Underwater(FOG_CONTROL)){
    diffuse.rgb *= texture2D(TEXTURE_1, uv1).rgb;
    diffuse.rgb *= inColor.rgb;
    diffuse.r *= 0.3;
    diffuse = mix(diffuse, vec4(0.5,0.5,0.5,0.6), wt);
    diffuse.rgb += diffuse.rgb * mix(vec3(0.0,0.0,1.0), AGL, pow(uv1.x, 1.0));
    
}else{
   
    diffuse = mix(diffuse,mix(vec4(ctemp,1.0),w,BL),wt);
    highp vec4 d_l = diffuse;
    diffuse.rgb *= mix(mix(vec3(1.0,1.0,1.0),mix(DC, CC, chuva ),uv1.y*1.1), vec3(1.0,1.0,1.0), wt);
    
    diffuse.rgb = mix(diffuse.rgb*NC.rgb,diffuse.rgb*NCn.rgb, noite);

    diffuse.rgb *= mix(vec3(1.0,1.0,1.0), vec3(1.0,0.7,0.0), mix(0.0,dss, uv1.y*1.1));
	//sombra
  diffuse.rgb *= mix(1.0, SC, min(100.0 * mix(smoothstep(uv1.y - biasY, 0.1, 0.0),smoothstep(shdX - biasX, 0.7, 0.0),0.5), 1.0));
    
   highp vec3 dt = tex.rgb*inColor.rgb;
   diffuse.rgb = light(diffuse.rgb, d_l.rgb, L, uv1,diat,chuva,dss);
}
   
   diffuse.rgb = mix(diffuse.rgb, mix(vec3(0.5,0.5,0.5),FOG_COLOR.rgb, noite), length(-wpos * 0.55) / FAR_CHUNKS_DISTANCE * chuva * uv1.y);
   highp vec3 fogt = vec3(0.0,0.0,0.0);
   fogt = mix(diffuse.rgb, FOG_COLOR.rgb * vec3(0.9,0.95,1.3), (length(-wpos * 0.55) / FAR_CHUNKS_DISTANCE * 0.55) * diat * uv1.y);
   fogt = mix(fogt, diffuse.rgb, chuva);
   diffuse.rgb = mix(fogt, diffuse.rgb, wt);
}}


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

	gl_FragColor = diffuse;
	
#endif // BYPASS_PIXEL_SHADER
}
