A6: Sea, sky and weather
Weather.at(tide, tick) derives the weather from the tide's schedule alone, so every client sees the same sky with nothing new on the wire: the storm builds from calm at the first tide to a gale at sudden death, and the light runs from mid-afternoon to dusk at the time limit. WeatherView applies it each sync to the sky, sun, fog, ambient light, the sea's roughness and rain around the camera (CPUParticles3D, for both renderers). Under water the view closes into SEA_MURK, the rain stops and the surface is a rippled ceiling seen from below. The sea shader darkens open water and adds chop and whitecaps with the storm, and keeps a breathing foam band at the waterline with a fainter line beyond. In the Compatibility renderer, whose depth texture the shader can't read (the sea came out all foam), water depth comes from a texture of the terrain's heights instead. Sea colours move into Palette; the rain and wind beds follow the weather. Tests: test_weather (7). make check passes (611 tests). Captures: make sea-captures (every tide phase, calm beach, the last land in the gale, under water, and three in the Compatibility renderer). Closes #46 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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
|
||||
@@ -0,0 +1 @@
|
||||
uid://31fwehvb62uw
|
||||
@@ -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
|
||||
@@ -0,0 +1 @@
|
||||
uid://cn5bdl46khp2e
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user