// __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;
in highp vec3 position;
in vec3 wpos;

#ifdef FOG
varying vec4 fogColor;
#endif

#include "uniformShaderConstants.h"
#include "util.h"
#include ".renderchunks.h"

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

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);
#endif
	

bool unWater = bool(float(uv1.y < 0.9 && abs((2.0 * position.y - 15.0) / 16.0 + clamp((position.y - 14.75),0.,1.0) - uv1.y) < 0.2) + float(uv1.y == 0.0) * clamp((position.y - wpos.y - 14.75),0.,1.0));

float rain = smoothstep(0.55,0.1,FOG_CONTROL.r)*uv1.y;
vec4 ch = texture2D( TEXTURE_1, vec2(0.0,1.0) ) ;
float siang = pow(min(max(1.1-pow(ch.r,5.0),0.0),2.0),3.0);
float malam = min(pow(1.0-ch.r,2.0)*2.0,1.0);
float sore =pow(max(min(1.0-FOG_COLOR.b,1.0),0.0),1.0);

float nolight = 1.0 - uv1.x;

vec3 sdp = mix(mix(vec3(1.3),vec3(0.5),smoothstep(0.875,0.86,uv1.y)),vec3(1.5),malam);

vec3 N = normalize(cross(normalize(dFdx(position)), normalize(dFdy(position))));

vec3 drz = mix(vec3(1.3),vec3(0.54),abs(N.x));

vec3 drx = mix(vec3(2.1),mix(vec3(1.1)*FOG_COLOR.b,vec3(0.6*FOG_COLOR.b),sore),abs(N.x));

vec3 drl = mix(mix(drz,drx,sore),vec3(1.2),rain);

#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

float uvx = pow(uv1.x, 3.3);

vec3 warna_siang = mix(vec3(1.2),vec3(0.6),rain);
vec3 warna_sore = vec3(0.50,0.45,0.30);
vec3 warna_malam = mix(vec3(1.0),vec3(0.5,0.6,0.7)*1.2,nolight);
vec3 warna =mix(warna_siang,warna_sore, siang );
warna =mix(warna,warna_malam,malam);

vec3 trc = mix(vec3(0.48,0.30,0.02)*0.2,vec3(0.46,0.36,0.05)*0.7,malam);

diffuse.rgb *= warna;
diffuse.rgb += (trc*2.0)*max(0.0,uv1.x);

#if !defined(ALWAYS_LIT)
diffuse *= texture2D(TEXTURE_1, vec2(uvx, uv1.y));
#endif

diffuse.rgb *= mix(vec3(1.3),drl,nolight);


// if(color.g > color.b){ diffuse.rgb *= warna*0.6;  }else{

 if(N.y>0.5){ 
diffuse.rgb *= mix(warna,vec3(0.94,0.89,0.89)*0.65,nolight); 
diffuse.rgb = mix(diffuse.rgb, mix(diffuse.rgb, raySun(wpos,diffuse.rgb*FOG_COLOR.r*1.5*(1.0-FOG_COLOR.b)), 0.021), sore);
}





if(abs(N.z)>0.5){ 
diffuse.rgb *= vec3(1.); 
diffuse.rgb = mix(diffuse.rgb, mix(diffuse.rgb, raySun(wpos,diffuse.rgb*FOG_COLOR.r*0.6*(1.0-FOG_COLOR.b)), 0.028), sore);
} 

#ifndef SEASONS
	#if !USE_ALPHA_TEST && !defined(BLEND)
		diffuse.a = inColor.a;
	#endif

// diffuse.rgb = mix(diffuse.rgb, FOG_COLOR.rgb, clamp(length(-wpos/FAR_CHUNKS_DISTANCE),0.,0.1));
    
	diffuse.rgb *= mix(diffuse.rgb,mix(sdp,vec3(1.2),rain),nolight);

	diffuse.rgb *= inColor.rgb;

   diffuse.rgb = Hyra(diffuse.rgb)*0.9;

