1 |
- !function t(e,n,r){function s(i,c){if(!n[i]){if(!e[i]){var h="function"==typeof require&&require;if(!c&&h)return h(i,!0);if(o)return o(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var u=n[i]={exports:{}};e[i][0].call(u.exports,function(t){var n=e[i][1][t];return s(n||t)},u,u.exports,t,e,n,r)}return n[i].exports}for(var o="function"==typeof require&&require,i=0;i<r.length;i++)s(r[i]);return s}({1:[function(t,e,n){t("./src/pathfinding").registerAll()},{"./src/pathfinding":7}],2:[function(t,e,n){const r=t("./BinaryHeap"),s=t("./utils.js");class o{static init(t){for(let e=0;e<t.length;e++){const n=t[e];n.f=0,n.g=0,n.h=0,n.cost=1,n.visited=!1,n.closed=!1,n.parent=null}}static cleanUp(t){for(let e=0;e<t.length;e++){const n=t[e];delete n.f,delete n.g,delete n.h,delete n.cost,delete n.visited,delete n.closed,delete n.parent}}static heap(){return new r(function(t){return t.f})}static search(t,e,n){this.init(t);const r=this.heap();for(r.push(e);r.size()>0;){const e=r.pop();if(e===n){let t=e;const n=[];for(;t.parent;)n.push(t),t=t.parent;return this.cleanUp(n),n.reverse()}e.closed=!0;const s=this.neighbours(t,e);for(let t=0,o=s.length;t<o;t++){const o=s[t];if(o.closed)continue;const i=e.g+o.cost,c=o.visited;if(!c||i<o.g){if(o.visited=!0,o.parent=e,!o.centroid||!n.centroid)throw new Error("Unexpected state");o.h=o.h||this.heuristic(o.centroid,n.centroid),o.g=i,o.f=o.g+o.h,c?r.rescoreElement(o):r.push(o)}}}return[]}static heuristic(t,e){return s.distanceToSquared(t,e)}static neighbours(t,e){const n=[];for(let r=0;r<e.neighbours.length;r++)n.push(t[e.neighbours[r]]);return n}}e.exports=o},{"./BinaryHeap":3,"./utils.js":6}],3:[function(t,e,n){class r{constructor(t){this.content=[],this.scoreFunction=t}push(t){this.content.push(t),this.sinkDown(this.content.length-1)}pop(){const t=this.content[0],e=this.content.pop();return this.content.length>0&&(this.content[0]=e,this.bubbleUp(0)),t}remove(t){const e=this.content.indexOf(t),n=this.content.pop();e!==this.content.length-1&&(this.content[e]=n,this.scoreFunction(n)<this.scoreFunction(t)?this.sinkDown(e):this.bubbleUp(e))}size(){return this.content.length}rescoreElement(t){this.sinkDown(this.content.indexOf(t))}sinkDown(t){const e=this.content[t];for(;t>0;){const n=(t+1>>1)-1,r=this.content[n];if(!(this.scoreFunction(e)<this.scoreFunction(r)))break;this.content[n]=e,this.content[t]=r,t=n}}bubbleUp(t){const e=this.content.length,n=this.content[t],r=this.scoreFunction(n);for(;;){const s=t+1<<1,o=s-1;let i,c=null;if(o<e){const t=this.content[o];(i=this.scoreFunction(t))<r&&(c=o)}if(s<e){const t=this.content[s];this.scoreFunction(t)<(null===c?r:i)&&(c=s)}if(null===c)break;this.content[t]=this.content[c],this.content[c]=n,t=c}}}e.exports=r},{}],4:[function(t,e,n){const r=t("./utils");class s{constructor(){this.portals=[]}push(t,e){void 0===e&&(e=t),this.portals.push({left:t,right:e})}stringPull(){const t=this.portals,e=[];let n,s,o,i=0,c=0,h=0;n=t[0].left,s=t[0].left,o=t[0].right,e.push(n);for(let a=1;a<t.length;a++){const u=t[a].left,l=t[a].right;if(r.triarea2(n,o,l)<=0){if(!(r.vequal(n,o)||r.triarea2(n,s,l)>0)){e.push(s),s=n=s,o=n,c=i=c,h=i,a=i;continue}o=l,h=a}if(r.triarea2(n,s,u)>=0){if(!(r.vequal(n,s)||r.triarea2(n,o,u)<0)){e.push(o),s=n=o,o=n,c=i=h,h=i,a=i;continue}s=u,c=a}}return 0!==e.length&&r.vequal(e[e.length-1],t[t.length-1].left)||e.push(t[t.length-1].left),this.path=e,e}}e.exports=s},{"./utils":6}],5:[function(t,e,n){const r=t("./utils"),s=t("./AStar"),o=t("./Channel");var i=1,c=function(t){console.log("Vertices:",t.vertices.length,"polygons:",t.faces.length);var e=[],n=t.vertices,s=t.faceVertexUvs;t.faces.forEach(t=>{e.push({id:i++,vertexIds:[t.a,t.b,t.c],centroid:t.centroid,normal:t.normal,neighbours:[]})});var o={polygons:e,vertices:n,faceVertexUvs:s};return e.forEach(t=>{!function(t,e){t.neighbours=[];for(var n=0,s=e.polygons.length;n<s;n++)t===e.polygons[n]||t.centroid.distanceToSquared(e.polygons[n].centroid)>1e4||r.array_intersect(t.vertexIds,e.polygons[n].vertexIds).length>=2&&t.neighbours.push(e.polygons[n])}(t,o)}),o},h=function(t){var e={};t.vertices.forEach(t=>{t.x=r.roundNumber(t.x,2),t.y=r.roundNumber(t.y,2),t.z=r.roundNumber(t.z,2)}),e.vertices=t.vertices;var n=function(t){var e=[],n=0,r=function(t){t.neighbours.forEach(e=>{void 0===e.group&&(e.group=t.group,r(e))})};return t.polygons.forEach(t=>{void 0===t.group&&(t.group=n++,r(t)),e[t.group]||(e[t.group]=[]),e[t.group].push(t)}),console.log("Groups built: ",e.length),e}(t);e.groups=[];var s=function(t,e){for(var n=0;n<t.length;n++)if(e===t[n])return n};return n.forEach(t=>{var n=[];t.forEach(e=>{var o=[];e.neighbours.forEach(e=>{o.push(s(t,e))});var i=[];e.neighbours.forEach(t=>{i.push(function(t,e){var n=t.vertexIds,r=e.vertexIds,s=[];return n.forEach(t=>{r.includes(t)&&s.push(t)}),s.length<2?[]:(s.includes(n[0])&&s.includes(n[n.length-1])&&n.push(n.shift()),s.includes(r[0])&&s.includes(r[r.length-1])&&r.push(r.shift()),s=[],n.forEach(t=>{r.includes(t)&&s.push(t)}),s)}(e,t))}),e.centroid.x=r.roundNumber(e.centroid.x,2),e.centroid.y=r.roundNumber(e.centroid.y,2),e.centroid.z=r.roundNumber(e.centroid.z,2),n.push({id:s(t,e),neighbours:o,vertexIds:e.vertexIds,centroid:e.centroid,portals:i})}),e.groups.push(n)}),e},a={};e.exports={buildNodes:function(t){var e=function(t){return r.computeCentroids(t),t.mergeVertices(),c(t)}(t);return h(e)},setZoneData:function(t,e){a[t]=e},getGroup:function(t,e){if(!a[t])return null;var n=null,s=Math.pow(50,2);return a[t].groups.forEach((t,o)=>{t.forEach(t=>{var i=r.distanceToSquared(t.centroid,e);i<s&&(n=o,s=i)})}),n},getRandomNode:function(t,e,n,s){if(!a[t])return new THREE.Vector3;n=n||null,s=s||0;var o=[];return a[t].groups[e].forEach(t=>{n&&s?r.distanceToSquared(n,t.centroid)<s*s&&o.push(t.centroid):o.push(t.centroid)}),r.sample(o)||new THREE.Vector3},getClosestNode:function(t,e,n,s=!1){const o=a[e].groups[n],i=a[e].vertices;let c=null,h=1/0;return o.forEach(e=>{const n=r.distanceToSquared(e.centroid,t);n<h&&(!s||r.isVectorInPolygon(t,e,i))&&(c=e,h=n)}),c},findPath:function(t,e,n,r){const i=a[n].groups[r],c=a[n].vertices,h=this.getClosestNode(t,n,r),u=this.getClosestNode(e,n,r,!0);if(!h||!u)return null;const l=s.search(i,h,u),d=function(t,e){for(var n=0;n<t.neighbours.length;n++)if(t.neighbours[n]===e.id)return t.portals[n]},v=new o;v.push(t);for(let t=0;t<l.length;t++){const e=l[t],n=l[t+1];if(n){const t=d(e,n);v.push(c[t[0]],c[t[1]])}}v.push(e),v.stringPull();const f=v.path.map(t=>new THREE.Vector3(t.x,t.y,t.z));return f.shift(),f}}},{"./AStar":2,"./Channel":4,"./utils":6}],6:[function(t,e,n){class r{static computeCentroids(t){var e,n,r;for(e=0,n=t.faces.length;e<n;e++)(r=t.faces[e]).centroid=new THREE.Vector3(0,0,0),r.centroid.add(t.vertices[r.a]),r.centroid.add(t.vertices[r.b]),r.centroid.add(t.vertices[r.c]),r.centroid.divideScalar(3)}static roundNumber(t,e){var n=Number(t+"").toFixed(parseInt(e));return parseFloat(n)}static sample(t){return t[Math.floor(Math.random()*t.length)]}static mergeVertexIds(t,e){var n=[];if(t.forEach(t=>{e.indexOf(t)>=0&&n.push(t)}),n.length<2)return[];n.includes(t[0])&&n.includes(t[t.length-1])&&t.push(t.shift()),n.includes(e[0])&&n.includes(e[e.length-1])&&e.push(e.shift()),n=[],t.forEach(t=>{e.includes(t)&&n.push(t)});for(var r=n[1],s=n[0],o=t.slice();o[0]!==r;)o.push(o.shift());for(var i=0,c=e.slice();c[0]!==s;)if(c.push(c.shift()),i++>10)throw new Error("Unexpected state");return c.shift(),c.pop(),o=o.concat(c)}static setPolygonCentroid(t,e){var n=new THREE.Vector3,r=e.vertices;t.vertexIds.forEach(t=>{n.add(r[t])}),n.divideScalar(t.vertexIds.length),t.centroid.copy(n)}static cleanPolygon(t,e){for(var n=[],r=e.vertices,s=0;s<t.vertexIds.length;s++){var o,i,c,h,a=r[t.vertexIds[s]];0===s?(o=t.vertexIds[1],i=t.vertexIds[t.vertexIds.length-1]):s===t.vertexIds.length-1?(o=t.vertexIds[0],i=t.vertexIds[t.vertexIds.length-2]):(o=t.vertexIds[s+1],i=t.vertexIds[s-1]),c=r[o],h=r[i];var u=c.clone().sub(a),l=h.clone().sub(a),d=u.angleTo(l);if(d>Math.PI-.01&&d<Math.PI+.01){var v=[];t.neighbours.forEach(e=>{e.vertexIds.includes(t.vertexIds[s])||v.push(e)}),t.neighbours=v}else n.push(t.vertexIds[s])}t.vertexIds=n,setPolygonCentroid(t,e)}static isConvex(t,e){var n=e.vertices;if(t.vertexIds.length<3)return!1;for(var r=!0,s=[],o=0;o<t.vertexIds.length;o++){var i,c,h=n[t.vertexIds[o]];0===o?(i=n[t.vertexIds[1]],c=n[t.vertexIds[t.vertexIds.length-1]]):o===t.vertexIds.length-1?(i=n[t.vertexIds[0]],c=n[t.vertexIds[t.vertexIds.length-2]]):(i=n[t.vertexIds[o+1]],c=n[t.vertexIds[o-1]]);var a=i.clone().sub(h),u=c.clone().sub(h),l=a.angleTo(u);if(l,l===Math.PI||0===l)return!1;var d=a.cross(u).y;s.push(d)}return s.forEach(t=>{0===t&&(r=!1)}),s[0]>0?s.forEach(t=>{t<0&&(r=!1)}):s.forEach(t=>{t>0&&(r=!1)}),r}static distanceToSquared(t,e){var n=t.x-e.x,r=t.y-e.y,s=t.z-e.z;return n*n+r*r+s*s}static isPointInPoly(t,e){for(var n=!1,r=-1,s=t.length,o=s-1;++r<s;o=r)(t[r].z<=e.z&&e.z<t[o].z||t[o].z<=e.z&&e.z<t[r].z)&&e.x<(t[o].x-t[r].x)*(e.z-t[r].z)/(t[o].z-t[r].z)+t[r].x&&(n=!n);return n}static isVectorInPolygon(t,e,n){var r=1e5,s=-1e5,o=[];return e.vertexIds.forEach(t=>{r=Math.min(n[t].y,r),s=Math.max(n[t].y,s),o.push(n[t])}),!!(t.y<s+.5&&t.y>r-.5&&this.isPointInPoly(o,t))}static triarea2(t,e,n){var r=e.x-t.x,s=e.z-t.z;return(n.x-t.x)*s-r*(n.z-t.z)}static vequal(t,e){return this.distanceToSquared(t,e)<1e-5}static array_intersect(){let t,e,n,r,s,o,i=[],c={};for(o=arguments.length-1,n=arguments[0].length,e=0,t=0;t<=o;t++)(r=arguments[t].length)<n&&(e=t,n=r);for(t=0;t<=o;t++){s=arguments[r=t===e?0:t||e].length;for(var h=0;h<s;h++){var a=arguments[r][h];c[a]===t-1?t===o?(i.push(a),c[a]=0):c[a]=t:0===t&&(c[a]=0)}}return i}}e.exports=r},{}],7:[function(t,e,n){e.exports={"nav-mesh":t("./nav-mesh"),"nav-controller":t("./nav-controller"),system:t("./system"),registerAll:function(t){this._registered||((t=t||window.AFRAME).components["nav-mesh"]||t.registerComponent("nav-mesh",this["nav-mesh"]),t.components["nav-controller"]||t.registerComponent("nav-controller",this["nav-controller"]),t.systems.nav||t.registerSystem("nav",this.system),this._registered=!0)}}},{"./nav-controller":8,"./nav-mesh":9,"./system":10}],8:[function(t,e,n){e.exports={schema:{destination:{type:"vec3"},active:{default:!1},speed:{default:2}},init:function(){this.system=this.el.sceneEl.systems.nav,this.system.addController(this),this.path=[],this.raycaster=new THREE.Raycaster},remove:function(){this.system.removeController(this)},update:function(){this.path.length=0},tick:function(){var t=new THREE.Vector3,e=new THREE.Vector3,n=new THREE.Vector3;return function(r,s){var o=this.el,i=this.data,c=this.raycaster,h=i.speed*s/1e3;if(i.active){if(this.path.length||(this.path=this.system.getPath(this.el.object3D,t.copy(i.destination)),this.path=this.path||[],o.emit("nav-start")),!this.path.length)return console.warn("[nav] Unable to find path to %o.",i.destination),this.el.setAttribute("nav-controller",{active:!1}),void o.emit("nav-end");var a=o.object3D.position,u=this.path[0];e.subVectors(u,a);var l;if(e.length()<h){if(this.path.shift(),!this.path.length)return this.el.setAttribute("nav-controller",{active:!1}),void o.emit("nav-end");l=this.path[0]}else n.copy(e.setLength(h)).add(a),l=u;l.y=a.y,o.object3D.lookAt(l),c.ray.origin.copy(n),c.ray.origin.y+=1.5,c.ray.direction.y=-1;var d=c.intersectObject(this.system.getNavMesh());d.length?(e.subVectors(d[0].point,a),a.add(e.setLength(h))):a.copy(n)}}}()}},{}],9:[function(t,e,n){e.exports={init:function(){this.system=this.el.sceneEl.systems.nav,this.loadNavMesh(),this.el.addEventListener("model-loaded",this.loadNavMesh.bind(this))},loadNavMesh:function(){var t=this.el.getObject3D("mesh");if(t){var e;t.traverse(function(t){t.isMesh&&(e=t)}),e&&this.system.setNavMesh(e)}}}},{}],10:[function(t,e,n){var r=t("three-pathfinding");e.exports={init:function(){this.navMesh=null,this.nodes=null,this.controllers=new Set},setNavMesh:function(t){var e=t.geometry.isBufferGeometry?(new THREE.Geometry).fromBufferGeometry(t.geometry):t.geometry;this.navMesh=new THREE.Mesh(e),this.nodes=r.buildNodes(this.navMesh.geometry),r.setZoneData("level",this.nodes)},getNavMesh:function(){return this.navMesh},addController:function(t){this.controllers.add(t)},removeController:function(t){this.controllers.remove(t)},getPath:function(t,e){var n=t.el.object3D.position,s=r.getGroup("level",n);return r.findPath(n,e,"level",s)}}},{"three-pathfinding":5}]},{},[1]);
|