kdtree3 — 3D Spatial Search & Collision Utilities


Efficient spatial querying and coarse collision detection using 3D k-d trees, rays, and bounding volumes


This module centers around KdTree3, a k-d tree that indexes items by their axis-aligned bounding boxes (AABBs). Building a KdTree3 up-front lets you run fast queries such as:

  • “Which objects intersect this AABB?”
  • “Which objects are inside (or intersect) the camera frustum?”
  • “Which objects does this picking ray hit?”

In practice, this is a high-performance way to reduce the number of candidates before doing more expensive, exact tests.

Choose one of the builders depending on what you want to index:

Once built, you can query the tree with common spatial volumes:

  • Frustum culling to quickly find potentially visible SceneObjects
  • Ray picking to find selection candidates under the cursor
  • Marquee / box selection using an AABB or frustum derived from screen-space drag
npm install @xeokit/sdk
import { Scene } from "@xeokit/sdk/scene";
import { TrianglesPrimitive } from "@xeokit/sdk/constants";
import { createSceneObjectsKdTree3, searchKdTree3WithAABB } from "@xeokit/sdk/kdtree3";

// 1) Build a simple scene with one object
const scene = new Scene();
const sceneModel = scene.createModel({ id: "myModel" }).value;

sceneModel.createGeometry({
id: "theGeometry",
primitive: TrianglesPrimitive,
positions: [10.07, 0, 11.07, 9.58, 3.11, 11.07, 8.15 ...],
indices: [21, 0, 1, 1, 22, 21, 22, 1, 2, 2, 23, 22, 23 ...]
});

sceneModel.createMesh({
id: "tableTopMesh",
geometryId: "theGeometry",
position: [0, -3, 0],
scale: [6, 0.5, 6],
rotation: [0, 0, 0],
color: [1.0, 0.3, 1.0]
});

sceneModel.createObject({
id: "tableTopSceneObject",
meshIds: ["tableTopMesh"]
});

// 2) Build a KdTree3 over all SceneObjects in the scene
const kdTree = createSceneObjectsKdTree3(Object.values(scene.objects));

// 3) Query candidates intersecting an AABB
const intersectingObjects = searchKdTree3WithAABB({
kdTree,
aabb: [0, 0, 0, 10, 10, 10]
});

console.log(intersectingObjects);

Classes

KdTree3
PrimsKdTree3
SceneObjectsKdTree3
SceneObjectsPrimsKdTree3

Interfaces

KdItem3D
KdLinePrim
KdNode3
KdPointPrim
KdSceneObjectPrim
KdTree3Params
KdTrianglePrim

Functions

createPrimsKdTree3
createSceneObjectPrimsKdTree3
createSceneObjectsKdTree3
searchKdTree3WithAABB
searchKdTree3WithFrustum
searchKdTree3WithRay