Three.js AR Room Scanning (Spatial Mapping & Plane Detection)

Spatial mapping like ARKit and ARCore in Three.js, where a simulated depth sensor sweeps a room, hits accumulate into an 8 cm voxel map, and RANSAC detects floor, wall, table, seat and ceiling planes with labelled extents and coverage stats.

Key topics: room scanning, spatial mapping, plane detection, ransac, voxel map, augmented reality

What this code does

- Depth Sensor: each frame casts a jittered 32 × 24 ray grid from the device frustum into the (hidden) real room, with range noise growing with distance² like ToF sensors.
- Voxel Hash: hits are quantised to 8 cm cells keyed by integer coordinates; hit counts give confidence and summed normals the orientation.
- RANSAC: random triples propose planes, snapped to gravity-aligned horizontal or vertical as AR frameworks do; the plane with most inliers within 0.8 voxels wins, then its inliers are removed.
- Labelling: horizontal planes become floor, seat, table or ceiling by height and vertical ones walls; each shows its extent and area.
- Coverage: the voxel count times the cell area estimates how much room surface has been observed.
- Controls: scanning, sweep speed, voxel colouring (plane, normal, confidence), depth noise, show the real room and clear scan.

Continue Learning

JavaScript Implementation

ES6+
// Spatial mapping: accumulate depth hits into a voxel hash, then find planes with RANSAC
for (const hit of depthFrame) {
  const key = `${Math.floor(hit.x / v)},${Math.floor(hit.y / v)},${Math.floor(hit.z / v)}`
  voxels.set(key, (voxels.get(key) || 0) + 1)
}

function ransacPlane (pts) {
  let best
  for (let i = 0; i < 160; i++) {
    const [a, b, c] = sample3(pts), n = cross(b - a, c - a).normalize(), d = -n.dot(a)
    const inliers = pts.filter(p => Math.abs(n.dot(p) + d) < 0.06).length
    if (!best || inliers > best.inliers) best = { n, d, inliers }
  }
  return best                                    // label: floor / table / wall by normal & height
}