Three.js Heat Diffusion Simulation (Heat Equation PDE)

Heat equation simulation in Three.js with spatially varying conductivity across copper, steel, insulators and air, Dirichlet or insulated boundaries, isotherms, a 3D temperature relief and shift-drag painting.

Key topics: three.js heat equation, heat diffusion, pde simulation, finite difference, thermal conductivity, isotherms

What this code does

- Finite Volumes: ∂T/∂t = ∇·(α∇T) uses face-averaged diffusivity, so heat flows correctly across material boundaries.
- Stability: the explicit scheme is sub-stepped to keep Δt ≤ Δx²/(4·α_max); the HUD shows how many sub-steps each frame needs.
- Materials: copper conducts about 8× faster than steel here, and insulators and air hold heat back (shown hatched and dark).
- Boundaries: cold edges hold T = 0 (Dirichlet), while insulated edges have zero flux (Neumann) and let heat build up.
- Visualization: an 8-bit `DataTexture` drives both height and an inferno colormap, with isotherm lines every 0.1.
- Controls: scenario, boundary type, speed, source power, surface cooling, heat/cool brush, height and isotherms (shift + drag to paint).

Continue Learning

JavaScript Implementation

ES6+
// Explicit heat equation on a grid, drawn as a DataTexture
const N = 128
let T = new Float32Array(N * N)
let next = new Float32Array(N * N)
const alpha = 0.24 // stable while α·Δt/Δx² ≤ 0.25
const data = new Uint8Array(N * N * 4)
const texture = new THREE.DataTexture(data, N, N)
scene.add(new THREE.Mesh(new THREE.PlaneGeometry(10, 10), new THREE.MeshBasicMaterial({ map: texture })))

function step () {
  T[N / 2 + (N / 2) * N] = 1 // hot spot
  for (let j = 1; j < N - 1; j++) {
    for (let i = 1; i < N - 1; i++) {
      const k = i + j * N
      next[k] = T[k] + alpha * (T[k + 1] + T[k - 1] + T[k + N] + T[k - N] - 4 * T[k])
    }
  }
  ;[T, next] = [next, T]
}

function animate () {
  requestAnimationFrame(animate)
  for (let s = 0; s < 20; s++) step()
  for (let k = 0; k < N * N; k++) data.set([T[k] * 255, T[k] * 80, 40, 255], k * 4)
  texture.needsUpdate = true
  renderer.render(scene, camera)
}
animate()