diff options
author | dusoleil <howcansocksbereal@gmail.com> | 2022-09-15 07:38:57 -0400 |
---|---|---|
committer | dusoleil <howcansocksbereal@gmail.com> | 2022-09-15 07:38:57 -0400 |
commit | fb12038102da5f225779e9735a8ab47b880913e5 (patch) | |
tree | 07296f8e391df2e42dc7af99396c21730bfb6aa6 /water | |
parent | 764f4d2787418f760e20546ab7abfa07718988e9 (diff) | |
download | game_jam49-fb12038102da5f225779e9735a8ab47b880913e5.tar.gz game_jam49-fb12038102da5f225779e9735a8ab47b880913e5.zip |
Fix water refraction and remove ghosting
Diffstat (limited to 'water')
-rw-r--r-- | water/Wave.gdshader | 32 |
1 files changed, 22 insertions, 10 deletions
diff --git a/water/Wave.gdshader b/water/Wave.gdshader index 9e4aff3..ee06fe0 100644 --- a/water/Wave.gdshader +++ b/water/Wave.gdshader @@ -16,6 +16,7 @@ uniform sampler2D noise; uniform float foam_amount = 0.6; uniform float murkiness = 4.0; +uniform float refraction_amount = 0.01; varying float vertex_y; @@ -66,6 +67,17 @@ void vertex() NORMAL = normalize(cross(BINORMAL,TANGENT)); } +float get_depth(vec2 coords, sampler2D tex, mat4 proj) +{ + float depth = texture(tex, coords).r; + vec3 depth_coords_clip = vec3(coords, depth); + depth_coords_clip = depth_coords_clip * 2.0 - 1.0; + vec4 depth_coords_view = proj * vec4(depth_coords_clip,1.0); + depth_coords_view.xyz /= depth_coords_view.w; + depth = -depth_coords_view.z; + return depth; +} + void fragment() { ALPHA = 1.0; @@ -75,27 +87,27 @@ void fragment() NORMALMAP = texture(noise, UV + (TIME * vec2(-1.0,0.0) * 0.05)).xyz; NORMALMAP_DEPTH = 0.3; - float depth = texture(DEPTH_TEXTURE, SCREEN_UV).r; - vec3 depth_coords_clip = vec3(SCREEN_UV, depth); - depth_coords_clip = depth_coords_clip * 2.0 - 1.0; - vec4 depth_coords_view = INV_PROJECTION_MATRIX * vec4(depth_coords_clip,1.0); - depth_coords_view.xyz /= depth_coords_view.w; - depth = -depth_coords_view.z; + float depth = get_depth(SCREEN_UV, DEPTH_TEXTURE, INV_PROJECTION_MATRIX); float water_depth = -VERTEX.z; float submersion = depth - water_depth; - vec4 water_color = mix(deep_water_colour, water_colour, clamp(vertex_y,0.0,1.0)); float foam = clamp((foam_amount - submersion) / foam_amount, 0.0, 1.0); ALBEDO = mix(water_color, vec4(1.0), foam).rgb; vec3 oriented_normalmap = vec3(TANGENT*NORMALMAP.x + NORMAL*NORMALMAP.y + BINORMAL*NORMALMAP.z); vec3 normal = normalize(mix(NORMAL,oriented_normalmap,NORMALMAP_DEPTH)); - vec2 refraction_uv = SCREEN_UV + (normal.xz * 0.005); + vec2 refraction_uv = SCREEN_UV + (normal.xz * (refraction_amount / exp(water_depth/20.0))); float murky = murkiness * ROUGHNESS; + depth = get_depth(refraction_uv, DEPTH_TEXTURE, INV_PROJECTION_MATRIX); + submersion = depth - water_depth; murky *= submersion; + float depth_visibility = 0.0; + if(submersion >= 0.0) + { + depth_visibility = exp(-murky); + depth_visibility = clamp(depth_visibility,0.0,1.0); + } float depth_scatter = exp(murky); - float depth_visibility = exp(-murky); - depth_visibility = clamp(depth_visibility,0.0,1.0); vec4 bg_color = textureLod(SCREEN_TEXTURE, refraction_uv, depth_scatter); EMISSION = mix(water_color, bg_color, depth_visibility).rgb * (1.0 - water_color.a); } |