From 38b247487309ca89bf2d94bb321f12059861db9d Mon Sep 17 00:00:00 2001 From: Malfurious Date: Wed, 14 Sep 2022 03:50:09 -0400 Subject: Consolidate water-related assets Stray files are moved into a new directory. The water MeshInstance is made into a new scene, as I will be adding components to it. --- Float.gd | 2 +- Main.tscn | 47 ++-------------------- Water.gd | 113 ---------------------------------------------------- Wave.gd | 48 ---------------------- Wave.gdshader | 101 ---------------------------------------------- project.godot | 2 +- water/Water.gd | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++ water/Water.tscn | 46 +++++++++++++++++++++ water/Wave.gd | 48 ++++++++++++++++++++++ water/Wave.gdshader | 101 ++++++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 314 insertions(+), 307 deletions(-) delete mode 100644 Water.gd delete mode 100644 Wave.gd delete mode 100644 Wave.gdshader create mode 100644 water/Water.gd create mode 100644 water/Water.tscn create mode 100644 water/Wave.gd create mode 100644 water/Wave.gdshader diff --git a/Float.gd b/Float.gd index 7855bea..4cf7dda 100644 --- a/Float.gd +++ b/Float.gd @@ -5,7 +5,7 @@ var angular_drag = 0.5 func _physics_process(_delta): var parent = get_parent() - var water = get_node("/root/Main/Water") + var water = get_node("/root/Main/Water/MeshInstance") #self.global_transform.origin.y = water.height(self.global_transform.origin) var height = self.global_transform.origin.y var wave_height = water.height(self.global_transform.origin) diff --git a/Main.tscn b/Main.tscn index 2906276..1c1c29b 100644 --- a/Main.tscn +++ b/Main.tscn @@ -1,10 +1,9 @@ -[gd_scene load_steps=19 format=2] +[gd_scene load_steps=14 format=2] [ext_resource path="res://character/fps_controller/fps_controller.tscn" type="PackedScene" id=1] [ext_resource path="res://icon.png" type="Texture" id=2] +[ext_resource path="res://water/Water.tscn" type="PackedScene" id=3] [ext_resource path="res://Float.tscn" type="PackedScene" id=4] -[ext_resource path="res://Water.gd" type="Script" id=5] -[ext_resource path="res://Wave.gdshader" type="Shader" id=7] [sub_resource type="BoxShape" id=1] extents = Vector3( 5, 0.5, 5 ) @@ -16,32 +15,6 @@ size = Vector3( 10, 1, 10 ) albedo_texture = ExtResource( 2 ) uv1_triplanar = true -[sub_resource type="PlaneMesh" id=9] -size = Vector2( 500, 500 ) -subdivide_width = 500 -subdivide_depth = 500 - -[sub_resource type="OpenSimplexNoise" id=17] - -[sub_resource type="NoiseTexture" id=18] -seamless = true -as_normalmap = true -noise = SubResource( 17 ) - -[sub_resource type="ShaderMaterial" id=11] -shader = ExtResource( 7 ) -shader_param/wave1 = Quat( 0.25, 0.10472, 2.38732, 509.65 ) -shader_param/wave1_dir = Vector2( 1, 1 ) -shader_param/wave2 = Quat( 0.25, 0.202683, 1.23345, 366.334 ) -shader_param/wave2_dir = Vector2( 1, 0.6 ) -shader_param/wave3 = Quat( 0.25, 0.349066, 0.716197, 279.147 ) -shader_param/wave3_dir = Vector2( 1, 1.3 ) -shader_param/water_colour = Color( 0.694118, 0.945098, 0.941176, 0.368627 ) -shader_param/deep_water_colour = Color( 0.25098, 0.623529, 0.788235, 0.54902 ) -shader_param/foam_amount = 0.6 -shader_param/murkiness = 4.0 -shader_param/noise = SubResource( 18 ) - [sub_resource type="BoxShape" id=19] extents = Vector3( 250, 1, 250 ) @@ -96,20 +69,8 @@ transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -5.0045, 0, 5.02626 ) [node name="DirectionalLight" type="DirectionalLight" parent="."] transform = Transform( 0.863334, 0.0994421, -0.494738, 0, 0.980392, 0.197058, 0.504633, -0.170127, 0.846406, -12.2908, 5.96757, 0 ) -[node name="Water" type="MeshInstance" parent="."] -transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0.00538254, -1.02502, 0.0534515 ) -mesh = SubResource( 9 ) -material/0 = SubResource( 11 ) -script = ExtResource( 5 ) -wave1_steepness = 0.25 -wave1_wavelength = 60.0 -wave1_direction = Vector2( 1, 1 ) -wave2_steepness = 0.25 -wave2_wavelength = 31.0 -wave2_direction = Vector2( 1, 0.6 ) -wave3_steepness = 0.25 -wave3_wavelength = 18.0 -wave3_direction = Vector2( 1, 1.3 ) +[node name="Water" parent="." instance=ExtResource( 3 )] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0 ) [node name="Ground" type="StaticBody" parent="."] transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -10, 0 ) diff --git a/Water.gd b/Water.gd deleted file mode 100644 index da7666c..0000000 --- a/Water.gd +++ /dev/null @@ -1,113 +0,0 @@ -extends MeshInstance -tool - -export var wave1_steepness = 0.5 setget _set_wave1_steepness -export var wave1_wavelength = 10.0 setget _set_wave1_wavelength -export var wave1_direction = Vector2(1.0,0.0) setget _set_wave1_direction - -export var wave2_steepness = 0.5 setget _set_wave2_steepness -export var wave2_wavelength = 10.0 setget _set_wave2_wavelength -export var wave2_direction = Vector2(1.0,0.0) setget _set_wave2_direction - -export var wave3_steepness = 0.5 setget _set_wave3_steepness -export var wave3_wavelength = 10.0 setget _set_wave3_wavelength -export var wave3_direction = Vector2(1.0,0.0) setget _set_wave3_direction - -func _set_wave_prop(idx, prop, val): - if val == null: return - if idx >= waves.size(): return - waves[idx].set(prop,val) - _update_wave(0) - -func _set_wave1_steepness(val): - wave1_steepness = val - _set_wave_prop(0,"steepness",val) - -func _set_wave1_wavelength(val): - wave1_wavelength = val - _set_wave_prop(0,"wavelength",val) - -func _set_wave1_direction(val): - wave1_direction = val - _set_wave_prop(0,"direction",val) - -func _set_wave2_steepness(val): - wave2_steepness = val - _set_wave_prop(1,"steepness",val) - -func _set_wave2_wavelength(val): - wave2_wavelength = val - _set_wave_prop(1,"wavelength",val) - -func _set_wave2_direction(val): - wave2_direction = val - _set_wave_prop(1,"direction",val) - -func _set_wave3_steepness(val): - wave3_steepness = val - _set_wave_prop(2,"steepness",val) - -func _set_wave3_wavelength(val): - wave3_wavelength = val - _set_wave_prop(2,"wavelength",val) - -func _set_wave3_direction(val): - wave3_direction = val - _set_wave_prop(2,"direction",val) - -var _time = 0.0 setget _set_time -var waves = [] -func _ready(): - waves.push_back(Wave.new(wave1_steepness,wave1_wavelength,wave1_direction,_time)) - waves.push_back(Wave.new(wave2_steepness,wave2_wavelength,wave2_direction,_time)) - waves.push_back(Wave.new(wave3_steepness,wave3_wavelength,wave3_direction,_time)) - -func _wave_to_quad(wave): - return Quat(wave.steepness,wave._k,wave._a,wave._t) - -func _update_wave(idx): - var material = self.get_surface_material(0) - material.set_shader_param("wave" + str(idx+1), _wave_to_quad(waves[idx])) - material.set_shader_param("wave" + str(idx+1) + "_dir", waves[idx].direction) - -func _set_time(val): - _time = val - for idx in waves.size(): - waves[idx]._time = _time - _update_wave(idx) - -func _physics_process(delta): - self._time += delta - #self._time = 0.0 - -func gertsner_wave(pos, idx): - if idx >= waves.size(): return 0.0 - var wave = waves[idx] - var k = wave._k - var a = wave._a - var t = wave._t - - var d = wave.direction.normalized() - var f = k * (d.dot(Vector2(pos.x,pos.z)) - t) - - var tmp = a * cos(f) - var new_pos = Vector3(d.x * tmp, a * sin(f), d.y * tmp) - - return new_pos - -func sample_wave(pos): - var new_pos = Vector3(pos.x,0,pos.z) - for idx in waves.size(): - new_pos += gertsner_wave(pos, idx) - return new_pos - -func height(pos): - var old_pos = pos - var y = old_pos.y - for _i in range(2): - var new_pos = sample_wave(old_pos) - y = new_pos.y - new_pos = old_pos - (new_pos - old_pos) - old_pos = new_pos - y += self.global_transform.origin.y - return y diff --git a/Wave.gd b/Wave.gd deleted file mode 100644 index c876831..0000000 --- a/Wave.gd +++ /dev/null @@ -1,48 +0,0 @@ -class_name Wave -extends Resource - -export var steepness = 1.0 setget set_steepness -export var wavelength = 1.0 setget set_wavelength -export var direction = Vector2(1.0,0.0) setget set_direction - -var _k = 1.0 setget _set_k -var _a = 1.0 setget _set_a -var _c = 1.0 setget _set_c -var _time = 0.0 setget _set_time -var _t = 1.0 setget _set_t - -func _init(s, w, d, t): - self.steepness = s - self.wavelength = w - self.direction = d - self._time = t - -func set_steepness(val): - steepness = val - self._a = steepness / _k - -func set_wavelength(val): - wavelength = val - self._k = TAU / wavelength - -func set_direction(val): - direction = val - -func _set_k(val): - _k = val - self._a = steepness / _k - self._c = sqrt(9.8 / _k) - -func _set_a(val): - _a = val - -func _set_c(val): - _c = val - self._t = _c * _time - -func _set_time(val): - _time = val - self._t = _c * _time - -func _set_t(val): - _t = val diff --git a/Wave.gdshader b/Wave.gdshader deleted file mode 100644 index 9e4aff3..0000000 --- a/Wave.gdshader +++ /dev/null @@ -1,101 +0,0 @@ -shader_type spatial; -render_mode cull_disabled, depth_draw_always; - -uniform vec4 wave1 = vec4(0.5,0.5,0.5,1.0); -uniform vec2 wave1_dir = vec2(1.0,0.0); - -uniform vec4 wave2 = vec4(0.5,0.5,0.5,1.0); -uniform vec2 wave2_dir = vec2(1.0,0.0); - -uniform vec4 wave3 = vec4(0.5,0.5,0.5,1.0); -uniform vec2 wave3_dir = vec2(1.0,0.0); - -uniform vec4 water_colour: hint_color; -uniform vec4 deep_water_colour : hint_color; -uniform sampler2D noise; - -uniform float foam_amount = 0.6; -uniform float murkiness = 4.0; - -varying float vertex_y; - -vec3 gerstner_wave(vec4 wave, vec2 wave_dir, vec3 pos, inout vec3 tangent, inout vec3 binormal) -{ - float steepness = wave.x; - float k = wave.y; - float a = wave.z; - float t = wave.w; - - vec2 d = normalize(wave_dir); - float f = k * (dot(d,pos.xz) - t); - - float tmp1 = steepness * sin(f); - float tmp2 = steepness * cos(f); - - tangent += vec3( - -d.x * d.x * tmp1, - d.x * tmp2, - -d.x * d.y * tmp1 - ); - - binormal += vec3( - -d.x * d.y * tmp1, - d.y * tmp2, - -d.y * d.y * tmp1 - ); - - float tmp3 = a * cos(f); - vec3 new_pos = vec3( - d.x * tmp3, - a * sin(f), - d.y * tmp3 - ); - - return new_pos; -} - -void vertex() -{ - vec3 global_vertex = (WORLD_MATRIX * vec4(VERTEX,1.0)).xyz; - TANGENT = vec3(1.0,0.0,0.0); - BINORMAL = vec3(0.0,0.0,1.0); - VERTEX += gerstner_wave(wave1,wave1_dir,global_vertex,TANGENT,BINORMAL); - VERTEX += gerstner_wave(wave2,wave2_dir,global_vertex,TANGENT,BINORMAL); - VERTEX += gerstner_wave(wave3,wave3_dir,global_vertex,TANGENT,BINORMAL); - vertex_y = VERTEX.y; - NORMAL = normalize(cross(BINORMAL,TANGENT)); -} - -void fragment() -{ - ALPHA = 1.0; - SPECULAR = 0.4; - ROUGHNESS = 0.05; - - 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 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); - float murky = murkiness * ROUGHNESS; - murky *= submersion; - 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); -} diff --git a/project.godot b/project.godot index 75d4b3d..fea7a33 100644 --- a/project.godot +++ b/project.godot @@ -12,7 +12,7 @@ _global_script_classes=[ { "base": "Resource", "class": "Wave", "language": "GDScript", -"path": "res://Wave.gd" +"path": "res://water/Wave.gd" } ] _global_script_class_icons={ "Wave": "" diff --git a/water/Water.gd b/water/Water.gd new file mode 100644 index 0000000..da7666c --- /dev/null +++ b/water/Water.gd @@ -0,0 +1,113 @@ +extends MeshInstance +tool + +export var wave1_steepness = 0.5 setget _set_wave1_steepness +export var wave1_wavelength = 10.0 setget _set_wave1_wavelength +export var wave1_direction = Vector2(1.0,0.0) setget _set_wave1_direction + +export var wave2_steepness = 0.5 setget _set_wave2_steepness +export var wave2_wavelength = 10.0 setget _set_wave2_wavelength +export var wave2_direction = Vector2(1.0,0.0) setget _set_wave2_direction + +export var wave3_steepness = 0.5 setget _set_wave3_steepness +export var wave3_wavelength = 10.0 setget _set_wave3_wavelength +export var wave3_direction = Vector2(1.0,0.0) setget _set_wave3_direction + +func _set_wave_prop(idx, prop, val): + if val == null: return + if idx >= waves.size(): return + waves[idx].set(prop,val) + _update_wave(0) + +func _set_wave1_steepness(val): + wave1_steepness = val + _set_wave_prop(0,"steepness",val) + +func _set_wave1_wavelength(val): + wave1_wavelength = val + _set_wave_prop(0,"wavelength",val) + +func _set_wave1_direction(val): + wave1_direction = val + _set_wave_prop(0,"direction",val) + +func _set_wave2_steepness(val): + wave2_steepness = val + _set_wave_prop(1,"steepness",val) + +func _set_wave2_wavelength(val): + wave2_wavelength = val + _set_wave_prop(1,"wavelength",val) + +func _set_wave2_direction(val): + wave2_direction = val + _set_wave_prop(1,"direction",val) + +func _set_wave3_steepness(val): + wave3_steepness = val + _set_wave_prop(2,"steepness",val) + +func _set_wave3_wavelength(val): + wave3_wavelength = val + _set_wave_prop(2,"wavelength",val) + +func _set_wave3_direction(val): + wave3_direction = val + _set_wave_prop(2,"direction",val) + +var _time = 0.0 setget _set_time +var waves = [] +func _ready(): + waves.push_back(Wave.new(wave1_steepness,wave1_wavelength,wave1_direction,_time)) + waves.push_back(Wave.new(wave2_steepness,wave2_wavelength,wave2_direction,_time)) + waves.push_back(Wave.new(wave3_steepness,wave3_wavelength,wave3_direction,_time)) + +func _wave_to_quad(wave): + return Quat(wave.steepness,wave._k,wave._a,wave._t) + +func _update_wave(idx): + var material = self.get_surface_material(0) + material.set_shader_param("wave" + str(idx+1), _wave_to_quad(waves[idx])) + material.set_shader_param("wave" + str(idx+1) + "_dir", waves[idx].direction) + +func _set_time(val): + _time = val + for idx in waves.size(): + waves[idx]._time = _time + _update_wave(idx) + +func _physics_process(delta): + self._time += delta + #self._time = 0.0 + +func gertsner_wave(pos, idx): + if idx >= waves.size(): return 0.0 + var wave = waves[idx] + var k = wave._k + var a = wave._a + var t = wave._t + + var d = wave.direction.normalized() + var f = k * (d.dot(Vector2(pos.x,pos.z)) - t) + + var tmp = a * cos(f) + var new_pos = Vector3(d.x * tmp, a * sin(f), d.y * tmp) + + return new_pos + +func sample_wave(pos): + var new_pos = Vector3(pos.x,0,pos.z) + for idx in waves.size(): + new_pos += gertsner_wave(pos, idx) + return new_pos + +func height(pos): + var old_pos = pos + var y = old_pos.y + for _i in range(2): + var new_pos = sample_wave(old_pos) + y = new_pos.y + new_pos = old_pos - (new_pos - old_pos) + old_pos = new_pos + y += self.global_transform.origin.y + return y diff --git a/water/Water.tscn b/water/Water.tscn new file mode 100644 index 0000000..0cf4a50 --- /dev/null +++ b/water/Water.tscn @@ -0,0 +1,46 @@ +[gd_scene load_steps=7 format=2] + +[ext_resource path="res://water/Wave.gdshader" type="Shader" id=1] +[ext_resource path="res://water/Water.gd" type="Script" id=2] + +[sub_resource type="PlaneMesh" id=9] +size = Vector2( 500, 500 ) +subdivide_width = 500 +subdivide_depth = 500 + +[sub_resource type="OpenSimplexNoise" id=17] + +[sub_resource type="NoiseTexture" id=18] +seamless = true +as_normalmap = true +noise = SubResource( 17 ) + +[sub_resource type="ShaderMaterial" id=11] +shader = ExtResource( 1 ) +shader_param/wave1 = Quat( 0.25, 0.10472, 2.38732, 2652.57 ) +shader_param/wave1_dir = Vector2( 1, 1 ) +shader_param/wave2 = Quat( 0.25, 0.202683, 1.23345, 1906.65 ) +shader_param/wave2_dir = Vector2( 1, 0.6 ) +shader_param/wave3 = Quat( 0.25, 0.349066, 0.716197, 1452.87 ) +shader_param/wave3_dir = Vector2( 1, 1.3 ) +shader_param/water_colour = Color( 0.694118, 0.945098, 0.941176, 0.368627 ) +shader_param/deep_water_colour = Color( 0.25098, 0.623529, 0.788235, 0.54902 ) +shader_param/foam_amount = 0.6 +shader_param/murkiness = 4.0 +shader_param/noise = SubResource( 18 ) + +[node name="Water" type="Spatial"] + +[node name="MeshInstance" type="MeshInstance" parent="."] +mesh = SubResource( 9 ) +material/0 = SubResource( 11 ) +script = ExtResource( 2 ) +wave1_steepness = 0.25 +wave1_wavelength = 60.0 +wave1_direction = Vector2( 1, 1 ) +wave2_steepness = 0.25 +wave2_wavelength = 31.0 +wave2_direction = Vector2( 1, 0.6 ) +wave3_steepness = 0.25 +wave3_wavelength = 18.0 +wave3_direction = Vector2( 1, 1.3 ) diff --git a/water/Wave.gd b/water/Wave.gd new file mode 100644 index 0000000..c876831 --- /dev/null +++ b/water/Wave.gd @@ -0,0 +1,48 @@ +class_name Wave +extends Resource + +export var steepness = 1.0 setget set_steepness +export var wavelength = 1.0 setget set_wavelength +export var direction = Vector2(1.0,0.0) setget set_direction + +var _k = 1.0 setget _set_k +var _a = 1.0 setget _set_a +var _c = 1.0 setget _set_c +var _time = 0.0 setget _set_time +var _t = 1.0 setget _set_t + +func _init(s, w, d, t): + self.steepness = s + self.wavelength = w + self.direction = d + self._time = t + +func set_steepness(val): + steepness = val + self._a = steepness / _k + +func set_wavelength(val): + wavelength = val + self._k = TAU / wavelength + +func set_direction(val): + direction = val + +func _set_k(val): + _k = val + self._a = steepness / _k + self._c = sqrt(9.8 / _k) + +func _set_a(val): + _a = val + +func _set_c(val): + _c = val + self._t = _c * _time + +func _set_time(val): + _time = val + self._t = _c * _time + +func _set_t(val): + _t = val diff --git a/water/Wave.gdshader b/water/Wave.gdshader new file mode 100644 index 0000000..9e4aff3 --- /dev/null +++ b/water/Wave.gdshader @@ -0,0 +1,101 @@ +shader_type spatial; +render_mode cull_disabled, depth_draw_always; + +uniform vec4 wave1 = vec4(0.5,0.5,0.5,1.0); +uniform vec2 wave1_dir = vec2(1.0,0.0); + +uniform vec4 wave2 = vec4(0.5,0.5,0.5,1.0); +uniform vec2 wave2_dir = vec2(1.0,0.0); + +uniform vec4 wave3 = vec4(0.5,0.5,0.5,1.0); +uniform vec2 wave3_dir = vec2(1.0,0.0); + +uniform vec4 water_colour: hint_color; +uniform vec4 deep_water_colour : hint_color; +uniform sampler2D noise; + +uniform float foam_amount = 0.6; +uniform float murkiness = 4.0; + +varying float vertex_y; + +vec3 gerstner_wave(vec4 wave, vec2 wave_dir, vec3 pos, inout vec3 tangent, inout vec3 binormal) +{ + float steepness = wave.x; + float k = wave.y; + float a = wave.z; + float t = wave.w; + + vec2 d = normalize(wave_dir); + float f = k * (dot(d,pos.xz) - t); + + float tmp1 = steepness * sin(f); + float tmp2 = steepness * cos(f); + + tangent += vec3( + -d.x * d.x * tmp1, + d.x * tmp2, + -d.x * d.y * tmp1 + ); + + binormal += vec3( + -d.x * d.y * tmp1, + d.y * tmp2, + -d.y * d.y * tmp1 + ); + + float tmp3 = a * cos(f); + vec3 new_pos = vec3( + d.x * tmp3, + a * sin(f), + d.y * tmp3 + ); + + return new_pos; +} + +void vertex() +{ + vec3 global_vertex = (WORLD_MATRIX * vec4(VERTEX,1.0)).xyz; + TANGENT = vec3(1.0,0.0,0.0); + BINORMAL = vec3(0.0,0.0,1.0); + VERTEX += gerstner_wave(wave1,wave1_dir,global_vertex,TANGENT,BINORMAL); + VERTEX += gerstner_wave(wave2,wave2_dir,global_vertex,TANGENT,BINORMAL); + VERTEX += gerstner_wave(wave3,wave3_dir,global_vertex,TANGENT,BINORMAL); + vertex_y = VERTEX.y; + NORMAL = normalize(cross(BINORMAL,TANGENT)); +} + +void fragment() +{ + ALPHA = 1.0; + SPECULAR = 0.4; + ROUGHNESS = 0.05; + + 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 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); + float murky = murkiness * ROUGHNESS; + murky *= submersion; + 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); +} -- cgit v1.2.3 From dfc6b2b98755fa583286395c2bf6a08b30bfa56f Mon Sep 17 00:00:00 2001 From: Malfurious Date: Wed, 14 Sep 2022 04:38:20 -0400 Subject: Extend water area to 10km x 10km Horizon LOS in water should be typically around 5km, so extend the dimentions of the water to allow 5km in any direction. With regard to the wave shader, we extend the original MeshInstance to 2000x2000 using a reasonable tradeoff of virtex subdivisions. The remainder of the area is covered by another water plane, subdivided to a much lower level of detail. The area at the center of the large 'outer' water plane is subtracted so that the two do not overlap. Co-authored-by: dusoleil --- character/fps_controller/fps_controller.tscn | 1 + water/Water.tscn | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/character/fps_controller/fps_controller.tscn b/character/fps_controller/fps_controller.tscn index c0009d5..d187121 100644 --- a/character/fps_controller/fps_controller.tscn +++ b/character/fps_controller/fps_controller.tscn @@ -14,6 +14,7 @@ shape = SubResource( 1 ) [node name="Camera" type="Camera" parent="."] transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.796127, 0 ) +far = 5000.0 [node name="RayCast" type="RayCast" parent="Camera"] cast_to = Vector3( 0, 0, -3 ) diff --git a/water/Water.tscn b/water/Water.tscn index 0cf4a50..3724e82 100644 --- a/water/Water.tscn +++ b/water/Water.tscn @@ -1,10 +1,10 @@ -[gd_scene load_steps=7 format=2] +[gd_scene load_steps=9 format=2] [ext_resource path="res://water/Wave.gdshader" type="Shader" id=1] [ext_resource path="res://water/Water.gd" type="Script" id=2] [sub_resource type="PlaneMesh" id=9] -size = Vector2( 500, 500 ) +size = Vector2( 2000, 2000 ) subdivide_width = 500 subdivide_depth = 500 @@ -29,6 +29,14 @@ shader_param/foam_amount = 0.6 shader_param/murkiness = 4.0 shader_param/noise = SubResource( 18 ) +[sub_resource type="PlaneMesh" id=19] +size = Vector2( 10000, 10000 ) +subdivide_width = 100 +subdivide_depth = 100 + +[sub_resource type="PlaneMesh" id=20] +size = Vector2( 2000, 2000 ) + [node name="Water" type="Spatial"] [node name="MeshInstance" type="MeshInstance" parent="."] @@ -44,3 +52,11 @@ wave2_direction = Vector2( 1, 0.6 ) wave3_steepness = 0.25 wave3_wavelength = 18.0 wave3_direction = Vector2( 1, 1.3 ) + +[node name="CSGMesh" type="CSGMesh" parent="."] +mesh = SubResource( 19 ) +material = SubResource( 11 ) + +[node name="CSGMesh" type="CSGMesh" parent="CSGMesh"] +operation = 2 +mesh = SubResource( 20 ) -- cgit v1.2.3 From 36d2b133c9bd922574299720cd274003b84b8cf8 Mon Sep 17 00:00:00 2001 From: Malfurious Date: Wed, 14 Sep 2022 04:48:19 -0400 Subject: Update ground floor to match size of the detailed water area --- Main.tscn | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Main.tscn b/Main.tscn index 1c1c29b..a6c3420 100644 --- a/Main.tscn +++ b/Main.tscn @@ -16,10 +16,10 @@ albedo_texture = ExtResource( 2 ) uv1_triplanar = true [sub_resource type="BoxShape" id=19] -extents = Vector3( 250, 1, 250 ) +extents = Vector3( 1000, 0.1, 1000 ) [sub_resource type="PlaneMesh" id=20] -size = Vector2( 500, 500 ) +size = Vector2( 2000, 2000 ) [sub_resource type="SpatialMaterial" id=21] albedo_color = Color( 0.941176, 0.658824, 0.215686, 1 ) -- cgit v1.2.3