// __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;

#ifdef FOG
varying vec4 fogColor;
#endif

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

varying float gfx;
varying float morefog;
varying float fogb;
varying highp vec3 position;
varying highp vec3 look_vector;
varying highp vec3 fragment_pos;
varying highp vec3 d;

varying float fray; varying float fog1; varying float rfog; varying float dfog; varying float atmos1; varying float atmos2; varying float atmos3;


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

mat4 rotationMatrix(
vec3 axis, float angle){ 
axis = normalize(axis); 
float s = sin(angle); 
float c = cos(angle); 
float oc = 1.0 - c; 
return mat4(oc * axis.x * axis.x + c, 
oc * axis.x * axis.y - axis.z * s, 
oc * axis.z * axis.x + axis.y * s, 
0.0, oc * axis.x * axis.y + axis.z * s, 
oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, 
0.0, oc * axis.z * axis.x - axis.y * s, 
oc * axis.y * axis.z + axis.x * s, 
oc * axis.z * axis.z + c, 
0.0, 0.0, 0.0, 0.0, 1.0);
}

///Code From Github
///https://gist.github.com/yiwenl/3f804e80d0930e34a0b33359259b556c


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	
#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;
float wk = texture2D(TEXTURE_1, vec2(0., 1.)).r;
wk = clamp((wk-.5)*2., 0., 1.); 

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 ng = 1.-wk*uv1.y; 

float sd = 0.7-abs(.6-wk)*uv1.y;

float sre = 0.7-abs(.6-wk)*uv1.y; 
float sray = 0.6-abs(.6-wk)*uv1.y;

float diff = texture2D(TEXTURE_1, vec2(0, 1)).r,
    sss = (1.0 - min((diff - 0.5) * 3.0, 1.0)) * max((diff - 0.4) * 3.0, 0.0) * 2.0 * uv1.y;
    sss *= 1.0 - rain;

float nolight = 1.0 - uv1.x;
float cave = 1.0 - uv1.y;

vec3 warna_siang = mix(vec3(0.9),vec3(0.6),rain);
vec3 warna_sore = vec3(1.0,1.0,0.5);
vec3 warna_malam = mix(vec3(1.0),mix(vec3(0.8,0.8, 0.88)*0.4,vec3(0.2),rain),nolight);
vec3 warna = mix(warna_siang,warna_sore, siang );
warna = mix(warna, vec3(3.5), cave);
warna = mix(warna,warna_malam,malam);

vec3 trc = mix(vec3(0.53,0.3,0.02)*0.3,vec3(0.53,0.3,0.02)*0.75,malam);


float nl = 1.0;

vec4 uvs = texture2D( TEXTURE_1, vec2(uv1.x*0.65,uv1.y));

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

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

diffuse.rgb *= warna;

#ifndef SEASONS
	#if !USE_ALPHA_TEST && !defined(BLEND)
		diffuse.a = inColor.a;
	#endif	
	diffuse.rgb *= mix(inColor.rgb,normalize(inColor.rgb),sss);
#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

diffuse.rgb += trc*pow(max(0.0,uv1.x),3.0);
 

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

vec3 drz = mix(vec3(1.2),vec3(0.25),abs(N.z));
vec3 drx = mix(vec3(1.2),mix(vec3(1.2)*FOG_COLOR.b,vec3(0.5*FOG_COLOR.b),sore),abs(N.x));
vec3 drl = mix(drz,drx,sss);

diffuse.rgb *= mix(mix(vec3(1.3),mix(vec3(0.5),mix(vec3(0.6),vec3(0.2),sore),sss),abs(N.y)),vec3(1.0),malam);

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

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


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(look_vector.xz) / max(100.0, abs(look_vector.z) * 4.0)));

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

 highp float ref = view_angle; 

vec3 refpos = (vec4(d.xyz,0)*rotationMatrix(vec3(radians(90.0),
0,0),radians(90.0))).xyz;

refpos /= abs(refpos.y);
refpos.xz -= vec2(5.,-4.0);
refpos /= 10.0;
refpos.x = -refpos.x;


vec3 refposx = (vec4(d.zyx,0)*rotationMatrix(vec3(radians(90.0),
0,0),radians(90.0))).xyz;

refposx /= abs(refposx.y);
refposx.xz -= vec2(5.,-4.0);
refposx /= 10.0;
refposx.x = -refposx.x;

vec4 day = vec4(1.); vec4 night = vec4(0.2,0.2,0.6,0.7);
vec4 dusk = vec4(0.9,.5,.25,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)
vec4 refl;
if(abs(refpos.x) > 0.0 && (refpos.z) > 0.36){
refl = texture2D(TEXTURE_0, vec2(refpos.x + (ref * 0.005), refpos.z));
}

vec4 reflx;
if(abs(refposx.x) > 0.0 && (refposx.z) > 0.365){
reflx = texture2D(TEXTURE_0, vec2(refposx.x + (ref * 0.005), refposx.z));
}

vec4 frx = max(refl,reflx);


vec4 cloud = mix(diffuse, sqrt(mix(cclr,vec4(0.9,.7,.25,1.0)*FOG_COLOR.r,sore)), fbm(((-d.xz * 0.1) / (d.y - length(d.xz) * 0.18)) * ref));

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

vec3 wp = d;
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.7,1.25,1.30)*0.4,vec3(0.7,1.0,1.6)*0.6,sore);

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

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

diffuse += clamp(mix(vec4(0.0),mix(vec4(0.0),FOG_COLOR*vec4(0.5),smoothstep(0.0,1.0,rsun)),1.0),0.0,1.0); 

diffuse = mix(diffuse, cloud, ((cloud.a*0.45)* uv1.y));

diffuse.a = mix(0.31,0.8,malam);


#endif 
}else{ diffuse.rgb *= mix(vec3(1.0),mix(sdp,vec3(1.2),rain),nolight); }

diffuse.rgb = Hyra(diffuse.rgb)*mix(vec3(1.2),vec3(0.2),rain);

vec3 df = mix( diffuse.rgb, mix(FOG_COLOR.rgb*vec3(0.6,0.8,1.0),vec3(3.0,3.0,5.0),malam), atmos1*mix(mix(1.0,min(0.5,3.0),malam),0.1,rain) );
vec3 dr = mix( diffuse.rgb, vec3(1.2), mix(rfog,0.0,malam) );
vec3 ds = mix( diffuse.rgb,FOG_COLOR.r*(vec3(1.0,0.7,0.35)*FOG_COLOR.r), fray); vec3 fgo = mix(df,dr,rain); diffuse.rgb = mix(fgo,ds,sore);

#ifdef FOG
diffuse.rgb = mix( diffuse.rgb,mix(FOG_COLOR.rgb,FOG_COLOR.r*(vec3(1.0,0.75,0.45)*1.6*FOG_COLOR.r),sray), fogColor.a );
#endif

	gl_FragColor = diffuse;	
#endif 

}