Three.js Underground Cave System (Noise-Carved Tunnel)

Explore a noise-carved cave in Three.js by headlamp, with a winding tunnel swept along a spline and displaced by 3D fbm, two large chambers, stalactites and stalagmites, glowing crystal clusters with point lights, and an underground pool.

Key topics: procedural cave, three.js cave, noise terrain, spotlight flashlight, stalactites, underground environment

What this code does

- Tunnel Sweep: rings of vertices are placed along a CatmullRom path using a stable up vector, and each radius is pushed out by 3D fbm sampled at the wall.
- Chambers & Floor: Gaussian bumps along the path open two chambers, and the lower half of each ring is flattened into a walkable floor.
- Formations: stalactites hang from ceiling-facing grid vertices and stalagmites rise from floor vertices, as two instanced cone meshes.
- Crystals: emissive octahedra cluster on chamber walls, pointing back into the cave, with a few pulsing point lights.
- Lighting: near-black ambient, a camera-mounted SpotLight with shadows, and dense exponential fog.
- Controls: flashlight, crystal glow, wall roughness (rebuilds the cave), fog density and tour.

Continue Learning

JavaScript Implementation

ES6+
// Cave tunnel: sweep a ring along a curve, pushing each vertex out by 3-D noise
for (let i = 0; i <= segments; i++) {
  const u = i / segments, p = path.getPointAt(u), t = path.getTangentAt(u)
  const side = new THREE.Vector3().crossVectors(t, up).normalize(), ringUp = new THREE.Vector3().crossVectors(side, t)
  for (let j = 0; j <= radial; j++) {
    const a = j / radial * Math.PI * 2
    const dir = side.clone().multiplyScalar(Math.cos(a)).addScaledVector(ringUp, Math.sin(a))
    const r = 4.2 + fbm3(p.clone().addScaledVector(dir, 4).multiplyScalar(0.12)) * 3.2
    positions.push(...p.clone().addScaledVector(dir, r).toArray())
  }
}
// Render with side: THREE.BackSide so we see the inside; stalactites = cones at raycast hits