diff --git a/water/Caustic.png b/water/Caustic.png new file mode 100644 index 0000000..e1cf7db Binary files /dev/null and b/water/Caustic.png differ diff --git a/water/Caustic.png.import b/water/Caustic.png.import new file mode 100644 index 0000000..be66671 --- /dev/null +++ b/water/Caustic.png.import @@ -0,0 +1,25 @@ +[remap] + +importer="texture_array" +type="TextureArray" +path="res://.import/Caustic.png-83d58ee04a2e46ae9b1eac21320e0bc3.texarr" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://water/Caustic.png" +dest_files=[ "res://.import/Caustic.png-83d58ee04a2e46ae9b1eac21320e0bc3.texarr" ] + +[params] + +compress/mode=0 +compress/no_bptc_if_rgb=false +flags/repeat=true +flags/filter=true +flags/mipmaps=true +flags/anisotropic=false +flags/srgb=2 +slices/horizontal=8 +slices/vertical=8 diff --git a/water/Foam.png b/water/Foam.png new file mode 100644 index 0000000..a499223 Binary files /dev/null and b/water/Foam.png differ diff --git a/water/Foam.png.import b/water/Foam.png.import new file mode 100644 index 0000000..3357b21 --- /dev/null +++ b/water/Foam.png.import @@ -0,0 +1,35 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/Foam.png-7d99bc032ec52df842a38df4c4ac4710.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://water/Foam.png" +dest_files=[ "res://.import/Foam.png-7d99bc032ec52df842a38df4c4ac4710.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=true +flags/filter=true +flags/mipmaps=true +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=true +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +process/normal_map_invert_y=false +stream=false +size_limit=0 +detect_3d=false +svg/scale=1.0 diff --git a/water/Water.material b/water/Water.material new file mode 100644 index 0000000..acf0f99 Binary files /dev/null and b/water/Water.material differ diff --git a/water/Water.shader b/water/Water.shader new file mode 100644 index 0000000..89e12b3 --- /dev/null +++ b/water/Water.shader @@ -0,0 +1,190 @@ +/* +Realistic Water Shader for GODOT 3.1.1 + +Copyright (c) 2019 UnionBytes, Achim Menzel (alias AiYori) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this +software and associated documentation files (the "Software"), to deal in the Software +without restriction, including without limitation the rights to use, copy, modify, +merge, publish, distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR +PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE +OR OTHER DEALINGS IN THE SOFTWARE. + +-- UnionBytes +-- YouTube: +*/ + + +// For this shader min. GODOT 3.1.1 is required, because 3.1 has a depth buffer bug! +shader_type spatial; +render_mode cull_back,diffuse_burley,specular_schlick_ggx, blend_mix; + + +// 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 + +// Surface settings: +uniform vec2 sampler_scale = vec2(0.25, 0.25); // Scale for the sampler +uniform vec2 sampler_direction= vec2(0.05, 0.04); // Direction and speed for the sampler offset + +uniform sampler2D uv_sampler : hint_aniso; // UV motion sampler for shifting the normalmap +uniform vec2 uv_sampler_scale = vec2(0.25, 0.25); // UV sampler scale +uniform float uv_sampler_strength = 0.04; // UV shifting strength + +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_black; // Foam sampler +uniform float foam_level = 0.5; // 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 : hint_color; // Color for deep places in the water, medium to dark blue +uniform vec4 color_shallow : hint_color; // 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_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 + +varying mat4 inv_mvp; // Inverse ModelViewProjection matrix -> Needed for caustic projection +varying vec3 vertex_pos; + + +// Wave function: +vec4 wave(vec4 parameter, vec2 position, float time, inout vec3 tangent, inout vec3 binormal) +{ + float wave_steepness = parameter.z; + float wave_length = parameter.w; + + float k = 2.0 * 3.14159265359 / wave_length; + float c = sqrt(9.8 / k); + vec2 d = normalize(parameter.xy); + 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)))); + + return vec4(d.x * (a * cos(f)), a * sin(f) * 0.25, d.y * (a * cos(f)), 0.0); +} + + +// Vertex shader: +void vertex() +{ + float time = TIME * wave_speed; + + vec4 vertex = vec4(VERTEX, 1.0); + vec3 vertex_position = (WORLD_MATRIX * vertex).xyz; + + vertex_tangent = vec3(0.0, 0.0, 0.0); + vertex_binormal = vec3(0.0, 0.0, 0.0); + + vec3 tmp_tangent = vertex_tangent; + vec3 tmp_binormal = vertex_binormal; + vertex += wave(wave_a, vertex_position.xz, time, tmp_tangent, tmp_binormal); + vertex += wave(wave_b, vertex_position.xz, time, tmp_tangent, tmp_binormal); + vertex += wave(wave_c, vertex_position.xz, time, tmp_tangent, tmp_binormal); + vertex_tangent = tmp_tangent; + vertex_binormal = tmp_binormal; + + 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)); + NORMAL = vertex_normal; + + UV = vertex.xz * sampler_scale; + + VERTEX = vertex.xyz; + + inv_mvp = inverse(PROJECTION_MATRIX * MODELVIEW_MATRIX); + vertex_pos = vertex_position; +} + + +// Fragment shader: +void fragment() +{ + // Calculation of the UV with the UV motion sampler + vec2 uv_offset = sampler_direction * TIME; + vec2 uv_sampler_uv = UV * uv_sampler_scale + uv_offset; + vec2 uv_sampler_uv_offset = uv_sampler_strength * texture(uv_sampler, uv_sampler_uv).rg * 2.0 - 1.0; + vec2 uv = UV + uv_sampler_uv_offset; + + // Normalmap: + vec3 normalmap = texture(normalmap_a_sampler, uv - uv_offset * 2.0 + vertex_pos.xz).rgb * 0.75; // 75 % sampler A + normalmap += texture(normalmap_b_sampler, uv + uv_offset + vertex_pos.xz).rgb * 0.25; // 25 % sampler B +// vec3 normalmap = texture(normalmap_b_sampler, uv + uv_offset).rgb; + + // 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; + + // Ground depth: + float depth_raw = texture(DEPTH_TEXTURE, ref_uv).r * 2.0 - 1.0; + 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_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; + + vec3 dye_color = mix(color_shallow.rgb, color_deep.rgb, depth_blend_pow); + vec3 color = mix(screen_color*dye_color, dye_color*0.25, depth_blend_pow*0.5); + + // Caustic screen projection + vec4 caustic_screenPos = vec4(ref_uv*2.0-1.0, depth_raw, 1.0); + vec4 caustic_localPos = inv_mvp * caustic_screenPos; + caustic_localPos = vec4(caustic_localPos.xyz/caustic_localPos.w, caustic_localPos.w); + + vec2 caustic_Uv = caustic_localPos.xz / vec2(1024.0) + 0.5; + vec4 caustic_color = texture(caustic_sampler, vec3(caustic_Uv*300.0, mod(TIME*14.0, 16.0))); + + color *= 1.0 + pow(caustic_color.r, 1.50) * (1.0-depth_blend) * 6.0; + + // Foam: + if(depth + VERTEX.z < foam_level && depth > vertex_height-0.1) + { + 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); + } + + // Set all values: + ALBEDO = color; + METALLIC = 0.1; + ROUGHNESS = 0.2; + SPECULAR = 0.2 + depth_blend_pow * 0.4; + NORMALMAP = normalmap; + NORMALMAP_DEPTH = 0.3; +} diff --git a/water/Water_N_A.png b/water/Water_N_A.png new file mode 100644 index 0000000..e4f59af Binary files /dev/null and b/water/Water_N_A.png differ diff --git a/water/Water_N_A.png.import b/water/Water_N_A.png.import new file mode 100644 index 0000000..0ef59c2 --- /dev/null +++ b/water/Water_N_A.png.import @@ -0,0 +1,35 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/Water_N_A.png-ab0fffc5ebbddbcc10a6a59768d1c169.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://water/Water_N_A.png" +dest_files=[ "res://.import/Water_N_A.png-ab0fffc5ebbddbcc10a6a59768d1c169.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=1 +flags/repeat=true +flags/filter=true +flags/mipmaps=true +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=false +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +process/normal_map_invert_y=false +stream=false +size_limit=0 +detect_3d=false +svg/scale=1.0 diff --git a/water/Water_N_B.png b/water/Water_N_B.png new file mode 100644 index 0000000..2c64fbd Binary files /dev/null and b/water/Water_N_B.png differ diff --git a/water/Water_N_B.png.import b/water/Water_N_B.png.import new file mode 100644 index 0000000..86c1eec --- /dev/null +++ b/water/Water_N_B.png.import @@ -0,0 +1,35 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/Water_N_B.png-741729783a5eabf68121e72ffaa755fa.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://water/Water_N_B.png" +dest_files=[ "res://.import/Water_N_B.png-741729783a5eabf68121e72ffaa755fa.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=1 +flags/repeat=true +flags/filter=true +flags/mipmaps=true +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=false +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +process/normal_map_invert_y=false +stream=false +size_limit=0 +detect_3d=false +svg/scale=1.0 diff --git a/water/Water_UV.png b/water/Water_UV.png new file mode 100644 index 0000000..74ad47a Binary files /dev/null and b/water/Water_UV.png differ diff --git a/water/Water_UV.png.import b/water/Water_UV.png.import new file mode 100644 index 0000000..72072d7 --- /dev/null +++ b/water/Water_UV.png.import @@ -0,0 +1,35 @@ +[remap] + +importer="texture" +type="StreamTexture" +path="res://.import/Water_UV.png-c19bab0433a0d1a3f801dfdf67056faf.stex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://water/Water_UV.png" +dest_files=[ "res://.import/Water_UV.png-c19bab0433a0d1a3f801dfdf67056faf.stex" ] + +[params] + +compress/mode=0 +compress/lossy_quality=0.7 +compress/hdr_mode=0 +compress/bptc_ldr=0 +compress/normal_map=0 +flags/repeat=true +flags/filter=true +flags/mipmaps=true +flags/anisotropic=false +flags/srgb=2 +process/fix_alpha_border=false +process/premult_alpha=false +process/HDR_as_SRGB=false +process/invert_color=false +process/normal_map_invert_y=false +stream=false +size_limit=0 +detect_3d=false +svg/scale=1.0