Three.js Curve & Spline Editor (Catmull-Rom, B-Spline)

Interactive spline editor in Three.js with draggable control points (TransformControls), centripetal, chordal and uniform Catmull–Rom, cubic B-spline and polyline curves, tube meshes, Frenet frames and a constant-speed rider.

Key topics: three.js spline, catmull rom, b-spline, curve editor, transformcontrols, frenet frames

What this code does

- Catmull–Rom: an interpolating curve through every point; centripetal parameterization avoids cusps and self-loops.
- B-Spline: a custom `Curve` with uniform cubic basis functions approximates the control polygon and is C² smooth.
- Editing: click a control point to attach `TransformControls`, and the curve, tube and frames rebuild on every change.
- Arc Length: `getPointAt` reparameterizes by distance, so the rider moves at constant speed even with uneven spacing.
- Frenet Frames: tangent, normal and binormal arrows show the moving frame that tubes and sweeps use.
- Controls: curve type, tension, closed loop, tube, frames, rider, speed, add and remove point.

Continue Learning

JavaScript Implementation

ES6+
import { TransformControls } from 'three/examples/jsm/controls/TransformControls.js'

const handles = [[-3, 0.5, -1], [-1, 2, -2.5], [2, 0.5, -2], [3, 1.5, 1], [-1, 1, 2.5]].map(([x, y, z]) => {
  const m = new THREE.Mesh(new THREE.SphereGeometry(0.14), new THREE.MeshBasicMaterial({ color: 0xfacc15 }))
  m.position.set(x, y, z)
  scene.add(m)
  return m
})
const line = new THREE.Line(new THREE.BufferGeometry(), new THREE.LineBasicMaterial({ color: 0x38bdf8 }))
scene.add(line)

function rebuild () {
  const curve = new THREE.CatmullRomCurve3(handles.map(h => h.position), true, 'centripetal')
  line.geometry.setFromPoints(curve.getSpacedPoints(300))
}
const gizmo = new TransformControls(camera, renderer.domElement)
gizmo.addEventListener('dragging-changed', e => { orbit.enabled = !e.value })
gizmo.addEventListener('objectChange', rebuild)
gizmo.attach(handles[0])
scene.add(gizmo.getHelper())
rebuild()