Skip to content

Water Mesh

Updated

Real-time water surface with waves, foam, rain, reflections and caustics.

Type
ent-water-mesh
Script class
WaterMeshEntity
Family
Meshes

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.
  • waveHeight and waveLength: 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, caustics and rain: 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.
  1. Add a Water Mesh entity to the scene (see Scenes and hierarchy) at the height of the water line.
  2. Set size to cover the lake or sea, then color, waveHeight and waveLength for its character.
  3. Make sure the scene has a Directional Light, or the water shows no sun glint.
  4. Add a Water Volume if the camera can go under the surface, and a Signal Emitter to hear when it does.
  5. For several tiles of one sea, give them all the same windDirection and waveLength, and a Cache Mesh each.

Goal: turn the default sea into a small, still pond with clear water.

CalmPond.jx
#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; setProps changes only the keys passed.
  • Short, low waves (waveLength 1.5, waveHeight 0.05) read as a pond rather than a sea.
  • foam: 0 removes the whitecaps, and a higher clarity lets the bottom show through more water.

Goal: when another script sends weather/storm, the sea builds up over twelve seconds: taller waves, more foam, rain.

SeaStorm.jx
#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));
}
  • blend mixes two weather states with a factor t from 0 to 1 and writes them with one setProps.
  • change starts 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.
  • windDirection and waveLength are left alone, so neighbouring tiles of the same sea stay seamless.

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.

DiveMusic.jx
#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, emerge and exit as the camera’s lens crosses the waterline, in play only.
  • Submerge arrives once the whole lens is under water, so the music drops then, not at the first splash; Emerge arrives as the lens starts to come out.
  • The events have no data: the script only compares event.eventType with EventType.WaterVolume constants.
  • Tiles that must meet without a seam need the same windDirection and waveLength. Vary only waveHeight, foam, rain and 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.

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-transform Required One Position, rotation and scale of the entity, with optional editor-only per-axis locks.
Water Mesh comp-water-mesh 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.1 ≤ x ≤ 1000
windDirection 0
foam 0.5 0 ≤ x ≤ 1
clarity 3 0.01 ≤ x ≤ 100
caustics 0.8 0 ≤ x ≤ 1
rain 0 0 ≤ x ≤ 1
rainSize 0.2 0.05 ≤ x ≤ 2

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.

Run it in the browser, read its scripts, and download it as an XR Creator project.

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.

WaterMeshEntity.jx
#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 });
}