@recast-navigation/three 0.0.0-canary-20261030-064e4acc
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/LICENSE +21 -0
- package/README.md +181 -0
- package/dist/debug/debug-drawer.d.ts +48 -0
- package/dist/debug/index.d.ts +1 -0
- package/dist/helpers/crowd-helper.d.ts +19 -0
- package/dist/helpers/index.d.ts +3 -0
- package/dist/helpers/nav-mesh-helper.d.ts +18 -0
- package/dist/helpers/tile-cache-helper.d.ts +17 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.mjs +430 -0
- package/dist/utils/generators.d.ts +5 -0
- package/dist/utils/get-positions-and-indices.d.ts +2 -0
- package/dist/utils/index.d.ts +2 -0
- package/package.json +56 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License
|
|
2
|
+
|
|
3
|
+
Copyright © 2025 Isaac Mason
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
# @recast-navigation/three
|
|
2
|
+
|
|
3
|
+
Three.js nav mesh generation and visualisation helpers for [`recast-navigation`](https://github.com/isaac-mason/recast-navigation-js/tree/main/packages/recast-navigation).
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
> npm install @recast-navigation/three recast-navigation
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Importing
|
|
14
|
+
|
|
15
|
+
To import the three.js glue, you can either use the `three` entrypoint in `recast-navigation`:
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { init } from 'recast-navigation'
|
|
19
|
+
import { ... } from '@recast-navigation/three';
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Or you can use the packages directly:
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { init } from '@recast-navigation/core'
|
|
26
|
+
import { ... } from '@recast-navigation/three';
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### Initialization
|
|
30
|
+
|
|
31
|
+
Before you can use the library, you must initialize it. This is an asynchronous operation.
|
|
32
|
+
|
|
33
|
+
Calling `init()` after the library has already been initialized will return a promise that resolves immediately.
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { init } from 'recast-navigation';
|
|
37
|
+
|
|
38
|
+
await init();
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Generating a NavMesh
|
|
42
|
+
|
|
43
|
+
This package provides convenience functions for generating nav meshes from THREE.Mesh objects.
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
import { init } from 'recast-navigation';
|
|
47
|
+
import { threeToSoloNavMesh, threeToTiledNavMesh, threeToTileCache } from '@recast-navigation/three';
|
|
48
|
+
import * as THREE from 'three';
|
|
49
|
+
|
|
50
|
+
/* initialize the library */
|
|
51
|
+
await init();
|
|
52
|
+
|
|
53
|
+
/* create your scene */
|
|
54
|
+
const scene = new THREE.Scene();
|
|
55
|
+
// ...
|
|
56
|
+
|
|
57
|
+
/* get meshes to generate the navmesh from */
|
|
58
|
+
const meshes: THREE.Mesh[] = [];
|
|
59
|
+
scene.traverse((child) => {
|
|
60
|
+
if (child instanceof THREE.Mesh) {
|
|
61
|
+
meshes.push(child);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
/* generate a solo navmesh */
|
|
66
|
+
const { success, navMesh } = threeToSoloNavMesh(meshes, {
|
|
67
|
+
// ... nav mesh generation config ...
|
|
68
|
+
}});
|
|
69
|
+
|
|
70
|
+
/* generate a tiled navmesh */
|
|
71
|
+
const { success, navMesh } = threeToTiledNavMesh(meshes, {
|
|
72
|
+
tileSize: 16,
|
|
73
|
+
// ... nav mesh generation config ...
|
|
74
|
+
}});
|
|
75
|
+
|
|
76
|
+
/* generate a tile cache with support for temporary obstacles */
|
|
77
|
+
const { success, navMesh, tileCache } = threeToTileCache(meshes, {
|
|
78
|
+
tileSize: 16,
|
|
79
|
+
// ... nav mesh generation config ...
|
|
80
|
+
}});
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Interacting with a NavMesh
|
|
84
|
+
|
|
85
|
+
You can documentation for interacting with the generated navmesh in the core library README:
|
|
86
|
+
|
|
87
|
+
https://github.com/isaac-mason/recast-navigation-js
|
|
88
|
+
|
|
89
|
+
This library provides helpers that are used in conjunction with the core library.
|
|
90
|
+
|
|
91
|
+
### Debug Drawer
|
|
92
|
+
|
|
93
|
+
This package provides a `DebugDrawer` utility that can visualise a navmesh and other generation intermediates.
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
import { DebugDrawer } from '@recast-navigation/three';
|
|
97
|
+
|
|
98
|
+
const debugDrawer = new DebugDrawer();
|
|
99
|
+
|
|
100
|
+
// draw a navmesh
|
|
101
|
+
debugDrawer.drawNavMesh(navMesh);
|
|
102
|
+
|
|
103
|
+
// clear the debug drawer
|
|
104
|
+
debugDrawer.reset();
|
|
105
|
+
|
|
106
|
+
// dispose of threejs and wasm resources
|
|
107
|
+
debugDrawer.dispose();
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
See the debug examples for more: https://recast-navigation-js.isaacmason.com/examples/#example-debug-navmesh
|
|
111
|
+
|
|
112
|
+
### Helpers
|
|
113
|
+
|
|
114
|
+
This package provides helpers for visualizing various recast-navigation objects in three.js.
|
|
115
|
+
|
|
116
|
+
#### `NavMeshHelper`
|
|
117
|
+
|
|
118
|
+
```ts
|
|
119
|
+
import { NavMeshHelper } from '@recast-navigation/three';
|
|
120
|
+
|
|
121
|
+
const navMeshHelper = new NavMeshHelper(navMesh);
|
|
122
|
+
|
|
123
|
+
scene.add(navMeshHelper);
|
|
124
|
+
|
|
125
|
+
// update the helper when the navmesh changes
|
|
126
|
+
navMeshHelper.update();
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
#### `TileCacheHelper`
|
|
130
|
+
|
|
131
|
+
Visualises obstacles in a `TileCache`.
|
|
132
|
+
|
|
133
|
+
```ts
|
|
134
|
+
import { TileCacheHelper } from '@recast-navigation/three';
|
|
135
|
+
|
|
136
|
+
const tileCacheHelper = new TileCacheHelper(tileCache);
|
|
137
|
+
|
|
138
|
+
scene.add(tileCacheHelper);
|
|
139
|
+
|
|
140
|
+
// update the helper after adding or removing obstacles
|
|
141
|
+
tileCacheHelper.update();
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
#### `CrowdHelper`
|
|
145
|
+
|
|
146
|
+
Visualises agents in a `Crowd`.
|
|
147
|
+
|
|
148
|
+
```ts
|
|
149
|
+
import { CrowdHelper } from '@recast-navigation/three';
|
|
150
|
+
|
|
151
|
+
const crowdHelper = new CrowdHelper(crowd);
|
|
152
|
+
|
|
153
|
+
scene.add(crowdHelper);
|
|
154
|
+
|
|
155
|
+
// update the helper after updating the crowd
|
|
156
|
+
crowdHelper.update();
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
#### Custom Materials
|
|
160
|
+
|
|
161
|
+
You can optionally provide custom materials to the helpers.
|
|
162
|
+
|
|
163
|
+
```ts
|
|
164
|
+
// NavMeshHelper
|
|
165
|
+
const navMeshMaterial = new THREE.MeshBasicMaterial({ color: 'red' });
|
|
166
|
+
const navMeshHelper = new NavMeshHelper(navMesh, {
|
|
167
|
+
navMeshMaterial,
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// TileCacheHelper
|
|
171
|
+
const obstacleMaterial = new THREE.MeshBasicMaterial({ color: 'blue' });
|
|
172
|
+
const tileCacheHelper = new TileCacheHelper(tileCache, {
|
|
173
|
+
obstacleMaterial,
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
// CrowdHelper
|
|
177
|
+
const agentMaterial = new THREE.MeshBasicMaterial({ color: 'red' });
|
|
178
|
+
const crowdHelper = new CrowdHelper(crowd, {
|
|
179
|
+
agentMaterial,
|
|
180
|
+
});
|
|
181
|
+
```
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { type DebugDrawerPrimitive, type NavMesh, type NavMeshQuery, type RecastCompactHeightfield, type RecastContourSet, type RecastHeightfield, type RecastHeightfieldLayer, type RecastHeightfieldLayerSet, type RecastPolyMesh, type RecastPolyMeshDetail } from '@recast-navigation/core';
|
|
2
|
+
import * as THREE from 'three';
|
|
3
|
+
import { LineMaterial } from 'three/addons/lines/LineMaterial.js';
|
|
4
|
+
export type DebugDrawerParams = {
|
|
5
|
+
triMaterial?: THREE.Material;
|
|
6
|
+
pointMaterial?: THREE.Material;
|
|
7
|
+
lineMaterial?: LineMaterial;
|
|
8
|
+
};
|
|
9
|
+
export declare class DebugDrawer extends THREE.Group {
|
|
10
|
+
triMaterial: THREE.Material;
|
|
11
|
+
pointMaterial: THREE.Material;
|
|
12
|
+
pointGeometry: THREE.SphereGeometry;
|
|
13
|
+
lineMaterial: LineMaterial;
|
|
14
|
+
private debugDrawerUtils;
|
|
15
|
+
constructor({ triMaterial, pointMaterial, lineMaterial, }?: DebugDrawerParams);
|
|
16
|
+
drawPrimitives(primitives: DebugDrawerPrimitive[]): void;
|
|
17
|
+
drawHeightfieldSolid(hf: RecastHeightfield): void;
|
|
18
|
+
drawHeightfieldWalkable(hf: RecastHeightfield): void;
|
|
19
|
+
drawCompactHeightfieldSolid(chf: RecastCompactHeightfield): void;
|
|
20
|
+
drawCompactHeightfieldRegions(chf: RecastCompactHeightfield): void;
|
|
21
|
+
drawCompactHeightfieldDistance(chf: RecastCompactHeightfield): void;
|
|
22
|
+
drawHeightfieldLayer(layer: RecastHeightfieldLayer, idx: number): void;
|
|
23
|
+
drawHeightfieldLayers(lset: RecastHeightfieldLayerSet): void;
|
|
24
|
+
drawRegionConnections(cset: RecastContourSet, alpha?: number): void;
|
|
25
|
+
drawRawContours(cset: RecastContourSet, alpha?: number): void;
|
|
26
|
+
drawContours(cset: RecastContourSet, alpha?: number): void;
|
|
27
|
+
drawPolyMesh(mesh: RecastPolyMesh): void;
|
|
28
|
+
drawPolyMeshDetail(dmesh: RecastPolyMeshDetail): void;
|
|
29
|
+
drawNavMesh(mesh: NavMesh, flags?: number): void;
|
|
30
|
+
drawNavMeshWithClosedList(mesh: NavMesh, query: NavMeshQuery, flags?: number): void;
|
|
31
|
+
drawNavMeshNodes(query: NavMeshQuery): void;
|
|
32
|
+
drawNavMeshBVTree(mesh: NavMesh): void;
|
|
33
|
+
drawNavMeshPortals(mesh: NavMesh): void;
|
|
34
|
+
/**
|
|
35
|
+
* @param col color in 0xRRGGBBAA format
|
|
36
|
+
*/
|
|
37
|
+
drawNavMeshPolysWithFlags(mesh: NavMesh, flags: number, col: number): void;
|
|
38
|
+
/**
|
|
39
|
+
* @param col color in 0xRRGGBBAA format
|
|
40
|
+
*/
|
|
41
|
+
drawNavMeshPoly(mesh: NavMesh, ref: number, col: number): void;
|
|
42
|
+
reset(): void;
|
|
43
|
+
dispose(): void;
|
|
44
|
+
private drawPoints;
|
|
45
|
+
private drawLines;
|
|
46
|
+
private drawTris;
|
|
47
|
+
private drawQuads;
|
|
48
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './debug-drawer';
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Crowd } from '@recast-navigation/core';
|
|
2
|
+
import { type Material, Mesh, Object3D } from 'three';
|
|
3
|
+
export type CrowdHelperParams = {
|
|
4
|
+
agentMaterial?: Material;
|
|
5
|
+
};
|
|
6
|
+
export declare class CrowdHelper extends Object3D {
|
|
7
|
+
agentMeshes: Map<number, Mesh>;
|
|
8
|
+
recastCrowd: Crowd;
|
|
9
|
+
agentMaterial: Material;
|
|
10
|
+
constructor(crowd: Crowd, params?: CrowdHelperParams);
|
|
11
|
+
/**
|
|
12
|
+
* Update the three debug view of the crowd agents.
|
|
13
|
+
*
|
|
14
|
+
* This should be called after updating the crowd.
|
|
15
|
+
*/
|
|
16
|
+
update(): void;
|
|
17
|
+
private createAgentMesh;
|
|
18
|
+
private updateAgentGeometry;
|
|
19
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type NavMesh } from '@recast-navigation/core';
|
|
2
|
+
import { BufferGeometry, type Material, Mesh, Object3D } from 'three';
|
|
3
|
+
export type NavMeshHelperParams = {
|
|
4
|
+
navMeshMaterial?: Material;
|
|
5
|
+
};
|
|
6
|
+
export declare class NavMeshHelper extends Object3D {
|
|
7
|
+
navMesh: NavMesh;
|
|
8
|
+
mesh: Mesh;
|
|
9
|
+
navMeshMaterial: Material;
|
|
10
|
+
navMeshGeometry: BufferGeometry;
|
|
11
|
+
constructor(navMesh: NavMesh, params?: NavMeshHelperParams);
|
|
12
|
+
/**
|
|
13
|
+
* Update the three debug nav mesh.
|
|
14
|
+
*
|
|
15
|
+
* This should be called after updating the nav mesh.
|
|
16
|
+
*/
|
|
17
|
+
update(): void;
|
|
18
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Obstacle, TileCache } from '@recast-navigation/core';
|
|
2
|
+
import { type Material, Mesh, Object3D } from 'three';
|
|
3
|
+
export type TileCacheHelperParams = {
|
|
4
|
+
obstacleMaterial?: Material;
|
|
5
|
+
};
|
|
6
|
+
export declare class TileCacheHelper extends Object3D {
|
|
7
|
+
tileCache: TileCache;
|
|
8
|
+
obstacleMeshes: Map<Obstacle, Mesh>;
|
|
9
|
+
obstacleMaterial: Material;
|
|
10
|
+
constructor(tileCache: TileCache, params?: TileCacheHelperParams);
|
|
11
|
+
/**
|
|
12
|
+
* Update the obstacle meshes.
|
|
13
|
+
*
|
|
14
|
+
* This should be called after adding or removing obstacles.
|
|
15
|
+
*/
|
|
16
|
+
update(): void;
|
|
17
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,430 @@
|
|
|
1
|
+
import { DebugDrawerUtils, getNavMeshPositionsAndIndices } from '@recast-navigation/core';
|
|
2
|
+
import * as THREE from 'three';
|
|
3
|
+
import { Object3D, MeshBasicMaterial, Vector3, Mesh, CylinderGeometry, BufferGeometry, BufferAttribute, BoxGeometry } from 'three';
|
|
4
|
+
import { LineMaterial } from 'three/addons/lines/LineMaterial.js';
|
|
5
|
+
import { LineSegments2 } from 'three/addons/lines/LineSegments2.js';
|
|
6
|
+
import { LineSegmentsGeometry } from 'three/addons/lines/LineSegmentsGeometry.js';
|
|
7
|
+
import { mergePositionsAndIndices, generateSoloNavMesh, generateTileCache, generateTiledNavMesh } from '@recast-navigation/generators';
|
|
8
|
+
|
|
9
|
+
const _color = new THREE.Color();
|
|
10
|
+
class DebugDrawer extends THREE.Group {
|
|
11
|
+
triMaterial;
|
|
12
|
+
pointMaterial;
|
|
13
|
+
pointGeometry = new THREE.SphereGeometry(0.02, 32, 32);
|
|
14
|
+
lineMaterial;
|
|
15
|
+
debugDrawerUtils;
|
|
16
|
+
constructor({
|
|
17
|
+
triMaterial,
|
|
18
|
+
pointMaterial,
|
|
19
|
+
lineMaterial
|
|
20
|
+
} = {}) {
|
|
21
|
+
super();
|
|
22
|
+
this.debugDrawerUtils = new DebugDrawerUtils();
|
|
23
|
+
this.triMaterial = triMaterial ?? new THREE.MeshBasicMaterial({
|
|
24
|
+
vertexColors: true,
|
|
25
|
+
transparent: true,
|
|
26
|
+
opacity: 0.4,
|
|
27
|
+
depthWrite: false
|
|
28
|
+
});
|
|
29
|
+
this.pointMaterial = pointMaterial ?? new THREE.MeshBasicMaterial();
|
|
30
|
+
this.lineMaterial = lineMaterial ?? new LineMaterial({
|
|
31
|
+
color: 0xffffff,
|
|
32
|
+
linewidth: 2,
|
|
33
|
+
vertexColors: true,
|
|
34
|
+
polygonOffset: true,
|
|
35
|
+
polygonOffsetFactor: -4,
|
|
36
|
+
polygonOffsetUnits: -10
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
drawPrimitives(primitives) {
|
|
40
|
+
for (const primitive of primitives) {
|
|
41
|
+
switch (primitive.type) {
|
|
42
|
+
case 'points':
|
|
43
|
+
this.drawPoints(primitive);
|
|
44
|
+
break;
|
|
45
|
+
case 'lines':
|
|
46
|
+
this.drawLines(primitive);
|
|
47
|
+
break;
|
|
48
|
+
case 'tris':
|
|
49
|
+
this.drawTris(primitive);
|
|
50
|
+
break;
|
|
51
|
+
case 'quads':
|
|
52
|
+
this.drawQuads(primitive);
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
drawHeightfieldSolid(hf) {
|
|
58
|
+
const primitives = this.debugDrawerUtils.drawHeightfieldSolid(hf);
|
|
59
|
+
this.drawPrimitives(primitives);
|
|
60
|
+
}
|
|
61
|
+
drawHeightfieldWalkable(hf) {
|
|
62
|
+
const primitives = this.debugDrawerUtils.drawHeightfieldWalkable(hf);
|
|
63
|
+
this.drawPrimitives(primitives);
|
|
64
|
+
}
|
|
65
|
+
drawCompactHeightfieldSolid(chf) {
|
|
66
|
+
const primitives = this.debugDrawerUtils.drawCompactHeightfieldSolid(chf);
|
|
67
|
+
this.drawPrimitives(primitives);
|
|
68
|
+
}
|
|
69
|
+
drawCompactHeightfieldRegions(chf) {
|
|
70
|
+
const primitives = this.debugDrawerUtils.drawCompactHeightfieldRegions(chf);
|
|
71
|
+
this.drawPrimitives(primitives);
|
|
72
|
+
}
|
|
73
|
+
drawCompactHeightfieldDistance(chf) {
|
|
74
|
+
const primitives = this.debugDrawerUtils.drawCompactHeightfieldDistance(chf);
|
|
75
|
+
this.drawPrimitives(primitives);
|
|
76
|
+
}
|
|
77
|
+
drawHeightfieldLayer(layer, idx) {
|
|
78
|
+
const primitives = this.debugDrawerUtils.drawHeightfieldLayer(layer, idx);
|
|
79
|
+
this.drawPrimitives(primitives);
|
|
80
|
+
}
|
|
81
|
+
drawHeightfieldLayers(lset) {
|
|
82
|
+
const primitives = this.debugDrawerUtils.drawHeightfieldLayers(lset);
|
|
83
|
+
this.drawPrimitives(primitives);
|
|
84
|
+
}
|
|
85
|
+
drawRegionConnections(cset, alpha = 1) {
|
|
86
|
+
const primitives = this.debugDrawerUtils.drawRegionConnections(cset, alpha);
|
|
87
|
+
this.drawPrimitives(primitives);
|
|
88
|
+
}
|
|
89
|
+
drawRawContours(cset, alpha = 1) {
|
|
90
|
+
const primitives = this.debugDrawerUtils.drawRawContours(cset, alpha);
|
|
91
|
+
this.drawPrimitives(primitives);
|
|
92
|
+
}
|
|
93
|
+
drawContours(cset, alpha = 1) {
|
|
94
|
+
const primitives = this.debugDrawerUtils.drawContours(cset, alpha);
|
|
95
|
+
this.drawPrimitives(primitives);
|
|
96
|
+
}
|
|
97
|
+
drawPolyMesh(mesh) {
|
|
98
|
+
const primitives = this.debugDrawerUtils.drawPolyMesh(mesh);
|
|
99
|
+
this.drawPrimitives(primitives);
|
|
100
|
+
}
|
|
101
|
+
drawPolyMeshDetail(dmesh) {
|
|
102
|
+
const primitives = this.debugDrawerUtils.drawPolyMeshDetail(dmesh);
|
|
103
|
+
this.drawPrimitives(primitives);
|
|
104
|
+
}
|
|
105
|
+
drawNavMesh(mesh, flags = 0) {
|
|
106
|
+
const primitives = this.debugDrawerUtils.drawNavMesh(mesh, flags);
|
|
107
|
+
this.drawPrimitives(primitives);
|
|
108
|
+
}
|
|
109
|
+
drawNavMeshWithClosedList(mesh, query, flags = 0) {
|
|
110
|
+
const primitives = this.debugDrawerUtils.drawNavMeshWithClosedList(mesh, query, flags);
|
|
111
|
+
this.drawPrimitives(primitives);
|
|
112
|
+
}
|
|
113
|
+
drawNavMeshNodes(query) {
|
|
114
|
+
const primitives = this.debugDrawerUtils.drawNavMeshNodes(query);
|
|
115
|
+
this.drawPrimitives(primitives);
|
|
116
|
+
}
|
|
117
|
+
drawNavMeshBVTree(mesh) {
|
|
118
|
+
const primitives = this.debugDrawerUtils.drawNavMeshBVTree(mesh);
|
|
119
|
+
this.drawPrimitives(primitives);
|
|
120
|
+
}
|
|
121
|
+
drawNavMeshPortals(mesh) {
|
|
122
|
+
const primitives = this.debugDrawerUtils.drawNavMeshPortals(mesh);
|
|
123
|
+
this.drawPrimitives(primitives);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* @param col color in 0xRRGGBBAA format
|
|
127
|
+
*/
|
|
128
|
+
drawNavMeshPolysWithFlags(mesh, flags, col) {
|
|
129
|
+
const primitives = this.debugDrawerUtils.drawNavMeshPolysWithFlags(mesh, flags, col);
|
|
130
|
+
this.drawPrimitives(primitives);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* @param col color in 0xRRGGBBAA format
|
|
134
|
+
*/
|
|
135
|
+
drawNavMeshPoly(mesh, ref, col) {
|
|
136
|
+
const primitives = this.debugDrawerUtils.drawNavMeshPoly(mesh, ref, col);
|
|
137
|
+
this.drawPrimitives(primitives);
|
|
138
|
+
}
|
|
139
|
+
// todo:
|
|
140
|
+
// - drawTileCacheLayerAreas
|
|
141
|
+
// - drawTileCacheLayerRegions
|
|
142
|
+
// - drawTileCacheContours
|
|
143
|
+
// - drawTileCachePolyMesh
|
|
144
|
+
reset() {
|
|
145
|
+
for (const child of this.children) {
|
|
146
|
+
if (child instanceof THREE.Mesh || child instanceof LineSegments2) {
|
|
147
|
+
child.geometry.dispose();
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
this.clear();
|
|
151
|
+
}
|
|
152
|
+
dispose() {
|
|
153
|
+
this.debugDrawerUtils.dispose();
|
|
154
|
+
this.reset();
|
|
155
|
+
this.pointGeometry.dispose();
|
|
156
|
+
this.triMaterial.dispose();
|
|
157
|
+
this.pointMaterial.dispose();
|
|
158
|
+
this.lineMaterial.dispose();
|
|
159
|
+
}
|
|
160
|
+
drawPoints(primitive) {
|
|
161
|
+
const geometry = this.pointGeometry;
|
|
162
|
+
const instancedMesh = new THREE.InstancedMesh(geometry, this.pointMaterial, primitive.vertices.length / 3);
|
|
163
|
+
for (let point = 0; point < primitive.vertices.length / 7; point++) {
|
|
164
|
+
const [x, y, z, r, g, b] = primitive.vertices[point];
|
|
165
|
+
instancedMesh.setMatrixAt(point, new THREE.Matrix4().setPosition(x, y, z));
|
|
166
|
+
instancedMesh.setColorAt(point, _color.setRGB(r, g, b));
|
|
167
|
+
}
|
|
168
|
+
instancedMesh.instanceMatrix.needsUpdate = true;
|
|
169
|
+
this.add(instancedMesh);
|
|
170
|
+
}
|
|
171
|
+
drawLines(primitive) {
|
|
172
|
+
const lineSegmentsGeometry = new LineSegmentsGeometry();
|
|
173
|
+
const positions = [];
|
|
174
|
+
const colors = [];
|
|
175
|
+
for (let i = 0; i < primitive.vertices.length; i += 2) {
|
|
176
|
+
const [x1, y1, z1, r1, g1, b1] = primitive.vertices[i];
|
|
177
|
+
const [x2, y2, z2, r2, g2, b2] = primitive.vertices[i + 1];
|
|
178
|
+
positions.push(x1, y1, z1);
|
|
179
|
+
positions.push(x2, y2, z2);
|
|
180
|
+
colors.push(r1, g1, b1);
|
|
181
|
+
colors.push(r2, g2, b2);
|
|
182
|
+
}
|
|
183
|
+
lineSegmentsGeometry.setPositions(positions);
|
|
184
|
+
lineSegmentsGeometry.setColors(colors);
|
|
185
|
+
const lineSegments = new LineSegments2(lineSegmentsGeometry, this.lineMaterial);
|
|
186
|
+
this.add(lineSegments);
|
|
187
|
+
}
|
|
188
|
+
drawTris(primitive) {
|
|
189
|
+
const geometry = new THREE.BufferGeometry();
|
|
190
|
+
const positions = new Float32Array(primitive.vertices.length * 3);
|
|
191
|
+
const colors = new Float32Array(primitive.vertices.length * 3);
|
|
192
|
+
for (let i = 0; i < primitive.vertices.length; i++) {
|
|
193
|
+
const [x, y, z, r, g, b] = primitive.vertices[i];
|
|
194
|
+
positions[i * 3 + 0] = x;
|
|
195
|
+
positions[i * 3 + 1] = y;
|
|
196
|
+
positions[i * 3 + 2] = z;
|
|
197
|
+
colors[i * 3 + 0] = r;
|
|
198
|
+
colors[i * 3 + 1] = g;
|
|
199
|
+
colors[i * 3 + 2] = b;
|
|
200
|
+
}
|
|
201
|
+
geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3));
|
|
202
|
+
geometry.setAttribute('color', new THREE.BufferAttribute(colors, 3));
|
|
203
|
+
const material = this.triMaterial;
|
|
204
|
+
const mesh = new THREE.Mesh(geometry, material);
|
|
205
|
+
this.add(mesh);
|
|
206
|
+
}
|
|
207
|
+
drawQuads(primitive) {
|
|
208
|
+
const positions = [];
|
|
209
|
+
const colors = [];
|
|
210
|
+
for (let i = 0; i < primitive.vertices.length; i += 4) {
|
|
211
|
+
const vertices = [primitive.vertices[i], primitive.vertices[i + 1], primitive.vertices[i + 2], primitive.vertices[i], primitive.vertices[i + 2], primitive.vertices[i + 3]];
|
|
212
|
+
for (const [x, y, z, r, g, b] of vertices) {
|
|
213
|
+
positions.push(x, y, z);
|
|
214
|
+
colors.push(r, g, b);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
const geometry = new THREE.BufferGeometry();
|
|
218
|
+
geometry.setAttribute('position', new THREE.BufferAttribute(new Float32Array(positions), 3));
|
|
219
|
+
geometry.setAttribute('color', new THREE.BufferAttribute(new Float32Array(colors), 3));
|
|
220
|
+
const material = this.triMaterial;
|
|
221
|
+
const mesh = new THREE.Mesh(geometry, material);
|
|
222
|
+
this.add(mesh);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
class CrowdHelper extends Object3D {
|
|
227
|
+
agentMeshes = new Map();
|
|
228
|
+
recastCrowd;
|
|
229
|
+
agentMaterial;
|
|
230
|
+
constructor(crowd, params) {
|
|
231
|
+
super();
|
|
232
|
+
this.recastCrowd = crowd;
|
|
233
|
+
this.agentMaterial = params?.agentMaterial ?? new MeshBasicMaterial({
|
|
234
|
+
color: 'red'
|
|
235
|
+
});
|
|
236
|
+
this.update();
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Update the three debug view of the crowd agents.
|
|
240
|
+
*
|
|
241
|
+
* This should be called after updating the crowd.
|
|
242
|
+
*/
|
|
243
|
+
update() {
|
|
244
|
+
const agents = this.recastCrowd.getAgents();
|
|
245
|
+
const unseen = new Set(this.agentMeshes.keys());
|
|
246
|
+
for (const agent of agents) {
|
|
247
|
+
unseen.delete(agent.agentIndex);
|
|
248
|
+
const position = agent.position();
|
|
249
|
+
const velocity = agent.velocity();
|
|
250
|
+
let agentMesh = this.agentMeshes.get(agent.agentIndex);
|
|
251
|
+
if (agentMesh === undefined) {
|
|
252
|
+
agentMesh = this.createAgentMesh(agent);
|
|
253
|
+
this.add(agentMesh);
|
|
254
|
+
this.agentMeshes.set(agent.agentIndex, agentMesh);
|
|
255
|
+
} else {
|
|
256
|
+
this.updateAgentGeometry(agentMesh, agent);
|
|
257
|
+
}
|
|
258
|
+
agentMesh.position.set(position.x, position.y + agent.height / 2, position.z);
|
|
259
|
+
agentMesh.lookAt(new Vector3().copy(agentMesh.position).add(velocity));
|
|
260
|
+
}
|
|
261
|
+
for (const agentId of unseen) {
|
|
262
|
+
const agentMesh = this.agentMeshes.get(agentId);
|
|
263
|
+
if (agentMesh) {
|
|
264
|
+
this.remove(agentMesh);
|
|
265
|
+
this.agentMeshes.delete(agentId);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
createAgentMesh(agent) {
|
|
270
|
+
const mesh = new Mesh();
|
|
271
|
+
mesh.material = this.agentMaterial;
|
|
272
|
+
this.updateAgentGeometry(mesh, agent);
|
|
273
|
+
mesh.userData = {
|
|
274
|
+
radius: agent.radius,
|
|
275
|
+
height: agent.height
|
|
276
|
+
};
|
|
277
|
+
return mesh;
|
|
278
|
+
}
|
|
279
|
+
updateAgentGeometry(agentMesh, agentParams) {
|
|
280
|
+
if (agentMesh.userData.radius !== agentParams.radius || agentMesh.userData.height !== agentParams.height) {
|
|
281
|
+
const geometry = new CylinderGeometry(agentParams.radius, agentParams.radius, agentParams.height);
|
|
282
|
+
agentMesh.geometry.dispose();
|
|
283
|
+
agentMesh.geometry = geometry;
|
|
284
|
+
agentMesh.userData.radius = agentParams.radius;
|
|
285
|
+
agentMesh.userData.height = agentParams.height;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
class NavMeshHelper extends Object3D {
|
|
291
|
+
navMesh;
|
|
292
|
+
mesh;
|
|
293
|
+
navMeshMaterial;
|
|
294
|
+
navMeshGeometry;
|
|
295
|
+
constructor(navMesh, params) {
|
|
296
|
+
super();
|
|
297
|
+
this.navMesh = navMesh;
|
|
298
|
+
this.navMeshGeometry = new BufferGeometry();
|
|
299
|
+
this.navMeshMaterial = params?.navMeshMaterial ? params.navMeshMaterial : new MeshBasicMaterial({
|
|
300
|
+
color: 'orange',
|
|
301
|
+
transparent: true,
|
|
302
|
+
opacity: 0.7,
|
|
303
|
+
depthWrite: false
|
|
304
|
+
});
|
|
305
|
+
this.update();
|
|
306
|
+
this.mesh = new Mesh(this.navMeshGeometry, this.navMeshMaterial);
|
|
307
|
+
this.add(this.mesh);
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Update the three debug nav mesh.
|
|
311
|
+
*
|
|
312
|
+
* This should be called after updating the nav mesh.
|
|
313
|
+
*/
|
|
314
|
+
update() {
|
|
315
|
+
const [positions, indices] = getNavMeshPositionsAndIndices(this.navMesh);
|
|
316
|
+
this.navMeshGeometry.setAttribute('position', new BufferAttribute(Float32Array.from(positions), 3));
|
|
317
|
+
this.navMeshGeometry.setIndex(new BufferAttribute(Uint32Array.from(indices), 1));
|
|
318
|
+
this.navMeshGeometry.computeVertexNormals();
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
class TileCacheHelper extends Object3D {
|
|
323
|
+
tileCache;
|
|
324
|
+
obstacleMeshes = new Map();
|
|
325
|
+
obstacleMaterial;
|
|
326
|
+
constructor(tileCache, params) {
|
|
327
|
+
super();
|
|
328
|
+
this.tileCache = tileCache;
|
|
329
|
+
this.obstacleMaterial = params?.obstacleMaterial ? params.obstacleMaterial : new MeshBasicMaterial({
|
|
330
|
+
color: 'red',
|
|
331
|
+
wireframe: true,
|
|
332
|
+
wireframeLinewidth: 2
|
|
333
|
+
});
|
|
334
|
+
this.update();
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Update the obstacle meshes.
|
|
338
|
+
*
|
|
339
|
+
* This should be called after adding or removing obstacles.
|
|
340
|
+
*/
|
|
341
|
+
update() {
|
|
342
|
+
const unseen = new Set(this.obstacleMeshes.keys());
|
|
343
|
+
for (const [, obstacle] of this.tileCache.obstacles) {
|
|
344
|
+
const obstacleMesh = this.obstacleMeshes.get(obstacle);
|
|
345
|
+
unseen.delete(obstacle);
|
|
346
|
+
if (!obstacleMesh) {
|
|
347
|
+
const {
|
|
348
|
+
position
|
|
349
|
+
} = obstacle;
|
|
350
|
+
const mesh = new Mesh(undefined, this.obstacleMaterial);
|
|
351
|
+
mesh.position.copy(position);
|
|
352
|
+
if (obstacle.type === 'box') {
|
|
353
|
+
const {
|
|
354
|
+
halfExtents,
|
|
355
|
+
angle
|
|
356
|
+
} = obstacle;
|
|
357
|
+
mesh.geometry = new BoxGeometry(halfExtents.x * 2, halfExtents.y * 2, halfExtents.z * 2);
|
|
358
|
+
mesh.rotation.y = angle;
|
|
359
|
+
} else if (obstacle.type === 'cylinder') {
|
|
360
|
+
const {
|
|
361
|
+
radius,
|
|
362
|
+
height
|
|
363
|
+
} = obstacle;
|
|
364
|
+
mesh.geometry = new CylinderGeometry(radius, radius, height, 16);
|
|
365
|
+
mesh.position.y += height / 2;
|
|
366
|
+
} else {
|
|
367
|
+
throw new Error(`Unknown obstacle type: ${obstacle}`);
|
|
368
|
+
}
|
|
369
|
+
this.add(mesh);
|
|
370
|
+
this.obstacleMeshes.set(obstacle, mesh);
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
for (const obstacle of unseen) {
|
|
374
|
+
const obstacleMesh = this.obstacleMeshes.get(obstacle);
|
|
375
|
+
if (obstacleMesh) {
|
|
376
|
+
this.remove(obstacleMesh);
|
|
377
|
+
this.obstacleMeshes.delete(obstacle);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
const _position = new Vector3();
|
|
384
|
+
const getPositionsAndIndices = meshes => {
|
|
385
|
+
const toMerge = [];
|
|
386
|
+
for (const mesh of meshes) {
|
|
387
|
+
const positionAttribute = mesh.geometry.attributes.position;
|
|
388
|
+
if (!positionAttribute || positionAttribute.itemSize !== 3) {
|
|
389
|
+
continue;
|
|
390
|
+
}
|
|
391
|
+
mesh.updateMatrixWorld();
|
|
392
|
+
const positions = new Float32Array(positionAttribute.array);
|
|
393
|
+
for (let i = 0; i < positions.length; i += 3) {
|
|
394
|
+
const pos = _position.set(positions[i], positions[i + 1], positions[i + 2]);
|
|
395
|
+
mesh.localToWorld(pos);
|
|
396
|
+
positions[i] = pos.x;
|
|
397
|
+
positions[i + 1] = pos.y;
|
|
398
|
+
positions[i + 2] = pos.z;
|
|
399
|
+
}
|
|
400
|
+
let indices = mesh.geometry.getIndex()?.array;
|
|
401
|
+
if (indices === undefined) {
|
|
402
|
+
// this will become indexed when merging with other meshes
|
|
403
|
+
const ascendingIndex = [];
|
|
404
|
+
for (let i = 0; i < positionAttribute.count; i++) {
|
|
405
|
+
ascendingIndex.push(i);
|
|
406
|
+
}
|
|
407
|
+
indices = ascendingIndex;
|
|
408
|
+
}
|
|
409
|
+
toMerge.push({
|
|
410
|
+
positions,
|
|
411
|
+
indices
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
return mergePositionsAndIndices(toMerge);
|
|
415
|
+
};
|
|
416
|
+
|
|
417
|
+
const threeToSoloNavMesh = (meshes, navMeshGeneratorConfig = {}, keepIntermediates = false) => {
|
|
418
|
+
const [positions, indices] = getPositionsAndIndices(meshes);
|
|
419
|
+
return generateSoloNavMesh(positions, indices, navMeshGeneratorConfig, keepIntermediates);
|
|
420
|
+
};
|
|
421
|
+
const threeToTiledNavMesh = (meshes, navMeshGeneratorConfig = {}, keepIntermediates = false) => {
|
|
422
|
+
const [positions, indices] = getPositionsAndIndices(meshes);
|
|
423
|
+
return generateTiledNavMesh(positions, indices, navMeshGeneratorConfig, keepIntermediates);
|
|
424
|
+
};
|
|
425
|
+
const threeToTileCache = (meshes, navMeshGeneratorConfig = {}, keepIntermediates = false) => {
|
|
426
|
+
const [positions, indices] = getPositionsAndIndices(meshes);
|
|
427
|
+
return generateTileCache(positions, indices, navMeshGeneratorConfig, keepIntermediates);
|
|
428
|
+
};
|
|
429
|
+
|
|
430
|
+
export { CrowdHelper, DebugDrawer, NavMeshHelper, TileCacheHelper, getPositionsAndIndices, threeToSoloNavMesh, threeToTileCache, threeToTiledNavMesh };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type SoloNavMeshGeneratorConfig, type TileCacheGeneratorConfig, type TiledNavMeshGeneratorConfig } from '@recast-navigation/generators';
|
|
2
|
+
import type { Mesh } from 'three';
|
|
3
|
+
export declare const threeToSoloNavMesh: (meshes: Mesh[], navMeshGeneratorConfig?: Partial<SoloNavMeshGeneratorConfig>, keepIntermediates?: boolean) => import("@recast-navigation/generators").GenerateSoloNavMeshResult;
|
|
4
|
+
export declare const threeToTiledNavMesh: (meshes: Mesh[], navMeshGeneratorConfig?: Partial<TiledNavMeshGeneratorConfig>, keepIntermediates?: boolean) => import("@recast-navigation/generators").GenerateTiledNavMeshResult;
|
|
5
|
+
export declare const threeToTileCache: (meshes: Mesh[], navMeshGeneratorConfig?: Partial<TileCacheGeneratorConfig>, keepIntermediates?: boolean) => import("@recast-navigation/generators").TileCacheGeneratorResult;
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@recast-navigation/three",
|
|
3
|
+
"description": "Utilities for using recast-navigation with Three.js",
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "git+https://github.com/isaac-mason/recast-navigation-js.git",
|
|
7
|
+
"directory": "packages/recast-navigation-three"
|
|
8
|
+
},
|
|
9
|
+
"author": {
|
|
10
|
+
"name": "Isaac Mason",
|
|
11
|
+
"email": "isaac@isaacmason.com",
|
|
12
|
+
"url": "https://isaacmason.com/"
|
|
13
|
+
},
|
|
14
|
+
"version": "0.0.0-canary-20261030-064e4acc",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"homepage": "https://github.com/isaac-mason/recast-navigation-js",
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/isaac-mason/recast-navigation-js/issues"
|
|
19
|
+
},
|
|
20
|
+
"type": "module",
|
|
21
|
+
"main": "./dist/index.mjs",
|
|
22
|
+
"module": "./dist/index.mjs",
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"files": [
|
|
25
|
+
"dist/**",
|
|
26
|
+
"README.md",
|
|
27
|
+
"LICENSE"
|
|
28
|
+
],
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@recast-navigation/core": "0.0.0-canary-20261030-064e4acc",
|
|
31
|
+
"@recast-navigation/generators": "0.0.0-canary-20261030-064e4acc"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"@types/three": "0.x.x",
|
|
35
|
+
"three": "0.x.x"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@babel/core": "^8.0.10",
|
|
39
|
+
"@babel/preset-env": "^8.0.7",
|
|
40
|
+
"@babel/preset-typescript": "^8.0.7",
|
|
41
|
+
"@rollup/plugin-babel": "^7.1.0",
|
|
42
|
+
"@rollup/plugin-commonjs": "^29.0.3",
|
|
43
|
+
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
44
|
+
"@rollup/plugin-typescript": "^12.3.1",
|
|
45
|
+
"@types/three": "^0.186.0",
|
|
46
|
+
"rollup": "^4.66.0",
|
|
47
|
+
"rollup-plugin-copy": "^3.5.0",
|
|
48
|
+
"rollup-plugin-filesize": "^10.0.0",
|
|
49
|
+
"three": "^0.186.1",
|
|
50
|
+
"typescript": "^6.0.3"
|
|
51
|
+
},
|
|
52
|
+
"scripts": {
|
|
53
|
+
"build": "rollup --config rollup.config.js --bundleConfigAsCjs",
|
|
54
|
+
"test": "tsc"
|
|
55
|
+
}
|
|
56
|
+
}
|