Namespace kdtree3

xeokit 3D Collision Utilities


Efficient spatial searching and collision detection using 3D k-d trees, rays, and boundaries.


The KdTree3 provides a fast spatial search index that organizes 3D objects with axis-aligned boundaries, allowing efficient queries for intersections with other objects, volumes, and rays.

This module includes functions for building and searching KdTree3 instances:

Building KdTree3s:

  • createPrimsKdTree3 - Creates a KdTree3 containing primitives from geometry arrays, organized by their 3D boundaries.
  • createSceneObjectPrimsKdTree3 - Creates a KdTree3 containing primitives from SceneObjects, organized by their world-space boundaries.
  • createSceneObjectsKdTree3 - Creates a KdTree3 containing SceneObjects, organized by their world-space boundaries.

Searching KdTree3s:

With these utilities, applications can implement:

  • Frustum culling for SceneObjects
  • Ray-picking for object selection
  • Marquee selection of multiple objects

Install the xeokit SDK:

npm install @xeokit/sdk

Searching for SceneObjects intersecting a 3D world-space boundary:

import { Scene } from "@xeokit/sdk/scene";
import { SDKError } from "@xeokit/sdk/core";
import { TrianglesPrimitive } from "@xeokit/sdk/constants";
import { createSceneObjectsKdTree3, searchKdTree3WithAABB } from "@xeokit/sdk/kdtree3";

const scene = new Scene();
const sceneModel = scene.createModel({ id: "myModel" });

if (sceneModel instanceof SDKError) {
console.error(sceneModel.message);
} else {
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.createLayerMesh({
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"]
});

sceneModel.build().then(() => {
const kdTree = createSceneObjectsKdTree3(Object.values(scene.objects));

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