<VertexShader>
#version 420
layout(location = 0) in vec3 in_pos;
layout(location = 2) in vec2 in_uv;

layout(location = 0) out vec2 out_uv;

void main() {
	gl_Position = vec4(in_pos, 1);//mat_p2d * vec4(inPosition,1);
	out_uv = in_uv;
	out_uv.y = 1 - out_uv.y;
}

</VertexShader>
<FragmentShader>
#version 420
layout(location = 0) in vec2 in_uv;
uniform sampler2D tex0;
uniform sampler2D tex1;
//layout(binding = 3) uniform sampler2D tex0;
out vec4 color;
uniform float t = 0;
uniform float intensity = 0;
uniform float intensity2 = 0;

struct Melt {
	vec4 d;
	vec4 e;
};


layout (std140, binding=0) uniform MeltData {
	Melt melt[80];
};
uniform int num_melts = 0;

vec2 ripples(vec2 a) {
	vec2 da = a - vec2(0.5, 0.5);
	float r = sqrt(da.x*da.x + da.y*da.y*0.5);
	vec2 rr = da / r;
	return a + rr * sin(r * 10.1 + t) * 0.02 * intensity;
}

vec2 stir(vec2 a) {
	float speed = 0.7;
	float amplitude = 0.003;// + intensity * 0.01;
	float tt = t * speed;
	vec2 b = vec2(
		a.x + sin(a.x*32 + tt*1.21) * sin(-a.y * 43 + tt*0.82) * amplitude,
		a.y + sin(a.x*50 + tt) * sin(a.y * 60 + tt*1.7) * amplitude);
	//vec2 b = vec2(a.x, a.y + sin(a.x*30.02 + t) * 0.1);
	return b;
}

vec2 do_melt(vec2 a) {
	vec2 b = a;
	
	float cc = cos(t*0.2);
	float ss = sin(t*0.2);
	
	for (int i=0; i<num_melts; i++) {
		vec2 p = melt[i].d.xy - vec2(0.5,0.5);
		p = vec2(cc * p.x + ss * p.y, cc * p.y - ss * p.x) + vec2(0.5,0.5);
		vec2 d = (a - p);// * vec2(1,1.4);
		
		float r = melt[i].d.z;
		float l = intensity2*0.7;//t * melt[i].d.w;
		d.y -= l*0.1;
		vec2 dir = -normalize(p - vec2(0.5,0.5));//vec2(0,1);
		vec2 dir2 = vec2(-dir.y,dir.x);
		//if (length(d) < r*3) {
			float f = exp(-(pow(dot(d, dir2)*15, 2) + pow(dot(d, dir)/l, 2)) / r / r * 2);
			vec2 mm = dir2 * sin(dot(d, dir2)) - dir * (dot(d, dir)*0.8- l * 0.08);
			b += f * mm;// + (1-f) * b;
		//}
	}
	return b;
}

vec4 slow_animation(vec2 a) {
	float amplitude = 0.06;
	float y0 = 0.6;
	if (a.x > 0.3)
		y0 = 0.8;
	if (a.x > 0.42)
		y0 = 0.66;
	if (a.x > 0.5)
		y0 = 0.44;
	if (a.x > 0.6)
		y0 = 0.66;
	float omega = 0.6;
	float step = 0.5;
	float tt = t * omega;
	float t0 = floor(tt / step) * step;
	float t1 = t0 + step;
	float f = (tt - t0) / step;
	vec2 b0 = vec2(a.x + sin(t0) * amplitude * max(0, y0-a.y), a.y);
	vec4 c0 = texture(tex0, b0);
	vec2 b1 = vec2(a.x + sin(t1) * amplitude * max(0, y0-a.y), a.y);
	vec4 c1 = texture(tex0, b1);
	return mix(c0, c1, f);//smoothstep(0,1,f));
	//return (1-f) * c0 + f * c1;
}

void main() {
	vec2 a = in_uv;
	a = ripples(a);
	a = do_melt(a);
	vec4 mask = texture(tex1, a);
	
	vec2 b = stir(a);
	color = texture(tex0, b);
	
	color = (1-mask.r) * color + mask.r * slow_animation(a);
}
</FragmentShader>