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

varying highp vec3 fragment_pos;
varying highp vec3 look_vector;
varying highp vec3 position;
varying highp vec3 look;
varying highp float sunfx;
varying float cont;
varying highp vec4 awn;
	varying float wfog;
   varying vec4 cclr;
	varying float rfog;
	varying float dfog;
   varying float af2;
	varying float af3;

#include "uniformShaderConstants.h"
#include "util.h"
#include ".set"

LAYOUT_BINDING(0) uniform sampler2D TEXTURE_0;
LAYOUT_BINDING(1) uniform sampler2D TEXTURE_1;
LAYOUT_BINDING(2) uniform sampler2D TEXTURE_2;
uniform float RENDER_DISTANCE;
uniform vec2 FOG_CONTROL;
uniform vec4 FOG_COLOR;
uniform highp float TIME;

#define CLOUD_OCTAVES		0.45
#define CLOUD_SHARPNESS		0.0010

highp float hash( highp float n )
{ return fract(sin(n)*43758.5453);
}

highp float noisec( in highp vec2 x )
{
highp vec2 p = floor(x);
	highp vec2 f = fract(x);
    highp float n = p.x + p.y*57.0;
    	highp float res = mix(mix( hash(n+  0.0), hash(n+  1.0),f.x), mix( hash(n+ 57.0), hash(n+ 58.0),f.x),f.y);
    	return 1.0 - res;
}

highp float fbm(highp vec2 p)
{
highp float cover = CLOUD_OCTAVES;
highp float sharpness = CLOUD_SHARPNESS;
highp float time = TIME;
highp float z = 3.8;
highp float rz = 0.0;
p *= 11.5;
p += vec2(4.0,2.0)*time*0.005;
for(int i = 0; i < 6; i++)
{
rz += noisec(p)/z;
z *= 2.1; p *= 2.3;
p -= time*0.006*pow(z,0.6)/2.0;
}
highp float den = 4.0 * pow(abs(rz), 2.0);
den *= 2.0;
highp float c = den - (1.0 - cover);
if( c < 0.0 )
  { c = 0.0; }
den = 1.0 - (pow(sharpness, c));
return den;
}

highp vec3 PositionTransform(highp vec3 pos, highp float height){
 highp float dis = length(pos.xz);
 highp float ang = atan(dis, height);
 return pos * cos(ang);
}

highp float cos_between_vecs(highp vec3 v1, highp vec3 v2)
{
 return (v1.x * v2.x + v1.y * v2.y  + v1.z * v2.z) / length(v1) / length(v2);
}

highp float randff(highp float n)
{ return fract(cos(n) * 50.288);
}

highp float smoothrand(highp float pos)
{
 highp float start = floor(pos);
 highp float smoothy = smoothstep(0.0, 1.0, fract(pos));
 return mix(randff(start), randff(start + 1.0), smoothy);
}

highp float akihiro2d(highp float horizont, highp float forward)
{
 highp float start = floor(forward);
 highp float smoothy = smoothstep(0.0, 1.0, fract(forward));
 return mix(smoothrand(horizont + randff(start) * 1000.0), smoothrand(horizont + randff(start + 1.0) * 1000.0), smoothy); }

highp float CheapWaterMap(highp vec3 pos)
{
pos.z += TIME * 0.26;
 highp float wave1 = akihiro2d(pos.z+pos.z+pos.x, pos.x+pos.x+pos.z);
 highp float wave2 = sin(wave1) * 1.0 + 0.5;
float interp = sin(TIME * 4.2 + pos.x+pos.z+pos.x+pos.z+pos.x+pos.z) * sin(pos.x) * 0.5;
 return mix(wave1, wave2, interp);
}

highp vec3 WaterNormalMap(highp vec3 pos)
{
 highp float value = (CheapWaterMap(pos + vec3(0.0, 0.0, 0.5)) - CheapWaterMap(pos)) * 1.0;
 return vec3(0.0, 1.0, -value);
}

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);
vec4 uvs = texture2D( TEXTURE_1,vec2(uv1.x*0.65,uv1.y));
#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 rn = (1.0-pow(FOG_CONTROL.y,5.0))*uv1.y;
vec4 ch = texture2D( TEXTURE_1, vec2(0.0,1.0) ) ;
float siang = pow(min(max(1.1-pow(ch.r,10.0),0.0),1.0),3.0);
float sore =pow(max(min(1.0-FOG_COLOR.b*1.2,1.0),0.0),0.5);
float malam = min(pow(1.0-ch.r,2.0)*2.0,1.0);
float set_bengis =pow(max(min(1.0-FOG_COLOR.r*1.5,1.0),0.0),0.3);
float bengi = set_bengis*uv1.y;

float cave = 1.0 - uv1.y;

vec3 wtr = vec3(1.0);
wtr = normalize(wtr);

vec3 wsi = mix(wtr,vec3(0.77,0.77,0.6),rn);
vec3 wso = FOG_COLOR.rgb;
vec3 wma = vec3(0.24,0.24,0.13);
vec3 wt =mix(wsi,wso, siang );
wt =mix(wt,wma,malam);

