Repository navigation
Water
A WaterSurface draws a moving sea: a grid pushed around by up to eight Gerstner waves, with foam on the crests, ripples, and reflections of the sky. Buoyancy floats nodes on it. The waves you see and the waves scripts sample come from the same numbers, so a ship sits on the crest that is drawn under it.
The design is in ADR 0072. This guide is about using it.
In the editor, choose Create > Water. You get a node with a WaterSurface and a preset of four waves: a long swell, two crossing seas and a short chop. In code:
import { GameObject, WaterSurface } from "myco";
const sea = GameObject.create("Sea");
const water = sea.addBehavior(WaterSurface); // starts with the same four-wave preset
water.size = 200;The grid is a square of size units centred on the node, in the node's XZ plane. Keep the node level: move it and turn it about Y, but don't pitch or roll it (sampling assumes the water's up is the world's). The waves continue past the grid's edge for sampling, but only the grid is drawn.
| Field | What it does |
|---|---|
size |
Side of the square, in the node's units. |
resolution |
Vertices per side (2–1024, default 128). Tiers whose shader level is "low" draw half. Short waves need enough vertices: at least four per wavelength looks right. |
waves |
Up to 8 waves. Each has direction (degrees around Y, 0 = +X, 90 = +Z), wavelength (crest to crest), steepness (0 flat to 1 sharp) and speed (0 uses the natural deep-water speed √(g·L/2π); a negative speed runs the wave backward). Edited as a list in the Inspector. |
waveScale |
Multiplies every wave's height (and its sideways motion). Raise it for a storm. |
deepColor, shallowColor
|
Looking straight down and in troughs you see the deep colour; at grazing angles and on crests, the shallow one. |
foamColor, foamAmount
|
Foam forms where the surface is squeezed together, which is where crests fold. 0 is none; 1 foams readily. |
normalDetail |
Strength of two layers of scrolling ripples on top of the waves. |
roughness, reflectivity
|
How blurry and how strong the Fresnel reflection of the sky and reflection probes is. |
opacity |
Below 1 the water is drawn transparent. |
interaction |
How much the water reacts to what is in it: "auto" (default: the graphics tier's water quality), "low", "medium", "high" or "off" (no wakes). See Wakes. |
farWakes |
Draw wakes beyond the wake field as trails (default on). |
depthMap |
The baked shore map (a .waterdepth file): how deep the water is under each point and where the shore runs. Made by Bake shoreline (see Shorelines); empty is open water. |
shoreResolution |
Texels a side when the shore map is baked (default 256: 0.4 m on a 100 m surface). |
splashEffect, impactEffect
|
The particle effects played where something falls in and where waves strike a wall or a hull. Empty (default): the engine's own. See Splashes and spray. |
Choosing waves: a wave's height is steepness × wavelength / 2π. The steepnesses of all waves should add up to well under 1, or crests fold over themselves and loop. Spread directions over 90° or so, and use wavelengths that aren't multiples of each other, so the pattern doesn't visibly repeat.
The water is lit like any other material: lights, received shadows, the World's Environment (and any ReflectionProbe whose box holds the water's centre), and fog. It doesn't cast shadows.
import { Water } from "myco";
const s = Water.sample(x, z); // the World's first enabled WaterSurface; null without one
if (s && s.height > y) {
// (x, y, z) is under water; s.normal is the surface's up there
}water.sample(x, z) asks one surface. Both return { height, normal } in world space, at the water clock's current time. Gerstner waves move water sideways as well as up, so the sampler finds the point of the drawn surface that is actually above (x, z).
Water.time is the water clock: the World's scaled time (so pausing time freezes the waves), or the server tick in a networked World.
Add Buoyancy to a node to float it on the World's first enabled surface. It samples the water at a few local points: by default the four corners of size (x is the width, y the length along local Z); set points for a different shape, such as more points along a long hull.
Physics mode (default) needs a dynamic RigidBody on the node. Each point under water pushes the body up with a force of depth × strength × mass × g / points, at that point, so the waves pitch and roll it. At strength 1 the body floats with its points one unit deep on average; at 2, half a unit. While under water, drag and angularDrag (per second, scaled by how much is submerged) slow it down, which stops it bobbing forever.
const box = GameObject.create("Crate");
box.addBehavior(RigidBody).mass = 20;
box.addBehavior(BoxCollider);
const b = box.addBehavior(Buoyancy);
b.size.set(1, 1);
b.strength = 2;Kinematic mode places the node on the waves without physics: its height becomes the mean water height at its points, and its pitch and roll follow the plane through them, eased by follow (per second; 0 snaps). The node's heading is kept. Use it for scripted ships and props.
submerged is the fraction of points under water at the last fixed step (always 1 in kinematic mode).
Buoyancy runs in the fixed step, after scripts' fixedUpdate and before 3D physics. It needs no renderer, so a dedicated server floats objects the same way the clients do.
Put a WaterWake on a moving node, such as a boat, and the water draws its wake in its own shader. The wake bends the surface's light and reflections and foams like the crests do, as strong as the node is fast. The component measures the node's speed itself, so no script has to feed it. The node's +Z is its bow.
| Field | What it does |
|---|---|
strength |
0–1, scales the whole wake. Set it to 0 (or disable the component) to hide the wake at once, for example while the boat is hidden or sunk. |
fullSpeed |
Speed (m/s) at which the wake is full size. Below 0.5 m/s there is none. |
width |
Width of the wake at the stern (m); the hull pressed into the field is a little over half as wide. |
life |
How long the far trail lasts behind the boat (s). |
bow, stern
|
Where the stem and the stern are along the node's +Z (m; the stern is usually negative). |
foam, ripples
|
0–1: how white the wake is, and how strongly the far trail's waves bend the surface. |
Wakes are drawn two ways by distance (ADR 0459), blended over the edge between them:
- Near the camera, a simulated wave height field around the point the camera looks at. Hulls press into it, and heap a ridge along each side of the bow (at the bow's flare) that rises and travels out as a V. The waves spread, cross and fade on their own. It also raises and lowers the surface.
- Beyond it, trails: foam, Kelvin ripples and a bow wave worked out per pixel along the path each stern took, for the wakes nearest the camera.
The graphics tier's water quality (Project settings > Graphics > Water), or the surface's interaction, sets the size (ADR 0663):
| low | medium | high | |
|---|---|---|---|
| Field | 128 cells over 64 m | 192 over 80 m | 256 over 96 m |
| CPU a frame (about) | 0.3 ms | 0.7 ms | 1.2 ms |
| Trails beyond the field | 8 | 12 | 16 |
Bow spray while a boat moves is not part of the wake: use a particle effect at the bow, as the pirates sample does. Spray from waves striking the hull is: see Splashes and spray. Wakes are only for the eye. Water.sample and buoyancy don't see them, and servers keep none.
Waves don't know about the land on their own: they keep their open-sea shape over a beach or against a cliff. A baked shore map tells the surface how deep the water is (ADR 0663).
To bake one, select the water node and press Bake shoreline under the WaterSurface in the Inspector. The editor casts a ray straight down at every texel of a shoreResolution × shoreResolution grid over the surface, against the scene's static meshes: MeshRenderers whose node (and the nodes above it) has no dynamic or kinematic RigidBody and no CharacterController. Decor rocks without colliders count; floating crates, ships and characters don't, and neither do sprites, particles or the water itself. A 256² bake casts 65 536 rays, a few thousand a frame, with a progress bar and Cancel.
- Rays start just above the highest crest the waves can reach. Anything the rays start inside, such as a cliff or a pier pile rising out of the water, is solid ground; the water under a bridge or an arch stays water.
- Where no static mesh is under a ray, the water is deep.
- The map is saved beside the scene as
<scene>.water/<node>.waterdepth(a scene atscenes/bay.scene.jsongetsscenes/bay.water/Sea.waterdepth), and the surface'sdepthMapis set to it in one undo step. Baking again overwrites the filedepthMapnames. Game exports ship it like any other project file.
The map remembers what it was baked from. Move a static mesh, change the surface's size, transform, waves or shoreResolution, and the Inspector says Shoreline out of date — bake again. It doesn't notice edits inside a mesh file: bake again by hand after changing a model.
Every World loads the map when its scene loads, the dedicated server included, so buoyancy floats things on the same shoaled waves that are drawn. A missing or broken file logs one warning and the surface behaves as open water.
The water throws two kinds of one-shot particle bursts by itself (ADR 0663 §3):
-
Splashes: anything that disturbs the water (a
Buoyancybody, a dynamic or kinematic collider, aWaterDisturber) splashes when it goes in falling faster than 1.5 m/s: a crown of droplets thrown up and out, and a little mist. Faster and bigger bodies splash bigger;WaterDisturber.splashscales it (0: none). -
Impact spray: a wave crest rising against an obstacle hard enough throws a tall sheet of spray up and out from it that breaks into droplets and mist. The obstacles are the walls of a baked shore map (steep shores, cliffs, piers; see Shorelines) within 60 m of the camera's focus, and the hull sides of every
WaterWakethere, and of everyWaterDisturberwithimpactSprayabove 0. AWaterWakethrows spray at full size unless its node also has aWaterDisturber, whoseimpactSpraythen scales it (0: none). Each point fires at most every 0.6 s. Calm water throws nothing; raisewaveScalefor a storm and spray flies along walls and up hull sides with no other setup.
Bursts scale with how hard the hit was (more particles, faster and bigger, within limits) and with the graphics tier's particle budget, like any particle effect. The water quality decides how many wall points are checked a frame (16, 32 or 64, taking turns) and at most 32 bursts play at once; interaction "off" turns it all off. Set splashEffect or impactEffect to a .particles.json of your own to replace the look: its +Y is thrown upward (for impacts, up and out from the obstacle, with X along the wall), and the effect's emission rates and bursts are scaled by the hit. Give it a number property named burstSize and it is set to the burst's size scale (0.6 to 1.8), for start sizes; the engine's own effects multiply their start sizes by it. Use world space for the systems, so gravity pulls straight down.
Two lifecycle hooks tell scripts about them, for a sound or an effect of their own. Define the method and the node hears them; nothing to subscribe:
-
onWaterSplash(e: WaterSplashEvent)on the node that fell in:e.position(where it went in, on the water),e.speed(downward, m/s),e.size(about 1 for a 1 m² body at 5 m/s) ande.surface. -
onWaterImpact(e: WaterImpactEvent)on the hull's node fore.kind"hull", and on the water's node for"wall":e.position,e.normal(out from the obstacle into the water),e.strength(how fast the water hit, m/s),e.waveHeight(m),e.sizeande.surface.
import { Audio, GameBehavior, Mathf, type WaterImpactEvent, type WaterSplashEvent } from "myco";
export class HullSounds extends GameBehavior {
onWaterImpact(e: WaterImpactEvent): void {
Audio.playOneShot("sfx/wave-slap.wav", { position: e.position, volume: Mathf.clamp(e.size, 0.2, 1), bus: "SFX" });
}
onWaterSplash(e: WaterSplashEvent): void {
Audio.playOneShot("sfx/splash.wav", { position: e.position, volume: Mathf.clamp(0.3 + e.size * 0.5, 0, 1) });
}
}They arrive in late update, after the scripts' own lateUpdate, on the frame the burst plays. The event object is reused: read it during the call and don't keep it. They are the same on every graphics tier, so a sound doesn't drop out on low settings.
They are visual feedback only. They fire only where water is drawn (never on a dedicated server), only near the camera, and aren't networked or deterministic: two players can hear different impacts. Never drive gameplay from them; use Buoyancy.submerged or physics triggers for that.
One field changes both the look and the physics:
water.waveScale = Mathf.lerp(water.waveScale, stormy ? 2.5 : 1, Time.deltaTime);Every World computes the same waves from the same clock: in a networked World the water clock is Time.network.serverTick / tickRate, so a server and its clients agree within a frame. Wave settings are scene data, the same everywhere. Fields you change at runtime, such as waveScale for a storm, aren't sent by themselves: keep the storm state in a replicated field of your own and set waveScale from it on every World.
- Gerstner waves repeat; eight waves hide it from a ship-game camera, not from close up.
- Shorelines need a bake (see Shorelines); meshes drawn through thin-instance batches (
instancing: "thin"materials) and models still loading aren't seen by it. No refraction, no underwater view. - Wakes don't move floating objects (sampling and buoyancy ignore them).
- One grid per surface (no camera-following rings for very large oceans).
- The water node must be level for sampling, and the water isn't picked by clicking in the Scene view (select it in the Hierarchy).
This wiki is generated from docs/wiki/ and the myco module's source on every push to master. Change pages there in a pull request: edits made here are not kept.
Basics
Gameplay
Rendering
Audio
- Playing sound
- Why is there no sound?
- Playlists
- Interactive music
- Sound generators
- Spatial audio
- Audio mixer
- Live mixing
- Loudness
- Audio profiler
- Audio takes
Reference