Add water to a Three.js game
Start with a plane and a crate. Use the same wave height at the same position and time for both, and the crate will ride the surface you draw.
Units and axes
Three.js is Y-up. Treat one unit as one metre and put still water at y = 0. Wave heights, camera heights and hull sizes then mean something. PlaneGeometry is built in the XY plane, so rotate it onto XZ before you displace it.
Geometry or shading
Move vertices for anything the player can see or touch: swell that lifts a boat, a crest on the horizon, a trough the camera dips into. Normal maps, glints and foam can stay in the material, since nothing collides with them. The water shader guide shows how to add a moving reflection. If a wave is smaller than the gap between your vertices, it belongs in the shader. Breaking surf needs both, which the shoreline surf guide covers.
A plain Three.js start
This page needs nothing but Three.js r184 from a CDN. Save both files in one folder.
<!-- index.html -->
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>html, body { margin: 0; height: 100%; overflow: hidden; }</style>
<script type="importmap">{
"imports": { "three": "https://cdn.jsdelivr.net/npm/three@0.184.0/build/three.module.js" }
}</script>
<script type="module" src="./water.js"></script>
</head>
<body></body>
</html>
// water.js
import * as THREE from "three";
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(innerWidth, innerHeight);
document.body.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xbcd6e4);
scene.add(new THREE.HemisphereLight(0xe4f1ff, 0x1d3338, 1.5));
const sun = new THREE.DirectionalLight(0xffffff, 2.5);
sun.position.set(20, 30, -10);
scene.add(sun);
const camera = new THREE.PerspectiveCamera(55, innerWidth / innerHeight, 0.1, 500);
camera.position.set(0, 5, 16);
camera.lookAt(0, 0, 0);
// One wave function. The mesh and the game both read it.
function waveHeight(x, z, t) {
return 0.4 * Math.sin(0.35 * x + 1.2 * t)
+ 0.25 * Math.sin(0.5 * z + 0.2 * x - 0.9 * t);
}
const geometry = new THREE.PlaneGeometry(80, 80, 160, 160);
geometry.rotateX(-Math.PI / 2); // XY plane onto XZ, Y up
const sea = new THREE.Mesh(geometry, new THREE.MeshStandardMaterial({
color: 0x1f5f78, roughness: 0.2, metalness: 0.1,
}));
scene.add(sea);
const crate = new THREE.Mesh(
new THREE.BoxGeometry(1, 1, 1),
new THREE.MeshStandardMaterial({ color: 0x9c7a4f })
);
crate.position.set(2, 0, 3);
scene.add(crate);
const position = geometry.attributes.position;
let t = 0;
let last = performance.now();
renderer.setAnimationLoop((now) => {
const dt = Math.min((now - last) / 1000, 0.05);
last = now;
t += dt;
for (let i = 0; i < position.count; i++) {
position.setY(i, waveHeight(position.getX(i), position.getZ(i), t));
}
position.needsUpdate = true;
geometry.computeVertexNormals();
crate.position.y = waveHeight(crate.position.x, crate.position.z, t) + 0.2;
renderer.render(scene, camera);
});
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
});
Serve the folder with python3 -m http.server 8080 and open http://localhost:8080/. Modules won't load from file://. The crate’s centre should sit 0.2 metres above the surface. If the gap changes as a wave passes, check that both calls to waveHeight() use the same coordinates and time.
Keep play and render in sync
The game owns the clock. Advance t by a clamped dt in seconds, so a tab coming back from the background doesn't jump the sea forward. Each frame, advance the water, place whatever floats on it, then render. Anything that reads the surface in between sees the sea the player sees. A boat needs more than one sample point, and the boat buoyancy guide picks up from there. For a surface visitors can touch, see how to place a ripple at a pointer hit.
Past this point you'll want reflections, foam, wakes and a sea that doesn't visibly repeat. That's the part Tidewater covers.
With Tidewater 1.6
Unzip the Tidewater download and rename the extracted folder to tidewater/ beside your game page. It includes Three.js under vendor/, so no CDN is needed.
my-game/
index.html
game.js
tidewater/ ← the unpacked download (src/, assets/, vendor/ …)
<!-- index.html: the import map comes before any module script -->
<script type="importmap">{
"imports": {
"three": "./tidewater/vendor/three/build/three.module.min.js",
"three/webgpu": "./tidewater/vendor/three/build/three.webgpu.min.js",
"three/tsl": "./tidewater/vendor/three/build/three.tsl.min.js",
"three/addons/": "./tidewater/vendor/three/examples/jsm/",
"tidewater/": "./tidewater/src/"
}
}</script>
<script type="module" src="./game.js"></script>
// game.js
import * as THREE from "three";
import { createRenderer } from "tidewater/renderer.js";
import { createWater } from "tidewater/Tidewater.js";
// WebGPU when the browser has it, WebGL2 otherwise. Same materials on both.
const { renderer, backend } = await createRenderer();
document.body.appendChild(renderer.domElement);
console.log("Tidewater backend:", backend);
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(55, innerWidth / innerHeight, 0.1, 5000);
camera.position.set(0, 4, 14);
camera.lookAt(0, 0, 0);
// Resolves once the water maps have loaded and the sea mode is applied.
const water = await createWater({ scene, camera, renderer, mode: "tropical", quality: "high" });
const crate = new THREE.Mesh(
new THREE.BoxGeometry(1, 1, 1),
new THREE.MeshStandardMaterial({ color: 0x9c7a4f })
);
scene.add(crate);
water.buoyancy.attach(crate, { ride: 0.1 });
let last = performance.now();
renderer.setAnimationLoop((now) => {
const dt = Math.min((now - last) / 1000, 0.05);
last = now;
// 1. Move the camera, players and AI.
// 2. Advance the water. This also floats the crate.
water.update(dt);
// 3. Gameplay can read the surface this frame will draw.
const ocean = water.ocean;
const surface = ocean.sampleHeight(camera.position.x, camera.position.z, ocean.simTime);
const underwater = camera.position.y < surface; // e.g. muffle the audio
renderer.render(scene, camera);
});
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
});
Serve my-game/ the same way. The console prints webgpu or webgl, and the crate bobs on a reflective sea under a sky. To check the fallback, pass { forceWebGL: true } to createRenderer() and reload.
In an existing game, the renderer is the main change. Tidewater's node materials need the WebGPURenderer that createRenderer() returns, so it replaces your WebGLRenderer. Built-in materials carry over, but custom GLSL ShaderMaterial code doesn't. Pass the PerspectiveCamera you render with, and keep your own objects off layer 3, which createWater() turns on for its sky. Call update() once per frame after the camera and anything self-propelled have moved. It reads the camera position and measures wake speeds, then floats attached bodies, so read heights after it.
If your game precompiles shaders, render the first water frame before calling renderer.compileAsync(). Precompiling first produced a black canvas in our Chrome WebGPU test. See the createWater reference for renderer and scene options.