Skip to content

Commit

Permalink
Minimize false depth intersections
Browse files Browse the repository at this point in the history
Clamps depth with 'real' depth. Fixes #18
  • Loading branch information
Razoric480 committed Aug 17, 2020
1 parent b599b2b commit fe936f1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
5 changes: 5 additions & 0 deletions godot/Shaders/water_3d.shader
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion godot/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit fe936f1

Please sign in to comment.