
// Water animation shader

uniform sampler2D texture;
uniform vec2 mapScroll;
uniform vec2 screenScale;
uniform float time;
uniform int type; // 1 - water, 2 - lava, 3 - acid

void main()
{
	vec2 texSize = vec2(512, 512);
	
	// invert map scroll x
	vec2 invertedMapScroll = mapScroll;
	invertedMapScroll[0] = -invertedMapScroll[0];	

	vec2 uv = (gl_FragCoord.xy - invertedMapScroll)/texSize;

	// get colour from texture for sea bed
	vec4 textureCol = vec4(texture2D(texture, uv).xyz, 1);
	
	// add yellow (sand)
	//textureCol = mix(textureCol, vec4(1,1,0,1), 0.5);
	
	// surface colour
	//vec4 surfaceCol = vec4(0.1, 0.4, 0.5, 1.0);
	//vec4 surfaceCol = vec4(0.1, 0.5, 0.6, 1.0);
	vec4 surfaceCol;
	if (type == 1) 
	{
		// water
		surfaceCol = vec4(0.1, 0.6, 0.8, 1.0);
	}
	else
	{
		// lava or acid, sample the texture at 0,0
		surfaceCol = vec4(texture2D(texture, vec2(0, 0)).xyz, 1);
	}
	
	// ------------------------------------------------------------------------------
	// Based on code from https://www.shadertoy.com/view/wt2GRt

	vec4 k = vec4(time) * 0.3;
	k.xy = uv * 7.0;
	float val1 = length(0.5-fract(k.xyw*=mat3(vec3(-2.0,-1.0,0.0), vec3(3.0,-1.0,1.0), vec3(1.0,-1.0,-1.0))*1.0));
	float val2 = length(0.5-fract(k.xyw*=mat3(vec3(-2.0,-1.0,0.0), vec3(3.0,-1.0,1.0), vec3(1.0,-1.0,-1.0))*0.1));
	float val3 = length(0.5-fract(k.xyw*=mat3(vec3(-2.0,-1.0,0.0), vec3(3.0,-1.0,1.0), vec3(1.0,-1.0,-1.0))*1.0));

	vec4 col = vec4 ( pow(min(min(val1,val2),val3), 8.0) * 3.75) + mix(surfaceCol, textureCol, 0.075);
	
	// ------------------------------------------------------------------------------

	//gl_FragColor = col;
	gl_FragColor = min(col, vec4(0.8,0.8,0.9,1)); // limit foam brightness
}
