From fe936f16fe50d80a93716bd574376c9970a66a21 Mon Sep 17 00:00:00 2001 From: Francois Belair Date: Mon, 17 Aug 2020 13:06:24 -0400 Subject: [PATCH] Minimize false depth intersections Clamps depth with 'real' depth. Fixes #18 --- godot/Shaders/water_3d.shader | 5 +++++ godot/project.godot | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/godot/Shaders/water_3d.shader b/godot/Shaders/water_3d.shader index 591b36c..7ffa285 100644 --- a/godot/Shaders/water_3d.shader +++ b/godot/Shaders/water_3d.shader @@ -29,11 +29,16 @@ void vertex() { void fragment() { vec2 uv = SCREEN_UV + (texture(refraction_noise, UV + (TIME * movement_direction) * refraction_speed).rg * 2.0 - 1.0) * refraction_strength; + float real_depth = texture(DEPTH_TEXTURE, SCREEN_UV).r * 2.0 - 1.0; + real_depth = PROJECTION_MATRIX[3][2] / (real_depth + PROJECTION_MATRIX[2][2]) + VERTEX.z; + //Get the raw linear depth from the depth texture into a [-1, 1] range float depth = texture(DEPTH_TEXTURE, uv).r * 2.0 - 1.0; //Recreate linear depth of the intersecting geometry using projection matrix, and subtract the vertex of the sphere depth = PROJECTION_MATRIX[3][2] / (depth + PROJECTION_MATRIX[2][2]) + VERTEX.z; + depth = max(depth, real_depth); + float intersection = clamp(depth / foam_amount, 0, 1) * foam_cutoff; vec4 out_color = mix(shallow_color, deep_color, clamp((depth / depth_distance), 0, 1)); diff --git a/godot/project.godot b/godot/project.godot index e112f5c..802b378 100644 --- a/godot/project.godot +++ b/godot/project.godot @@ -104,5 +104,5 @@ jump={ quality/2d/use_pixel_snap=true quality/shadows/filter_mode=2 quality/reflections/irradiance_max_size=2048 -quality/filters/msaa=3 +quality/filters/msaa=2 environment/default_environment="res://default_env.tres"