vec3 wp = wpos;
vec3 d = wpos;

vec3 origin_water_normal = vec3(0.3,0.35,0.8);
vec3 frag_water_normal = normalize(WaterNormalMap(position));

highp vec3 water_normal = mix(frag_water_normal, origin_water_normal, min(1.0, length(wpos.xz) / max(100.0, abs(wpos.z) * 4.0)));

highp float view_angle = acos(abs(cos_between_vecs(wpos, water_normal)));

 highp float ref = view_angle; 

vec4 day = vec4(1.); vec4 night = vec4(0.2,0.2,0.6,0.7);
vec4 dusk = vec4(0.99,.5,.21,1.0); vec4 ujan = vec4(0.13,0.13,0.13,0.5);
vec4 cclr = mix(mix(mix(day, dusk, siang), night, malam),ujan, rain);

if(color.b + color.b  > color.r + color.g){
#if defined(BLEND)

// float cca = smoothstep(0.33,1.0,fbm2D(p.xz*11.));

vec4 cloud = mix(diffuse, sqrt(mix(cclr,vec4(0.9,.5,.1,0.5),sore)), fbm2D(((-d.xz * 1.0)/ (d.y - length(d.xz) * 0.15)) * ref));

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

float rsun = clamp(1.-length(wp.zy/wp.x*12.0*vec2(0.2,0.075))-ref*0.1,0.0,1.0);

vec3 wclr = mix(vec3(0.35,0.71,0.9)*1.5,vec3(0.35,0.71,0.9)*0.85,sore);

diffuse.rgb = pow(view_angle / 2., 1.4)*vec3(wclr);

diffuse.rgb *= mix(vec3(wclr), vec3(FOG_COLOR.rgb), dot(normalize(d), normalize(vec3(0,d.y,0))));

// diffuse = mix(diffuse, cloud, ((cloud.a*0.5)));

diffuse.a *= 0.1*length(d)/RENDER_DISTANCE*0.8;
diffuse.a += inColor.a*dot(clamp(length(-d) / RENDER_DISTANCE*0.8,0.0,1.),2.0);
diffuse.a += mix(mix(0.24,0.5,sore),0.7,malam);

diffuse.rgb = mix( diffuse.rgb, FOG_COLOR.rgb*0.94, clamp(length(-d.xz) / RENDER_DISTANCE*1.9,0.0,0.7));


#endif 

}



#else
	vec2 uv = inColor.xy;
	diffuse.rgb *= mix(vec3(1.0,1.0,1.0), texture2D( TEXTURE_2, uv).rgb, inColor.b);
	diffuse.rgb *= inColor.aaa;
	diffuse.a = 1.0;
#endif

diffuse.rgb = mix(diffuse.rgb, FOG_COLOR.rgb*vec3(0.3,0.4,0.7), clamp(length(-wpos.xz/FAR_CHUNKS_DISTANCE*0.5),0.,0.2));

vec3 cray = vec3(1.1,0.85,0.55)*sore;

float sray = clamp(length(-wpos.x)/FAR_CHUNKS_DISTANCE*2.0*FOG_COLOR.r,0.0,0.5);


diffuse.rgb = mix(diffuse.rgb, mix(mix(vec3(0.8,0.6,.5), vec3(0.4, 0.7, 0.89), rain), cray, sore), clamp(length(wpos.xz)*mix(0.017,(1.0/FAR_CHUNKS_DISTANCE*0.38),sore), 0.0, mix(0.12,0.8*FOG_COLOR.r,sore)));

#ifdef FOG
diffuse.rgb = mix( diffuse.rgb, fogColor.rgb*vec3(0.65,0.85,1.0), fogColor.a*mix(0.45,0.35,sore) );
#endif
	gl_FragColor = diffuse;
	
#endif // BYPASS_PIXEL_SHADER
}
