
// Water animation shader

#version 130

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

#include "Shared/Perlin.frag"

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

	// invert screen offset x
	vec2 invertedScreenOffset = screenOffset;
	invertedScreenOffset.x = -invertedScreenOffset.x;

	vec2 uv = ((invertedScreenOffset + gl_FragCoord.xy) - invertedMapScroll)/texSize;
	//vec2 uv = (gl_TexCoord[0].xy * vec2(2048, 1024)) / texSize;
	//vec2 uv = ((gl_TexCoord[0].xy * vec2(2048, 1024)) + mapScroll) / texSize;
	//vec2 uv = gl_TexCoord[0].xy;

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


	// shine
	vec4 pixel = gl_FragColor;
	if(pixel.a > 0.5)
	{
        vec2 screenPos = invertedScreenOffset + gl_FragCoord.xy;
        screenPos.y = screenSize.y - screenPos.y;

        vec2 uvPerlin = screenPos + (mapScroll * 0.9);//(gl_TexCoord[0].xy * textureSize);

        //uvPerlin *= 0.00001;
        //uvPerlin += (time * 0.00005);
        uvPerlin *= 0.0001;
        uvPerlin += (time * 0.005);

        uint seed = 12382u;
        float sample = perlin2D(uvPerlin, 20, 1, seed);

        //if(sample > 0.5)
        {
            vec4 noisePixel = vec4(sample, sample, sample, 1);

            float shine = 0.5;
            vec4 bright = vec4(shine, shine, shine, 1.0);
            if(bright.a > 0 && bright.r > 0 && sample > 0)
            {
                //bright *= 1.2;
                //bright *= 0.6;
                bright *= 0.7;
                float a = pixel.a;

                pixel = pixel + (noisePixel * bright);

                pixel.a = a;
            }
        }
    }
	gl_FragColor = pixel;
}
