Boat wakes in Three.js

When a boat rounds a bend, foam from the start of the turn is still sitting where the stern passed. Save those stern positions in world space to draw a wake that follows the boat’s route.

An overhead view of a boat and a curved foam wake fading into blue water
Older foam has spread sideways and faded. The fresh part of the trail starts at the stern.

Test the turn

Pause the boat partway around the bend. Switch between World space and Boat space to compare the foam against the same view of the hull. In Boat space, the demo draws a straight trail along the boat’s current heading. In World space, the curve follows the positions the stern actually passed. Run the wake example

Record the stern

Record the emitter at the back of the hull. For a model whose bow points along local −Z, the stern is on local +Z. If the boat sits under a parent transform, use the group’s localToWorld() to get the stern position after steering. Keep each sample’s world X and Z, sideways unit vector, time and cumulative distance. Add the current stern as the first row when drawing so there’s always foam at the emitter. Three.js localToWorld

Emit a sample every 0.45 metres. If the boat moves far enough to cover more than one interval in an update, insert intermediate samples along that segment. One sample per frame makes that spacing depend on both speed and frame rate. For a teleport or a respawn, clear the history before drawing again. Otherwise the interpolation will leave a line right across the map.

Build the foam ribbon

Make two vertices per row by offsetting the sample to either side. Join each pair of rows with two triangles. The example allocates room for 100 rows and reuses the arrays each frame. Set needsUpdate after writing the attributes and use setDrawRange() to submit just the occupied triangles. With an indexed geometry, the draw range counts indices. Three.js BufferAttribute · Three.js setDrawRange

Divide the sample age by 4.4 seconds to get a value from zero to one. Older rows grow wider and more transparent as the trail spreads. The shader receives age, distance along the trail and a coordinate from −1 to 1 across it. It uses the last two values to break up the foam into streaks near the edges, while the centre wash fades sooner.

Full example source

