summaryrefslogtreecommitdiffstats
path: root/water
diff options
context:
space:
mode:
authorMalfurious <m@lfurio.us>2022-09-14 03:50:09 -0400
committerMalfurious <m@lfurio.us>2022-09-14 03:50:09 -0400
commit38b247487309ca89bf2d94bb321f12059861db9d (patch)
tree619a3acc15ca7051d5011b2ebaf18284ca1fad72 /water
parent0d930d5548e4f705cf80f9c2334b804ab1359538 (diff)
downloadgame_jam49-38b247487309ca89bf2d94bb321f12059861db9d.tar.gz
game_jam49-38b247487309ca89bf2d94bb321f12059861db9d.zip
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.
Diffstat (limited to 'water')
-rw-r--r--water/Water.gd113
-rw-r--r--water/Water.tscn46
-rw-r--r--water/Wave.gd48
-rw-r--r--water/Wave.gdshader101
4 files changed, 308 insertions, 0 deletions
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);
+}