W6: Water and swimming
The simulation has a global sea level (Simulation.sea_level, changed with set_sea_level and read every tick, so the tide can move it mid-match). Walkers wade more slowly in water over WADE_DEPTH; in water SWIM_DEPTH deep they swim: floating head-out, sprint-swimming, diving with crouch, kicking out with jump, and standing again in the shallows. Falling into deep water does no fall damage. Water tracks breath, stamina and drowning in whole ticks: swimming spends stamina, an exhausted swimmer slips under, and with no breath left the tide phase's drown damage lands once a second. All of it, plus the sea level, is in the state hash. WaterView draws the sea surface at the simulation's level with a procedural shader: depth-tinted colour, foam at the waterline, ripples. Closes #13 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
// Tidewall sea: one transparent surface at the simulation's sea level. Colour deepens
|
||||
// with the water's thickness, read from the depth buffer, so shallows over sand show
|
||||
// green-grey and open water turns slate blue. A pale band of foam marks the waterline,
|
||||
// where the tide is about to reach. Small procedural ripples move the normals. No
|
||||
// textures: every pattern is generated here.
|
||||
shader_type spatial;
|
||||
render_mode blend_mix, depth_draw_always, cull_disabled, specular_schlick_ggx;
|
||||
|
||||
uniform sampler2D depth_texture : hint_depth_texture, filter_linear;
|
||||
uniform vec3 shallow_colour : source_color = vec3(0.42, 0.55, 0.50);
|
||||
uniform vec3 deep_colour : source_color = vec3(0.10, 0.22, 0.30);
|
||||
uniform vec3 foam_colour : source_color = vec3(0.86, 0.88, 0.84);
|
||||
// Metres of water over which the colour goes from shallow to deep.
|
||||
uniform float depth_scale = 7.0;
|
||||
// Width of the foam at the waterline, in metres of water thickness.
|
||||
uniform float foam_width = 0.6;
|
||||
uniform float ripple_strength = 0.25;
|
||||
|
||||
varying vec3 world_position;
|
||||
|
||||
float hash(vec2 p) {
|
||||
p = fract(p * vec2(123.34, 456.21));
|
||||
p += dot(p, p + 45.32);
|
||||
return fract(p.x * p.y);
|
||||
}
|
||||
|
||||
float value_noise(vec2 p) {
|
||||
vec2 i = floor(p);
|
||||
vec2 f = fract(p);
|
||||
vec2 u = f * f * (3.0 - 2.0 * f);
|
||||
return mix(mix(hash(i), hash(i + vec2(1.0, 0.0)), u.x),
|
||||
mix(hash(i + vec2(0.0, 1.0)), hash(i + vec2(1.0, 1.0)), u.x), u.y);
|
||||
}
|
||||
|
||||
float waves(vec2 p, float t) {
|
||||
return value_noise(p * 0.45 + vec2(t * 0.11, t * 0.07)) * 0.6
|
||||
+ value_noise(p * 1.3 - vec2(t * 0.19, -t * 0.05)) * 0.4;
|
||||
}
|
||||
|
||||
void vertex() {
|
||||
world_position = (MODEL_MATRIX * vec4(VERTEX, 1.0)).xyz;
|
||||
}
|
||||
|
||||
void fragment() {
|
||||
vec2 p = world_position.xz;
|
||||
float t = TIME;
|
||||
|
||||
// Thickness of water between this surface and whatever is behind it.
|
||||
float scene_depth = texture(depth_texture, SCREEN_UV).r;
|
||||
vec4 behind = INV_PROJECTION_MATRIX * vec4(SCREEN_UV * 2.0 - 1.0, scene_depth, 1.0);
|
||||
behind.xyz /= behind.w;
|
||||
float thickness = max(-behind.z - (-VERTEX.z), 0.0);
|
||||
// Convert view distance to roughly vertical metres using the view angle.
|
||||
float vertical = thickness * clamp(abs(normalize(VERTEX).y) + 0.25, 0.25, 1.0);
|
||||
|
||||
float deep = 1.0 - exp(-vertical / depth_scale);
|
||||
vec3 water = mix(shallow_colour, deep_colour, deep);
|
||||
|
||||
float e = 0.35;
|
||||
float h = waves(p, t);
|
||||
float hx = waves(p + vec2(e, 0.0), t) - h;
|
||||
float hz = waves(p + vec2(0.0, e), t) - h;
|
||||
// Ripples fade with distance, so the far sea doesn't shimmer into noise.
|
||||
float ripple = ripple_strength * exp(-length(VERTEX) / 140.0);
|
||||
vec3 n = normalize(vec3(-hx * ripple / e, 1.0, -hz * ripple / e));
|
||||
NORMAL = normalize((VIEW_MATRIX * vec4(n, 0.0)).xyz);
|
||||
|
||||
float foam_edge = 1.0 - smoothstep(0.0, foam_width, vertical + (h - 0.5) * 0.4);
|
||||
ALBEDO = mix(water, foam_colour, foam_edge * 0.85);
|
||||
ROUGHNESS = mix(0.12, 0.6, foam_edge);
|
||||
METALLIC = 0.0;
|
||||
SPECULAR = 0.5;
|
||||
ALPHA = clamp(mix(0.55, 0.96, deep) + foam_edge * 0.3, 0.0, 1.0);
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://b7qhmjym78vlr
|
||||
@@ -0,0 +1,58 @@
|
||||
## Draws the sea: one flat surface at the simulation's sea level, wide enough to reach
|
||||
## past the horizon from anywhere on the island. `follow(sim)` moves it whenever the
|
||||
## tide changes the level, so what players see is always the level the server uses.
|
||||
## The surface is the only thing here; drowning and swimming live in the simulation.
|
||||
class_name WaterView
|
||||
extends Node3D
|
||||
|
||||
const SHADER: Shader = preload("res://src/client/water/water.gdshader")
|
||||
## Side of the surface, in metres, centred on the world.
|
||||
const SIZE: float = 6000.0
|
||||
## Grid steps across the surface. It is flat, so few are needed.
|
||||
const SUBDIVISIONS: int = 32
|
||||
|
||||
var level: float = 0.0
|
||||
|
||||
var _surface: MeshInstance3D
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
if _surface == null:
|
||||
_build()
|
||||
|
||||
|
||||
## Places the surface at `sea_level`. Cheap; safe to call every frame.
|
||||
func set_level(sea_level: float) -> void:
|
||||
if _surface == null:
|
||||
_build()
|
||||
level = sea_level
|
||||
_surface.position.y = sea_level
|
||||
|
||||
|
||||
## Matches the surface to the simulation's current sea level.
|
||||
func follow(sim: Simulation) -> void:
|
||||
if not is_equal_approx(sim.sea_level, level) or _surface == null:
|
||||
set_level(sim.sea_level)
|
||||
|
||||
|
||||
func surface() -> MeshInstance3D:
|
||||
if _surface == null:
|
||||
_build()
|
||||
return _surface
|
||||
|
||||
|
||||
func _build() -> void:
|
||||
var mesh: PlaneMesh = PlaneMesh.new()
|
||||
mesh.size = Vector2(SIZE, SIZE)
|
||||
mesh.subdivide_width = SUBDIVISIONS
|
||||
mesh.subdivide_depth = SUBDIVISIONS
|
||||
var material: ShaderMaterial = ShaderMaterial.new()
|
||||
material.shader = SHADER
|
||||
mesh.material = material
|
||||
_surface = MeshInstance3D.new()
|
||||
_surface.name = "Sea"
|
||||
_surface.mesh = mesh
|
||||
_surface.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
|
||||
var centre: float = Tuning.WORLD_SIZE_METRES * 0.5
|
||||
_surface.position = Vector3(centre, level, centre)
|
||||
add_child(_surface)
|
||||
@@ -0,0 +1 @@
|
||||
uid://fkpqxmr813lf
|
||||
Reference in New Issue
Block a user