From 1c706c488a073db9d046f24ce2677c745614bfbd Mon Sep 17 00:00:00 2001 From: clayjohn Date: Tue, 17 Sep 2024 20:36:30 -0700 Subject: [PATCH] Fix various issues with Godot 4 version and get it working will with 4.2 --- .../art/water/Water.gdshader | 55 ++++++++---------- .../art/water/Water.material | Bin 718 -> 758 bytes 2 files changed, 25 insertions(+), 30 deletions(-) diff --git a/realistic_water_shader/art/water/Water.gdshader b/realistic_water_shader/art/water/Water.gdshader index 4264c4e..265be8a 100644 --- a/realistic_water_shader/art/water/Water.gdshader +++ b/realistic_water_shader/art/water/Water.gdshader @@ -20,9 +20,9 @@ uniform sampler2D SCREEN_TEXTURE : hint_screen_texture, filter_linear_mipmap; // Wave settings: uniform float wave_speed = 0.5; // Speed scale for the waves -uniform vec4 wave_a = vec4(1.0, 1.0, 0.35, 3.0); // xy = Direction, z = Steepness, w = Length -uniform vec4 wave_b = vec4(1.0, 0.6, 0.30, 1.55); // xy = Direction, z = Steepness, w = Length -uniform vec4 wave_c = vec4(1.0, 1.3, 0.25, 0.9); // xy = Direction, z = Steepness, w = Length +uniform vec4 wave_a = vec4(1.0, 0.4, 0.35, 3.0); // xy = Direction, z = Steepness, w = Length +uniform vec4 wave_b = vec4(-0.1, 0.6, 0.30, 1.55); // xy = Direction, z = Steepness, w = Length +uniform vec4 wave_c = vec4(-1.0, -0.8, 0.25, 0.9); // xy = Direction, z = Steepness, w = Length // Surface settings: uniform vec2 sampler_scale = vec2(0.25, 0.25); // Scale for the sampler @@ -36,23 +36,21 @@ uniform sampler2D normalmap_a_sampler : hint_normal; // Normalmap sampler A uniform sampler2D normalmap_b_sampler : hint_normal; // Normalmap sampler B uniform sampler2D foam_sampler : hint_default_black; // Foam sampler -uniform float foam_level = 0.5; // Foam level -> distance from the object (0.0 - 0.5) +uniform float foam_level = 0.75; // Foam level -> distance from the object (0.0 - 0.5) // Volume settings: uniform float refraction = 0.075; // Refraction of the water -uniform vec4 color_deep : source_color; // Color for deep places in the water, medium to dark blue -uniform vec4 color_shallow : source_color; // Color for lower places in the water, bright blue - green +uniform vec4 color_deep: source_color = vec4(0.322, 0.4, 0.502, 1.0); // Color for deep places in the water, medium to dark blue +uniform vec4 color_shallow : source_color = vec4(0.659, 0.749, 0.761, 1.0); // Color for lower places in the water, bright blue - green uniform float beers_law = 2.0; // Beers law value, regulates the blending size to the deep water level uniform float depth_offset = -0.75; // Offset for the blending // Projector for the water caustics: -uniform mat4 projector; // Projector matrix, mostly the matric of the sun / directlight uniform sampler2DArray caustic_sampler : hint_default_black; // Caustic sampler, (Texture array with 16 Textures for the animation) // Vertex -> Fragment: -varying float vertex_height; // Height of the water surface varying vec3 vertex_normal; // Vertex normal -> Needed for refraction calculation varying vec3 vertex_binormal; // Vertex binormal -> Needed for refraction calculation varying vec3 vertex_tangent; // Vertex tangent -> Needed for refraction calculation @@ -72,8 +70,8 @@ vec4 wave(vec4 parameter, vec2 position, float time, inout vec3 tangent, inout v float f = k * (dot(d, position) - c * time); float a = wave_steepness / k; - tangent += normalize(vec3(1.0-d.x * d.x * (wave_steepness * sin(f)), d.x * (wave_steepness * cos(f)), -d.x * d.y * (wave_steepness * sin(f)))); - binormal += normalize(vec3(-d.x * d.y * (wave_steepness * sin(f)), d.y * (wave_steepness * cos(f)), 1.0-d.y * d.y * (wave_steepness * sin(f)))); + tangent += normalize(vec3(1.0-d.x * d.x * (wave_steepness * sin(f)), d.x * (wave_steepness * cos(f)), -d.x * d.y * (wave_steepness * sin(f)))); + binormal += normalize(vec3(-d.x * d.y * (wave_steepness * sin(f)), d.y * (wave_steepness * cos(f)), 1.0-d.y * d.y * (wave_steepness * sin(f)))); return vec4(d.x * (a * cos(f)), a * sin(f) * 0.25, d.y * (a * cos(f)), 0.0); } @@ -84,12 +82,12 @@ void vertex() { float time = TIME * wave_speed; - vec4 vertex = vec4(VERTEX, 1.0); - vec3 vertex_position = (INV_VIEW_MATRIX * vertex).xyz; - - vec3 tang = vec3(0.0, 0.0, 0.0); - vec3 bin = vec3(0.0, 0.0, 0.0); + vec4 vertex = vec4(VERTEX, 1.0); + vec3 vertex_position = (MODEL_MATRIX * vertex).xyz; + vec3 tang = vec3(0.0, 0.0, 0.0); + vec3 bin = vec3(0.0, 0.0, 0.0); + vertex += wave(wave_a, vertex_position.xz, time, tang, bin); vertex += wave(wave_b, vertex_position.xz, time, tang, bin); vertex += wave(wave_c, vertex_position.xz, time, tang, bin); @@ -99,8 +97,6 @@ void vertex() vertex_position = vertex.xyz; - vertex_height = (PROJECTION_MATRIX * MODELVIEW_MATRIX * vertex).z; - TANGENT = vertex_tangent; BINORMAL = vertex_binormal; vertex_normal = normalize(cross(vertex_binormal, vertex_tangent)); @@ -110,7 +106,7 @@ void vertex() VERTEX = vertex.xyz; - inv_mvp = inverse(PROJECTION_MATRIX * MODELVIEW_MATRIX); + inv_mvp = inverse(PROJECTION_MATRIX * MODELVIEW_MATRIX); } @@ -130,16 +126,17 @@ void fragment() // Refraction UV: vec3 ref_normalmap = normalmap * 2.0 - 1.0; ref_normalmap = normalize(vertex_tangent*ref_normalmap.x + vertex_binormal*ref_normalmap.y + vertex_normal*ref_normalmap.z); - vec2 ref_uv = SCREEN_UV + (ref_normalmap.xy * refraction) / vertex_height; + vec2 ref_uv = SCREEN_UV + (ref_normalmap.xy * refraction) / -VERTEX.z; // Ground depth: - float depth_raw = texture(DEPTH_TEXTURE, ref_uv).r; - float depth = PROJECTION_MATRIX[3][2] / (depth_raw + PROJECTION_MATRIX[2][2]); - - float depth_blend = exp((depth+VERTEX.z + depth_offset) * -beers_law); - depth_blend = clamp(1.0-depth_blend, 0.0, 1.0); + float depth_raw = texture(DEPTH_TEXTURE, SCREEN_UV).r; + vec4 depth_view = INV_PROJECTION_MATRIX * vec4(SCREEN_UV * 2.0 - 1.0, depth_raw, 1.0); + float dist = distance(VERTEX, depth_view.xyz / depth_view.w); + + float depth_blend = exp((dist + depth_offset) * -beers_law); + depth_blend = clamp(1.0 - depth_blend, 0.0, 1.0); float depth_blend_pow = clamp(pow(depth_blend, 2.5), 0.0, 1.0); - + // Ground color: vec3 screen_color = textureLod(SCREEN_TEXTURE, ref_uv, depth_blend_pow * 2.5).rgb; @@ -157,12 +154,10 @@ void fragment() color *= 1.0 + pow(caustic_color.r, 1.50) * (1.0-depth_blend) * 6.0; // Foam: - if (depth + VERTEX.z < vertex_height-0.1) - { + float foam_depth = (1.0 - min(1.0, dist / 3.0)); float foam_noise = clamp(pow(texture(foam_sampler, (uv*4.0) - uv_offset).r, 10.0)*40.0, 0.0, 0.2); - float foam_mix = clamp(pow((1.0-(depth + VERTEX.z) + foam_noise), 8.0) * foam_noise * 0.4, 0.0, 1.0); - color = mix(color, vec3(1.0), foam_mix); - } + float foam_mix = clamp(pow(foam_depth + foam_noise, 8.0) * foam_noise * 0.4, 0.0, 1.0); + color = mix(color, vec3(1.0), foam_mix * smoothstep(0.0, 1.0, foam_level - dist)); // Set all values: ALBEDO = color; diff --git a/realistic_water_shader/art/water/Water.material b/realistic_water_shader/art/water/Water.material index a45d9c2c9f09e81bf61d7316726d82d8d53d31ec..8c651b3fa581447e0f68fc4b4e174420e0498b97 100644 GIT binary patch literal 758 zcmVMgX%U9X$0K-hA;0 z)P{iRzA|(ZN^QS6+5NOX)sEb3+hyOIxM(&+Bnj!-ZUwVNa`*9*3Irt%7xpE>6k5=l zRkMj-t2(Aq09OEO09SKmZ+j<?$%Fh8{C0mhWsg%*K>*Pfw!$pJV`X5fv42 z(ZX99LPNhmv8VyVgNPUlqn)A7L?Z|cB*8%@C`Qk;$Y>qsn<_RY;D&=(;Z{{0E>ZLi zN9DwI)!qxPy13le)j~V`7w|f})=0FjQq^h`0w|gqn*Spw2MrkfU$EolH+fMI<3mW6 z{j9PJI6h=oVS)hx1z|IWuKy+%7OSV^fPn%oDMBTe5kNs0&;Jw1IF7??v{qy(LZty2 z501lFte)(I)$>C_$?@{TlmffK!jkcPCHsFmjYvM;KfTXrg^b7$2}wZ;84iE~ zkusXv0TSR8F$ZFX8i651B$Qxgc{Mo(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