Three.js water shader for reflections

Put five pilings in the water and orbit the camera. Their reflections stay attached at the waterline and break up farther from the posts. This example uses the Three.js Water addon, then shows what to change when you need waves with height.

Foamy water and reflections beside a timber pier in Tidewater
A Tidewater shoreline uses a real surface reflection alongside moving wave geometry. Try the smaller reflection example below.

What the shader draws

Water.js renders a mirrored view of the scene into a texture. A scrolling normal map bends the reflected image and catches the sun. The plane stays at y = 0, so the pilings meet the water at the same height throughout the animation. Three.js ocean example · Water.js source for r184

Build the reflective surface

Run the reflection example. To edit it, save the full block as index.html and serve it with python3 -m http.server 8080. The imports and normal map come from Three.js r184 on jsDelivr.

<!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>Three.js water shader for reflections demo</title>
  <link rel="icon" href="data:,">
  <style>
    body { margin: 0; overflow: hidden; font: 1rem system-ui, sans-serif; }
    canvas { display: block; }
  </style>
  <script type="importmap">
    { "imports": {
      "three": "https://cdn.jsdelivr.net/npm/three@0.184.0/build/three.module.js"
    } }
  </script>
</head>
<body>
  <script type="module">
import * as THREE from "three";
import { Water } from "https://cdn.jsdelivr.net/npm/three@0.184.0/examples/jsm/objects/Water.js";
import { OrbitControls } from "https://cdn.jsdelivr.net/npm/three@0.184.0/examples/jsm/controls/OrbitControls.js";

const renderer = new THREE.WebGLRenderer({ antialias: true });
document.body.append(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x9fbfd2);
scene.fog = new THREE.FogExp2(0x9fbfd2, 0.017);
const camera = new THREE.PerspectiveCamera(46, 1, 0.1, 160);
camera.position.set(12, 5, 16);
const controls = new OrbitControls(camera, renderer.domElement);
controls.target.set(0, 0.4, -2);
controls.maxPolarAngle = Math.PI / 2 - 0.03;
controls.update();

const sunDirection = new THREE.Vector3(-0.6, 0.8, 0.3).normalize();
const sun = new THREE.DirectionalLight(0xffe7c6, 2.2);
sun.position.copy(sunDirection);
scene.add(sun, new THREE.HemisphereLight(0xe5f5ff, 0x685143, 1.6));

// Three.js r184 Water addon. A normal map represents directions, so leave
// its colorSpace at NoColorSpace.
const normalUrl = "https://cdn.jsdelivr.net/gh/mrdoob/three.js@r184/examples/textures/waternormals.jpg";
const normals = await new THREE.TextureLoader().loadAsync(normalUrl);
normals.wrapS = normals.wrapT = THREE.RepeatWrapping;
const water = new Water(new THREE.PlaneGeometry(110, 110), {
  textureWidth: 512,
  textureHeight: 512,
  waterNormals: normals,
  sunDirection,
  sunColor: 0xffe7c6,
  waterColor: 0x28585c,
  distortionScale: 2.2,
  fog: true,
});
water.rotation.x = -Math.PI / 2;
scene.add(water);

// Pilings make the reflection and the geometry of the waterline easy to see.
const postMaterial = new THREE.MeshStandardMaterial({ color: 0xa68e6f, roughness: 0.86 });
const cap = new THREE.MeshStandardMaterial({ color: 0xedb050, roughness: 0.7 });
for (let i = 0; i < 5; i++) {
  const post = new THREE.Mesh(new THREE.CylinderGeometry(0.35, 0.43, 4.8, 12), postMaterial);
  post.position.set((i - 2) * 2.9, 1.05, -4 - i * 1.3);
  const collar = new THREE.Mesh(new THREE.CylinderGeometry(0.39, 0.4, 0.38, 12), cap);
  collar.position.copy(post.position).y += 2.1;
  scene.add(post, collar);
}

let previous = performance.now();
function resize() {
  renderer.setPixelRatio(Math.min(devicePixelRatio, 1.5));
  renderer.setSize(innerWidth, innerHeight);
  camera.aspect = innerWidth / innerHeight;
  camera.fov = camera.aspect < 1 ? 66 : 46;
  camera.updateProjectionMatrix();
}
addEventListener("resize", resize);
resize();
renderer.setAnimationLoop((now) => {
  water.material.uniforms.time.value += Math.min(Math.max(0, (now - previous) / 1000), 0.05);
  previous = now;
  renderer.render(scene, camera);
});
  </script>
</body>
</html>

Tune the reflection

Start with a distortionScale around 2 and orbit until you can see the posts at a shallow angle. Too much distortion breaks the reflected post into separate bright strips. If the water looks frozen, check that time.value advances in seconds and that the normal map wraps on both axes. Treat the normal map as data. An sRGB conversion changes the encoded directions and shifts the light.

A 512 × 512 target contains a quarter as many pixels as a 1024 × 1024 target. Start with 256 or 512 on a phone and compare the same view while moving the camera. The mirrored camera still draws objects, so reducing the texture size chiefly saves pixel work. If the draw calls from the reflected geometry dominate, simplify what that camera can see.

When waves need height

At a low camera angle, a crest may cut across the horizon or hide the bottom of a boat. Give those waves geometry and let the boat sample the same surface used for rendering. With Tidewater, the moving ocean, surface queries and reflection are available in the same runtime. Use its Three.js WebGPURenderer setup and choose the WebGPU or WebGL2 backend there. Displaced waves in plain Three.js · Tidewater runtime setup · Swell demo

For a pool with a visible tiled floor, compare the view from the deck with a view straight down into the basin. Render swimming pool water in Three.js

If this scene slows down on a phone, compare reflection size and draw calls from the same camera position. Profile water rendering on mobile.