Three.js Measurement Tool (Distance, Angle, Area, Snapping)

Interactive 3D measuring in Three.js covering point-to-point distance, three-point angles, polygon area and height above the floor, with vertex and grid snapping, unit conversion and CSS2D labels in a furnished room.

Key topics: three.js measurement, measure distance, cad tool, snapping, css2drenderer, bim viewer

What this code does

- Picking: clicks raycast the scene; small drags are ignored so orbiting does not place points.
- Vertex Snap: the nearest corner of the hit triangle within 15 cm replaces the hit point, so edges measure exactly.
- Grid Snap: x and z are rounded to the grid size for layout work.
- Distance & Height: lengths go between two points or straight down to the floor, with dashed guides.
- Angle & Area: the angle comes from the arms' dot product with an arc, and polygon area from Newell's method (½ Σ pᵢ × pᵢ₊₁).
- Labels: `CSS2DRenderer` keeps crisp HTML labels attached to 3D midpoints and centroids.
- Controls: tool, snapping, grid size, units and clear.

Continue Learning

JavaScript Implementation

ES6+
import { CSS2DRenderer, CSS2DObject } from 'three/examples/jsm/renderers/CSS2DRenderer.js'

const labels = new CSS2DRenderer()
labels.setSize(width, height)
labels.domElement.style.cssText = 'position:absolute;top:0;pointer-events:none'
document.querySelector('#app').appendChild(labels.domElement)

function measure (a, b) {
  scene.add(new THREE.Line(new THREE.BufferGeometry().setFromPoints([a, b]), new THREE.LineBasicMaterial({ color: 0xfacc15 })))
  const tag = document.createElement('div')
  tag.textContent = a.distanceTo(b).toFixed(2) + ' m'
  const label = new CSS2DObject(tag)
  label.position.copy(a).lerp(b, 0.5)  // follows the 3D midpoint
  scene.add(label)
}
// each frame: renderer.render(scene, camera); labels.render(scene, camera)