vec4 f1 = FOG_COLOR;
highp vec3 r = 1.0-ceil(fract(position.xyz));
vec3 trc_malam = mix(vec3(1.0,0.05,-0.7)*0.3,vec3(0.7,0.43,0.04)*0.6,cave);
vec3 trc_air = vec3(-1.5,1.0,5.5);
vec3 warna_siang = mix(vec3(1.0),vec3(0.4),rn);
vec3 warna_sore = vec3(0.4,0.25,0.1);
vec3 warna_malam = vec3(0.7);
vec3 warna =mix(warna_siang,warna_sore, siang );
warna =mix(warna,warna_malam,malam);

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

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

diffuse.rgb *= mix(warna,vec3(0.6,0.45,0.25),cave);

if(r.z == 1.0)
{ diffuse.rgb *= mix(mix(1.0,1.1,1.0),0.9,uv1.x); }
if(r.x == 1.0)
{ diffuse.rgb *= mix(mix(1.0,0.5,1.0),1.0,uv1.x); }


#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

diffuse.rgb += trc_malam*max(0.0,uv1.x-0.5);

if(getNetherMask(f1)){
diffuse.rgb *= vec3(2.0,2.5,0.5);}
diffuse.rgb = Hyra(diffuse.rgb)*1.08;
if(color.g > color.b)
{diffuse.rgb *= FOGw(vec3(1.0,.9,1.2)*0.88);}

if(uw(f1)){
diffuse.rgb *= mix(vec3(0.5,0.6,0.7),vec3(1.5),rn);
diffuse.rgb += diffuse.rgb*(vec3(trc_air)*1.25)*pow(uv1.x *1.2,1.0)*(1.0-uv1.y);

highp vec2 ur = position.xz;
ur = ur * 1.1;
float val = wnoise(ur, TIME);
diffuse.rgb += (vec3(0.8) * val)*(1.2-cave);
}

vec3 origin_water_normal = vec3(0.0, 1.8, 0.5);
vec3 frag_water_normal = normalize(WaterNormalMap(position*2.0));

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 = pow(view_angle * 1.2, 4.0);

#if defined(BLEND)||defined(FOG)
if(color.b + color.b  > color.r + color.g){
highp vec3 reflected_dir = normalize(reflect(look_vector, water_normal));
highp vec3 reflected_pos = reflected_dir * 100.0 / reflected_dir.x + look_vector;
vec4 cloud = mix(diffuse, sqrt(cclr), fbm(((-look.xz * 0.1) / (look.y - length(look.xz) * 0.18)) * view_angle));
diffuse.rgb *= (length(-look) / RENDER_DISTANCE);
diffuse = mix(diffuse, cloud, ((cloud.a*0.7)* uv1.y));
diffuse.rgb += pow(view_angle / 2., 1.4)*FOGw((wt)*0.80);

diffuse.a *= 0.4*(length(-look) / RENDER_DISTANCE*1.75);
diffuse.a += inColor.a*dot(clamp(length(-look) / RENDER_DISTANCE,0.0,1.0),2.0);
diffuse.a += 0.45;
diffuse.rgb = mix( diffuse.rgb, FOG_COLOR.rgb, cont);
}

diffuse.rgb = mix( diffuse.rgb,((sore*(raySun(look,diffuse.rgb)*((3.0*sore)))*(0.8-malam))*(0.8-rn))*(1.0-cave),0.025);

#endif

float uY = uv1.y;
vec3 shd = mix(vec3(1.0),vec3(0.82)+uv1.x*0.15,0.6-uv1.x);

float Y = uv1.y;
float C = color.a;

float sy = 0.881;
float sx = color.a;
if(color.a==0.0){
diffuse.rgb *= 1.45;
sy = 0.9749;
sx = color.g*1.3; }
if((uv1.y<sy-0.0010)||(sx<0.64)){diffuse.rgb *= shd;}
if((uv1.y<sy-0.0020)||(sx<0.63)){diffuse.rgb *= shd;}
if((uv1.x<sy-0.0030)||(sx<0.62)){diffuse.rgb *= shd;}
if((uv1.y<sy-0.0040)||(sx<0.61)){diffuse.rgb *= shd;}
if((uv1.y<sy-0.0039)||(sx<0.60)){diffuse.rgb *= shd;}

vec3 a1 = mix( diffuse.rgb,f1.rgb*vec3(0.50, 0.50, 0.64), dfog);
vec3 a2 = mix( diffuse.rgb,f1.rgb*vec3(1.5), rfog*(1.0-cave));

vec3 a4 = mix(a1, a2, rn);

diffuse.rgb = a4;

diffuse.rgb = mix( diffuse.rgb, f1.rgb*vec3(0.1,0.1,0.7), af2);
diffuse.rgb = mix( diffuse.rgb, f1.rgb, af3*vec3(0.2,0.3,0.7));

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


	gl_FragColor = diffuse;
	
#endif // BYPASS_PIXEL_SHADER
}