Three.js Underwater Scene (Caustics, Kelp & Fish)

An ocean floor in Three.js with animated caustics injected into standard materials, a coral reef of instanced forms, a 700-ribbon kelp forest bending in the current, schooling fish, marine snow, light shafts and murky blue fog.

Key topics: underwater scene, caustics shader, three.js ocean, kelp forest, fish school, environment

What this code does

- Caustics: two animated cellular (Voronoi) layers raised to a high power make bright interference lines, added in onBeforeCompile to upward-facing surfaces.
- Reef: five instanced coral and rock types scattered along a winding band, with per-instance colour jitter.
- Kelp: tall plane ribbons whose vertex shader bends them with the current, quadratically toward the tips.
- Fish: five schools follow wandering centres, each fish orbiting with a personal offset and oriented along its velocity.
- Atmosphere: marine snow drifts with the current, additive light shafts flicker down from the surface, and FogExp2 limits visibility.
- Controls: caustics, current, fish count, turbidity and light shafts.

Continue Learning

JavaScript Implementation

ES6+
// Underwater caustics: add an animated cellular pattern to lit, upward-facing surfaces
material.onBeforeCompile = (shader) => {
  shader.uniforms.uTime = uTime
  shader.fragmentShader = shader.fragmentShader.replace('#include <dithering_fragment>', `#include <dithering_fragment>
    float c = pow(1.0 - voronoi(vWorld.xz * 0.35, uTime), 6.0);      // bright cell borders
    gl_FragColor.rgb += vec3(0.55, 0.85, 1.0) * c * clamp(normal.y * 0.5 + 0.5, 0.0, 1.0);`)
}
scene.fog = new THREE.FogExp2(0x0a3d5c, 0.035)                       // murky blue water