forked from c-frame/aframe-extras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nav-mesh.js
42 lines (35 loc) · 1 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
/**
* 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;
scene.updateMatrixWorld();
this.system.setNavMeshGeometry(navMesh.geometry);
this.hasLoadedNavMesh = true;
}
});