Underwater fog and caustics in Three.js

Distant rocks fade into blue before the rocks near the camera do. Moving ribbons of light cross the sand and continue onto nearby stones. Build those two effects first and you can tune an underwater scene without a full ocean simulation.

Sunlight crossing sand and coral under a blue water surface in Tidewater
The Tidewater Dive scene uses light patterns on sand and stone. The example below isolates fog and projected light so you can tune them separately.

Set visibility

Use the same blue-green colour for scene.background and scene.fog so the sand fades into the distance without a sharp horizon. FogExp2 leaves the near terrain clear and thickens with distance. With a density of 0.057 in this scene, fog contributes roughly 73% of the mix at 20 units of view depth. Moving a rock farther away is a quick way to check the falloff. Three.js FogExp2 reference

Project the light

Run the caustics example. Use the button to toggle the light bands, then drag to orbit. To edit the scene, save the block as index.html and open it from a local server such as python3 -m http.server 8080.

<!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>Underwater fog and caustics in Three.js demo</title>
  <link rel="icon" href="data:,">
  <style>
    body { margin: 0; overflow: hidden; font: 1rem system-ui, sans-serif; }
    canvas { display: block; }
    button {
      position: fixed; left: 1rem; bottom: 1rem; z-index: 1; min-height: 44px;
      padding: .5rem .85rem; background: #f4f4f0; color: #1a1a18;
      border: 1px solid #deded7; border-radius: 8px; font: inherit;
      cursor: pointer;
    }
  </style>
  <script type="importmap">
    { "imports": {
      "three": "https://cdn.jsdelivr.net/npm/three@0.184.0/build/three.module.js"
    } }
  </script>
</head>
<body>
  <button type="button" id="toggle">Hide caustics</button>
  <script type="module">
import * as THREE from "three";
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(0x155365);
scene.fog = new THREE.FogExp2(0x155365, 0.057);
const camera = new THREE.PerspectiveCamera(50, 1, 0.1, 90);
camera.position.set(7, 3.9, 11);
const controls = new OrbitControls(camera, renderer.domElement);
controls.target.set(0, 1, -3);
controls.maxPolarAngle = Math.PI / 2 - 0.025;
controls.update();

const clock = { value: 0 };
const strength = { value: 0.43 };
function seabedMaterial(color) {
  const material = new THREE.MeshStandardMaterial({ color, roughness: 0.88 });
  material.onBeforeCompile = (shader) => {
    shader.uniforms.causticTime = clock;
    shader.uniforms.causticStrength = strength;
    shader.vertexShader = "varying vec3 bedWorld;\n" + shader.vertexShader.replace(
      "#include <worldpos_vertex>",
      "#include <worldpos_vertex>\n" +
      "bedWorld = (modelMatrix * vec4(transformed, 1.0)).xyz;"
    );
    shader.fragmentShader = `varying vec3 bedWorld;
      uniform float causticTime;
      uniform float causticStrength;
      ${shader.fragmentShader}`.replace(
      "#include <emissivemap_fragment>", `
      #include <emissivemap_fragment>
      vec2 p = bedWorld.xz * 1.45;
      float a = sin(p.x + 0.7 * sin(p.y + causticTime * 0.8));
      float b = sin(p.y * 1.27 - 0.7 * sin(p.x - causticTime * 0.6));
      float fold = abs(a + b * 0.78);
      float pixel = fwidth(fold);
      float line = 1.0 - smoothstep(0.02, 0.11 + pixel, fold);
      totalEmissiveRadiance += vec3(0.34, 0.83, 0.72)
        * line * causticStrength * (1.0 - smoothstep(0.12, 0.42, pixel));
      `
    );
  };
  return material;
}

const sand = new THREE.Mesh(new THREE.PlaneGeometry(140, 140), seabedMaterial(0xa38e61));
sand.rotation.x = -Math.PI / 2;
scene.add(sand);
const stone = seabedMaterial(0x536f6e);
for (let i = 0; i < 18; i++) {
  const boulder = new THREE.Mesh(new THREE.IcosahedronGeometry(1, 1), stone);
  const z = 4 - i * 1.9;
  boulder.scale.set(1.9 + 0.6 * Math.sin(i * 3), 0.75 + 0.17 * (i % 4), 1.3);
  boulder.rotation.y = i * 1.7;
  boulder.position.set(Math.cos(i * 2.4) * (4.4 + i * 0.14), 0.3, z);
  scene.add(boulder);
}
const sun = new THREE.DirectionalLight(0xe4fbff, 1.9);
sun.position.set(-4, 9, 2);
scene.add(sun, new THREE.HemisphereLight(0x89bfd0, 0x3a4c49, 1.15));

const toggle = document.querySelector("#toggle");
toggle.addEventListener("click", () => {
  strength.value = strength.value ? 0 : 0.43;
  toggle.textContent = strength.value ? "Hide caustics" : "Show caustics";
});
function resize() {
  renderer.setPixelRatio(Math.min(devicePixelRatio, 1.5));
  renderer.setSize(innerWidth, innerHeight);
  camera.aspect = innerWidth / innerHeight;
  camera.updateProjectionMatrix();
}
addEventListener("resize", resize);
resize();
renderer.setAnimationLoop((now) => {
  clock.value = now / 1000;
  renderer.render(scene, camera);
});
  </script>
</body>
</html>

Caustics are the light concentrated by the moving water surface. The example draws a stylized version by crossing two warped sine signals and adding narrow bright bands to totalEmissiveRadiance. Sampling bedWorld.xz keeps the lines aligned across different meshes and UV layouts. fwidth() estimates how much of the pattern is covered by a pixel, so distant fine lines fade before they flicker.

Cross the waterline

For a camera that moves between air and water, use whichever medium the eye is in. Compare the camera’s world Y to the water height at the eye’s X and Z. On a moving surface, sampling at the eye prevents the fog from switching in the middle of a trough. A small tolerance around the surface can keep jitter from triggering several crossings in a row.

Tidewater’s full scene uses directional underwater fog and can project the same caustic field onto the floor and props. Register a hull or pier as a sun occluder to dim the light it blocks. For a WebGPU example of refraction-based caustics in Three.js, see the official demo. Tidewater underwater setup · Caustic shadows · Tidewater Dive · Three.js caustics example

For a camera that can surface, use its distance from the waterline to control the sound as well as the light. Add underwater audio with Web Audio.