Feature Library

Fast rays through big meshes and many instances

0.72 GPU · 1.07 CPU ms
A still of the Fast rays through big meshes and many instances demo
Still
The live demo starts when it’s in view.Hover to pick · click the field to move the beacon · drag to orbit

Picking and line of sight that stay cheap when the scene is big.

What it costs a frame

0.72 ms
1.07 ms
GPU
0.72 ms
CPU, median
1.07 ms
CPU, 95th pct
1.79 ms
Draw calls
10
Weight
Moderate

Measured 13 November 2026 on RTX 4060 Laptop, 13th Gen Intel(R) Core(TM) i9-13900H, Chrome 157, 1920×1080 at 1×. 600 frames × 3 rounds, median kept (rounds ranged GPU 0.47–0.72, CPU 0.99–1.46 ms). What ran: Measured with the beacon circling, so all 32 sight lines are recast through the grids every frame (plus a few lines a frame recast each quick way in turn, and one through three’s own every 1.5 s), and a pick through the middle of the screen each frame. 4,000 stones, 40 walls and 20,000 terrain triangles. Phones: tiers are being measured next.

On every machine it was measured on
MachineGPU msCPU msCPU p95
RTX 4060 Laptop · 13th Gen Intel(R) Core(TM) i9-13900Hreference · measuring tool0.721.071.79
apple · Apple A19 Proin a browser tab · 16 November 20266.472.603.32
Intel Arc 140V (Xe2-LPG) · Intel Core Ultra 7 258Vin a browser tab · 15 November 20264.332.733.44

Bars against one 16.7 ms frame. A browser-tab run is timed in a visible tab without the tool’s scripted input, so read it as close, not exact.

What it needs

Renders with
WebGPU or WebGLWebGPU where there is one, WebGL2 where there is not.
Your browser
Runs hereNo WebGPU needed
Depends on
three.js 0.187.0No other packages

What it’s for

  1. Picking among thousands of instanced props, trees or stones
  2. Line of sight for many agents a frame (NPCs, cameras, creatures that look around)
  3. Rays against a big terrain or a merged town of walls
  4. Hover highlights in a large 3D scene without frame drops
  5. Placing things on the ground under the pointer
  6. Sound or light occlusion tests between many points

Pitfalls, in brief

  1. The mesh grid is over the mesh’s own x and z.
  2. Static geometry only: the grid is built once from the positions.
  3. The instance spheres are rebuilt only when version() changes.
  4. Hits come back in the order the cells were walked, not sorted: Raycaster.intersectObject sorts them, but a caller of mesh.raycast directly must sort them itself.
  5. firstHitOnly is a property you add to the Raycaster (as three-mesh-bvh does).
5 more in brief
  1. A cell that holds most of the mesh (a few huge triangles over the whole ground, a spire with thousands of triangles in one cell) is no faster than three.
  2. An array of materials, or a draw range that leaves part of the geometry out, falls back to three’s test (one material over several geometry groups is fine: three tests every triangle then too).
  3. Tall vertical faces stack in one cell: the grid is two-dimensional, so a wall of many rows of triangles puts them all in the cells along its foot.
  4. An instance scaled to zero is never hit (its sphere has no radius): that is how the source hides placed props.
  5. The model grid for an instanced batch (gridFrom, 300 triangles) is in the model’s own units: a 1 m stone with the default 2 m cell is one cell and pure overhead.
10 pitfalls in full, the hook-up guide, the module source and its tests come with the part.

Where it comes from

From HaTac. Fully custom code.

Amanatides and Woo, A Fast Voxel Traversal Algorithm for Ray Tracing (1987): The grid walk (a DDA stepping to whichever cell boundary is nearer), here in two dimensions. The algorithm, not code.

three-mesh-bvh (Garrett Johnson): The `raycaster.firstHitOnly` convention is borrowed so callers written for it work here. No code.

Jarzynski and Olano, Hash Functions for GPU Rendering (2020): The PCG hash the demo scatters its stones with.

raycastingpickingline of sightray accelerationuniform gridDDAspatial hashingInstancedMeshbounding spheresfirst hitterrainthree.jscpuno dependencies

Near it in the frame