diff --git a/CLAUDE.md b/CLAUDE.md index 4fb61cf..798d4e9 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -48,6 +48,7 @@ continuity. | `make models` | Rebuilds every item model (`assets/models/items/*.obj`, with grip data in `.json`) and icon (`assets/icons/items/*.png`) from `tools/models/items.py` in headless Blender on the CPU. Materials are named after `Palette` colours; `ItemModels` paints them. | | `make art` | Rebuilds every generated model: the player figure (`make figure`: `assets/models/figure/`, from `tools/models/figure.py`, about 3 s) and the items with their icons (`make models`). | | `make figure-captures` | Renders the player figure in every movement state, every action, and close up holding a carbine (`captures/figure_*.png`, about 1.5 min). Needs Docker. Look at them. | +| `make sea-captures` | Renders the sea after every tide phase from out at sea, a calm beach, the last land in the gale and the view under water (`captures/sea_*.png`), and three of them in the Compatibility renderer (`*_compat.png`), about 6 min. Needs Docker. Look at them. | | `make item-captures` | Renders items held in a row, over the shoulder, lying on the ground and the four tiers (`captures/items_*.png`, about 3 min). Needs Docker. Look at them. | | `make audio` | Regenerates every sound and the score's layers in `assets/audio/` from `tools/audio/` (about 6 s), with `manifest.json`. Commit the WAVs it changes. | | `make audio-check` | Checks each file in `assets/audio/` matches its manifest hash and its loudness target, and the mix order (tide loudest). Also run by `test_audio_assets`, which regenerates everything and compares. | diff --git a/Makefile b/Makefile index b9fb3c4..bf58d54 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # Tidewall build entry points. Everything runs headless with the repo-local engine. GODOT := tools/godot.sh -.PHONY: art figure figure-captures models item-captures audio audio-check check import parse test test-selfcheck smoke capture-test terrain-capture binaries verify-engine bot-matches match match-captures client-check client-captures hud-captures style-captures menu-captures chart-captures discovery-check fuzz soak soak-selfcheck server-package-check +.PHONY: art figure figure-captures sea-captures models item-captures audio audio-check check import parse test test-selfcheck smoke capture-test terrain-capture binaries verify-engine bot-matches match match-captures client-check client-captures hud-captures style-captures menu-captures chart-captures discovery-check fuzz soak soak-selfcheck server-package-check ## A8: regenerates every sound in assets/audio/ from tools/audio/ (about 6 s). audio: import @@ -29,6 +29,13 @@ figure: figure-captures: import @for stage in states actions close; do tools/capture.sh figure_$$stage captures/figure_$$stage.png; done +## A6: the sea at every tide phase from out at sea, a calm beach, the last land in the +## gale and the view under water (captures/sea_*.png, about 6 min), and three of them +## again in the Compatibility renderer (*_compat.png). Needs Docker. Look at them. +sea-captures: import + @for stage in tide_0 tide_1 tide_2 tide_3 tide_4 tide_5 tide_6 tide_7 shore_calm shore_gale underwater; do tools/capture.sh sea_$$stage captures/sea_$$stage.png; done + @for stage in tide_2 shore_gale underwater; do CAPTURE_RENDERER=compatibility tools/capture.sh sea_$$stage captures/sea_$${stage}_compat.png; done + ## A4: items held in the hand, over the shoulder, lying on the ground and by tier ## (captures/items_*.png, about 3 min). Needs Docker. Look at them. item-captures: import diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 48dc150..567cc11 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -620,3 +620,26 @@ is a piece placed. Being in the air waits `AIR_DELAY` before it shows, so steps slope don't flicker. `EntityView.animate_live = false` lets captures pose figures exactly. +## Sea, sky and weather (A6) + +`Weather.at(tide, tick)` works out the moment's weather from the tide's schedule alone, +so every client agrees with nothing new on the wire: `storm` builds from calm at the +first tide to a gale at sudden death, and `day` runs from mid-afternoon to dusk at the +time limit. Sky colours, the sun's height, colour and strength, fog, rain and wind all +follow from those two (colours from `Palette`). Before the tide starts it's a clear +afternoon. + +`WeatherView`, made by `MatchView`, applies a Weather on every sync: the procedural +sky's colours, the sun, the environment's fog and ambient light, `WaterView`'s +`sea_state`, and rain as `CPUParticles3D` around the camera, which works in both +renderers. When the camera is below the drawn sea (`WaterView.is_under`), the fog +turns to `SEA_MURK` and closes in, and the rain stops. `ClientSession` passes the rain +and wind to `SoundDirector`'s rain and wind beds. + +The sea shader darkens open water toward `SEA_STORM`, lengthens and steepens the chop +and raises whitecaps with `sea_state`, and keeps a breathing foam band at the +waterline with a fainter second line beyond it. Water depth comes from the depth +buffer in Forward+ (which also sees pieces and props) and from a texture of the +terrain's heights (`WaterView.set_ground`) in the Compatibility renderer, whose depth +texture the shader can't read. Seen from below, the surface is drawn as a rippled +ceiling of murk. diff --git a/docs/STYLE.md b/docs/STYLE.md index 3b0a578..498a8de 100644 --- a/docs/STYLE.md +++ b/docs/STYLE.md @@ -65,6 +65,16 @@ or `src/app`, and on any palette colour this table doesn't list. Values are sRGB | `SKY_HAZE` | 0.82, 0.78, 0.68 | Salt-hazed horizon | | `SKY_BELOW` | 0.18, 0.20, 0.20 | Below the horizon | | `SEA_FOG` | 0.78, 0.78, 0.74 | Distance fog | +| `STORM_SKY` | 0.36, 0.40, 0.44 | The gale's low cloud overhead | +| `STORM_HAZE` | 0.58, 0.60, 0.60 | The gale's horizon | +| `DUSK_HAZE` | 0.86, 0.66, 0.50 | The dusk horizon late in a match | +| `DUSK_SUN` | 1.0, 0.78, 0.56 | The low evening sun's light | +| `RAIN` | 0.80, 0.84, 0.86, 22% | Rain streaks | +| `SEA_SHALLOW` | 0.42, 0.55, 0.50 | Water over sand | +| `SEA_DEEP` | 0.10, 0.22, 0.30 | Open water | +| `SEA_STORM` | 0.13, 0.20, 0.22 | Open water in the gale | +| `SEA_FOAM` | 0.86, 0.88, 0.84 | Surf and whitecaps | +| `SEA_MURK` | 0.12, 0.28, 0.30 | The view under water | | `INK` | 0.16, 0.13, 0.11 | Printer's ink: outlines, hatching | | `PAPER` | 0.91, 0.87, 0.78 | Unbleached paper: HUD panels and text | | `PAPER_DIM` | 0.66, 0.62, 0.54 | Paper in shadow: panel edges, dimmed text | @@ -75,8 +85,25 @@ or `src/app`, and on any palette colour this table doesn't list. Values are sRGB | `WARNING` | 0.86, 0.32, 0.22 | HUD danger: the tide's next line, low health | | `SEA_INK` | 0.20, 0.34, 0.42 | The sea on the tide gauge and map | -The terrain shader's sand, marsh, grass, rock and summit colours and the sea shader's -shallow, deep and foam colours are shader uniforms. They follow the same rules. +The terrain shader's sand, marsh, grass, rock and summit colours are shader uniforms +and follow the same rules. The sea shader's colours are set from the `SEA_` entries +above by `WaterView`. + +## Weather and light (A6) + +A match runs from a clear salt-hazed afternoon to a gale at dusk, so the sky itself +tells the time. `Weather` works it out from the tide's schedule alone, the same on +every client: + +- **The storm** builds from calm at the first tide to a full gale at sudden death: + cloud lowers (`STORM_SKY`, `STORM_HAZE`), the haze thickens, the sea darkens to + `SEA_STORM` with whitecaps and heavier chop, and rain starts once it's past half. +- **The light** moves from mid-afternoon to dusk over the whole match: the sun drops + toward the sea, warms to `DUSK_SUN` and the horizon to `DUSK_HAZE`, and the storm + dims it further. +- **The shoreline** always has a foam line where the water is thin, breathing with + the swell, so the waterline reads at a glance in every phase. +- **Under water** the view closes in to `SEA_MURK` within a few metres. ## Shading: the woodcut print pass diff --git a/src/app/client_session.gd b/src/app/client_session.gd index 5b4117c..d9aea97 100644 --- a/src/app/client_session.gd +++ b/src/app/client_session.gd @@ -151,6 +151,9 @@ func _ready() -> void: func _process(delta: float) -> void: pump(delta) if sound != null: + if view != null: + sound.rain = view.weather.weather.rain() + sound.gale = view.weather.weather.wind() sound.update(game, mirror.own_id, view.camera.global_position if view != null else Vector3.ZERO, delta) if _status != null: _status.text = status_text() diff --git a/src/audio/sound_director.gd b/src/audio/sound_director.gd index 839621a..d374e11 100644 --- a/src/audio/sound_director.gd +++ b/src/audio/sound_director.gd @@ -53,6 +53,8 @@ var played: int = 0 var audible: bool = true ## Rain under the ambience, for weather (A6) to turn on: 0 to 1. var rain: float = 0.0 +## The wind's strength from the weather (A6), 0.25 to 1: the wind bed never drops below it. +var gale: float = 0.0 var _world: Simulation = null var _fx_seen: int = 0 @@ -331,6 +333,7 @@ func _hear_ambience(world: Simulation, own_id: int, listener: Vector3, _delta: f var me: SimEntity = world.entities.find(own_id) if own_id != 0 else null if me != null and (me.drop_state == Drop.FREEFALL or me.drop_state == Drop.GLIDING): wind = 1.0 + wind = maxf(wind, gale) _set_ambience(surf, wind, rain) diff --git a/src/client/match_view.gd b/src/client/match_view.gd index ceb9b28..2e3f910 100644 --- a/src/client/match_view.gd +++ b/src/client/match_view.gd @@ -20,6 +20,8 @@ var rig: CameraRig = CameraRig.new() ## The woodcut print pass over the camera (Look.add_print_pass). var print_pass: MeshInstance3D var sun: DirectionalLight3D +## The sky, light, haze, rain and murk of the moment (A6). +var weather: WeatherView ## Applies the player's graphics settings: the sun's shadows and the print pass. @@ -55,6 +57,11 @@ func setup(simulation: Simulation, local_id: int = 0) -> void: add_child(terrain) water = WaterView.new() add_child(water) + water.set_ground(sim.ground) + weather = WeatherView.new() + weather.name = "Weather" + add_child(weather) + weather.setup(world.environment, sun, water) pois = PoiView.new() pois.setup(sim.pois) add_child(pois) @@ -74,6 +81,7 @@ func setup(simulation: Simulation, local_id: int = 0) -> void: ## Matches everything drawn to the simulation as it is now. func sync() -> void: water.follow(sim) + weather.apply(Weather.at(sim.tide, sim.tick), camera.transform.origin) Look.set_sea_level(print_pass, water.level) props.sync() structures.sync() diff --git a/src/client/style/palette.gd b/src/client/style/palette.gd index adb451e..9aff2a4 100644 --- a/src/client/style/palette.gd +++ b/src/client/style/palette.gd @@ -72,6 +72,23 @@ const SKY_TOP: Color = Color(0.32, 0.50, 0.68) const SKY_HAZE: Color = Color(0.82, 0.78, 0.68) const SKY_BELOW: Color = Color(0.18, 0.20, 0.20) const SEA_FOG: Color = Color(0.78, 0.78, 0.74) +## Weather (A6): the gale's low cloud overhead and on the horizon, the dusk haze and the +## low sun's warm light, as the match wears on and the storm builds. +const STORM_SKY: Color = Color(0.36, 0.40, 0.44) +const STORM_HAZE: Color = Color(0.58, 0.60, 0.60) +const DUSK_HAZE: Color = Color(0.86, 0.66, 0.50) +const DUSK_SUN: Color = Color(1.0, 0.78, 0.56) +## Rain streaks, pale and mostly clear. +const RAIN: Color = Color(0.80, 0.84, 0.86, 0.22) + +# --- The sea (A6) ------------------------------------------------------------------- +## Water over sand, open water, the gale's darker open water and the foam. +const SEA_SHALLOW: Color = Color(0.42, 0.55, 0.50) +const SEA_DEEP: Color = Color(0.10, 0.22, 0.30) +const SEA_STORM: Color = Color(0.13, 0.20, 0.22) +const SEA_FOAM: Color = Color(0.86, 0.88, 0.84) +## Under the surface: the murk a swimmer sees. +const SEA_MURK: Color = Color(0.12, 0.28, 0.30) # --- Print and paper (the woodcut pass and the HUD) --------------------------------- ## Printer's ink: outlines and hatching, warm, never pure black. diff --git a/src/client/style/weather.gd b/src/client/style/weather.gd new file mode 100644 index 0000000..8c8ff7e --- /dev/null +++ b/src/client/style/weather.gd @@ -0,0 +1,95 @@ +## The weather and light at a moment of a match (A6), worked out from the tide's +## schedule alone, so every client sees the same sky without anything more on the +## wire: the tide and the tick are already replicated. +## +## - `storm`, 0 to 1: calm when the first tide starts, building through every phase +## to a full gale at sudden death. +## - `day`, 0 to 1: mid-afternoon at the first tide to dusk at the time limit. +## +## Everything else (sky, sun, fog, rain, wind, the sea's chop) follows from those two. +## Before the tide starts (the lobby and the drop) it is a clear afternoon. See +## docs/STYLE.md, "Weather and light". +class_name Weather +extends RefCounted + +## The sun's height above the horizon, in degrees, at the start and at dusk. +const SUN_HIGH: float = 38.0 +const SUN_LOW: float = 7.0 +## The sun's bearing: from over the sea to the south-west. +const SUN_BEARING: float = -130.0 +const SUN_ENERGY: float = 0.95 +## Fog density in clear air and in the full gale. +const FOG_CLEAR: float = 0.0005 +## Kept light enough to see across the island: the haze is mood, not cover. +const FOG_GALE: float = 0.0011 +## Under water: how thick the murk is. +const FOG_UNDER: float = 0.09 +## The storm's strength at which rain begins, and when it's at its heaviest. +const RAIN_FROM: float = 0.45 +const RAIN_FULL: float = 0.9 + +var storm: float = 0.0 +var day: float = 0.0 + + +## The weather for a tide (null before it starts) at a simulation tick. +static func at(tide: Tide, tick: int) -> Weather: + var made: Weather = Weather.new() + if tide == null: + return made + var elapsed: float = float(tick - tide.started_tick) + var gale_at: float = maxf(float(tide.sudden_death_offset()), 1.0) + var dusk_at: float = maxf(float(tide.time_limit_offset()), 1.0) + # Builds slowly at first and faster toward the end. + var progress: float = clampf(elapsed / gale_at, 0.0, 1.0) + made.storm = progress * progress * (3.0 - 2.0 * progress) + made.day = clampf(elapsed / dusk_at, 0.0, 1.0) + return made + + +## Rain, 0 to 1. +func rain() -> float: + return smoothstep(RAIN_FROM, RAIN_FULL, storm) + + +## Wind, 0.25 in calm air to 1 in the gale. +func wind() -> float: + return lerpf(0.25, 1.0, storm) + + +## The sun's height above the horizon, in degrees. +func sun_elevation() -> float: + return lerpf(SUN_HIGH, SUN_LOW, day) + + +func sun_colour() -> Color: + return Color.WHITE.lerp(Palette.DUSK_SUN, smoothstep(0.35, 1.0, day)) + + +func sun_energy() -> float: + return SUN_ENERGY * lerpf(1.0, 0.7, day) * lerpf(1.0, 0.55, storm) + + +func sky_top() -> Color: + return Palette.SKY_TOP.lerp(Palette.STORM_SKY, storm) + + +func sky_horizon() -> Color: + return Palette.SKY_HAZE.lerp(Palette.DUSK_HAZE, smoothstep(0.4, 1.0, day) * (1.0 - storm * 0.6)).lerp(Palette.STORM_HAZE, storm * 0.8) + + +func fog_colour() -> Color: + return Palette.SEA_FOG.lerp(Palette.STORM_HAZE, storm) + + +func fog_density() -> float: + return lerpf(FOG_CLEAR, FOG_GALE, storm) + + +func ambient_energy() -> float: + return lerpf(0.45, 0.38, storm) + + +## How rough the sea is, for the sea shader: 0 a calm swell, 1 a gale's chop. +func sea_state() -> float: + return storm diff --git a/src/client/style/weather.gd.uid b/src/client/style/weather.gd.uid new file mode 100644 index 0000000..5eb9442 --- /dev/null +++ b/src/client/style/weather.gd.uid @@ -0,0 +1 @@ +uid://31fwehvb62uw diff --git a/src/client/style/weather_view.gd b/src/client/style/weather_view.gd new file mode 100644 index 0000000..ad85831 --- /dev/null +++ b/src/client/style/weather_view.gd @@ -0,0 +1,103 @@ +## Puts a Weather on the screen (A6): the sky's colours, the sun's height, colour and +## strength, the haze, the sea's roughness, rain around the camera, and the murk when +## the camera is under water. MatchView calls apply() as it syncs; everything it +## changes is drawing only. +class_name WeatherView +extends Node3D + +## Rain falls in a box this wide around the camera, from this high above it. +const RAIN_BOX: Vector3 = Vector3(36.0, 2.0, 36.0) +const RAIN_HEIGHT: float = 14.0 +const RAIN_DROPS: int = 900 +const RAIN_SPEED: float = 22.0 + +var environment: Environment +var sun: DirectionalLight3D +var water: WaterView +## The weather last applied. +var weather: Weather = Weather.new() +var underwater: bool = false + +var _sky: ProceduralSkyMaterial +var _rain: CPUParticles3D +var _rain_paint: StandardMaterial3D + + +func setup(env: Environment, light: DirectionalLight3D, sea: WaterView) -> void: + environment = env + sun = light + water = sea + _sky = environment.sky.sky_material as ProceduralSkyMaterial if environment.sky != null else null + _rain = _build_rain() + add_child(_rain) + apply(weather, Vector3.ZERO) + + +## The rain's particles, for tests. +func rain() -> CPUParticles3D: + return _rain + + +## Shows `now` around a camera at `eye`. +func apply(now: Weather, eye: Vector3) -> void: + weather = now + if sun != null: + sun.rotation_degrees = Vector3(-now.sun_elevation(), Weather.SUN_BEARING, 0.0) + sun.light_color = now.sun_colour() + sun.light_energy = now.sun_energy() + if _sky != null: + _sky.sky_top_color = now.sky_top() + _sky.sky_horizon_color = now.sky_horizon() + _sky.ground_horizon_color = now.sky_horizon() + if water != null: + water.set_sea_state(now.sea_state()) + underwater = water != null and water.is_under(eye) + if environment != null: + environment.ambient_light_energy = now.ambient_energy() + if underwater: + environment.fog_light_color = Palette.SEA_MURK + environment.fog_density = Weather.FOG_UNDER + environment.fog_aerial_perspective = 0.0 + environment.fog_sky_affect = 1.0 + else: + environment.fog_light_color = now.fog_colour() + environment.fog_density = now.fog_density() + environment.fog_aerial_perspective = 0.4 + environment.fog_sky_affect = 1.0 + var wet: float = 0.0 if underwater else now.rain() + _rain.emitting = wet > 0.02 + _rain.visible = wet > 0.02 + _rain_paint.albedo_color = Color(Palette.RAIN, Palette.RAIN.a * wet) + _rain.position = eye + Vector3(0.0, RAIN_HEIGHT, 0.0) + # Wind leans the rain. + _rain.direction = Vector3(0.35 * now.wind(), -1.0, 0.15 * now.wind()).normalized() + + +func _build_rain() -> CPUParticles3D: + var drop: QuadMesh = QuadMesh.new() + drop.size = Vector2(0.012, 0.55) + _rain_paint = StandardMaterial3D.new() + _rain_paint.shading_mode = BaseMaterial3D.SHADING_MODE_UNSHADED + _rain_paint.transparency = BaseMaterial3D.TRANSPARENCY_ALPHA + _rain_paint.billboard_mode = BaseMaterial3D.BILLBOARD_FIXED_Y + _rain_paint.billboard_keep_scale = true + _rain_paint.albedo_color = Palette.RAIN + drop.material = _rain_paint + var particles: CPUParticles3D = CPUParticles3D.new() + particles.name = "Rain" + particles.mesh = drop + particles.amount = RAIN_DROPS + particles.lifetime = (RAIN_HEIGHT + 4.0) / RAIN_SPEED + particles.local_coords = false + particles.emission_shape = CPUParticles3D.EMISSION_SHAPE_BOX + particles.emission_box_extents = RAIN_BOX * 0.5 + particles.direction = Vector3.DOWN + particles.spread = 3.0 + particles.gravity = Vector3.ZERO + particles.initial_velocity_min = RAIN_SPEED * 0.9 + particles.initial_velocity_max = RAIN_SPEED * 1.1 + particles.preprocess = particles.lifetime + particles.emitting = false + particles.visible = false + particles.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF + return particles diff --git a/src/client/style/weather_view.gd.uid b/src/client/style/weather_view.gd.uid new file mode 100644 index 0000000..ab88f3f --- /dev/null +++ b/src/client/style/weather_view.gd.uid @@ -0,0 +1 @@ +uid://cn5bdl46khp2e diff --git a/src/client/water/water.gdshader b/src/client/water/water.gdshader index 3b7edd8..3e0352c 100644 --- a/src/client/water/water.gdshader +++ b/src/client/water/water.gdshader @@ -1,8 +1,10 @@ // 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. +// green-grey and open water turns slate blue. A band of foam marks the waterline, +// breathing in and out with the swell, with a second, fainter line just off it, so +// the shore reads at every tide. `sea_state` (A6, from Weather) darkens open water, +// lengthens and steepens the chop and raises whitecaps as the storm builds. 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; @@ -15,12 +17,24 @@ 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; +// 0 calm to 1 a full gale. +uniform float sea_state = 0.0; +uniform vec3 storm_colour : source_color = vec3(0.13, 0.20, 0.22); +uniform vec3 murk_colour : source_color = vec3(0.12, 0.28, 0.30); // Where the flood reaches: one texel per flood cell, white where flooded, covering // [0, flood_extent] metres on both axes. Dry cells inside it aren't drawn; outside it // is open sea. uniform bool use_flood_mask = false; uniform sampler2D flood_mask : filter_nearest, repeat_disable; uniform float flood_extent = 1024.0; +// How thick the water is: from the depth buffer (true), which also catches built pieces +// and props, or from the terrain's heights (false), for the Compatibility renderer, +// whose depth texture this can't read. Samples are at texel centres: sample i is +// i * terrain_cell metres from the origin. +uniform bool use_depth = true; +uniform sampler2D terrain_heights : filter_linear, repeat_disable; +uniform float terrain_cell = 1.0; +uniform float terrain_samples = 2.0; varying vec3 world_position; @@ -57,30 +71,59 @@ void fragment() { } } - // 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 vertical; + if (use_depth) { + // 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. + vertical = thickness * clamp(abs(normalize(VERTEX).y) + 0.25, 0.25, 1.0); + } else { + vec2 grid = (p / terrain_cell + 0.5) / terrain_samples; + float ground_height = texture(terrain_heights, clamp(grid, vec2(0.0), vec2(1.0))).r; + vertical = max(world_position.y - ground_height, 0.0); + } float deep = 1.0 - exp(-vertical / depth_scale); - vec3 water = mix(shallow_colour, deep_colour, deep); + vec3 open_water = mix(deep_colour, storm_colour, sea_state); + vec3 water = mix(shallow_colour, open_water, deep); + // The chop: longer, steeper waves as the storm builds, drifting with the wind. 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; + float scale = mix(1.0, 0.55, sea_state); + vec2 drift = vec2(t * mix(0.3, 1.4, sea_state), t * mix(0.1, 0.5, sea_state)); + vec2 q = p * scale + drift; + float h = waves(q, t); + float hx = waves(q + vec2(e, 0.0), t) - h; + float hz = waves(q + 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); + float ripple = ripple_strength * mix(1.0, 3.2, sea_state) * exp(-length(VERTEX) / mix(140.0, 260.0, sea_state)); 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); + // The shoreline: a foam band that breathes with the swell, and a fainter line + // beyond it where the last wave broke. + float swell = 0.5 + 0.5 * sin(t * 0.9 + value_noise(p * 0.05) * 6.283); + float width = foam_width * mix(1.0, 1.9, sea_state) * (0.75 + 0.5 * swell); + float foam_edge = 1.0 - smoothstep(0.0, width, vertical + (h - 0.5) * 0.4); + float second = width * 2.2 + 0.3; + float line = 1.0 - smoothstep(0.0, 0.12 + 0.1 * sea_state, abs(vertical - second + (h - 0.5) * 0.3)); + line *= step(0.45, value_noise(p * 0.7 + vec2(t * 0.2, 0.0))); + // Whitecaps on open water once the wind gets up. + float caps = smoothstep(0.78 - 0.18 * sea_state, 0.9, waves(q * 1.7 + 11.0, t * 1.3)) * smoothstep(0.2, 0.7, sea_state) * deep; + float foam = clamp(foam_edge * 0.85 + line * 0.5 + caps * 0.7, 0.0, 1.0); + ALBEDO = mix(water, foam_colour, foam); + ROUGHNESS = mix(mix(0.12, 0.3, sea_state), 0.6, foam); METALLIC = 0.0; SPECULAR = 0.5; - ALPHA = clamp(mix(0.55, 0.96, deep) + foam_edge * 0.3, 0.0, 1.0); + ALPHA = clamp(mix(0.55, 0.96, deep) + foam * 0.3, 0.0, 1.0); + if (!FRONT_FACING) { + // The surface seen from under water: a bright, rippled ceiling of murk. + ALBEDO = mix(murk_colour, shallow_colour, 0.35 + 0.3 * h); + NORMAL = -NORMAL; + ROUGHNESS = 0.4; + ALPHA = 0.88; + } } diff --git a/src/client/water/water_view.gd b/src/client/water/water_view.gd index bcf0523..106dd7a 100644 --- a/src/client/water/water_view.gd +++ b/src/client/water/water_view.gd @@ -5,6 +5,8 @@ ## a mask texture of the flooded cells goes to the shader, which leaves dry cells ## (a basin behind a breakwater) bare. The mask is rebuilt only when the flood pours. ## The surface is the only thing here; drowning and swimming live in the simulation. +## Its colours come from Palette's SEA_ entries, and set_sea_state() (A6, from Weather) +## roughens it as the storm builds. class_name WaterView extends Node3D @@ -15,6 +17,7 @@ const SIZE: float = 6000.0 const SUBDIVISIONS: int = 32 var level: float = 0.0 +var sea_state: float = 0.0 var _surface: MeshInstance3D var _material: ShaderMaterial @@ -71,6 +74,47 @@ func set_flood(flood: Flood) -> void: _material.set_shader_parameter("use_flood_mask", true) +## Gives the shader the terrain's heights, so it can tell how deep the water is where +## the renderer's depth texture can't be read (Compatibility). Forward+ keeps using +## the depth buffer, which also sees built pieces and props. +func set_ground(ground: SimGround) -> void: + if _surface == null: + _build() + if ground == null or ground.samples < 2: + return + var image: Image = Image.create_from_data(ground.samples, ground.samples, false, Image.FORMAT_RF, ground.heights.to_byte_array()) + _material.set_shader_parameter("terrain_heights", ImageTexture.create_from_image(image)) + _material.set_shader_parameter("terrain_cell", ground.cell) + _material.set_shader_parameter("terrain_samples", float(ground.samples)) + + +## Whether the shader reads water depth from the depth buffer. +static func depth_readable() -> bool: + return RenderingServer.get_current_rendering_method() != "gl_compatibility" + + +## How rough the sea is drawn: 0 calm to 1 a full gale. +func set_sea_state(state: float) -> void: + if _surface == null: + _build() + state = clampf(state, 0.0, 1.0) + if is_equal_approx(state, sea_state): + return + sea_state = state + _material.set_shader_parameter("sea_state", state) + + +## True if `point` is under the sea as drawn: below the level where the sea shows. +func is_under(point: Vector3) -> bool: + return point.y < level and shows_water_at(point.x, point.z) + + +func material() -> ShaderMaterial: + if _surface == null: + _build() + return _material + + ## True if the sea is drawn over the world point (x, z): flooded, or off the grid. func shows_water_at(x: float, z: float) -> bool: if _mask_flood == null: @@ -94,6 +138,13 @@ func _build() -> void: mesh.subdivide_depth = SUBDIVISIONS _material = ShaderMaterial.new() _material.shader = SHADER + _material.set_shader_parameter("shallow_colour", Palette.SEA_SHALLOW) + _material.set_shader_parameter("deep_colour", Palette.SEA_DEEP) + _material.set_shader_parameter("storm_colour", Palette.SEA_STORM) + _material.set_shader_parameter("foam_colour", Palette.SEA_FOAM) + _material.set_shader_parameter("murk_colour", Palette.SEA_MURK) + _material.set_shader_parameter("sea_state", sea_state) + _material.set_shader_parameter("use_depth", depth_readable()) mesh.material = _material _surface = MeshInstance3D.new() _surface.name = "Sea" diff --git a/test/capture/sea_scene.gd b/test/capture/sea_scene.gd new file mode 100644 index 0000000..c826add --- /dev/null +++ b/test/capture/sea_scene.gd @@ -0,0 +1,106 @@ +## Capture scenario for the sea, sky and weather (A6), on seed 2's island in the real +## MatchView, with the tide set as the server would have it: +## +## - tide_0: the first tide holding, a clear afternoon. tide_1 to tide_7: just after +## each phase's rise, the storm building and the light lowering. All from one camera +## out at sea, looking up one slope, so the climbing waterline is compared directly. +## - shore_calm and shore_gale: standing on the beach at the first tide, and on the +## last land after the fifth rise, looking along the waterline: foam, chop, rain and +## the lowering light. (Seed 2's highest summit goes under at the sixth rise, so +## tide_6 and tide_7 show only sea over where the island was.) +## - underwater: below the surface just off that beach, looking up the slope. +extends Node3D + +@export var stage: String = "tide_0" +@export var match_seed: int = 2 + +var sim: Simulation +var view: MatchView + + +func _ready() -> void: + sim = Simulation.new(match_seed) + sim.start_tide() + var levels: PackedFloat32Array = sim.tide.levels + var after_rise: int = 0 + if stage.begins_with("tide_"): + after_rise = stage.trim_prefix("tide_").to_int() + elif stage == "shore_gale": + # The last land: on most islands the next rise covers the highest summit. + after_rise = Tide.phase_count() - 2 + var elapsed: int = _elapsed_after(after_rise) + sim.tide = Tide.new(sim.tick - elapsed, levels) + sim.set_sea_level(sim.tide.level_at(sim.tick)) + + var shore: Array = _shore(sim.sea_level) + var edge: Vector3 = shore[0] + var inland: Vector3 = shore[1] + view = MatchView.new() + view.setup(sim, 0) + add_child(view) + view.sync() + if stage.begins_with("tide_"): + # Out at sea, looking in at this tide's waterline and the land above it. + var eye: Vector3 = edge - inland * 150.0 + Vector3(0.0, 55.0, 0.0) + var target: Vector3 = edge + inland * 70.0 + target.y = sim.ground.terrain_height(target.x, target.z) + view.look_from(eye, target) + elif stage == "underwater": + # Out from the waterline until the water is a few metres deep. + var under: Vector3 = edge + for _step: int in 200: + if sim.ground.terrain_height(under.x, under.z) < sim.sea_level - 4.0: + break + under -= inland * 2.0 + under.y = sim.sea_level - 1.8 + var look: Vector3 = edge + inland * 20.0 + look.y = sim.ground.terrain_height(look.x, look.z) + 1.0 + view.look_from(under, look) + else: + # On the beach, looking along the waterline and out a little. + var along: Vector3 = inland.cross(Vector3.UP).normalized() + var stand: Vector3 = edge + inland * 5.0 + stand.y = sim.ground.terrain_height(stand.x, stand.z) + 1.7 + var look: Vector3 = edge + along * 45.0 - inland * 30.0 + look.y = sim.sea_level + view.look_from(stand, look) + view.sync() + view.settle() + + +## Ticks from the tide's start to two seconds after phase `rises` - 1's rise ends, or +## into the first hold for 0. +func _elapsed_after(rises: int) -> int: + var at: float = 0.0 + if rises == 0: + return roundi(20.0 * Tuning.TICK_RATE_HZ) + for index: int in rises: + var phase: Dictionary = Tuning.TIDE_PHASES[index] + at += float(phase["hold"]) + float(phase["rise"]) + return roundi((at + 2.0) * Tuning.TICK_RATE_HZ) + + +## [where the sea at `level` meets the land, the unit direction inland from there], +## on the bearing from the highest summit whose shore rises most gently. +func _shore(level: float) -> Array: + var summit: Vector3 = sim.island.summits[0] + var best: Array = [] + var best_run: float = 0.0 + for bearing: int in 24: + var out: Vector3 = Vector3(cos(TAU * bearing / 24.0), 0.0, sin(TAU * bearing / 24.0)) + var at: Vector3 = summit + var step: int = 0 + # Down to the sea itself, not a dry hollow below its level. + while step < 500 and (sim.ground.terrain_height(at.x, at.z) > level or (sim.flood != null and not sim.flood.is_flooded(at.x, at.z))): + at += out * 2.0 + step += 1 + if step >= 500 or step == 0: + continue + # How far inland before the land is 12 m above the water: longer is gentler. + var run: float = 0.0 + while run < 300.0 and sim.ground.terrain_height(at.x - out.x * run, at.z - out.z * run) < level + 12.0: + run += 2.0 + if run > best_run: + best_run = run + best = [Vector3(at.x, level, at.z), -out] + return best if not best.is_empty() else [Vector3(summit.x, level, summit.z), Vector3.FORWARD] diff --git a/test/capture/sea_scene.gd.uid b/test/capture/sea_scene.gd.uid new file mode 100644 index 0000000..7e8e6e5 --- /dev/null +++ b/test/capture/sea_scene.gd.uid @@ -0,0 +1 @@ +uid://ujp18lcuhtt5 diff --git a/test/capture/sea_shore_calm.tscn b/test/capture/sea_shore_calm.tscn new file mode 100644 index 0000000..13ad063 --- /dev/null +++ b/test/capture/sea_shore_calm.tscn @@ -0,0 +1,7 @@ +[gd_scene load_steps=2 format=3] + +[ext_resource type="Script" path="res://test/capture/sea_scene.gd" id="1"] + +[node name="CaptureSea" type="Node3D"] +script = ExtResource("1") +stage = "shore_calm" diff --git a/test/capture/sea_shore_gale.tscn b/test/capture/sea_shore_gale.tscn new file mode 100644 index 0000000..d62299b --- /dev/null +++ b/test/capture/sea_shore_gale.tscn @@ -0,0 +1,7 @@ +[gd_scene load_steps=2 format=3] + +[ext_resource type="Script" path="res://test/capture/sea_scene.gd" id="1"] + +[node name="CaptureSea" type="Node3D"] +script = ExtResource("1") +stage = "shore_gale" diff --git a/test/capture/sea_tide_0.tscn b/test/capture/sea_tide_0.tscn new file mode 100644 index 0000000..2f5ae9f --- /dev/null +++ b/test/capture/sea_tide_0.tscn @@ -0,0 +1,7 @@ +[gd_scene load_steps=2 format=3] + +[ext_resource type="Script" path="res://test/capture/sea_scene.gd" id="1"] + +[node name="CaptureSea" type="Node3D"] +script = ExtResource("1") +stage = "tide_0" diff --git a/test/capture/sea_tide_1.tscn b/test/capture/sea_tide_1.tscn new file mode 100644 index 0000000..38868fb --- /dev/null +++ b/test/capture/sea_tide_1.tscn @@ -0,0 +1,7 @@ +[gd_scene load_steps=2 format=3] + +[ext_resource type="Script" path="res://test/capture/sea_scene.gd" id="1"] + +[node name="CaptureSea" type="Node3D"] +script = ExtResource("1") +stage = "tide_1" diff --git a/test/capture/sea_tide_2.tscn b/test/capture/sea_tide_2.tscn new file mode 100644 index 0000000..439b75b --- /dev/null +++ b/test/capture/sea_tide_2.tscn @@ -0,0 +1,7 @@ +[gd_scene load_steps=2 format=3] + +[ext_resource type="Script" path="res://test/capture/sea_scene.gd" id="1"] + +[node name="CaptureSea" type="Node3D"] +script = ExtResource("1") +stage = "tide_2" diff --git a/test/capture/sea_tide_3.tscn b/test/capture/sea_tide_3.tscn new file mode 100644 index 0000000..6acad4f --- /dev/null +++ b/test/capture/sea_tide_3.tscn @@ -0,0 +1,7 @@ +[gd_scene load_steps=2 format=3] + +[ext_resource type="Script" path="res://test/capture/sea_scene.gd" id="1"] + +[node name="CaptureSea" type="Node3D"] +script = ExtResource("1") +stage = "tide_3" diff --git a/test/capture/sea_tide_4.tscn b/test/capture/sea_tide_4.tscn new file mode 100644 index 0000000..59466ea --- /dev/null +++ b/test/capture/sea_tide_4.tscn @@ -0,0 +1,7 @@ +[gd_scene load_steps=2 format=3] + +[ext_resource type="Script" path="res://test/capture/sea_scene.gd" id="1"] + +[node name="CaptureSea" type="Node3D"] +script = ExtResource("1") +stage = "tide_4" diff --git a/test/capture/sea_tide_5.tscn b/test/capture/sea_tide_5.tscn new file mode 100644 index 0000000..c37c2ce --- /dev/null +++ b/test/capture/sea_tide_5.tscn @@ -0,0 +1,7 @@ +[gd_scene load_steps=2 format=3] + +[ext_resource type="Script" path="res://test/capture/sea_scene.gd" id="1"] + +[node name="CaptureSea" type="Node3D"] +script = ExtResource("1") +stage = "tide_5" diff --git a/test/capture/sea_tide_6.tscn b/test/capture/sea_tide_6.tscn new file mode 100644 index 0000000..03c5f54 --- /dev/null +++ b/test/capture/sea_tide_6.tscn @@ -0,0 +1,7 @@ +[gd_scene load_steps=2 format=3] + +[ext_resource type="Script" path="res://test/capture/sea_scene.gd" id="1"] + +[node name="CaptureSea" type="Node3D"] +script = ExtResource("1") +stage = "tide_6" diff --git a/test/capture/sea_tide_7.tscn b/test/capture/sea_tide_7.tscn new file mode 100644 index 0000000..8a4ecc0 --- /dev/null +++ b/test/capture/sea_tide_7.tscn @@ -0,0 +1,7 @@ +[gd_scene load_steps=2 format=3] + +[ext_resource type="Script" path="res://test/capture/sea_scene.gd" id="1"] + +[node name="CaptureSea" type="Node3D"] +script = ExtResource("1") +stage = "tide_7" diff --git a/test/capture/sea_underwater.tscn b/test/capture/sea_underwater.tscn new file mode 100644 index 0000000..ef0bc72 --- /dev/null +++ b/test/capture/sea_underwater.tscn @@ -0,0 +1,7 @@ +[gd_scene load_steps=2 format=3] + +[ext_resource type="Script" path="res://test/capture/sea_scene.gd" id="1"] + +[node name="CaptureSea" type="Node3D"] +script = ExtResource("1") +stage = "underwater" diff --git a/test/client/test_weather.gd b/test/client/test_weather.gd new file mode 100644 index 0000000..ed32ab3 --- /dev/null +++ b/test/client/test_weather.gd @@ -0,0 +1,113 @@ +extends TestCase + +## A6: the weather and light follow the tide's schedule (replicated state only), and +## the match view shows them: sky, sun, haze, the sea's roughness, rain and the view +## under water. + + +func test_before_the_tide_it_is_a_clear_afternoon() -> void: + var calm: Weather = Weather.at(null, 500) + assert_eq(calm.storm, 0.0) + assert_eq(calm.day, 0.0) + assert_eq(calm.rain(), 0.0) + assert_eq(calm.sky_top(), Palette.SKY_TOP) + assert_eq(calm.sky_horizon(), Palette.SKY_HAZE) + assert_near(calm.fog_density(), Weather.FOG_CLEAR, 0.000001) + assert_near(calm.sun_elevation(), Weather.SUN_HIGH, 0.001) + + +func test_the_storm_builds_through_every_phase_to_a_gale_at_sudden_death() -> void: + var tide: Tide = Tide.new(1000) + var last: float = -1.0 + var at: int = 1000 + var step: int = maxi(tide.sudden_death_offset() / 40, 1) + while at <= 1000 + tide.sudden_death_offset(): + var now: Weather = Weather.at(tide, at) + assert_true(now.storm >= last, "never eases: %f after %f" % [now.storm, last]) + last = now.storm + at += step + assert_near(Weather.at(tide, 1000 + tide.sudden_death_offset()).storm, 1.0, 0.0001) + assert_near(Weather.at(tide, 1000 + tide.time_limit_offset()).storm, 1.0, 0.0001, "still a gale in sudden death") + assert_eq(Weather.at(tide, 1000).storm, 0.0, "calm at the first tide") + var gale: Weather = Weather.at(tide, 1000 + tide.sudden_death_offset()) + assert_eq(gale.rain(), 1.0) + assert_near(gale.wind(), 1.0, 0.0001) + assert_eq(gale.sky_top(), Palette.STORM_SKY) + assert_true(gale.fog_density() > Weather.FOG_CLEAR * 1.5) + assert_true(gale.fog_density() <= 0.0012, "haze is mood, not cover") + var halfway: Weather = Weather.at(tide, 1000 + tide.sudden_death_offset() / 4) + assert_eq(halfway.rain(), 0.0, "no rain early on") + + +func test_the_light_lowers_and_warms_to_dusk() -> void: + var tide: Tide = Tide.new(0) + var noon: Weather = Weather.at(tide, 0) + var dusk: Weather = Weather.at(tide, tide.time_limit_offset()) + assert_near(dusk.day, 1.0, 0.0001) + assert_near(dusk.sun_elevation(), Weather.SUN_LOW, 0.001) + assert_eq(dusk.sun_colour(), Palette.DUSK_SUN) + assert_eq(noon.sun_colour(), Color.WHITE) + assert_true(dusk.sun_energy() < noon.sun_energy() * 0.5, "dusk in a gale is dim") + assert_true(dusk.sun_elevation() > 0.0, "the sun never sets inside a match") + + +func test_every_client_sees_the_same_weather_from_the_same_tick() -> void: + var one: Weather = Weather.at(Tide.new(300), 9000) + var two: Weather = Weather.at(Tide.new(300), 9000) + assert_eq(one.storm, two.storm) + assert_eq(one.day, two.day) + assert_eq(one.sky_horizon(), two.sky_horizon()) + + +func test_the_match_view_shows_the_weather() -> void: + var sim: Simulation = Simulation.new(3, SimGround.flat(-20.0)) + var view: MatchView = MatchView.new() + view.setup(sim, 0) + var sky: ProceduralSkyMaterial = view.weather.environment.sky.sky_material as ProceduralSkyMaterial + assert_eq(sky.sky_top_color, Palette.SKY_TOP) + assert_false(view.weather.rain().emitting, "no rain in the lobby") + sim.tide = Tide.new(-Tide.new(0).sudden_death_offset()) + view.sync() + assert_eq(sky.sky_top_color, Palette.STORM_SKY) + assert_near(view.water.sea_state, 1.0, 0.0001) + assert_near(float(view.water.material().get_shader_parameter("sea_state")), 1.0, 0.0001) + assert_true(view.weather.rain().emitting, "rain in the gale") + assert_near(view.sun.rotation_degrees.x, -Weather.at(sim.tide, sim.tick).sun_elevation(), 0.001) + assert_near(view.weather.environment.fog_density, Weather.at(sim.tide, sim.tick).fog_density(), 0.000001) + view.free() + + +func test_under_water_the_view_closes_in_and_the_rain_stops() -> void: + var sim: Simulation = Simulation.new(3, SimGround.flat(-20.0)) + sim.tide = Tide.new(-Tide.new(0).sudden_death_offset()) + var view: MatchView = MatchView.new() + view.setup(sim, 0) + view.look_from(Vector3(500.0, sim.sea_level - 2.0, 500.0), Vector3(510.0, sim.sea_level - 2.0, 500.0)) + view.sync() + assert_true(view.weather.underwater) + assert_eq(view.weather.environment.fog_light_color, Palette.SEA_MURK) + assert_near(view.weather.environment.fog_density, Weather.FOG_UNDER, 0.000001) + assert_false(view.weather.rain().emitting, "no rain under water") + view.look_from(Vector3(500.0, sim.sea_level + 2.0, 500.0), Vector3(510.0, sim.sea_level, 500.0)) + view.sync() + assert_false(view.weather.underwater) + assert_ne(view.weather.environment.fog_light_color, Palette.SEA_MURK) + view.free() + + +func test_the_sea_takes_its_colours_from_the_palette_and_a_heightmap() -> void: + var view: WaterView = WaterView.new() + var material: ShaderMaterial = view.material() + assert_eq(material.get_shader_parameter("deep_colour"), Palette.SEA_DEEP) + assert_eq(material.get_shader_parameter("storm_colour"), Palette.SEA_STORM) + assert_eq(material.get_shader_parameter("murk_colour"), Palette.SEA_MURK) + var island: Island = Island.generate(2) + view.set_ground(island.ground) + var heights: Texture2D = material.get_shader_parameter("terrain_heights") + assert_not_null(heights) + assert_eq(heights.get_width(), island.ground.samples) + assert_near(float(material.get_shader_parameter("terrain_cell")), island.ground.cell, 0.0001) + view.set_level(5.0) + assert_true(view.is_under(Vector3(1.0, 4.0, 1.0))) + assert_false(view.is_under(Vector3(1.0, 6.0, 1.0))) + view.free() diff --git a/test/client/test_weather.gd.uid b/test/client/test_weather.gd.uid new file mode 100644 index 0000000..91d8e13 --- /dev/null +++ b/test/client/test_weather.gd.uid @@ -0,0 +1 @@ +uid://dpo814u5wxlnb