Water Mesh
Updated
Real-time water surface with waves, foam, rain, reflections and caustics.
- Type
ent-water-mesh- Script class
WaterMeshEntity- Family
- Meshes
Overview
Section titled “Overview”A Water Mesh is a real-time water surface: a square plane of waves, whitecaps, raindrop rings and caustics that reflects the sky and the sun. You see the scene through it, the floor bent by the waves and fading into the water’s tint with depth. Use it for pools, ponds, lakes, rivers and oceans.
Its look comes from the required Water Mesh component (comp-water-mesh). Every prop belongs to its own plane: rain on one lake never rains on another. Waves always rise along the world’s up axis, whatever the plane’s rotation. The sun’s glint and the light through wave crests come from the scene’s first Directional Light. In the editor the sea moves only while a water plane is selected; in play it always moves.
Optional components complete it: a Water Volume lets the camera dive under the surface and reports when it does, and a Cache Mesh adds the plane to the shared water pool, where every cached plane is one draw. Objects foam where they meet the water only when they carry a Water Foam.
The props that matter most:
size: metres across the square plane.color: the tint deep water fades to.waveHeightandwaveLength: the height of the tallest waves and their typical length, in metres. Around 1.5 m of wavelength suits a pond, 10 m or more open sea.windDirection: where the wind blows the waves, in degrees around the up axis.foam,causticsandrain: amounts from 0 to 1 for whitecaps, caustic light on the floor and raindrop rings.clarity: metres of water through which the floor stays visible from above.
Set it up
Section titled “Set it up”- Add a Water Mesh entity to the scene (see Scenes and hierarchy) at the height of the water line.
- Set
sizeto cover the lake or sea, thencolor,waveHeightandwaveLengthfor its character. - Make sure the scene has a Directional Light, or the water shows no sun glint.
- Add a Water Volume if the camera can go under the surface, and a Signal Emitter to hear when it does.
- For several tiles of one sea, give them all the same
windDirectionandwaveLength, and a Cache Mesh each.
Examples
Section titled “Examples”Calm a pond at startup
Section titled “Calm a pond at startup”Goal: turn the default sea into a small, still pond with clear water.
#pragma lifecycle(startup)
function startup() { const pond = findEntity('Pond').getComponent(WaterMeshComponent); pond.setProps({ size: 12, color: '#1f5e4a', waveHeight: 0.05, waveLength: 1.5, foam: 0, clarity: 6, });}getComponent(WaterMeshComponent)returns the water’s component;setPropschanges only the keys passed.- Short, low waves (
waveLength1.5,waveHeight0.05) read as a pond rather than a sea. foam: 0removes the whitecaps, and a higherclaritylets the bottom show through more water.
Raise a storm
Section titled “Raise a storm”Goal: when another script sends weather/storm, the sea builds up over twelve seconds: taller waves, more foam, rain.
#pragma lifecycle(startup)
const CALM = { waveHeight: 0.3, foam: 0.4, rain: 0 };const STORM = { waveHeight: 2.2, foam: 0.9, rain: 0.8 };
let sea;let tween = null;
function blend(from, to, t) { sea.setProps({ waveHeight: from.waveHeight + (to.waveHeight - from.waveHeight) * t, foam: from.foam + (to.foam - from.foam) * t, rain: from.rain + (to.rain - from.rain) * t, });}
function change(to, seconds) { if (tween) tween.stop(); const { waveHeight, foam, rain } = sea.props; const from = { waveHeight, foam, rain }; tween = Tween.value(0, 1, (t) => blend(from, to, t), { duration: seconds, ease: Ease.InOutSine });}
function startup() { sea = findEntity('Ocean').getComponent(WaterMeshComponent); on('weather/storm', () => change(STORM, 12)); on('weather/calm', () => change(CALM, 20));}blendmixes two weather states with a factortfrom 0 to 1 and writes them with onesetProps.changestarts from the sea’s current props, so a storm called off halfway calms down from where it got to. A running tween is stopped first.windDirectionandwaveLengthare left alone, so neighbouring tiles of the same sea stay seamless.
React when the camera dives
Section titled “React when the camera dives”Goal: muffle the music while the camera is under water.
The water has a Water Volume and a Signal Emitter whose signal is lake; a music entity is named Music.
#pragma lifecycle(startup)
function startup() { const music = findEntity('Music').getComponent(AudioComponent);
on('lake', (event) => { if (event.eventType === EventType.WaterVolume.Submerge) { music.setProps({ volume: 0.2 }); } else if (event.eventType === EventType.WaterVolume.Emerge) { music.setProps({ volume: 1 }); } });}- The Water Volume sends
enter,submerge,emergeandexitas the camera’s lens crosses the waterline, in play only. Submergearrives once the whole lens is under water, so the music drops then, not at the first splash;Emergearrives as the lens starts to come out.- The events have no data: the script only compares
event.eventTypewithEventType.WaterVolumeconstants.
- Tiles that must meet without a seam need the same
windDirectionandwaveLength. Vary onlywaveHeight,foam,rainand colours per tile. - Pooled water (with a Cache Mesh) cannot be clipped by a Mask. Remove the Cache Mesh from a plane that must be masked.
- No sun glint? Add a Directional Light: the water reads the scene’s first one.
Components
Section titled “Components”What a new Water Mesh is made of. Required components are always present and cannot be removed; the others can. Per entity says whether an entity can hold one of that component or several. Layer, Lock and Enable are on every placed entity, and Transform holds its position, rotation and scale.
| Component | Type | Role | Per entity | What it does |
|---|---|---|---|---|
| Layer | comp-layer |
Required | One | Puts the entity on a render/raycast layer so cameras and rays can include or skip it. |
| Lock | comp-lock |
Required | One | Editor-only flag that stops the entity from being selected or moved. |
| Enable | comp-enable |
Required | One | Turns the entity and its children on or off: hides them and stops their behavior. |
| Transform | comp- |
Required | One | Position, rotation and scale of the entity, with optional editor-only per-axis locks. |
| Water Mesh | comp- |
Required | One | Real-time water surface with waves, foam, rain, reflections and caustics. |
You can also add 7 optional components, such as Cache Mesh, Enable On Signal, Outline, Selectable, Signal Emitter, Silhouette.
The settings of a new entity, as the runtime creates it. Change them in the Properties panel of XR Creator, or from a script with getComponent(…).setProps({ … }).
| Prop | Default | Range |
|---|---|---|
size |
20 |
0 ≤ x ≤ 10000 |
color |
"#0e4d64" |
|
waveHeight |
0.3 |
0.01 ≤ x ≤ 20 |
waveLength |
8 |
0. |
windDirection |
0 |
|
foam |
0.5 |
0 ≤ x ≤ 1 |
clarity |
3 |
0. |
caustics |
0.8 |
0 ≤ x ≤ 1 |
rain |
0 |
0 ≤ x ≤ 1 |
rainSize |
0.2 |
0.05 ≤ x ≤ 2 |
Events
Section titled “Events”With a Signal Emitter on the entity, these events reach scripts as { entityId, eventType, data }. Compare eventType with the EventType constants.
It reports the events every entity has: Transform::change (and position-change, rotation-change, scale-change), Enable::change, Layer::change and Lock::change.
Live example
Section titled “Live example”Run it in the browser, read its scripts, and download it as an XR Creator project.
One tween changes wave height, foam and rain together on four ocean tiles, turning a calm sea into a storm and back.
Press Storm to raise the waves over twelve seconds, and Calm to let them settle. Drag to orbit the camera, scroll to zoom.
From a script
Section titled “From a script”Find the entity by its name, check its class, and work with its components. Name the entity in the Hierarchy first; this example expects My Water Mesh.
#pragma lifecycle(startup)
function startup() { // Find the entity by the name it has in the Hierarchy. const entity = findEntity('My Water Mesh'); if (!(entity instanceof WaterMeshEntity)) return;
// Its Water Mesh component holds the settings listed under Props. const component = entity.getComponent(WaterMeshComponent); log('size is', component.props.size); component.setProps({ size: 30 });}