Three.js Vector Field Streamlines (Tornado, ABC Flow, Double Gyre)

General 3D vector-field tracer in Three.js with RK4 streamlines, flowing dash animation, arrow glyphs and advected particles, covering a Burgers vortex, chaotic ABC flow, point charges, the time-dependent double gyre and a linear saddle.

Key topics: three.js vector field, streamlines, flow visualization, rk4, abc flow, double gyre, burgers vortex, cfd

What this code does

- Streamlines: curves tangent to the field are integrated with RK4 at a fixed arc-length step (h = Δs/|v|) so they stay evenly sampled.
- Flowing Dashes: each vertex stores its distance along the line, and the fragment shader scrolls dashes in the flow direction.
- Burgers Vortex: inflow, stretching updraft and a Gaussian-cored swirl give the classic tornado funnel.
- ABC Flow: a steady solution of Euler's equations whose streamlines are chaotic, with no turbulence needed.
- Double Gyre: a periodically sloshing two-cell flow, the standard benchmark for Lagrangian coherent structures, re-traced as it changes.
- Glyphs & Tracers: optional arrow `InstancedMesh` and advected particles show speed and direction everywhere.
- Controls: field, streamline count and length, dash speed, glyphs, tracers and reseed.

Continue Learning

JavaScript Implementation

ES6+
// Trace streamlines of a vector field with RK4 and draw them as lines
const field = (p) => new THREE.Vector3(Math.sin(p.z) + 0.8 * Math.cos(p.y), 0.7 * Math.sin(p.x) + Math.cos(p.z), 0.8 * Math.sin(p.y) + 0.7 * Math.cos(p.x)) // ABC flow

function rk4 (p, h) {
  const k1 = field(p)
  const k2 = field(p.clone().addScaledVector(k1, h / 2))
  const k3 = field(p.clone().addScaledVector(k2, h / 2))
  const k4 = field(p.clone().addScaledVector(k3, h))
  return p.clone().addScaledVector(k1, h / 6).addScaledVector(k2, h / 3).addScaledVector(k3, h / 3).addScaledVector(k4, h / 6)
}

for (let i = 0; i < 80; i++) {
  let p = new THREE.Vector3().randomDirection().multiplyScalar(Math.random() * 8)
  const pts = [p]
  for (let s = 0; s < 300; s++) {
    p = rk4(p, 0.05)
    pts.push(p)
  }
  scene.add(new THREE.Line(
    new THREE.BufferGeometry().setFromPoints(pts),
    new THREE.LineBasicMaterial({ color: new THREE.Color().setHSL(i / 80, 0.8, 0.6) })
  ))
}