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



#ifdef GL_FRAGMENT_PRECISION_HIGH
	varying highp vec3 cPos;
#else
	varying mediump vec3 cPos;
#endif



varying POS3 wPos;

//___________________POM_MAP_____________________

varying vec3 screen_pos;
varying highp vec3 world_pos;

//==√::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::>


//______________THE_END_COLOR_________________

varying float f_pos;

//==√::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::>


//_______________UN_WATER_WAVE_______________

varying highp vec3 w_pos;

//==√::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::>


//_______________WATER_WAVE________________

varying POS3 t_pos;
varying POS3 h_pos;
varying float far_f;

//==√::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::>


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




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

vec4 rgb2hsv(vec4 rgb){
    highp float rgbMax = max(rgb.r, max(rgb.g, rgb.b));
    highp float rgbMin = min(rgb.r, min(rgb.g, rgb.b));
    highp float h      = 0.0;
    highp float s      = 0.0;
    highp float v      = rgbMax;
    highp float delta  = rgbMax - rgbMin;
    if (delta != 0.0) {
        if (rgbMax == rgb.r) {h = (rgb.g - rgb.b) / delta;}
        else if (rgbMax == rgb.g) {h = 2.0 + (rgb.b - rgb.r) / delta;}
        else {h = 4.0 + (rgb.r - rgb.g) / delta;}
    }
    h *= 60.0;
    h  = h < 0.0 ? h + 360.0 : h;
    if (rgbMax != 0.0) {s = delta / rgbMax;}
    return vec4(h, s, v, rgb.a);
}

highp float hash11(highp float p){
	highp vec3 p3  = vec3(fract(p * 0.1031));
	p3 += dot(p3, p3.yzx + 19.19);
	return fract((p3.x + p3.y) * p3.z);
}

highp float random(highp float p){
	p = p/3.0+TIME;
	return mix(hash11(floor(p)),hash11(ceil(p)),smoothstep(0.0,1.0,fract(p)))*2.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;

//___________________POM_MAP_____________________


screen_pos.xy = pos.xy/(pos.z+1.0);
screen_pos.z = min(1.0,pos.z);
world_pos = worldPos.xyz;


//✧✦☙༺☆<:::::::::::::::::::::::::::::::::::::>☆༻❧✦✧


//______________THE_END_COLOR_________________


f_pos = length(-worldPos.xyz) / FAR_CHUNKS_DISTANCE;
//f_pos = pos.z / FAR_CHUNKS_DISTANCE;


//✧✦☙༺☆<:::::::::::::::::::::::::::::::::::::>☆༻❧✦✧


//_______________UN_WATER_WAVE_______________


highp vec3 wav_pos = vec3(0.0);
w_pos =worldPos.xyz+VIEW_POS;
vec4 f_color =FOG_COLOR;


if((f_color.b>.15&&(f_color.b>f_color.g*1.2)&&(f_color.b>=f_color.r*1.8)&&(f_color.g>f_color.r*1.6))||(f_color.g*1.2>=f_color.b)&&(f_color.r*1.3<f_color.g)){


wav_pos.x = (sin(TIME*5.0 + w_pos.x*2.0) + sin(TIME*5.2 + w_pos.z*1.2))*0.05 ;
wav_pos.y = (sin(TIME*5.0 + w_pos.x*1.5) + sin(TIME*5.2 + w_pos.z*1.2))*0.03 ;
gl_Position.xy +=wav_pos.xy;

}//under water


//✧✦☙༺☆<:::::::::::::::::::::::::::::::::::::>☆༻❧✦✧




//_______________WATER_WAVE_______________


    t_pos = worldPos.xyz;
h_pos = worldPos.xyz+VIEW_POS;
far_f = pos.z / FAR_CHUNKS_DISTANCE;


//✧✦☙༺☆<:::::::::::::::::::::::::::::::::::::>☆༻❧✦✧



		cPos = POSITION.xyz;
		wPos = worldPos.xyz;



#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.018*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.012; }
}
#endif
#endif




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

///// waves
#if defined ALPHA_TEST && defined(LEAVES_WAVES)
	if(color.g > color.b){
		POS3 l = POSITION.xyz;
		l.y = abs(l.y-8.0);
		gl_Position.s += sin(TIME * 3.5 + 2.0 * l.x + 2.0 * l.z + l.y) * 0.015 * random(l.x+l.y+l.z);
	}
#endif

#if !defined(BYPASS_PIXEL_SHADER) && !defined(ALPHA_TEST) && defined(WATER_WAVES)
	vec4 hsv = rgb2hsv(color);
	if (hsv.x >= 120.0 && hsv.x <= 270.0) {
		POS3 l = worldPos.xyz + VIEW_POS;
		gl_Position.t += sin(TIME * 3.5 + 2.0 * l.x + 2.0 * l.z + l.y) * 0.04 * fract(POSITION.y) * random(l.x+l.y+l.z);
		color.a *= 0.5;
	}
#endif

///// apply fog

#ifdef FOG
	float len = cameraDepth / RENDER_DISTANCE;
	#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

///// blended layer (mostly water) magic
#ifdef BLEND

	if(color.a < 0.95) {
		#ifdef FANCY  /////enhance water
		#else

			vec4 surfColor = vec4(color.rgb, 1.0);
			color = surfColor;
		#endif //FANCY

		float cameraDist = cameraDepth / FAR_CHUNKS_DISTANCE;
		float alphaFadeOut = clamp(cameraDist, 0.0, 1.0);
		color.a = mix(color.a, 1.0, alphaFadeOut);
	}
#endif

#ifndef BYPASS_PIXEL_SHADER
	#ifndef FOG

		color.rgb += FOG_COLOR.rgb * 0.000001;
	#endif
#endif
}
