#ifdef VERTEX
out vec2 lmcoord;
out vec2 texcoord;
out vec4 glcolor;

out vec3 worldPos;

uniform mat4 gbufferModelViewInverse;

uniform vec3 cameraPosition;


attribute vec3 mc_Entity; 

out float is_water;
out vec3 ssNormal;
out vec3 Wnormal;
void main() {
    gl_Position = ftransform();

    texcoord = (gl_TextureMatrix[0] * gl_MultiTexCoord0).xy;
    lmcoord  = (gl_TextureMatrix[1] * gl_MultiTexCoord1).xy;
    glcolor  = gl_Color;

    worldPos = (gbufferModelViewInverse * gl_ModelViewMatrix * gl_Vertex).xyz;
	worldPos +=cameraPosition;

	ssNormal = gl_NormalMatrix * gl_Normal;
	Wnormal = gl_Normal;



	if(mc_Entity.x == 1){
		is_water = 1.0;
	}else{
		is_water = 0.;
	}
}
#endif


#ifdef FRAGMENT
uniform sampler2D lightmap;
uniform sampler2D texture;

uniform sampler2D colortex14;

uniform mat4 gbufferModelViewInverse;
uniform vec3 shadowLightPosition;
uniform vec3 cameraPosition;

in vec2 lmcoord;
in vec2 texcoord;
in vec4 glcolor;
in vec3 Wnormal;


in vec3 worldPos;

uniform float frameTimeCounter;

in vec3 ssNormal;

in float is_water;

uniform int worldTime;
uniform float rainStrength;

uniform float viewHeight;
uniform float viewWidth;

#include "/lib/Conf/Sky_conf.glsl"
#include "/lib/util/time.glsl"
#include "/lib/sky/Sky_Palet.glsl"
#include "/lib/sky/Phases.glsl"

#include "/lib/water/WaterH.glsl"
#include "/lib/Conf/Water_conf.glsl"
#include "/lib/Color/Water_Color.glsl"

#include "/lib/Conf/Lighmap_conf.glsl"
#include "/lib/Color/Torch_color.glsl"
#include "/lib/Lighting/Lightmap.glsl"


void main() {
	vec4 color = texture2D(texture, texcoord) * glcolor;
	color *= texture2D(lightmap, lmcoord);

	vec4 phases = GetAtmospherePhases();
	
	vec2 p = worldPos.xz;

	vec3 water = vec3(0);

	vec3 Normal = vec3(0);

	if(is_water == 1.0){
		
	vec3 wave = WaterDisplacement(p);

	vec2 flow = vec2(0.02, 0.015) * frameTimeCounter;

	vec2 uv = p * 0.01;

	uv += vec2(sin(wave.x * 8.0), cos(wave.y * 16.0)) * 0.03;


	float n1 = texture2D(colortex14,uv  + flow).r;
	float n2 = texture2D(colortex14,uv  - flow * 0.7).r;
	float n3 = texture2D(colortex14,uv  + flow * 1.6).r;

	float waterPattern =
		n1 * 0.55 +
		n2 * 0.30 +
		n3 * 0.15;

	float waterMask =
    waterPattern * 0.7 +
    wave.y * 0.3;

	waterMask = clamp(waterMask,0.0,1.0);	

	vec3 waterColor = vec3(WATER_R, WATER_G, WATER_B);

	float crest = smoothstep(0.65, 0.9, waterPattern + wave.y * 2.0);

	vec3 dark  = waterColor * 0.35;
	vec3 mid   = waterColor * 0.75;
	vec3 light = waterColor;
	vec3 foam  = crest * mix(waterColor, vec3(1.0), 0.75);

	water = mix(dark, mid, smoothstep(0.15, 0.45, waterMask));
	water = mix(water, light, smoothstep(0.45, 0.80, waterMask));
	water = mix(water, foam, smoothstep(0.90, 1.00, waterMask));

	Normal = WaterNormal(p);

	float dist = length(worldPos - cameraPosition);

	float alpha = mix(0.3, 0.65, smoothstep(3.0, 25.0, dist));
	
	#if WATER_TYPE == WATER_ARTISTIC
	color = vec4(water,alpha);
	#elif WATER_TYPE == WATER_VANILLA
	color = mix(vec4(waterColor,1.),color,0.7);
	#endif

	}



		color.rgb *= GetLightmapColor(lmcoord, phases);



/* DRAWBUFFERS:056*/
	gl_FragData[0] = color; //gcolor
	gl_FragData[1] = vec4(ssNormal * 0.5 + 0.5, is_water);
	gl_FragData[2] = vec4(gl_FragCoord.z,0.0,0.0,1.0);
}
#endif

