Three.js Fluid Simulation (Stable Fluids, Navier–Stokes)

Real-time 2D Navier–Stokes fluid in Three.js using Jos Stam's Stable Fluids with vorticity confinement, three colored dye channels and a 3D height-field, plus shift-drag to stir.

Key topics: three.js fluid simulation, navier stokes, stable fluids, jos stam, vorticity confinement, cfd, height field

What this code does

- Stable Fluids: each frame diffuses and advects velocity with a semi-Lagrangian back-trace, then projects it to be divergence-free.
- Pressure Projection: a Gauss–Seidel solve for pressure removes divergence so the fluid stays incompressible.
- Vorticity Confinement: re-injects small swirls lost to numerical dissipation, which keeps the flow lively.
- Dye Transport: three dye fields (RGB) are advected by the velocity and fade with a persistence factor.
- GPU Height-Field: the 128² dye grid is uploaded as a `DataTexture`, and a shader displaces and shades a 3D surface from it.
- Controls: auto emitters, force, vorticity, viscosity, dye persistence, pressure iterations, height and clear (shift + drag to stir).

Continue Learning

JavaScript Implementation

ES6+
// Minimal Stable Fluids (Jos Stam) density advection on a 64² grid drawn as a texture
const N = 64
const IX = (i, j) => i + (N + 2) * j
const size = (N + 2) * (N + 2)
const u = new Float32Array(size)
const v = new Float32Array(size)
let dens = new Float32Array(size)
let prev = new Float32Array(size)

function advect (d, d0, dt) {
  for (let j = 1; j <= N; j++) {
    for (let i = 1; i <= N; i++) {
      const x = Math.min(Math.max(i - dt * N * u[IX(i, j)], 0.5), N + 0.5)
      const y = Math.min(Math.max(j - dt * N * v[IX(i, j)], 0.5), N + 0.5)
      const i0 = Math.floor(x)
      const j0 = Math.floor(y)
      const s = x - i0
      const t = y - j0
      d[IX(i, j)] = (1 - s) * ((1 - t) * d0[IX(i0, j0)] + t * d0[IX(i0, j0 + 1)]) + s * ((1 - t) * d0[IX(i0 + 1, j0)] + t * d0[IX(i0 + 1, j0 + 1)])
    }
  }
}

// a fixed swirling velocity field
for (let j = 1; j <= N; j++) {
  for (let i = 1; i <= N; i++) {
    u[IX(i, j)] = -(j - N / 2) * 0.002
    v[IX(i, j)] = (i - N / 2) * 0.002
  }
}

const data = new Uint8Array(N * N * 4)
const texture = new THREE.DataTexture(data, N, N)
const renderer = new THREE.WebGLRenderer()
renderer.setSize(width, height)
document.querySelector('#app').appendChild(renderer.domElement)
const scene = new THREE.Scene()
const camera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0, 1)
scene.add(new THREE.Mesh(new THREE.PlaneGeometry(2, 2), new THREE.MeshBasicMaterial({ map: texture })))

function animate () {
  requestAnimationFrame(animate)
  dens[IX(N / 2 + 10, N / 2)] += 30
  ;[dens, prev] = [prev, dens]
  advect(dens, prev, 0.1)
  for (let j = 0; j < N; j++) {
    for (let i = 0; i < N; i++) {
      const c = Math.min(255, dens[IX(i + 1, j + 1)] * 255)
      data.set([c * 0.3, c * 0.7, c, 255], (i + j * N) * 4)
    }
  }
  texture.needsUpdate = true
  renderer.render(scene, camera)
}
animate()