FERRETGame engine over Babylon.js
Ferret

@ferret/waves

19 exports · source in packages/waves/src

Classes

DeckWater class

class DeckWater
readonly nx: number
readonly nz: number
readonly cell: number
readonly depth: Float32Arraydepth of the water in each cell, metres (row-major, x fastest)
readonly wet: Float32Arrayhow wet each cell is, 0 dry .. 1 wet; stays after the water has gone and dries by the wind and sun
shipped: numbercumulative volumes, m3: taken aboard over the rail, shed through scuppers
shed: number
dried: numberevaporated or dried, m3
constructor(readonly spec: DeckSpec)
get volume(): numberthe volume of water on the deck, m3
get mass(): numberits mass, kg
get cog(): V3its centre in the body frame, m (the deck centre when there is no water)
depthAt(x: number, z: number): numberthe depth at a body-frame point (x, z), metres
wetAt(x: number, z: number): numberhow wet the deck is at a body-frame point, 0..1
load(f: Pick<DeckFrame, 'toWorld'>): { force: V3; at: V3 }the weight of the deck water as a force and where it acts, in the world: hand it to Vessel.applyForce each step
step(dt: number, f: DeckFrame, o: { dryRate?: number } = {}): voiddt seconds: the sea over the rail comes aboard, the water on the deck runs under geff, leaves by the scuppers, and the deck dries

packages/waves/src/deck.ts:57

SeaFromFlow class

class SeaFromFlow

A sea that follows the wind of a @ferret/flow field. update(t) reads the wind and returns a new list of waves when the wind has changed enough (a front arrives, a storm builds) and null while the sea stays as it is; hand the list to WaterSurfaceView.setWaves. The result is a pure function of the field and the time, so every peer that updates with the same times gets the same sea. The wave phases jump when the list changes, so keep the steps large rather than small.

waves: Wave[]
constructor(private field: FlowField, o: SeaFromFlowOptions = {})
wind(t: number): { speed: number; dir: number }the wind now: speed m/s and the direction the waves go, degrees (0 = +x)
update(t: number): Wave[] | null

packages/waves/src/index.ts:153

Spray class

class Spray

Spray: drops thrown where the hull meets the sea at speed (the bow, the flare, a wave slamming the side), carried by the wind and falling back. Deterministic: the drops come from a hash of the seed and a counter, so peers that make the same calls see the same spray.

readonly drops: Drop[]
constructor(private o: { seed?: number; max?: number; wind?: V3 } = {})
emit(p: SprayPoint, sea: (x: number, z: number) => number, water: (x: number, z: number) => V3, dt: number, scale = 1): numberThrow spray from p for dt seconds. sea gives the surface height and water the water's own velocity there (orbital motion of the waves, current). The rate grows with the square of the speed of the hull through the water and falls to nothing when the point is clear of the sea or slower than 3 m/s.
setWind(w: V3): voidthe wind that carries the drops, m/s (world)
step(dt: number, sea?: (x: number, z: number) => number): voidfall under gravity and drag against the wind; drops die when their life is out or they hit the sea

packages/waves/src/deck.ts:178

Wake class

class Wake

The trail of ships on the water: white water that spreads and fades behind each hull. A ship calls add every frame with its position, speed and beam; foam(x, z, t) reads 0..1 for the picture. The trail widens at the Kelvin angle (about 19.5 degrees) and fades over life seconds. A pure function of what was added, so peers that add the same samples see the same wake. push also stirs a WaterSurface of

constructor(private o: { life?: number; spacing?: number; max?: number } = {})
get life(): number
add(x: number, z: number, t: number, speed: number, width = 4): voida hull at (x, z) moving at speed m/s, width m across; samples closer than spacing m to the last are dropped
prune(t: number): voiddrop what has faded; call now and then
get samples(): number
foam(x: number, z: number, t: number): numberwhite water at a point, 0..1
push(surface: { disturb(x: number, z: number, volume: number, radius?: number): void }, x: number, z: number, speed: number, width: number, dt: number): voidpush the hull's displacement into a harbour surface: the volume a hull of this size moving at speed sweeps aside in dt seconds

packages/waves/src/index.ts:192

WaveField class

class WaveField
constructor(waves: Wave[], private shoal?: AmplitudeFn)
get waves(): number
displacement(x: number, z: number, t: number): V3the move of the rest point (x, z) at time t: sideways (x, z) and up (y), as the shader of buildWater does it
height(x: number, z: number, t: number, iterations = 4): numberthe height of the surface above the rest level over the world point (x, z): the rest point whose move lands there is found first
normal(x: number, z: number, t: number): V3the unit normal of the surface at the rest point (x, z)
velocity(x: number, z: number, t: number): V3the velocity of the water particles at the surface (orbital motion), m/s: what carries a floating thing and rocks a boat
foam(x: number, z: number, t: number): numberHow much the crest breaks here, 0 (smooth) .. 1 (white water): the Jacobian of the sideways move is 1 on a calm surface and falls towards zero where the water piles up; foam comes where it falls below about 0.6.

