// Found this on GLSL sandbox. I really liked it, changed a few things and made it tileable.
// :)
// by David Hoskins.
// Original water turbulence effect by joltz0r


#define TAU 6.28318530718
#define MAX_ITER 5

void mainImage( out vec4 fragColor, in vec2 fragCoord ){
	float time = iTime * .5+23.0;
    // uv should be the 0-1 uv of texture...
	vec2 uv = fragCoord.xy / iResolution.xy;
    
    // - Texture luma
    vec3 luma = texture(iChannel0, uv).rgb;
    
#ifdef SHOW_TILING
	vec2 p = mod(uv*TAU*2.0, TAU)-250.0;
#else
    vec2 p = mod(uv*TAU, TAU)-250.0;
#endif
	vec2 i = vec2(p);
	float c = 1.0;
	float inten = .005;

	for (int n = 0; n < MAX_ITER; n++){
		float t = time * (1.0 - (3.5 / float(n+1)));
		i = (p - (luma.rb * 0.5)) + vec2(cos(t - i.x) + sin(t + i.y + (luma.r * 0.2)), sin(t - i.y) + cos(t + i.x + (luma.r * 0.2)));
		c += 1.0/length(vec2(p.x / (sin(i.x+t)/inten),p.y / (cos(i.y+t)/inten))) + (luma.r * -0.06);
	}
	c /= float(MAX_ITER);
	c = 1.17-pow(c, 1.4);
    vec3 newColor = vec3(pow(abs(c), 14.0));
    
    // -   
	vec3 colour = newColor;
    colour = clamp(colour + vec3(0.0, 0.25, 0.3), 0.1, 1.0);

    // - 
	fragColor = vec4(colour, 1.0);
}
