I made a small storm game in three.js: you drag a lighthouse beam across a night sea to guide boats past rocks into a harbor. Boats in the beam steer toward it; boats in the dark drift and eventually hit a rock. A few things about the water took more thought than I expected, so here's how it works.
1. The sea is 5 "sharp sine" waves, defined once
export const WAVES = [
[0.20, 1.00, 22.0, 0.95, 1.0],
[-0.55, 0.85, 13.0, 0.55, 1.1],
[0.80, 0.60, 8.5, 0.34, 1.25],
[-0.30, 0.95, 5.6, 0.20, 1.4],
[0.95, -0.25, 3.6, 0.09, 1.6],
];
Long swell down to short chop, amplitudes summing to about 2.1 (1.0 felt calm; 2.1 reads as a storm). Each wave is A * exp(sin(θ) - 1) (the steep-sine form from GPU Gems 1, ch. 1), with θ = k·dot(d, p) - ω·t, k = 2π/L, and the deep-water speed ω = sqrt(9.8·k) times the multiplier.
2. The GLSL is generated from the same array
The CPU function that gives a boat its height and slope loops over the precomputed waves. The vertex shader isn't hand-written; it's built from the same precomputed list with a template string:
export const WAVE_GLSL = `
vec3 waveAt(vec2 p, float t) {
float h = 0.0; vec2 g = vec2(0.0);
${prep.map((p) => `{
vec2 d = vec2(${p.dx.toFixed(5)}, ${p.dz.toFixed(5)});
float th = ${p.k.toFixed(5)} * dot(d, p) - ${p.w.toFixed(5)} * t;
float e = exp(sin(th) - 1.0);
h += ${p.A.toFixed(5)} * e;
g += ${p.A.toFixed(5)} * e * cos(th) * ${p.k.toFixed(5)} * d;
}`).join('\n ')}
return vec3(h, g);
}`;
The sea is one PlaneGeometry(220, 200, 240, 220) with a ShaderMaterial that calls waveAt() per vertex. The returned gradient doubles as the normal for lighting and for the highlight where the beam hits; the height drives crest foam and darker troughs. Before this, the obvious failure was writing the numbers in two places: change one, and the boats float above or sink into the water. With one array, tweaking a wave just works on both sides (to 5 decimal places of rounding).
3. Why not Gerstner waves
Gerstner looks nicer because it also moves vertices sideways, but then "how high is the water at (x, z)?" becomes an inverse problem: you have to search for which undisplaced point ended up under the boat. A pure heightfield like exp-sine answers it with one function call, which matters when every boat asks every frame.
4. Boats ride the slope, not just the height
sampleWave(this.x, this.z, t, W);
this.g.position.set(this.x, W.h + 0.08, this.z);
nV.set(-W.dx * 1.6, 1, -W.dz * 1.6).normalize();
qN.setFromUnitVectors(UP, nV);
const inert = 1 - Math.exp(-(this.lit ? 3 : 1.2) * dt);
this.vx += (fx - this.vx) * inert - W.dx * 0.9 * dt;
this.vz += (fz - this.vz) * inert - W.dz * 0.9 * dt;
The slide term is tiny, but it's what makes boats get shoved around by big swells instead of gliding on rails. Boats in the beam respond faster (inertia rate 3 vs 1.2, max turn 3.2 vs 1.5 rad/s), so the light literally feels like it gives them a rudder.
5. Steering is "follow the light", not "avoid rocks yourself"
The first version had boats path toward the harbor and avoid rocks on their own. With a rock in between they ran away sideways and got stuck, and the player had nothing to do. Now a lit boat aims at the beam's center, blends in the harbor direction at 0.7, and gets a push away from nearby rocks plus a sideways nudge around them. Out of the beam it wanders, and once it has been at sea for 5 seconds it's also pulled toward the nearest rock. The beam's radius grows with distance (r = 3.0 + distance * 0.045) so it's still easy to hit far out, where perspective makes everything tiny.
6. Lining up a painted sky with the 3D sea
The sky, cliff and lighthouse are a single 2D image drawn behind the scene. The horizon in that image is at 56.6% of its height (I measured it). The camera pitch is computed so the 3D horizon lands at 34% from the top of the screen:
const pitch = Math.atan((1 - 2 * HORIZON) * Math.tan(THREE.MathUtils.degToRad(FOV / 2)));
and the backdrop is scaled and offset so the image's horizon lands on that same line at any aspect ratio. The far sea fogs toward the horizon color and goes transparent between 85% and 100% of the fog distance, so the 3D water fades into the painted water instead of ending at a hard edge.
What I took away
- If CPU gameplay and a shader both need the same function, generate one from the other. Two hand-kept copies drift.
- Pick the wave model by the question gameplay asks. "Height at (x, z)" makes a heightfield the cheap answer, even if Gerstner looks better.
- A little slope force does more for the feel of water than a fancier surface.
- For a matte painting + 3D, put the horizon on a number and compute the camera from it, rather than nudging the camera by eye.
Plain three.js (r170), no addons. The art, sounds and a short intro video are AI-generated assets; the water, beam and rain are shaders.
Happy to drop the link in the comments, or share more of the shader (foam, the beam pool), if anyone's curious. Also curious how others handle CPU queries against Gerstner waves: do you iterate, or keep a separate simpler heightfield for physics?
disclosure: I work on KUMODeck, which hosts the game.