-
Notifications
You must be signed in to change notification settings - Fork 304
/
nav-mesh.js
44 lines (37 loc) · 1.12 KB
/
nav-mesh.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* nav-mesh
*
* Waits for a mesh to be loaded on the current entity, then sets it as the
* nav mesh in the pathfinding system.
*/
module.exports = AFRAME.registerComponent('nav-mesh', {
schema: {
nodeName: {type: 'string'}
},
init: function () {
this.system = this.el.sceneEl.systems.nav;
this.hasLoadedNavMesh = false;
this.nodeName = this.data.nodeName;
this.el.addEventListener('object3dset', this.loadNavMesh.bind(this));
},
play: function () {
if (!this.hasLoadedNavMesh) this.loadNavMesh();
},
loadNavMesh: function () {
var self = this;
const object = this.el.getObject3D('mesh');
const scene = this.el.sceneEl.object3D;
if (!object) return;
let navMesh;
object.traverse((node) => {
if (node.isMesh &&
(!self.nodeName || node.name === self.nodeName)) navMesh = node;
});
if (!navMesh) return;
const navMeshGeometry = navMesh.geometry.clone();
navMesh.updateWorldMatrix(true, false);
navMeshGeometry.applyMatrix4(navMesh.matrixWorld);
this.system.setNavMeshGeometry(navMeshGeometry);
this.hasLoadedNavMesh = true;
}
});