From 7ecfde2925b5511eebf06742c94bf0e5bff0d0ee Mon Sep 17 00:00:00 2001
From: Joshua Barretto <joshua.s.barretto@gmail.com>
Date: Sun, 3 Nov 2019 10:49:14 +0000
Subject: [PATCH] Better water

---
 assets/voxygen/shaders/fluid-frag.glsl | 26 ++++++++++++++++++--------
 assets/voxygen/texture/waves.png       |  4 ++--
 2 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/assets/voxygen/shaders/fluid-frag.glsl b/assets/voxygen/shaders/fluid-frag.glsl
index 12393caeb7..7ddc5d243d 100644
--- a/assets/voxygen/shaders/fluid-frag.glsl
+++ b/assets/voxygen/shaders/fluid-frag.glsl
@@ -38,6 +38,9 @@ void main() {
 	// Use an array to avoid conditional branching
 	vec3 f_norm = normals[norm_axis + norm_dir];
 
+	vec3 cam_to_frag = normalize(f_pos - cam_pos.xyz);
+	float frag_dist = length(f_pos - cam_pos.xyz);
+
 	/*
 	// Round the position to the nearest triangular grid cell
 	vec3 hex_pos = f_pos * 2.0;
@@ -58,13 +61,21 @@ void main() {
 	}
 	vec3 c_norm = cross(f_norm, b_norm);
 
-	vec3 nmap = normalize(
-		(srgb_to_linear(texture(t_waves, fract(f_pos.xy * 0.3 + tick.x * 0.04)).rgb) - 0.0) * 0.05
-		+ (srgb_to_linear(texture(t_waves, fract(f_pos.xy * 0.1 - tick.x * 0.08)).rgb) - 0.0) * 0.1
-		+ (srgb_to_linear(texture(t_waves, fract(-f_pos.yx * 0.06 - tick.x * 0.1)).rgb) - 0.0) * 0.1
-		+ (srgb_to_linear(texture(t_waves, fract(-f_pos.yx * 0.03 - tick.x * 0.01)).rgb) - 0.0) * 0.2
-		+ vec3(0, 0, 0.0)
+	vec3 nwarp = normalize(
+		texture(t_waves, fract(f_pos.xy * 0.03 + tick.x * 0.01)).xyz * 0.7 +
+		texture(t_waves, fract(-f_pos.yx * 0.03 + tick.x * 0.01)).xyz * 0.7 +
+		texture(t_waves, fract(f_pos.xy * 0.07 + tick.x * 0.02)).xyz * 0.5 +
+		texture(t_waves, fract(-f_pos.yx * 0.15 + tick.x * 0.02)).xyz * 0.3 +
+		texture(t_waves, fract(f_pos.xy * 0.25 + tick.x * 0.06)).xyz * 0.05 +
+		texture(t_waves, fract(-f_pos.yx * 0.5 + tick.x * 0.06)).xyz * 0.05
 	);
+	vec3 nmap = mix(vec3(0, 0, 1), normalize(
+		texture(t_waves, fract(f_pos.xy * 0.01 + nwarp.xy)).rgb * 1.0 +
+		texture(t_waves, fract(f_pos.xy * 0.1 + nwarp.xy)).rgb * 0.8 +
+		texture(t_waves, fract(f_pos.xy * 0.3 + nwarp.xy)).rgb * 0.6 +
+		texture(t_waves, fract(f_pos.xy * 0.9 + nwarp.xy * 0.3)).rgb * 0.2
+		+ vec3(-0.5, -0.5, 0)
+	), clamp(2.0 / pow(frag_dist, 0.5), 0, 1));
 
 	vec3 norm = f_norm * nmap.z + b_norm * nmap.x + c_norm * nmap.y;
 
@@ -81,7 +92,6 @@ void main() {
 	float fog_level = fog(f_pos.xyz, focus_pos.xyz, medium.x);
     vec3 fog_color = get_sky_color(normalize(f_pos - cam_pos.xyz), time_of_day.x, true);
 
-	vec3 cam_to_frag = normalize(f_pos - cam_pos.xyz);
 	vec3 reflect_ray_dir = reflect(cam_to_frag, norm);
 	// Hack to prevent the reflection ray dipping below the horizon and creating weird blue spots in the water
 	reflect_ray_dir.z = max(reflect_ray_dir.z, 0.05);
@@ -89,7 +99,7 @@ void main() {
 	vec3 reflect_color = get_sky_color(reflect_ray_dir, time_of_day.x, false) * f_light;
 	//reflect_color = vec3(reflect_color.r + reflect_color.g + reflect_color.b) / 3.0;
 	// 0 = 100% reflection, 1 = translucent water
-	float passthrough = pow(dot(faceforward(f_norm, f_norm, cam_to_frag), -cam_to_frag), 0.3);
+	float passthrough = pow(dot(faceforward(f_norm, f_norm, cam_to_frag), -cam_to_frag), 0.5);
 
 	vec4 color = mix(vec4(reflect_color * 2.0, 1.0), vec4(surf_color, 4.0 / (1.0 + diffuse_light * 2.0)), passthrough);
 
diff --git a/assets/voxygen/texture/waves.png b/assets/voxygen/texture/waves.png
index 3083b00a86..7bfe95f49f 100644
--- a/assets/voxygen/texture/waves.png
+++ b/assets/voxygen/texture/waves.png
@@ -1,3 +1,3 @@
 version https://git-lfs.github.com/spec/v1
-oid sha256:9beac98bc31b98390c7d0f3bba959831d7734518d521d4f2fe10d96d0756ea66
-size 15547
+oid sha256:aa9c308ee96ac61903552a88f3b4cf4733975ad499611f04714cdf053c582c99
+size 15148