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

#include "vertexVersionCentroid.h"
#if __VERSION__ >= 300
	#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 FOG
	varying vec4 fogColor;
#endif

#include "uniformWorldConstants.h"
#include "uniformPerFrameConstants.h"
#include "uniformShaderConstants.h"
#include "uniformRenderChunkConstants.h"

attribute POS4 POSITION;
attribute vec4 COLOR;
attribute vec2 TEXCOORD_0;
attribute vec2 TEXCOORD_1;
varying float gfx;
varying float fogb;
varying float morefog;
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;

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;

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;
#ifndef BYPASS_PIXEL_SHADER
    uv0 = TEXCOORD_0;
    uv1 = TEXCOORD_1;
	color = COLOR;
#endif

#ifdef ALPHA_TEST
if(color.g > color.b){
vec3 wave_pos = POSITION.xyz + VIEW_POS;
highp float Wave = sin(TIME * 0.9 + TIME * 4.1 + wave_pos.y+wave_pos.z+wave_pos.z+wave_pos.y+wave_pos.x) * sin(wave_pos.x) * 0.03;
gl_Position.xy += (Wave) * 1.0;}
#endif

#if defined(BLEND)||defined(FOG)
if(color.r < color.b && color.g > color.r){
vec3 water = worldPos.xyz + VIEW_POS;
gl_Position.xy += sin((TIME*1.6)+water.z+water.y+water.x+water.y+water.y+water.y)*0.08;
}
#endif


#if defined(FOG) || defined(BLEND)
	#ifdef FANCY
		vec3 relPos = -worldPos.xyz;
		float cameraDepth = length(relPos);
	#else
		float cameraDepth = pos.z;
	#endif
#endif

///// apply fog

#ifdef FOG
	float len = cameraDepth / RENDER_DISTANCE*0.8;
	#ifdef ALLOW_FADE
		len += RENDER_CHUNK_FOG_ALPHA;
	#endif

    fogColor.rgb = FOG_COLOR.rgb;
	fogColor.a = clamp((len - FOG_CONTROL.x) / (FOG_CONTROL.y - FOG_CONTROL.x), 0.0, 1.0);
#endif

#ifdef BLEND
	bool shouldBecomeOpaqueInTheDistance = color.a < 0.95;
	if(shouldBecomeOpaqueInTheDistance) {
		#ifdef FANCY  /////enhance water
			float cameraDist = cameraDepth / FAR_CHUNKS_DISTANCE;
			color = COLOR;
		#else
			vec4 surfColor = vec4(color.rgb, 1.0);
			color = surfColor;				
			vec3 relPos = -worldPos.xyz;
			float camDist = length(relPos);
			float cameraDist = camDist / FAR_CHUNKS_DISTANCE;
		#endif //FANCY		
		float alphaFadeOut = clamp(cameraDist, 0.0, 1.0);
		color.a = mix(color.a, 1.0, alphaFadeOut);
	}
#endif



d = worldPos.xyz;
gfx = length(-worldPos.xyz*1.5) / FAR_CHUNKS_DISTANCE*1.5;
morefog = length(-worldPos.xyz*0.51) / FAR_CHUNKS_DISTANCE*0.51;
fogb = length(worldPos.xyz) / FAR_CHUNKS_DISTANCE*0.18;
rfog = length(-worldPos.xyz*4.0) / FAR_CHUNKS_DISTANCE*7.0;
fogb = clamp(fogb, 0.0, 0.6);
gfx = clamp(gfx, 0.0, 0.10);

vec3 ray = -worldPos.xyz; float rray = 1.0-length(ray.yz*0.01); fray = length(-worldPos.x)/FAR_CHUNKS_DISTANCE*2.8*FOG_COLOR.r; fog1 = clamp(rray,0.0,1.0)*length(-worldPos.x)/RENDER_DISTANCE; dfog = length(-worldPos.xyz)/FAR_CHUNKS_DISTANCE*0.3; atmos1 = length(-worldPos.xz)/FAR_CHUNKS_DISTANCE; atmos2 = length(-worldPos.xyz*0.25)/FAR_CHUNKS_DISTANCE*0.24; atmos3 = length(-worldPos.xyz*0.30)/FAR_CHUNKS_DISTANCE*0.30; atmos1 = clamp(atmos1,0.0,0.2); atmos2 = clamp(atmos2,0.0,0.2); atmos3 = clamp(atmos3,0.0,0.2); rfog = clamp(rfog, 0.0, 0.3); rfog = length(-worldPos.xyz) / FAR_CHUNKS_DISTANCE*1.3;
fray = clamp(fray,0.0,0.6); dfog = clamp(dfog,0.0,0.24);
fog1 = clamp(fog1,0.0,1.5*FOG_COLOR.r); 


POS3 cam_fix = worldPos.xyz + VIEW_POS; 

position = POSITION.xyz + VIEW_POS.xyz; 

fragment_pos = cam_fix; fragment_pos.y += 0.0015; 

look_vector = fragment_pos - VIEW_POS; 


#ifndef BYPASS_PIXEL_SHADER
#ifdef FOG
		color.rgb += FOG_COLOR.rgb * 0.000001;
	#endif
#endif
}