packages/waves/src/index.ts:68

Functions

bodyGravity function

function bodyGravity(q: [number, number, number, number], accel: V3 = [0, 0, 0]): V3

the effective gravity in the body frame: world gravity minus the acceleration of the deck, rotated into the body by the conjugate of q (x, y, z, w)

packages/waves/src/deck.ts:51

seaScale function

function seaScale(wind: number): { peak: number; hs: number }

The peak wavelength (m) and the significant wave height (m) of a fully developed sea at a wind speed (m/s): Pierson-Moskowitz.

packages/waves/src/index.ts:30

seaState function

function seaState(wind: number, windDir = 0, o: SeaOptions = {}): Wave[]

The waves of a sea driven by a wind of wind m/s from the direction windDir (degrees, 0 = +x: where the waves go). The components are sampled from the Pierson-Moskowitz spectrum, log-spaced from short chop to the long waves, each with the amplitude the spectrum gives it and a direction spread around the wind; the result is a deterministic function of its arguments. Calm wind gives a flat sea.

packages/waves/src/index.ts:40

surfaceFn function

function surfaceFn(field: WaveField, level = 0, local?: { height(x: number, z: number): number; level?: number }): (x: number, z: number, t: number) => number

The height function a ship rides (Environment.surface of

packages/waves/src/index.ts:180

Interfaces

DeckFrame interface

interface DeckFrame

what the deck needs of the ship each step

toWorld: (p: V3) => V3a point in the body frame to the world
geff: V3gravity less the deck's acceleration, expressed in the body frame, m/s2 (use bodyGravity)
sea: (x: number, z: number, t: number) => numberthe height of the sea at a world point and time
t: number

packages/waves/src/deck.ts:40

DeckSpec interface

interface DeckSpec
length: numberdeck length along body x and width across body z, metres; the deck is centred on the body origin in x and z
width: number
deckY: numberthe deck's height in the body frame (body y), metres
rail: numberthe rail (bulwark) height above the deck, metres; water tops it where the sea stands higher than deck + rail
scuppers?: Scupper[]openings that let the deck water go over the side (default: one per side every 6 m)
cell?: numbercell edge, metres (default 1)
density?: numberwater density, kg/m3 (default 1025)
discharge?: numberdischarge coefficient of the rail weir and of the scuppers (default 0.6)
ingress?: numberhow much of the sea that tops the rail comes aboard, 0..1 (default 1): a flared bow throws most of it off

packages/waves/src/deck.ts:19

Drop interface

interface Drop
pos: V3
vel: V3
life: number
size: number

packages/waves/src/deck.ts:172

Scupper interface

interface Scupper
x: numberalong the deck (body x), m
side: -1 | 1side: -1 port, +1 starboard (across the deck, body z)
width: numberwidth of the opening, m

packages/waves/src/deck.ts:10

SeaFromFlowOptions interface

interface SeaFromFlowOptions extends SeaOptions
at?: [number, number]where (x, z) the wind is read: the middle of the water
height?: numberheight of the reading above the surface, m (the standard 10 m wind)
speedStep?: numberthe sea is made again only when the wind has changed this much: m/s ...
turnStep?: number... or turned this much, degrees. A real sea answers a changing wind slowly, so small changes leave it as it is.

packages/waves/src/index.ts:136

SeaOptions interface

interface SeaOptions
count?: numberhow many wave components (the shader takes up to 16)
seed?: number
spread?: numberhow widely the waves spread around the wind, degrees either side (a young sea is wide, a swell narrow)
sharpness?: number0..1 how sharp the crests are; the sum is held below the point where a crest loops over
fetch?: numberthe fetch (m): the stretch of open water the wind has blown over. Given, the sea is JONSWAP's fetch-limited one (a gale over a channel or a sea makes shorter, steeper waves than over an ocean); omitted, Pierson-Moskowitz's fully developed one

packages/waves/src/index.ts:16

SprayPoint interface

interface SprayPoint
pos: V3a point of the hull in the splash zone: world position and velocity of the ship there
vel: V3

packages/waves/src/deck.ts:166

Wave interface

interface Wave

the same shape as WaterWave of

wavelength: number
amplitude: number
steepness?: number
direction?: number

packages/waves/src/index.ts:11

Types

AmplitudeFn type

type AmplitudeFn = (x: number, z: number, amplitude: number, wavelength: number) => number

what a wave's amplitude becomes at the rest point (x, z): shoaling in shallow water, breaking, a lee; the same function the picture's shader uses, so the ship and the water agree

packages/waves/src/index.ts:66

V3 type

type V3 = [number, number, number]

Waves on open water, in the style of

packages/waves/src/index.ts:9