varying float height, z;
varying vec2 TexCoord;

uniform float t0, HeightWeight;
uniform sampler2D WavesTex0, WavesTex01;

void main()
{
	float tex = mix(texture2D(WavesTex0, TexCoord).a, texture2D(WavesTex01, TexCoord).a, t0);
	float clr = mix(tex, height, HeightWeight);
	
	float foam = mix(height, tex, 0.3);
	
	float wh = ((2.0*(clr-0.5)) * 0.7) * height;
	
	float h = max(0.0, wh - z);
	float blend = 1.0;
	if (h < 0.7)
	{
	    foam = 1.0-(h / 0.7);
	    if (h < 0.1)
			blend = h / 0.1;
		if (h < 0.05)
			foam *= (h / 0.05);
	}
	else
	    foam = 0.0;
	    
	    blend = 1.0;
	    
	gl_FragColor = vec4(clr, foam, blend, height);
}