@spatial-engine/core 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +72 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# @spatial-engine/core
|
|
2
|
+
|
|
3
|
+
The foundation layer for `spatial-engine`, a high-performance spatial partitioning and query library using a Data-Oriented Design (DOD) approach. It provides cache-friendly, zero-GC spatial data structures backed by flat `Float32Array` pools.
|
|
4
|
+
|
|
5
|
+
Everything lives in contiguous memory: no per-object heap allocations, no mid-frame GC pauses.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @spatial-engine/core
|
|
11
|
+
# or
|
|
12
|
+
pnpm add @spatial-engine/core
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Features
|
|
16
|
+
- **Octree with automatic subdivision** — insert, update, and remove axis-aligned bounding boxes.
|
|
17
|
+
- **Branchless slab-method raycast** — fast `rayIntersectsAABB` implementation.
|
|
18
|
+
- **Box query** — `octree.queryBox()` returns all objects whose AABB overlaps a query region natively (iterative DFS, no recursion).
|
|
19
|
+
- **Zero GC Pools** — `AABBPool`, `RayPool`, and `OctreeNodePool` allocate once and reuse. Call `.reset()` each frame.
|
|
20
|
+
- **LiDAR Web Worker** — run a full multi-ray sweep entirely off the main thread using shared memory.
|
|
21
|
+
- **`SharedArrayBuffer` support** — pools can be shared with Web Workers without copying data.
|
|
22
|
+
|
|
23
|
+
## Basic Usage
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
import { AABBPool, RayPool, OctreeNodePool, Octree } from '@spatial-engine/core';
|
|
27
|
+
|
|
28
|
+
// --- 1. Allocate pools once (e.g. on startup) ---
|
|
29
|
+
const nodePool = new OctreeNodePool(512);
|
|
30
|
+
const aabbPool = new AABBPool(512);
|
|
31
|
+
const rayPool = new RayPool(64);
|
|
32
|
+
const octree = new Octree(nodePool, aabbPool);
|
|
33
|
+
|
|
34
|
+
octree.setBounds(-100, -100, -100, 100, 100, 100);
|
|
35
|
+
|
|
36
|
+
// --- 2. Each frame: reset pools and re-insert objects ---
|
|
37
|
+
nodePool.reset();
|
|
38
|
+
aabbPool.reset();
|
|
39
|
+
|
|
40
|
+
const idx = aabbPool.allocate();
|
|
41
|
+
aabbPool.set(idx, -1, -1, -1, 1, 1, 1); // minX,minY,minZ,maxX,maxY,maxZ
|
|
42
|
+
octree.insert(idx);
|
|
43
|
+
|
|
44
|
+
// --- 3. Raycast ---
|
|
45
|
+
const rayIdx = rayPool.allocate();
|
|
46
|
+
rayPool.set(rayIdx, 0, 0, -10, 0, 0, 1); // origin (0,0,-10), direction +Z
|
|
47
|
+
|
|
48
|
+
const rayBuf = (rayPool as any).buffer as Float32Array;
|
|
49
|
+
const hit = octree.raycast(rayBuf, rayIdx * 6);
|
|
50
|
+
// hit: { objectIndex: 0, t: 9 } | null
|
|
51
|
+
|
|
52
|
+
// --- 4. Box query ---
|
|
53
|
+
const hits = octree.queryBox(-2, -2, -2, 2, 2, 2);
|
|
54
|
+
// hits: [0] (array of aabbPool indices)
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## LiDAR Web Worker
|
|
58
|
+
For scenes with many rays (e.g. simulated sensors), you can use the built-in worker implementation to run sweeps off the main thread using `SharedArrayBuffer` exports natively provided.
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import { AABBPool, RayPool, OctreeNodePool } from '@spatial-engine/core';
|
|
62
|
+
import type { LidarInitMessage, LidarSweepMessage } from '@spatial-engine/core';
|
|
63
|
+
// Import the robust built in web worker
|
|
64
|
+
const worker = new Worker(new URL('@spatial-engine/core/lidar-worker', import.meta.url), { type: 'module' });
|
|
65
|
+
```
|
|
66
|
+
(See root monorepo README for full worker example code).
|
|
67
|
+
|
|
68
|
+
## Use Cases
|
|
69
|
+
- **Game collision broad-phase**: `queryBox` for candidate pairs
|
|
70
|
+
- **Frustum culling**: `raycast` or `queryBox` to cull objects
|
|
71
|
+
- **LiDAR sensor simulation**: `createLidarProcessor` off thread
|
|
72
|
+
- **Physics broad-phase**: Find overlap candidates before narrow-phase
|
package/package.json
CHANGED