diff options
Diffstat (limited to '')
-rw-r--r-- | Main.tscn | 41 | ||||
-rw-r--r-- | Water.gd | 81 | ||||
-rw-r--r-- | Wave.gd | 48 | ||||
-rw-r--r-- | Wave.gdshader | 102 | ||||
-rw-r--r-- | project.godot | 10 |
5 files changed, 275 insertions, 7 deletions
@@ -1,7 +1,9 @@ -[gd_scene load_steps=14 format=2] +[gd_scene load_steps=18 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.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 ) @@ -18,11 +20,26 @@ size = Vector2( 500, 500 ) subdivide_width = 500 subdivide_depth = 500 -[sub_resource type="SpatialMaterial" id=22] -flags_transparent = true -params_cull_mode = 2 -params_depth_draw_mode = 1 -albedo_color = Color( 0.0509804, 0.792157, 0.807843, 0.67451 ) +[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, 1080.08 ) +shader_param/wave1_dir = Vector2( 1, 1 ) +shader_param/wave2 = Quat( 0.25, 0.202683, 1.23345, 776.359 ) +shader_param/wave2_dir = Vector2( 1, 0.6 ) +shader_param/wave3 = Quat( 0.25, 0.349066, 0.716197, 591.586 ) +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( 500, 1, 500 ) @@ -66,7 +83,17 @@ transform = Transform( 0.863334, 0.0994421, -0.494738, 0, 0.980392, 0.197058, 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( 22 ) +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="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 new file mode 100644 index 0000000..f9463e4 --- /dev/null +++ b/Water.gd @@ -0,0 +1,81 @@ +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 + @@ -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/Wave.gdshader b/Wave.gdshader new file mode 100644 index 0000000..ff04afd --- /dev/null +++ b/Wave.gdshader @@ -0,0 +1,102 @@ +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; + VERTEX = global_vertex; + 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 5208f67..75d4b3d 100644 --- a/project.godot +++ b/project.godot @@ -8,6 +8,16 @@ config_version=4 +_global_script_classes=[ { +"base": "Resource", +"class": "Wave", +"language": "GDScript", +"path": "res://Wave.gd" +} ] +_global_script_class_icons={ +"Wave": "" +} + [application] config/name="Game Jam 49" |