Three.js Lorenz Attractor & Chaos Visualizer

RK4-integrated strange attractors in Three.js — Lorenz, Rössler, Aizawa and Thomas — with fading trails and several nearby trajectories that show sensitive dependence on initial conditions.

Key topics: three.js lorenz attractor, chaos theory, strange attractor, butterfly effect, rk4, rossler, dynamical systems

What this code does

- Lorenz System: dx/dt = σ(y − x), dy/dt = x(ρ − z) − y, dz/dt = xy − βz with the classic σ = 10, ρ = 28, β = 8/3.
- RK4 Integration: a fourth-order Runge–Kutta step keeps the trajectory accurate at larger time steps.
- Butterfly Effect: trajectories start 0.001 apart and the HUD tracks how quickly their separation grows.
- Burn-in: each run integrates past the initial transient, so trails start on the attractor.
- Trail Buffers: fixed-size `BufferGeometry` lines shift their oldest points out and fade the tail with vertex colors.
- Controls: attractor, its parameters, trajectory count, initial offset, steps per frame and trail length.

Continue Learning

JavaScript Implementation

ES6+
const scene = new THREE.Scene()
const camera = new THREE.PerspectiveCamera(45, width / height, 0.1, 500)
camera.position.set(40, 20, 50)
camera.lookAt(0, 0, 25)
const renderer = new THREE.WebGLRenderer({ antialias: true })
renderer.setSize(width, height)
document.querySelector('#app').appendChild(renderer.domElement)

const sigma = 10
const rho = 28
const beta = 8 / 3
const max = 5000
const positions = new Float32Array(max * 3)
const geometry = new THREE.BufferGeometry()
geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3))
const line = new THREE.Line(geometry, new THREE.LineBasicMaterial({ color: 0x38bdf8 }))
line.frustumCulled = false
scene.add(line)

let x = 0.1
let y = 0
let z = 0
let n = 0

function animate () {
  requestAnimationFrame(animate)
  for (let s = 0; s < 4 && n < max; s++) {
    const dt = 0.005
    x += sigma * (y - x) * dt
    y += (x * (rho - z) - y) * dt
    z += (x * y - beta * z) * dt
    positions.set([x, y, z], n * 3)
    n++
  }
  geometry.setDrawRange(0, n)
  geometry.attributes.position.needsUpdate = true
  renderer.render(scene, camera)
}
animate()