// __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;
	varying highp vec3 wpos;
	varying highp vec3 posi;
#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;

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

#include "dect.h"
#include "inform/option.txt"

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;

    // Transform to view space before projection instead of all at once to avoid floating point errors
    // Not required for entities because they are already offset by camera translation before rendering
    // World position here is calculated above and can get huge
    POS4 pos = WORLDVIEW * worldPos;
    pos = PROJ * pos;
#endif
    gl_Position = pos;

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

///// find distance from the camera

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

highp float chuva = Rain(FOG_CONTROL);

highp vec4 Po =  POSITION;
highp vec4 ppa = Po;
highp float pa = sin((TIME * 0.80) + Po.x + Po.y) * 0.03 ;
highp float pac = sin((TIME * 4.0) + Po.x + Po.y) * 0.03 ;

highp vec3 Og = vec3(0.0,0.0,0.0);
highp vec2 T0 = floor(uv0.xy * vec2(64.0,32.0));


Og.xy += mix(sin(TIME * 1.2 + Po.x - Po.x + sin(TIME * 1.2 + Po.x )),sin(TIME - Po.x + Po.z + sin(TIME - Po.x )),sin(TIME));
Og.xy += (sin(TIME * 8.0 + Po.z) + sin(TIME * 8.0 + Po.x) * 0.05 ) * chuva;
Og *= 0.8;
Og *= pow(1.0 - (uv0.y * 32.0 - T0.y), 6.0);

#ifdef WAVE_GRASSES
//ender
if(End(FOG_COLOR)){}else{
//nether
if(Nether(FOG_CONTROL)){}else{
// PLANTAS DE VERDE
#ifdef ALPHA_TEST 

if(color.a == 0.0){
	gl_Position.xy += mix(pa, pac, chuva*TEXCOORD_1.y);
}else{
if(color.r > color.g){}else{
	if(color.r > color.b){
		//capin altas
		if(uv0.y < 0.2){ gl_Position.xyz += Og*TEXCOORD_1.y;}
		//short grass
		if(uv0.x < 0.44){}else{if(uv0.x < 0.52){ gl_Position.xyz += Og *TEXCOORD_1.y;}}

		//short grass
		if(uv0.x > 0.5){  if(uv0.x > 0.6){
			//green rope plant
			highp float gr = sin(TIME * 1.5 + Po.x + Po.z + Po.x + Po.z + Po.y) * sin(Po.z) * 0.03 ;
				   gr += (sin(TIME * 6.0 + Po.x + Po.z + Po.x + Po.z + Po.y) * sin(Po.x) * 0.05 ) * chuva;
			gl_Position.x += gr*TEXCOORD_1.y;
		 }else{
			gl_Position.xyz += Og*TEXCOORD_1.y;
		}}


		//sugar cane
		if(uv0.x < 0.4){ if(uv0.y < 0.2){}else{
				highp float sc = sin(TIME * 1.5 + Po.x + Po.z + Po.x + Po.z + Po.y) * sin(Po.z) * 0.01;
					     sc += (sin(TIME * 6.0 + Po.x + Po.z + Po.x + Po.z + Po.y) * sin(Po.x) * 0.01) * chuva;
				gl_Position.x += sc *TEXCOORD_1.y;
			 }}

	}
}}
#endif



// FLORES
#ifdef ALPHA_TEST
if(color.a == 0.0){}else{
if(color.r > color.b){}else{
if(uv0.y < 0.2){}else{
if(uv0.y > 0.5){}else{
	//batata E FUNGO DE NETHER
	if(uv0.x < 0.4){ if(uv0.x < 0.25){}else{ gl_Position.xyz += Og*TEXCOORD_1.y;
	}}}
    //flor de amarelo
	if(uv0.y < 0.4){if(uv0.x < 0.75){}else{ 
		gl_Position.xyz += Og*TEXCOORD_1.y;
	}}
}}}

#endif

}}
#endif
wpos = worldPos.xyz;
posi = POSITION.xyz;

///// 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
	//Mega hack: only things that become opaque are allowed to have vertex-driven transparency in the Blended layer...
	//to fix this we'd need to find more space for a flag in the vertex format. color.a is the only unused part
	bool shouldBecomeOpaqueInTheDistance = color.a < 0.95;
	if(shouldBecomeOpaqueInTheDistance) {
		#ifdef FANCY  /////enhance water
			float cameraDist = cameraDepth / FAR_CHUNKS_DISTANCE;
			color = COLOR;
		#else
			// Completely insane, but if I don't have these two lines in here, the water doesn't render on a Nexus 6
			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

#ifndef BYPASS_PIXEL_SHADER
	#ifndef FOG
		// If the FOG_COLOR isn't used, the reflection on NVN fails to compute the correct size of the constant buffer as the uniform will also be gone from the reflection data
		color.rgb += FOG_COLOR.rgb * 0.000001;
	#endif
#endif
}
