aframe-extras
Version:
Add-ons and examples for A-Frame VR.
45 lines (37 loc) • 1.13 kB
JavaScript
/**
* nav-mesh
*
* Waits for a mesh to be loaded on the current entity, then sets it as the
* nav mesh in the pathfinding system.
*/
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;
}
});