shader_type spatial;

uniform vec4 out_color : hint_color = vec4(0.0, 0.2, 1.0, 1.0);
uniform float amount : hint_range(0.2, 1.5) = 0.8;
uniform float beer_factor : hint_range(0.01, 1.5) = 0.2;

float generate_offset(float x, float z, float val1, float val2, float time) {
	float speed = 1.0;
	
	float radiansx = ((mod(x + z * x * val1, amount) / amount) + (time * speed) * mod(x * 0.8 + z, 1.5)) * 2.0 * 3.14;
	float radiansz = ((mod(val2 * (z * x + z * x), amount) / amount) + (time * speed) * 2.0 * mod(x, 2.0)) * 2.0 * 3.14;
	
	return amount * 0.5 * (sin(radiansz) + cos(radiansx));
}

vec3 apply_distortion(vec3 vertex, float time) {
	float xd = generate_offset(vertex.x, vertex.z, 0.2, 0.1, time);
	float yd = generate_offset(vertex.x, vertex.z, 0.1, 0.3, time);
	float zd = generate_offset(vertex.x, vertex.z, 0.15, 0.2, time);
	return vertex + vec3(xd, yd, zd);
}

void vertex() {
	VERTEX = apply_distortion(VERTEX, TIME * 0.1);
}

void fragment() {
	NORMAL = normalize(cross(dFdx(VERTEX), dFdy(VERTEX)));
	METALLIC = 0.6;
	SPECULAR = 0.5;
	ROUGHNESS = 0.2;
	
	ALBEDO = out_color.xyz;
	
	float depth = texture(DEPTH_TEXTURE, SCREEN_UV).r;
	
	depth = depth * 2.0 - 1.0;
	depth = PROJECTION_MATRIX[3][2] / (depth + PROJECTION_MATRIX[2][2]);
	depth = depth + VERTEX.z;
	
	depth = exp(-depth * beer_factor);
	
	ALPHA = clamp(1.0 - depth, 0.0, 1.0);
}