Shorelines and breaking waves in Three.js
Use the same bed heights to draw the beach and tell the water where it gets shallow. That gives waves a depth to break in and keeps the foam near the shore as the terrain curves.
Signed depth
In a Y-up scene, the ground height is its world Y coordinate. Tidewater’s depth field takes waterLevel - bedY(x, z) in metres. If the water plane sits at Y = 2, a bed at Y = 0 gives a depth of 2. Ground at Y = 4 gives a depth of −2. Zero is the still-water line.
Keep the sign when you bake. A wave that enters decreasing water depth slows and steepens. Tidewater uses the baked depths to fade the offshore spectrum and drive the shore swell and its whitewater.
Build a beach
Save this as shoreline.js next to the Tidewater download’s src/ and assets/ folders. Create shoreline.html in the same folder with a doctype and the Three.js import map from the download’s index.html. Add <script type="module" src="./shoreline.js"></script> to its body. Run python3 -m http.server 8080 there and open http://localhost:8080/. The runtime setup covers the import map if you’re adding this to an existing scene.
At X = 0, the beach crosses the waterline at Z = 0 and reaches 2 m deep at Z = 25. The sine term shifts that profile along Z to bend the coast. One bedY() function drives the rendered vertices and the baked depth field.
import * as THREE from "three/webgpu";
import { createRenderer } from "./src/renderer.js";
import { createWater } from "./src/Tidewater.js";
import { ShallowWater } from "./src/ShallowWater.js";
const level = 2;
const width = 320;
const shoreZ = (x) => 7 * Math.sin(x / 42);
const bedY = (x, z) => {
const depth = THREE.MathUtils.clamp((z - shoreZ(x)) * 0.08, -7, 13);
return level - depth;
};
const scene = new THREE.Scene();
const aspect = innerWidth / innerHeight;
const camera = new THREE.PerspectiveCamera(48, aspect, 0.1, 5000);
camera.position.set(-62, 18, -31);
camera.lookAt(0, level, 20);
const { renderer } = await createRenderer();
document.body.style.margin = "0";
renderer.domElement.style.display = "block";
document.body.append(renderer.domElement);
const terrain = new THREE.PlaneGeometry(width, width, 160, 160);
terrain.rotateX(-Math.PI / 2);
const vertices = terrain.attributes.position;
for (let i = 0; i < vertices.count; i++) {
vertices.setY(i, bedY(vertices.getX(i), vertices.getZ(i)));
}
vertices.needsUpdate = true;
terrain.computeVertexNormals();
const sand = new THREE.MeshStandardMaterial({ color: 0xbfa780 });
scene.add(new THREE.Mesh(terrain, sand));
const shallow = new ShallowWater({
size: 256, worldSize: width, origin: [0, 0], waterLevel: level,
period: 7, amplitude: 0.75, steepness: 0.62
});
shallow.setDepthFunction((x, z) => level - bedY(x, z)).bake();
const water = await createWater({
scene, camera, renderer,
mode: "beach", quality: "med", waterLevel: level,
ocean: { shallowWater: shallow }
});
let last = performance.now();
renderer.setAnimationLoop((now) => {
water.update(Math.min((now - last) / 1000, 0.05));
last = now;
renderer.render(scene, camera);
});
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
});
For baked level geometry, give ShallowWater the terrain and seabed meshes instead. Call scene.updateMatrixWorld(true) before baking so the raycasts use world transforms. Include the meshes that define the coast and leave props out of the list.
shallow.setDepthFromObjects([landMesh, seabedMesh], { fallbackDepth: 15 }).bake();
Each ray miss takes the fallback depth. A gap in your imported mesh can become open water in the bake, so inspect the field before you tune the waves. Run setDepthFromObjects() again and rebake after moving or editing the meshes.
Swell and foam
Start with a seven-second swell and adjust amplitude until you can see it break over your shelf. Use water.foam.setShore() to adjust the whitewater separately. In your sand material, darken a strip just above the zero-depth contour and fade it toward dry sand to give the receding water a wet edge.
Give a cove less swell with setExposureFunction(). Here the shore swell ramps from 25% on the negative-X side to full strength on the open beach.
shallow.setExposureFunction((x) =>
0.25 + 0.75 * THREE.MathUtils.smoothstep(x, -24, 14)
).bake();
Check the waterline
- Everything looks like open water
- Call
water.waves.setDebugView(19). It shows the depth on the water mesh. Parts of the red land band can be hidden by the terrain. Queryshallow.sampleDepth(x, z)on either side of the shore to check the sign directly. Confirm the bake covers both sides and that you passedshallowWaterintocreateWater(). - The break line follows the grid
- Reduce
worldSizearound the playable beach or increasesize. The example’s 320 m field over 256 samples gives about 1.25 m per cell. Keep a strip of dry ground inside the bake so it can locate the shoreline. - The hull floats above the waves
water.waves.sampleHeight(x, z)gives the current surface in world Y. Assign it directly to the floating point. If the hull has a separate draft or pivot offset, apply that in the hull setup.
If the depth looks right, view 20 traces distance from shore and 21 highlights where waves break. Set the view to 0 to return to the rendered surface. See the coast API reference for the bake settings.
For a built shoreline, the waterfront visualization guide shows how to compare the waterline against a quay. For a moving hull, see boat buoyancy and wakes.
Flowing water
A signed depth field also gives you a way to slow a current over shallows. The river flow map guide uses the surface current to carry floating markers downstream.