1 |
- !function e(t,n,r){function i(s,u){if(!n[s]){if(!t[s]){var a="function"==typeof require&&require;if(!u&&a)return a(s,!0);if(o)return o(s,!0);var c=new Error("Cannot find module '"+s+"'");throw c.code="MODULE_NOT_FOUND",c}var h=n[s]={exports:{}};t[s][0].call(h.exports,function(e){var n=t[s][1][e];return i(n||e)},h,h.exports,e,t,n,r)}return n[s].exports}for(var o="function"==typeof require&&require,s=0;s<r.length;s++)i(r[s]);return i}({1:[function(e,t,n){"use strict";e("./src/pathfinding")},{"./src/pathfinding":2}],2:[function(e,t,n){"use strict";e("./nav-mesh"),e("./nav-agent"),e("./system")},{"./nav-agent":3,"./nav-mesh":4,"./system":5}],3:[function(e,t,n){"use strict";t.exports=AFRAME.registerComponent("nav-agent",{schema:{destination:{type:"vec3"},active:{default:!1},speed:{default:2}},init:function(){this.system=this.el.sceneEl.systems.nav,this.system.addAgent(this),this.group=null,this.path=[],this.raycaster=new THREE.Raycaster},remove:function(){this.system.removeAgent(this)},update:function(){this.path.length=0},updateNavLocation:function(){this.group=null,this.path=[]},tick:function(){var e=new THREE.Vector3,t=new THREE.Vector3,n=new THREE.Vector3;return function(r,i){var o=this.el,s=this.data,u=this.raycaster,a=s.speed*i/1e3;if(s.active){if(!this.path.length){var c=this.el.object3D.position;this.group=this.group||this.system.getGroup(c),this.path=this.system.getPath(c,e.copy(s.destination),this.group)||[],o.emit("nav-start")}if(!this.path.length)return console.warn("[nav] Unable to find path to %o.",s.destination),this.el.setAttribute("nav-agent",{active:!1}),void o.emit("nav-end");var h=o.object3D.position,l=this.path[0];t.subVectors(l,h);var f=void 0;if(t.length()<a){if(this.path.shift(),!this.path.length)return this.el.setAttribute("nav-agent",{active:!1}),void o.emit("nav-end");n.copy(h),f=this.path[0]}else n.copy(t.setLength(a)).add(h),f=l;f.y=h.y,o.object3D.lookAt(f),u.ray.origin.copy(n),u.ray.origin.y+=1.5,u.ray.direction.y=-1;var v=u.intersectObject(this.system.getNavMesh());v.length?(t.subVectors(v[0].point,h),h.add(t.setLength(a))):h.copy(n)}}}()})},{}],4:[function(e,t,n){"use strict";t.exports=AFRAME.registerComponent("nav-mesh",{init:function(){this.system=this.el.sceneEl.systems.nav,this.hasLoadedNavMesh=!1,this.el.addEventListener("model-loaded",this.loadNavMesh.bind(this))},play:function(){this.hasLoadedNavMesh||this.loadNavMesh()},loadNavMesh:function(){var e=this.el.getObject3D("mesh"),t=this.el.sceneEl.object3D;if(e){var n=void 0;if(e.traverse(function(e){e.isMesh&&(n=e)}),n){var r=n.geometry.isBufferGeometry?(new THREE.Geometry).fromBufferGeometry(n.geometry):n.geometry.clone();t.updateMatrixWorld(),r.applyMatrix(n.matrixWorld),this.system.setNavMeshGeometry(r),this.hasLoadedNavMesh=!0}}}})},{}],5:[function(e,t,n){"use strict";var r=e("three-pathfinding"),i=new r,o="level";t.exports=AFRAME.registerSystem("nav",{init:function(){this.navMesh=null,this.agents=new Set},setNavMeshGeometry:function(e){this.navMesh=new THREE.Mesh(e),i.setZoneData(o,r.createZone(e)),Array.from(this.agents).forEach(function(e){return e.updateNavLocation()})},getNavMesh:function(){return this.navMesh},addAgent:function(e){this.agents.add(e)},removeAgent:function(e){this.agents.delete(e)},getPath:function(e,t,n){return i.findPath(e,t,o,n)},getGroup:function(e){return i.getGroup(o,e)},getNode:function(e,t){return i.getClosestNode(e,o,t,!0)},clampStep:function(e,t,n,r,s){return this.navMesh&&r?i.clampStep(e,t,r,o,n,s):(s.copy(t),this.navMesh?this.getNode(t,n):null)}})},{"three-pathfinding":10}],6:[function(e,t,n){"use strict";var r=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),i=e("./BinaryHeap"),o=e("./utils.js"),s=function(){function e(){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e)}return r(e,null,[{key:"init",value:function(e){for(var t=0;t<e.length;t++){var n=e[t];n.f=0,n.g=0,n.h=0,n.cost=1,n.visited=!1,n.closed=!1,n.parent=null}}},{key:"cleanUp",value:function(e){for(var t=0;t<e.length;t++){var n=e[t];delete n.f,delete n.g,delete n.h,delete n.cost,delete n.visited,delete n.closed,delete n.parent}}},{key:"heap",value:function(){return new i(function(e){return e.f})}},{key:"search",value:function(e,t,n){this.init(e);var r=this.heap();for(r.push(t);r.size()>0;){var i=r.pop();if(i===n){for(var o=i,s=[];o.parent;)s.push(o),o=o.parent;return this.cleanUp(s),s.reverse()}i.closed=!0;for(var u=this.neighbours(e,i),a=0,c=u.length;a<c;a++){var h=u[a];if(!h.closed){var l=i.g+h.cost,f=h.visited;if(!f||l<h.g){if(h.visited=!0,h.parent=i,!h.centroid||!n.centroid)throw new Error("Unexpected state");h.h=h.h||this.heuristic(h.centroid,n.centroid),h.g=l,h.f=h.g+h.h,f?r.rescoreElement(h):r.push(h)}}}}return[]}},{key:"heuristic",value:function(e,t){return o.distanceToSquared(e,t)}},{key:"neighbours",value:function(e,t){for(var n=[],r=0;r<t.neighbours.length;r++)n.push(e[t.neighbours[r]]);return n}}]),e}();t.exports=s},{"./BinaryHeap":7,"./utils.js":11}],7:[function(e,t,n){"use strict";var r=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),i=function(){function e(t){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.content=[],this.scoreFunction=t}return r(e,[{key:"push",value:function(e){this.content.push(e),this.sinkDown(this.content.length-1)}},{key:"pop",value:function(){var e=this.content[0],t=this.content.pop();return this.content.length>0&&(this.content[0]=t,this.bubbleUp(0)),e}},{key:"remove",value:function(e){var t=this.content.indexOf(e),n=this.content.pop();t!==this.content.length-1&&(this.content[t]=n,this.scoreFunction(n)<this.scoreFunction(e)?this.sinkDown(t):this.bubbleUp(t))}},{key:"size",value:function(){return this.content.length}},{key:"rescoreElement",value:function(e){this.sinkDown(this.content.indexOf(e))}},{key:"sinkDown",value:function(e){for(var t=this.content[e];e>0;){var n=(e+1>>1)-1,r=this.content[n];if(!(this.scoreFunction(t)<this.scoreFunction(r)))break;this.content[n]=t,this.content[e]=r,e=n}}},{key:"bubbleUp",value:function(e){for(var t=this.content.length,n=this.content[e],r=this.scoreFunction(n);;){var i=e+1<<1,o=i-1,s=null,u=void 0;if(o<t){var a=this.content[o];(u=this.scoreFunction(a))<r&&(s=o)}if(i<t){var c=this.content[i];this.scoreFunction(c)<(null===s?r:u)&&(s=i)}if(null===s)break;this.content[e]=this.content[s],this.content[s]=n,e=s}}}]),e}();t.exports=i},{}],8:[function(e,t,n){"use strict";var r=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),i=e("./utils"),o=1,s=function(){function e(){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e)}return r(e,null,[{key:"buildZone",value:function(e){var t=this,n=this._buildNavigationMesh(e),r={};n.vertices.forEach(function(e){e.x=i.roundNumber(e.x,2),e.y=i.roundNumber(e.y,2),e.z=i.roundNumber(e.z,2)}),r.vertices=n.vertices;var o=this._buildPolygonGroups(n);r.groups=[];var s=function(e,t){for(var n=0;n<e.length;n++)if(t===e[n])return n};return o.forEach(function(e){var n=[];e.forEach(function(r){var o=r.neighbours.map(function(t){return s(e,t)}),u=r.neighbours.map(function(e){return t._getSharedVerticesInOrder(r,e)});r.centroid.x=i.roundNumber(r.centroid.x,2),r.centroid.y=i.roundNumber(r.centroid.y,2),r.centroid.z=i.roundNumber(r.centroid.z,2),n.push({id:s(e,r),neighbours:o,vertexIds:r.vertexIds,centroid:r.centroid,portals:u})}),r.groups.push(n)}),r}},{key:"_buildNavigationMesh",value:function(e){return i.computeCentroids(e),e.mergeVertices(),this._buildPolygonsFromGeometry(e)}},{key:"_buildPolygonGroups",value:function(e){var t=[],n=0;return e.polygons.forEach(function(e){void 0===e.group&&(e.group=n++,function e(t){t.neighbours.forEach(function(n){void 0===n.group&&(n.group=t.group,e(n))})}(e)),t[e.group]||(t[e.group]=[]),t[e.group].push(e)}),t}},{key:"_buildPolygonNeighbours",value:function(e,t){e.neighbours=[];for(var n=0,r=t.polygons.length;n<r;n++)if(e!==t.polygons[n]&&!(e.centroid.distanceToSquared(t.polygons[n].centroid)>1e4)){i.array_intersect(e.vertexIds,t.polygons[n].vertexIds).length>=2&&e.neighbours.push(t.polygons[n])}}},{key:"_buildPolygonsFromGeometry",value:function(e){var t=this,n=[],r=e.vertices,i=e.faceVertexUvs;e.faces.forEach(function(e){n.push({id:o++,vertexIds:[e.a,e.b,e.c],centroid:e.centroid,normal:e.normal,neighbours:[]})});var s={polygons:n,vertices:r,faceVertexUvs:i};return n.forEach(function(e){t._buildPolygonNeighbours(e,s)}),s}},{key:"_getSharedVerticesInOrder",value:function(e,t){var n=e.vertexIds,r=t.vertexIds,i=[];return n.forEach(function(e){r.includes(e)&&i.push(e)}),i.length<2?[]:(i.includes(n[0])&&i.includes(n[n.length-1])&&n.push(n.shift()),i.includes(r[0])&&i.includes(r[r.length-1])&&r.push(r.shift()),i.length=0,n.forEach(function(e){r.includes(e)&&i.push(e)}),i)}}]),e}();t.exports=s},{"./utils":11}],9:[function(e,t,n){"use strict";var r=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),i=e("./utils"),o=function(){function e(){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.portals=[]}return r(e,[{key:"push",value:function(e,t){void 0===t&&(t=e),this.portals.push({left:e,right:t})}},{key:"stringPull",value:function(){var e=this.portals,t=[],n=void 0,r=void 0,o=void 0,s=0,u=0,a=0;n=e[0].left,r=e[0].left,o=e[0].right,t.push(n);for(var c=1;c<e.length;c++){var h=e[c].left,l=e[c].right;if(i.triarea2(n,o,l)<=0){if(!(i.vequal(n,o)||i.triarea2(n,r,l)>0)){t.push(r),r=n=r,o=n,u=s=u,a=s,c=s;continue}o=l,a=c}if(i.triarea2(n,r,h)>=0){if(!(i.vequal(n,r)||i.triarea2(n,o,h)<0)){t.push(o),r=n=o,o=n,u=s=a,a=s,c=s;continue}r=h,u=c}}return 0!==t.length&&i.vequal(t[t.length-1],e[e.length-1].left)||t.push(e[e.length-1].left),this.path=t,t}}]),e}();t.exports=o},{"./utils":11}],10:[function(e,t,n){"use strict";var r=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),i=e("./utils"),o=e("./AStar"),s=e("./Builder"),u=e("./Channel"),a=function(){function e(){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.zones={}}return r(e,[{key:"setZoneData",value:function(e,t){this.zones[e]=t}},{key:"getGroup",value:function(e,t){if(!this.zones[e])return null;var n=null,r=Math.pow(50,2);return this.zones[e].groups.forEach(function(e,o){e.forEach(function(e){var s=i.distanceToSquared(e.centroid,t);s<r&&(n=o,r=s)})}),n}},{key:"getRandomNode",value:function(e,t,n,r){if(!this.zones[e])return new THREE.Vector3;n=n||null,r=r||0;var o=[];return this.zones[e].groups[t].forEach(function(e){n&&r?i.distanceToSquared(n,e.centroid)<r*r&&o.push(e.centroid):o.push(e.centroid)}),i.sample(o)||new THREE.Vector3}},{key:"getClosestNode",value:function(e,t,n){var r=arguments.length>3&&void 0!==arguments[3]&&arguments[3],o=this.zones[t].groups[n],s=this.zones[t].vertices,u=null,a=1/0;return o.forEach(function(t){var n=i.distanceToSquared(t.centroid,e);n<a&&(!r||i.isVectorInPolygon(e,t,s))&&(u=t,a=n)}),u}},{key:"findPath",value:function(e,t,n,r){var i=this.zones[n].groups[r],s=this.zones[n].vertices,a=this.getClosestNode(e,n,r),c=this.getClosestNode(t,n,r,!0);if(!a||!c)return null;var h=o.search(i,a,c),l=function(e,t){for(var n=0;n<e.neighbours.length;n++)if(e.neighbours[n]===t.id)return e.portals[n]},f=new u;f.push(e);for(var v=0;v<h.length;v++){var d=h[v],p=h[v+1];if(p){var g=l(d,p);f.push(s[g[0]],s[g[1]])}}f.push(t),f.stringPull();var y=f.path.map(function(e){return new THREE.Vector3(e.x,e.y,e.z)});return y.shift(),y}}],[{key:"createZone",value:function(e){return s.buildZone(e)}}]),e}();a.prototype.clampStep=function(){var e=new THREE.Vector3,t=new THREE.Plane,n=new THREE.Triangle,r=void 0,i=new THREE.Vector3,o=void 0;return function(s,u,a,c,h,l){var f=this.zones[c].vertices,v=this.zones[c].groups[h],d=[a],p={};p[a.id]=0,r=void 0,i.set(0,0,0),o=1/0,t.setFromCoplanarPoints(f[a.vertexIds[0]],f[a.vertexIds[1]],f[a.vertexIds[2]]),t.projectPoint(u,e),u.copy(e);for(var g=d.pop();g;g=d.pop()){n.set(f[g.vertexIds[0]],f[g.vertexIds[1]],f[g.vertexIds[2]]),n.closestPointToPoint(u,e),e.distanceToSquared(u)<o&&(r=g,i.copy(e),o=e.distanceToSquared(u));var y=p[g];if(!(y>2))for(var b=0;b<g.neighbours.length;b++){var m=v[g.neighbours[b]];m.id in p||(d.push(m),p[m.id]=y+1)}}return l.copy(i),r}}();t.exports=a},{"./AStar":6,"./Builder":8,"./Channel":9,"./utils":11}],11:[function(e,t,n){"use strict";var r=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),i=function(){function e(){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e)}return r(e,null,[{key:"computeCentroids",value:function(e){var t,n,r;for(t=0,n=e.faces.length;t<n;t++)(r=e.faces[t]).centroid=new THREE.Vector3(0,0,0),r.centroid.add(e.vertices[r.a]),r.centroid.add(e.vertices[r.b]),r.centroid.add(e.vertices[r.c]),r.centroid.divideScalar(3)}},{key:"roundNumber",value:function(e,t){var n=Number(e+"").toFixed(parseInt(t));return parseFloat(n)}},{key:"sample",value:function(e){return e[Math.floor(Math.random()*e.length)]}},{key:"mergeVertexIds",value:function(e,t){var n=[];if(e.forEach(function(e){t.indexOf(e)>=0&&n.push(e)}),n.length<2)return[];n.includes(e[0])&&n.includes(e[e.length-1])&&e.push(e.shift()),n.includes(t[0])&&n.includes(t[t.length-1])&&t.push(t.shift()),n=[],e.forEach(function(e){t.includes(e)&&n.push(e)});for(var r=n[1],i=n[0],o=e.slice();o[0]!==r;)o.push(o.shift());for(var s=0,u=t.slice();u[0]!==i;)if(u.push(u.shift()),s++>10)throw new Error("Unexpected state");return u.shift(),u.pop(),o=o.concat(u)}},{key:"setPolygonCentroid",value:function(e,t){var n=new THREE.Vector3,r=t.vertices;e.vertexIds.forEach(function(e){n.add(r[e])}),n.divideScalar(e.vertexIds.length),e.centroid.copy(n)}},{key:"cleanPolygon",value:function(e,t){for(var n=[],r=t.vertices,i=0;i<e.vertexIds.length;i++){var o,s,u,a,c=r[e.vertexIds[i]];0===i?(o=e.vertexIds[1],s=e.vertexIds[e.vertexIds.length-1]):i===e.vertexIds.length-1?(o=e.vertexIds[0],s=e.vertexIds[e.vertexIds.length-2]):(o=e.vertexIds[i+1],s=e.vertexIds[i-1]),u=r[o],a=r[s];var h=u.clone().sub(c),l=a.clone().sub(c),f=h.angleTo(l);if(f>Math.PI-.01&&f<Math.PI+.01){var v=[];e.neighbours.forEach(function(t){t.vertexIds.includes(e.vertexIds[i])||v.push(t)}),e.neighbours=v}else n.push(e.vertexIds[i])}e.vertexIds=n,this.setPolygonCentroid(e,t)}},{key:"isConvex",value:function(e,t){var n=t.vertices;if(e.vertexIds.length<3)return!1;for(var r=!0,i=[],o=0;o<e.vertexIds.length;o++){var s,u,a=n[e.vertexIds[o]];0===o?(s=n[e.vertexIds[1]],u=n[e.vertexIds[e.vertexIds.length-1]]):o===e.vertexIds.length-1?(s=n[e.vertexIds[0]],u=n[e.vertexIds[e.vertexIds.length-2]]):(s=n[e.vertexIds[o+1]],u=n[e.vertexIds[o-1]]);var c=s.clone().sub(a),h=u.clone().sub(a),l=c.angleTo(h);if(l,l===Math.PI||0===l)return!1;var f=c.cross(h).y;i.push(f)}return i.forEach(function(e){0===e&&(r=!1)}),i[0]>0?i.forEach(function(e){e<0&&(r=!1)}):i.forEach(function(e){e>0&&(r=!1)}),r}},{key:"distanceToSquared",value:function(e,t){var n=e.x-t.x,r=e.y-t.y,i=e.z-t.z;return n*n+r*r+i*i}},{key:"isPointInPoly",value:function(e,t){for(var n=!1,r=-1,i=e.length,o=i-1;++r<i;o=r)(e[r].z<=t.z&&t.z<e[o].z||e[o].z<=t.z&&t.z<e[r].z)&&t.x<(e[o].x-e[r].x)*(t.z-e[r].z)/(e[o].z-e[r].z)+e[r].x&&(n=!n);return n}},{key:"isVectorInPolygon",value:function(e,t,n){var r=1e5,i=-1e5,o=[];return t.vertexIds.forEach(function(e){r=Math.min(n[e].y,r),i=Math.max(n[e].y,i),o.push(n[e])}),!!(e.y<i+.5&&e.y>r-.5&&this.isPointInPoly(o,e))}},{key:"triarea2",value:function(e,t,n){var r=t.x-e.x,i=t.z-e.z;return(n.x-e.x)*i-r*(n.z-e.z)}},{key:"vequal",value:function(e,t){return this.distanceToSquared(e,t)<1e-5}},{key:"array_intersect",value:function(){var e=void 0,t=void 0,n=void 0,r=void 0,i=void 0,o=[],s={},u=void 0;for(u=arguments.length-1,n=arguments[0].length,t=0,e=0;e<=u;e++)(r=arguments[e].length)<n&&(t=e,n=r);for(e=0;e<=u;e++){i=arguments[r=e===t?0:e||t].length;for(var a=0;a<i;a++){var c=arguments[r][a];s[c]===e-1?e===u?(o.push(c),s[c]=0):s[c]=e:0===e&&(s[c]=0)}}return o}}]),e}();t.exports=i},{}]},{},[1]);
|