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

#if __VERSION__ >= 300
	#define attribute in
	#define varying out

	#ifdef MSAA_FRAMEBUFFER_ENABLED
		#define _centroid centroid
	#else
		#define _centroid
	#endif

	#ifndef BYPASS_PIXEL_SHADER
		_centroid out vec2 uv0;
		_centroid out vec2 uv1;
	#endif
#else
	#ifndef BYPASS_PIXEL_SHADER
		varying vec2 uv0;
		varying vec2 uv1;
	#endif
#endif

#ifndef BYPASS_PIXEL_SHADER
	varying vec4 color;
#endif

#ifdef LOW_PRECISION
	varying vec4 fogColor;
	varying vec4 fogCoIor;
	varying float moreFog;
#endif

#ifdef NEAR_WATER
	varying float cameraDist;
#endif
varying vec3 screen_pos;

#ifdef AS_ENTITY_RENDERER
	uniform MAT4 WORLDVIEWPROJ;
#else
	uniform MAT4 WORLDVIEW;
	uniform MAT4 PROJ;
#endif
uniform vec4 FOG_COLOR;
uniform vec2 FOG_CONTROL;
uniform float RENDER_DISTANCE;
uniform vec2 VIEWPORT_DIMENSION;
uniform vec4 CURRENT_COLOR;		//current color r contains the offset to apply to the fog for the "fade in"
uniform POS4 CHUNK_ORIGIN_AND_SCALE;
uniform POS3 VIEW_POS;
uniform float FAR_CHUNKS_DISTANCE;
uniform highp float TIME;

attribute POS4 POSITION;
attribute vec4 COLOR;
attribute vec2 TEXCOORD_0;
attribute vec2 TEXCOORD_1;

const float rA = 1.0;
const float rB = 1.0;
const vec3 UNIT_Y = vec3(0,1,0);
const float DIST_DESATURATION = 56.0 / 255.0; //WARNING this value is also hardcoded in the water color, don'tchange

void main()
{
    POS4 worldPos;
#ifdef AS_ENTITY_RENDERER
		POS4 pos = WORLDVIEWPROJ * POSITION;
		worldPos = pos;
#else
    worldPos.xyz = (POSITION.xyz * CHUNK_ORIGIN_AND_SCALE.w) + CHUNK_ORIGIN_AND_SCALE.xyz;
    worldPos.w = 1.0;

    POS4 pos = WORLDVIEW * worldPos;
    pos = PROJ * pos;
#endif
    gl_Position = pos;
screen_pos.xy = pos.xy / (pos.z + 1.0);
screen_pos.z = pos.z;

#ifndef BYPASS_PIXEL_SHADER
    uv0 = TEXCOORD_0;
    uv1 = TEXCOORD_1;
	color = COLOR;
#endif

///// find distance from the camera

#ifdef ALPHA_TEST 
#ifdef FANCY
highp vec3 wave = POSITION.stp;
vec4 uvC = vec4(COLOR.rb,uv0);

if(TEXCOORD_1.y>TEXCOORD_1.x){
if((uvC.r>=uvC.g)&&(uvC.r>uvC.b)&&(uvC.r>=1.0)&&(uvC.g>=1.0)){
gl_Position.x += sin(TIME*4.0+wave.z+wave.z+wave.x+cos(TIME*3.+wave.y+wave.x ))* sin(wave.x+wave.y)* 0.01*uv1.y; }

if(COLOR.g>COLOR.r&&COLOR.a>0.0){
gl_Position.x += sin(TIME*4.+wave.z+wave.z+wave.z+cos(TIME*3.+wave.y+wave.x))*sin(wave.x+wave.y)*0.052*uv1.y; }

if(COLOR.a==0.0){
gl_Position.x += sin(TIME*4.0+wave.y+wave.z+wave.z+cos(TIME*2.5+wave.x))*sin(wave.y+wave.y)*uv1.y*0.022; }
}
#endif
#endif

if(FOG_CONTROL.x < 0.15){
vec3 gI_Position = VIEW_POS + worldPos.xyz;
gl_Position.x += sin((TIME*0.80)+gI_Position.z+gI_Position.y+gI_Position.x)*0.03;
}

#if defined(LOW_PRECISION) || defined(NEAR_WATER)
	#ifdef FANCY
		vec3 relPos = -worldPos.xyz;
		float cameraDepth = length(relPos);
		#ifdef NEAR_WATER
			cameraDist = cameraDepth / FAR_CHUNKS_DISTANCE;
		#endif
	#else
		float cameraDepth = pos.z;
		#ifdef NEAR_WATER
			vec3 relPos = -worldPos.xyz;
			float camDist = length(relPos);
			cameraDist = camDist / FAR_CHUNKS_DISTANCE*0.1;
		#endif
	#endif
#endif

///// apply fog

#ifdef LOW_PRECISION
	float len = cameraDepth / RENDER_DISTANCE*0.95;
	float Ien = cameraDepth / RENDER_DISTANCE;

	#ifdef ALLOW_FADE
		len += CURRENT_COLOR.r;
		Ien += CURRENT_COLOR.r;
	#endif

    fogColor.rgb = FOG_COLOR.rgb;
    fogCoIor.rgb = FOG_COLOR.rgb*vec3(0.9,0.95,1.55);
 
	fogColor.a = clamp((len - FOG_CONTROL.x) / (FOG_CONTROL.y - FOG_CONTROL.x), 0.0, 0.9);

vec3 rel = -worldPos.xyz*0.55;
float tambahfog = length(rel);
moreFog = tambahfog / FAR_CHUNKS_DISTANCE*0.55;

#endif

///// water magic
#ifdef NEAR_WATER
	#ifdef FANCY  /////enhance water
		float F = dot(normalize(relPos), UNIT_Y);
		F = 1.0 - max(F, 0.1);
		F = 1.0 - mix(F*F*F*F, 1.0, float(min(1.0, cameraDepth / FAR_CHUNKS_DISTANCE)));

		color.rg -= vec2(F * DIST_DESATURATION);

		vec4 depthColor = vec4(color.rgb * 0.75, 0.000099);
		vec4 traspColor = vec4(color.rgb * 0.85, 0.000099);
		vec4 surfColor = vec4(color.rgb, 0.75);

		vec4 nearColor = mix(traspColor, depthColor, color.a);
		color = mix(surfColor, nearColor, F);
	#else
		vec4 surfColor = vec4(color.rgb, 1.0);
		color = surfColor;
		color.a = pos.z / FAR_CHUNKS_DISTANCE + 0.5;
	#endif //FANCY
#endif
}