Save this block as index.html and serve it locally. The boat, water and foam are drawn in code. The only network dependency is Three.js r184.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="robots" content="noindex,follow">
  <title>Boat wakes in Three.js</title>
  <link rel="icon" href="data:,">
  <style>
    * { box-sizing: border-box; }
    html, body { margin: 0; height: 100%; }
    body { background: #203e46; color: #212320; font: 16px system-ui, sans-serif; }
    main { position: relative; width: 100%; height: 100%; }
    canvas { display: block; width: 100%; height: 100%; }
    fieldset { position: absolute; left: 16px; bottom: max(16px, env(safe-area-inset-bottom)); margin: 0; padding: 6px; border: 0; border-radius: 8px; background: #f4f4f0; display: flex; gap: 4px; }
    button { min-height: 44px; padding: 0 10px; font: inherit; color: inherit; border: 1px solid transparent; border-radius: 5px; background: transparent; cursor: pointer; }
    button[aria-pressed="true"] { background: #212320; color: #fff; }
    button:focus-visible { outline: 2px solid #976d23; outline-offset: 1px; }
    [hidden] { display: none; }
    [role="alert"] { margin: 16px; background: #f4f4f0; padding: 12px; }
  </style>
  <script type="importmap">
    { "imports": { "three": "https://cdn.jsdelivr.net/npm/three@0.184.0/build/three.module.js" } }
  </script>
</head>
<body>
  <main>
    <canvas aria-label="A boat sails around a bend, leaving a foam trail"></canvas>
    <p role="alert" hidden>This example needs WebGL 2.</p>
    <fieldset aria-label="Wake controls">
      <button type="button" data-space="world" aria-pressed="true">World space</button>
      <button type="button" data-space="boat" aria-pressed="false">Boat space</button>
      <button type="button" id="motion">Pause</button>
    </fieldset>
  </main>
  <script type="module">
import * as THREE from "three";

const canvas = document.querySelector("canvas");
let renderer;
try {
  renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
} catch (error) {
  canvas.hidden = true;
  document.querySelector("fieldset").hidden = true;
  document.querySelector('[role="alert"]').hidden = false;
  throw error;
}
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
const scene = new THREE.Scene();
const camera = new THREE.OrthographicCamera();
camera.position.set(13, 59, 18);
camera.lookAt(0, 0, 0);

const water = new THREE.Mesh(new THREE.PlaneGeometry(300, 300), new THREE.ShaderMaterial({
  uniforms: { time: { value: 0 } },
  vertexShader: `
    varying vec2 surface;
    void main() {
      vec4 world = modelMatrix * vec4(position, 1.0);
      surface = world.xz;
      gl_Position = projectionMatrix * viewMatrix * world;
    }
  `,
  fragmentShader: `
    uniform float time;
    varying vec2 surface;
    void main() {
      float swells = sin(surface.x * 0.27 + surface.y * 0.62 + time * 0.85);
      float ripple = sin(surface.x * 0.75 + surface.y * 2.0 + time * 1.35 + swells);
      float light = 0.49 + 0.17 * swells + 0.1 * ripple;
      vec3 color = mix(vec3(0.019, 0.059, 0.077), vec3(0.035, 0.17, 0.194), light);
      gl_FragColor = vec4(color, 1.0);
      #include <colorspace_fragment>
    }
  `,
}));
water.rotation.x = -Math.PI / 2;
scene.add(water);

const hullShape = new THREE.Shape();
hullShape.moveTo(-1.0, -1.9);
hullShape.lineTo(1.0, -1.9);
hullShape.lineTo(1.08, 0.55);
hullShape.quadraticCurveTo(1.0, 1.7, 0, 2.6);
hullShape.quadraticCurveTo(-1.0, 1.7, -1.08, 0.55);
hullShape.closePath();
const hull = new THREE.Mesh(
  new THREE.ExtrudeGeometry(hullShape, { depth: 0.48, bevelEnabled: false }),
  new THREE.MeshStandardMaterial({ color: 0xeee9d3 }),
);
hull.rotation.x = -Math.PI / 2;
const cabin = new THREE.Mesh(new THREE.BoxGeometry(1.4, 0.75, 1.75),
  new THREE.MeshStandardMaterial({ color: 0xb6522a }));
cabin.position.set(0, 0.82, -0.45);
const boat = new THREE.Group();
boat.add(hull, cabin);
scene.add(boat, new THREE.HemisphereLight(0xffffff, 0x42575a, 1.8));
const glass = new THREE.Mesh(new THREE.BoxGeometry(1.27, 0.12, 0.65),
  new THREE.MeshStandardMaterial({ color: 0x29464d }));
glass.position.set(0, 1.22, -0.94);
boat.add(glass);
const sun = new THREE.DirectionalLight(0xffe9c1, 2.4);
sun.position.set(3, 10, 6);
scene.add(sun);

const spacing = 0.45;
const lifetime = 4.4;
const capacity = 100;
const positions = new THREE.BufferAttribute(new Float32Array(capacity * 6), 3);
const foamData = new THREE.BufferAttribute(new Float32Array(capacity * 6), 3);
positions.setUsage(THREE.DynamicDrawUsage);
foamData.setUsage(THREE.DynamicDrawUsage);
const indices = [];
for (let row = 0; row < capacity - 1; row++) {
  const i = row * 2;
  indices.push(i, i + 1, i + 2, i + 1, i + 3, i + 2);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute("position", positions);
geometry.setAttribute("foamData", foamData);
geometry.setIndex(indices);
const foam = new THREE.Mesh(geometry, new THREE.ShaderMaterial({
  transparent: true,
  depthWrite: false,
  side: THREE.DoubleSide,
  vertexShader: `
    attribute vec3 foamData;
    varying vec3 data;
    void main() {
      data = foamData;
      gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
    }
  `,
  fragmentShader: `
    varying vec3 data;
    void main() {
      float across = data.x;
      float age = data.y;
      float along = data.z;
      float disturbance = sin(along * 2.7 + across * 4.0) * 0.1;
      float edges = 1.0 - smoothstep(0.045, 0.26, abs(abs(across + disturbance) - 0.67));
      float wash = exp(-7.0 * across * across) * (1.0 - age) * 0.52;
      float phase = along * 3.6 + across * 9.0;
      float streaks = 0.66 + 0.22 * sin(phase + 0.9 * sin(along * 4.7))
                            + 0.12 * sin(along * 11.9 - across * 13.6);
      float opacity = (edges * 0.9 + wash) * (1.0 - age) * streaks;
      gl_FragColor = vec4(0.78, 0.9, 0.87, opacity);
      #include <colorspace_fragment>
    }
  `,
}));
foam.frustumCulled = false;
scene.add(foam);

// The route supplies an example position and unit heading at any time.
function sternAt(time) {
  const angle = time * 0.55;
  const x = 15 * Math.cos(angle);
  const z = 10 * Math.sin(angle);
  const direction = new THREE.Vector2(-15 * Math.sin(angle), 10 * Math.cos(angle)).normalize();
  boat.position.set(x, 0.02, z);
  boat.rotation.y = Math.atan2(-direction.x, -direction.y);
  return { x: x - direction.x * 1.9, z: z - direction.y * 1.9,
    rightX: -direction.y, rightZ: direction.x, time };
}

const history = [];
let distance = 0;
let boatSpace = false;
function record(stern) {
  let last = history.at(-1);
  if (!last) {
    history.push({ ...stern, distance });
    return;
  }

  let dx = stern.x - last.x;
  let dz = stern.z - last.z;
  let gap = Math.hypot(dx, dz);
  while (gap >= spacing) {
    const ratio = spacing / gap;
    distance += spacing;
    last = {
      x: last.x + dx * ratio,
      z: last.z + dz * ratio,
      rightX: stern.rightX,
      rightZ: stern.rightZ,
      time: THREE.MathUtils.lerp(last.time, stern.time, ratio),
      distance,
    };
    history.push(last);
    dx = stern.x - last.x;
    dz = stern.z - last.z;
    gap = Math.hypot(dx, dz);
  }
  while (history.length && (stern.time - history[0].time > lifetime ||
      history.length >= capacity)) history.shift();
}

function updateRibbon(stern) {
  const last = history.at(-1);
  const head = { ...stern, distance: distance + Math.hypot(stern.x - last.x, stern.z - last.z) };
  const rows = [head, ...history.slice().reverse()];
  for (const [row, sample] of rows.entries()) {
    const age = THREE.MathUtils.clamp((stern.time - sample.time) / lifetime, 0, 1);
    const width = 0.9 + 3.7 * age;
    const behind = head.distance - sample.distance;
    const x = boatSpace ? stern.x - stern.rightZ * behind : sample.x;
    const z = boatSpace ? stern.z + stern.rightX * behind : sample.z;
    const rightX = boatSpace ? stern.rightX : sample.rightX;
    const rightZ = boatSpace ? stern.rightZ : sample.rightZ;
    positions.setXYZ(row * 2, x - width * rightX, 0.025, z - width * rightZ);
    positions.setXYZ(row * 2 + 1, x + width * rightX, 0.025, z + width * rightZ);
    foamData.setXYZ(row * 2, -1, age, sample.distance);
    foamData.setXYZ(row * 2 + 1, 1, age, sample.distance);
  }
  positions.needsUpdate = true;
  foamData.needsUpdate = true;
  geometry.setDrawRange(0, (rows.length - 1) * 6);
}

let time = 2.55;
// Fill the trail before the first render so comparison starts at a bend.
for (let past = time - lifetime; past < time; past += 1 / 60) record(sternAt(past));
const spaceButtons = document.querySelectorAll("[data-space]");
for (const button of spaceButtons) {
  button.addEventListener("click", () => {
    boatSpace = button.dataset.space === "boat";
    for (const choice of spaceButtons) choice.setAttribute("aria-pressed", String(choice === button));
    draw();
  });
}
let paused = matchMedia("(prefers-reduced-motion: reduce)").matches;
const motion = document.querySelector("#motion");
function syncMotion() { motion.textContent = paused ? "Play" : "Pause"; }
motion.addEventListener("click", () => {
  paused = !paused;
  syncMotion();
});
syncMotion();

function draw() {
  const stern = sternAt(time);
  record(stern);
  updateRibbon(stern);
  water.material.uniforms.time.value = time;
  renderer.render(scene, camera);
}
function resize() {
  const width = canvas.clientWidth;
  const height = canvas.clientHeight;
  const aspect = width / height;
  const vertical = Math.max(17.5, 22.5 / aspect);
  camera.left = -vertical * aspect;
  camera.right = vertical * aspect;
  camera.top = vertical;
  camera.bottom = -vertical;
  camera.updateProjectionMatrix();
  renderer.setSize(width, height, false);
  draw();
}
addEventListener("resize", resize);
resize();

let previous = performance.now();
renderer.setAnimationLoop((now) => {
  const dt = Math.min(Math.max(0, (now - previous) / 1000), 0.04);
  previous = now;
  if (paused) return;
  time += dt;
  draw();
});
  </script>
</body>
</html>

Fit the wake to your water

Use a short fade on tight turns. When the inner edge of the ribbon gets as wide as the turn radius, it can fold across itself. The easiest fix in a game is often to shorten the foam lifetime. On waves, sample the surface height at each edge vertex so the foam stays on top. Float the hull using the same surface samples. Float a boat on waves

The ribbon is a surface effect. Tidewater can also draw persistent hull wakes and send a pressure footprint into its local wave solver, so another object can ride the waves a boat leaves behind. Tidewater wake generators · Drive the launch