three-pathfinding
Advanced tools
Comparing version 0.14.0 to 0.14.1
@@ -1,2 +0,2 @@ | ||
var e,t=require("three"),r=function(){function e(){}return e.roundNumber=function(e,t){var r=Math.pow(10,t);return Math.round(e*r)/r},e.sample=function(e){return e[Math.floor(Math.random()*e.length)]},e.distanceToSquared=function(e,t){var r=e.x-t.x,n=e.y-t.y,o=e.z-t.z;return r*r+n*n+o*o},e.isPointInPoly=function(e,t){for(var r=!1,n=-1,o=e.length,i=o-1;++n<o;i=n)(e[n].z<=t.z&&t.z<e[i].z||e[i].z<=t.z&&t.z<e[n].z)&&t.x<(e[i].x-e[n].x)*(t.z-e[n].z)/(e[i].z-e[n].z)+e[n].x&&(r=!r);return r},e.isVectorInPolygon=function(e,t,r){var n=1e5,o=-1e5,i=[];return t.vertexIds.forEach(function(e){n=Math.min(r[e].y,n),o=Math.max(r[e].y,o),i.push(r[e])}),!!(e.y<o+.5&&e.y>n-.5&&this.isPointInPoly(i,e))},e.triarea2=function(e,t,r){return(r.x-e.x)*(t.z-e.z)-(t.x-e.x)*(r.z-e.z)},e.vequal=function(e,t){return this.distanceToSquared(e,t)<1e-5},e.mergeVertices=function(e,r){void 0===r&&(r=1e-4),r=Math.max(r,Number.EPSILON);for(var n={},o=e.getIndex(),i=e.getAttribute("position"),s=o?o.count:i.count,a=0,u=Object.keys(e.attributes),h={},c={},l=[],f=["getX","getY","getZ","getW"],p=0,v=u.length;p<v;p++)h[T=u[p]]=[],(_=e.morphAttributes[T])&&(c[T]=new Array(_.length).fill().map(function(){return[]}));var d=Math.log10(1/r),g=Math.pow(10,d);for(p=0;p<s;p++){var b=o?o.getX(p):p,y="",m=0;for(v=u.length;m<v;m++)for(var M=(x=e.getAttribute(T=u[m])).itemSize,w=0;w<M;w++)y+=~~(x[f[w]](b)*g)+",";if(y in n)l.push(n[y]);else{for(m=0,v=u.length;m<v;m++){var x=e.getAttribute(T=u[m]),_=e.morphAttributes[T],P=(M=x.itemSize,h[T]),z=c[T];for(w=0;w<M;w++){var k=f[w];if(P.push(x[k](b)),_)for(var I=0,E=_.length;I<E;I++)z[I].push(_[I][k](b))}}n[y]=a,l.push(a),a++}}var A=e.clone();for(p=0,v=u.length;p<v;p++){var T,S=e.getAttribute(T=u[p]),N=new S.array.constructor(h[T]);if(x=new t.BufferAttribute(N,S.itemSize,S.normalized),A.setAttribute(T,x),T in c)for(m=0;m<c[T].length;m++){var G=e.morphAttributes[T][m],B=(N=new G.array.constructor(c[T][m]),new t.BufferAttribute(N,G.itemSize,G.normalized));A.morphAttributes[T][m]=B}}return A.setIndex(l),A},e}(),n=function(){function e(e){this.content=[],this.scoreFunction=e}var t=e.prototype;return t.push=function(e){this.content.push(e),this.sinkDown(this.content.length-1)},t.pop=function(){var e=this.content[0],t=this.content.pop();return this.content.length>0&&(this.content[0]=t,this.bubbleUp(0)),e},t.remove=function(e){var t=this.content.indexOf(e),r=this.content.pop();t!==this.content.length-1&&(this.content[t]=r,this.scoreFunction(r)<this.scoreFunction(e)?this.sinkDown(t):this.bubbleUp(t))},t.size=function(){return this.content.length},t.rescoreElement=function(e){this.sinkDown(this.content.indexOf(e))},t.sinkDown=function(e){for(var t=this.content[e];e>0;){var r=(e+1>>1)-1,n=this.content[r];if(!(this.scoreFunction(t)<this.scoreFunction(n)))break;this.content[r]=t,this.content[e]=n,e=r}},t.bubbleUp=function(e){for(var t=this.content.length,r=this.content[e],n=this.scoreFunction(r);;){var o=e+1<<1,i=o-1,s=null,a=void 0;if(i<t&&(a=this.scoreFunction(this.content[i]))<n&&(s=i),o<t&&this.scoreFunction(this.content[o])<(null===s?n:a)&&(s=o),null===s)break;this.content[e]=this.content[s],this.content[s]=r,e=s}},e}(),o=function(){function e(){}return e.init=function(e){for(var t=0;t<e.length;t++){var r=e[t];r.f=0,r.g=0,r.h=0,r.cost=1,r.visited=!1,r.closed=!1,r.parent=null}},e.cleanUp=function(e){for(var t=0;t<e.length;t++){var r=e[t];delete r.f,delete r.g,delete r.h,delete r.cost,delete r.visited,delete r.closed,delete r.parent}},e.heap=function(){return new n(function(e){return e.f})},e.search=function(e,t,r){this.init(e);var n=this.heap();for(n.push(t);n.size()>0;){var o=n.pop();if(o===r){for(var i=o,s=[];i.parent;)s.push(i),i=i.parent;return this.cleanUp(s),s.reverse()}o.closed=!0;for(var a=this.neighbours(e,o),u=0,h=a.length;u<h;u++){var c=a[u];if(!c.closed){var l=o.g+c.cost,f=c.visited;if(!f||l<c.g){if(c.visited=!0,c.parent=o,!c.centroid||!r.centroid)throw new Error("Unexpected state");c.h=c.h||this.heuristic(c.centroid,r.centroid),c.g=l,c.f=c.g+c.h,f?n.rescoreElement(c):n.push(c)}}}}return[]},e.heuristic=function(e,t){return r.distanceToSquared(e,t)},e.neighbours=function(e,t){for(var r=[],n=0;n<t.neighbours.length;n++)r.push(e[t.neighbours[n]]);return r},e}(),i=function(){function e(){}return e.buildZone=function(e){var n=this,o=this._buildNavigationMesh(e),i={};o.vertices.forEach(function(e){e.x=r.roundNumber(e.x,2),e.y=r.roundNumber(e.y,2),e.z=r.roundNumber(e.z,2)}),i.vertices=o.vertices;var s=this._buildPolygonGroups(o);return i.groups=new Array(s.length),s.forEach(function(e,o){var s=new Map;e.forEach(function(e,t){s.set(e,t)});var a=new Array(e.length);e.forEach(function(e,o){var u=[];e.neighbours.forEach(function(e){return u.push(s.get(e))});var h=[];e.neighbours.forEach(function(t){return h.push(n._getSharedVerticesInOrder(e,t))});var c=new t.Vector3(0,0,0);c.add(i.vertices[e.vertexIds[0]]),c.add(i.vertices[e.vertexIds[1]]),c.add(i.vertices[e.vertexIds[2]]),c.divideScalar(3),c.x=r.roundNumber(c.x,2),c.y=r.roundNumber(c.y,2),c.z=r.roundNumber(c.z,2),a[o]={id:o,neighbours:u,vertexIds:e.vertexIds,centroid:c,portals:h}}),i.groups[o]=a}),i},e._buildNavigationMesh=function(e){return e=r.mergeVertices(e),this._buildPolygonsFromGeometry(e)},e._buildPolygonGroups=function(e){var t=[],r=function e(t){t.neighbours.forEach(function(r){void 0===r.group&&(r.group=t.group,e(r))})};return e.polygons.forEach(function(e){void 0!==e.group?t[e.group].push(e):(e.group=t.length,r(e),t.push([e]))}),t},e._buildPolygonNeighbours=function(e,t){var r=new Set,n=t[e.vertexIds[1]],o=t[e.vertexIds[2]];return t[e.vertexIds[0]].forEach(function(t){t!==e&&(n.includes(t)||o.includes(t))&&r.add(t)}),n.forEach(function(t){t!==e&&o.includes(t)&&r.add(t)}),r},e._buildPolygonsFromGeometry=function(e){for(var r=this,n=[],o=[],i=e.attributes.position,s=e.index,a=[],u=0;u<i.count;u++)o.push((new t.Vector3).fromBufferAttribute(i,u)),a[u]=[];for(var h=0;h<e.index.count;h+=3){var c=s.getX(h),l=s.getX(h+1),f=s.getX(h+2),p={vertexIds:[c,l,f],neighbours:null};n.push(p),a[c].push(p),a[l].push(p),a[f].push(p)}return n.forEach(function(e){e.neighbours=r._buildPolygonNeighbours(e,a)}),{polygons:n,vertices:o}},e._getSharedVerticesInOrder=function(e,t){var r=e.vertexIds,n=r[0],o=r[1],i=r[2],s=t.vertexIds,a=s.includes(n),u=s.includes(o),h=s.includes(i);return a&&u&&h?Array.from(r):a&&u?[n,o]:u&&h?[o,i]:a&&h?[i,n]:(console.warn("Error processing navigation mesh neighbors; neighbors with <2 shared vertices found."),[])},e}(),s=function(){function e(){this.portals=[]}var t=e.prototype;return t.push=function(e,t){void 0===t&&(t=e),this.portals.push({left:e,right:t})},t.stringPull=function(){var e,t,n,o=this.portals,i=[],s=0,a=0,u=0;t=o[0].left,n=o[0].right,i.push(e=o[0].left);for(var h=1;h<o.length;h++){var c=o[h].left,l=o[h].right;if(r.triarea2(e,n,l)<=0){if(!(r.vequal(e,n)||r.triarea2(e,t,l)>0)){i.push(t),t=e=t,n=e,a=s=a,u=s,h=s;continue}n=l,u=h}if(r.triarea2(e,t,c)>=0){if(!(r.vequal(e,t)||r.triarea2(e,n,c)<0)){i.push(n),t=e=n,n=e,a=s=u,u=s,h=s;continue}t=c,a=h}}return 0!==i.length&&r.vequal(i[i.length-1],o[o.length-1].left)||i.push(o[o.length-1].left),this.path=i,i},e}(),a=function(){function e(){this.zones={}}e.createZone=function(e){return i.buildZone(e)};var n=e.prototype;return n.setZoneData=function(e,t){this.zones[e]=t},n.getRandomNode=function(e,n,o,i){if(!this.zones[e])return new t.Vector3;o=o||null,i=i||0;var s=[];return this.zones[e].groups[n].forEach(function(e){o&&i?r.distanceToSquared(o,e.centroid)<i*i&&s.push(e.centroid):s.push(e.centroid)}),r.sample(s)||new t.Vector3},n.getClosestNode=function(e,t,n,o){void 0===o&&(o=!1);var i=this.zones[t].vertices,s=null,a=Infinity;return this.zones[t].groups[n].forEach(function(t){var n=r.distanceToSquared(t.centroid,e);n<a&&(!o||r.isVectorInPolygon(e,t,i))&&(s=t,a=n)}),s},n.findPath=function(e,r,n,i){var a=this.zones[n].groups[i],u=this.zones[n].vertices,h=this.getClosestNode(e,n,i,!0),c=this.getClosestNode(r,n,i,!0);if(!h||!c)return null;var l=o.search(a,h,c),f=function(e,t){for(var r=0;r<e.neighbours.length;r++)if(e.neighbours[r]===t.id)return e.portals[r]},p=new s;p.push(e);for(var v=0;v<l.length;v++){var d=l[v+1];if(d){var g=f(l[v],d);p.push(u[g[0]],u[g[1]])}}p.push(r),p.stringPull();var b=p.path.map(function(e){return new t.Vector3(e.x,e.y,e.z)});return b.shift(),b},e}();a.prototype.getGroup=(e=new t.Plane,function(t,n,o){if(void 0===o&&(o=!1),!this.zones[t])return null;for(var i=null,s=Math.pow(50,2),a=this.zones[t],u=0;u<a.groups.length;u++){var h=a.groups[u],c=Array.isArray(h),l=0;for(h=c?h:h[Symbol.iterator]();;){var f;if(c){if(l>=h.length)break;f=h[l++]}else{if((l=h.next()).done)break;f=l.value}var p=f;if(o&&(e.setFromCoplanarPoints(a.vertices[p.vertexIds[0]],a.vertices[p.vertexIds[1]],a.vertices[p.vertexIds[2]]),Math.abs(e.distanceToPoint(n))<.01&&r.isPointInPoly([a.vertices[p.vertexIds[0]],a.vertices[p.vertexIds[1]],a.vertices[p.vertexIds[2]]],n)))return u;var v=r.distanceToSquared(p.centroid,n);v<s&&(i=u,s=v)}}return i}),a.prototype.clampStep=function(){var e,r,n=new t.Vector3,o=new t.Plane,i=new t.Triangle,s=new t.Vector3,a=new t.Vector3;return function(t,u,h,c,l,f){var p=this.zones[c].vertices,v=this.zones[c].groups[l],d=[h],g={};g[h.id]=0,e=void 0,a.set(0,0,0),r=Infinity,o.setFromCoplanarPoints(p[h.vertexIds[0]],p[h.vertexIds[1]],p[h.vertexIds[2]]),o.projectPoint(u,n),s.copy(n);for(var b=d.pop();b;b=d.pop()){i.set(p[b.vertexIds[0]],p[b.vertexIds[1]],p[b.vertexIds[2]]),i.closestPointToPoint(s,n),n.distanceToSquared(s)<r&&(e=b,a.copy(n),r=n.distanceToSquared(s));var y=g[b.id];if(!(y>2))for(var m=0;m<b.neighbours.length;m++){var M=v[b.neighbours[m]];M.id in g||(d.push(M),g[M.id]=y+1)}}return f.copy(a),e}}();var u={PLAYER:new t.Color(15631215).convertGammaToLinear(2.2).getHex(),TARGET:new t.Color(14469912).convertGammaToLinear(2.2).getHex(),PATH:new t.Color(41903).convertGammaToLinear(2.2).getHex(),WAYPOINT:new t.Color(41903).convertGammaToLinear(2.2).getHex(),CLAMPED_STEP:new t.Color(14472114).convertGammaToLinear(2.2).getHex(),CLOSEST_NODE:new t.Color(4417387).convertGammaToLinear(2.2).getHex()},h=function(e){var r,n;function o(){var r;return(r=e.call(this)||this)._playerMarker=new t.Mesh(new t.SphereBufferGeometry(.25,32,32),new t.MeshBasicMaterial({color:u.PLAYER})),r._targetMarker=new t.Mesh(new t.BoxBufferGeometry(.3,.3,.3),new t.MeshBasicMaterial({color:u.TARGET})),r._nodeMarker=new t.Mesh(new t.BoxBufferGeometry(.1,.8,.1),new t.MeshBasicMaterial({color:u.CLOSEST_NODE})),r._stepMarker=new t.Mesh(new t.BoxBufferGeometry(.1,1,.1),new t.MeshBasicMaterial({color:u.CLAMPED_STEP})),r._pathMarker=new t.Object3D,r._pathLineMaterial=new t.LineBasicMaterial({color:u.PATH,linewidth:2}),r._pathPointMaterial=new t.MeshBasicMaterial({color:u.WAYPOINT}),r._pathPointGeometry=new t.SphereBufferGeometry(.08),r._markers=[r._playerMarker,r._targetMarker,r._nodeMarker,r._stepMarker,r._pathMarker],r._markers.forEach(function(e){e.visible=!1,r.add(e)}),r}n=e,(r=o).prototype=Object.create(n.prototype),r.prototype.constructor=r,r.__proto__=n;var i=o.prototype;return i.setPath=function(e){for(;this._pathMarker.children.length;)this._pathMarker.children[0].visible=!1,this._pathMarker.remove(this._pathMarker.children[0]);e=[this._playerMarker.position].concat(e);var r=new t.BufferGeometry;r.setAttribute("position",new t.BufferAttribute(new Float32Array(3*e.length),3));for(var n=0;n<e.length;n++)r.attributes.position.setXYZ(n,e[n].x,e[n].y+.2,e[n].z);this._pathMarker.add(new t.Line(r,this._pathLineMaterial));for(var o=0;o<e.length-1;o++){var i=new t.Mesh(this._pathPointGeometry,this._pathPointMaterial);i.position.copy(e[o]),i.position.y+=.2,this._pathMarker.add(i)}return this._pathMarker.visible=!0,this},i.setPlayerPosition=function(e){return this._playerMarker.position.copy(e),this._playerMarker.visible=!0,this},i.setTargetPosition=function(e){return this._targetMarker.position.copy(e),this._targetMarker.visible=!0,this},i.setNodePosition=function(e){return this._nodeMarker.position.copy(e),this._nodeMarker.visible=!0,this},i.setStepPosition=function(e){return this._stepMarker.position.copy(e),this._stepMarker.visible=!0,this},i.reset=function(){for(;this._pathMarker.children.length;)this._pathMarker.children[0].visible=!1,this._pathMarker.remove(this._pathMarker.children[0]);return this._markers.forEach(function(e){e.visible=!1}),this},o}(t.Object3D);exports.Pathfinding=a,exports.PathfindingHelper=h; | ||
var e=require("three");function t(e,r){return(t=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,r)}function r(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function n(e,t){var n;if("undefined"==typeof Symbol||null==e[Symbol.iterator]){if(Array.isArray(e)||(n=function(e,t){if(e){if("string"==typeof e)return r(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?r(e,t):void 0}}(e))||t&&e&&"number"==typeof e.length){n&&(e=n);var o=0;return function(){return o>=e.length?{done:!0}:{done:!1,value:e[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}return(n=e[Symbol.iterator]()).next.bind(n)}var o,i=function(){function t(){}return t.roundNumber=function(e,t){var r=Math.pow(10,t);return Math.round(e*r)/r},t.sample=function(e){return e[Math.floor(Math.random()*e.length)]},t.distanceToSquared=function(e,t){var r=e.x-t.x,n=e.y-t.y,o=e.z-t.z;return r*r+n*n+o*o},t.isPointInPoly=function(e,t){for(var r=!1,n=-1,o=e.length,i=o-1;++n<o;i=n)(e[n].z<=t.z&&t.z<e[i].z||e[i].z<=t.z&&t.z<e[n].z)&&t.x<(e[i].x-e[n].x)*(t.z-e[n].z)/(e[i].z-e[n].z)+e[n].x&&(r=!r);return r},t.isVectorInPolygon=function(e,t,r){var n=1e5,o=-1e5,i=[];return t.vertexIds.forEach(function(e){n=Math.min(r[e].y,n),o=Math.max(r[e].y,o),i.push(r[e])}),!!(e.y<o+.5&&e.y>n-.5&&this.isPointInPoly(i,e))},t.triarea2=function(e,t,r){return(r.x-e.x)*(t.z-e.z)-(t.x-e.x)*(r.z-e.z)},t.vequal=function(e,t){return this.distanceToSquared(e,t)<1e-5},t.mergeVertices=function(t,r){void 0===r&&(r=1e-4),r=Math.max(r,Number.EPSILON);for(var n={},o=t.getIndex(),i=t.getAttribute("position"),s=o?o.count:i.count,a=0,u=[],h=[],c=Math.log10(1/r),l=Math.pow(10,c),f=0;f<s;f++){var p=o?o.getX(f):f,d="";d+=~~(i.getX(p)*l)+",",d+=~~(i.getY(p)*l)+",",(d+=~~(i.getZ(p)*l)+",")in n?u.push(n[d]):(h.push(i.getX(p)),h.push(i.getY(p)),h.push(i.getZ(p)),n[d]=a,u.push(a),a++)}var v=new e.BufferAttribute(new Float32Array(h),i.itemSize,i.normalized),g=new e.BufferGeometry;return g.setAttribute("position",v),g.setIndex(u),g},t}(),s=function(){function e(e){this.content=[],this.scoreFunction=e}var t=e.prototype;return t.push=function(e){this.content.push(e),this.sinkDown(this.content.length-1)},t.pop=function(){var e=this.content[0],t=this.content.pop();return this.content.length>0&&(this.content[0]=t,this.bubbleUp(0)),e},t.remove=function(e){var t=this.content.indexOf(e),r=this.content.pop();t!==this.content.length-1&&(this.content[t]=r,this.scoreFunction(r)<this.scoreFunction(e)?this.sinkDown(t):this.bubbleUp(t))},t.size=function(){return this.content.length},t.rescoreElement=function(e){this.sinkDown(this.content.indexOf(e))},t.sinkDown=function(e){for(var t=this.content[e];e>0;){var r=(e+1>>1)-1,n=this.content[r];if(!(this.scoreFunction(t)<this.scoreFunction(n)))break;this.content[r]=t,this.content[e]=n,e=r}},t.bubbleUp=function(e){for(var t=this.content.length,r=this.content[e],n=this.scoreFunction(r);;){var o=e+1<<1,i=o-1,s=null,a=void 0;if(i<t&&(a=this.scoreFunction(this.content[i]))<n&&(s=i),o<t&&this.scoreFunction(this.content[o])<(null===s?n:a)&&(s=o),null===s)break;this.content[e]=this.content[s],this.content[s]=r,e=s}},e}(),a=function(){function e(){}return e.init=function(e){for(var t=0;t<e.length;t++){var r=e[t];r.f=0,r.g=0,r.h=0,r.cost=1,r.visited=!1,r.closed=!1,r.parent=null}},e.cleanUp=function(e){for(var t=0;t<e.length;t++){var r=e[t];delete r.f,delete r.g,delete r.h,delete r.cost,delete r.visited,delete r.closed,delete r.parent}},e.heap=function(){return new s(function(e){return e.f})},e.search=function(e,t,r){this.init(e);var n=this.heap();for(n.push(t);n.size()>0;){var o=n.pop();if(o===r){for(var i=o,s=[];i.parent;)s.push(i),i=i.parent;return this.cleanUp(s),s.reverse()}o.closed=!0;for(var a=this.neighbours(e,o),u=0,h=a.length;u<h;u++){var c=a[u];if(!c.closed){var l=o.g+c.cost,f=c.visited;if(!f||l<c.g){if(c.visited=!0,c.parent=o,!c.centroid||!r.centroid)throw new Error("Unexpected state");c.h=c.h||this.heuristic(c.centroid,r.centroid),c.g=l,c.f=c.g+c.h,f?n.rescoreElement(c):n.push(c)}}}}return[]},e.heuristic=function(e,t){return i.distanceToSquared(e,t)},e.neighbours=function(e,t){for(var r=[],n=0;n<t.neighbours.length;n++)r.push(e[t.neighbours[n]]);return r},e}(),u=function(){function t(){}return t.buildZone=function(t,r){var n=this,o=this._buildNavigationMesh(t,r),s={};o.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)}),s.vertices=o.vertices;var a=this._buildPolygonGroups(o);return s.groups=new Array(a.length),a.forEach(function(t,r){var o=new Map;t.forEach(function(e,t){o.set(e,t)});var a=new Array(t.length);t.forEach(function(t,r){var u=[];t.neighbours.forEach(function(e){return u.push(o.get(e))});var h=[];t.neighbours.forEach(function(e){return h.push(n._getSharedVerticesInOrder(t,e))});var c=new e.Vector3(0,0,0);c.add(s.vertices[t.vertexIds[0]]),c.add(s.vertices[t.vertexIds[1]]),c.add(s.vertices[t.vertexIds[2]]),c.divideScalar(3),c.x=i.roundNumber(c.x,2),c.y=i.roundNumber(c.y,2),c.z=i.roundNumber(c.z,2),a[r]={id:r,neighbours:u,vertexIds:t.vertexIds,centroid:c,portals:h}}),s.groups[r]=a}),s},t._buildNavigationMesh=function(e,t){return e=i.mergeVertices(e,t),this._buildPolygonsFromGeometry(e)},t._buildPolygonGroups=function(e){var t=[],r=function e(t){t.neighbours.forEach(function(r){void 0===r.group&&(r.group=t.group,e(r))})};return e.polygons.forEach(function(e){void 0!==e.group?t[e.group].push(e):(e.group=t.length,r(e),t.push([e]))}),t},t._buildPolygonNeighbours=function(e,t){var r=new Set,n=t[e.vertexIds[1]],o=t[e.vertexIds[2]];return t[e.vertexIds[0]].forEach(function(t){t!==e&&(n.includes(t)||o.includes(t))&&r.add(t)}),n.forEach(function(t){t!==e&&o.includes(t)&&r.add(t)}),r},t._buildPolygonsFromGeometry=function(t){for(var r=this,n=[],o=[],i=t.attributes.position,s=t.index,a=[],u=0;u<i.count;u++)o.push((new e.Vector3).fromBufferAttribute(i,u)),a[u]=[];for(var h=0;h<t.index.count;h+=3){var c=s.getX(h),l=s.getX(h+1),f=s.getX(h+2),p={vertexIds:[c,l,f],neighbours:null};n.push(p),a[c].push(p),a[l].push(p),a[f].push(p)}return n.forEach(function(e){e.neighbours=r._buildPolygonNeighbours(e,a)}),{polygons:n,vertices:o}},t._getSharedVerticesInOrder=function(e,t){var r=e.vertexIds,n=r[0],o=r[1],i=r[2],s=t.vertexIds,a=s.includes(n),u=s.includes(o),h=s.includes(i);return a&&u&&h?Array.from(r):a&&u?[n,o]:u&&h?[o,i]:a&&h?[i,n]:(console.warn("Error processing navigation mesh neighbors; neighbors with <2 shared vertices found."),[])},t}(),h=function(){function e(){this.portals=[]}var t=e.prototype;return t.push=function(e,t){void 0===t&&(t=e),this.portals.push({left:e,right:t})},t.stringPull=function(){var e,t,r,n=this.portals,o=[],s=0,a=0,u=0;t=n[0].left,r=n[0].right,o.push(e=n[0].left);for(var h=1;h<n.length;h++){var c=n[h].left,l=n[h].right;if(i.triarea2(e,r,l)<=0){if(!(i.vequal(e,r)||i.triarea2(e,t,l)>0)){o.push(t),t=e=t,r=e,a=s=a,u=s,h=s;continue}r=l,u=h}if(i.triarea2(e,t,c)>=0){if(!(i.vequal(e,t)||i.triarea2(e,r,c)<0)){o.push(r),t=e=r,r=e,a=s=u,u=s,h=s;continue}t=c,a=h}}return 0!==o.length&&i.vequal(o[o.length-1],n[n.length-1].left)||o.push(n[n.length-1].left),this.path=o,o},e}(),c=function(){function t(){this.zones={}}t.createZone=function(e,t){return void 0===t&&(t=1e-4),u.buildZone(e,t)};var r=t.prototype;return r.setZoneData=function(e,t){this.zones[e]=t},r.getRandomNode=function(t,r,n,o){if(!this.zones[t])return new e.Vector3;n=n||null,o=o||0;var s=[];return this.zones[t].groups[r].forEach(function(e){n&&o?i.distanceToSquared(n,e.centroid)<o*o&&s.push(e.centroid):s.push(e.centroid)}),i.sample(s)||new e.Vector3},r.getClosestNode=function(e,t,r,n){void 0===n&&(n=!1);var o=this.zones[t].vertices,s=null,a=Infinity;return this.zones[t].groups[r].forEach(function(t){var r=i.distanceToSquared(t.centroid,e);r<a&&(!n||i.isVectorInPolygon(e,t,o))&&(s=t,a=r)}),s},r.findPath=function(t,r,n,o){var i=this.zones[n].groups[o],s=this.zones[n].vertices,u=this.getClosestNode(t,n,o,!0),c=this.getClosestNode(r,n,o,!0);if(!u||!c)return null;var l=a.search(i,u,c),f=function(e,t){for(var r=0;r<e.neighbours.length;r++)if(e.neighbours[r]===t.id)return e.portals[r]},p=new h;p.push(t);for(var d=0;d<l.length;d++){var v=l[d+1];if(v){var g=f(l[d],v);p.push(s[g[0]],s[g[1]])}}p.push(r),p.stringPull();var y=p.path.map(function(t){return new e.Vector3(t.x,t.y,t.z)});return y.shift(),y},t}();c.prototype.getGroup=(o=new e.Plane,function(e,t,r){if(void 0===r&&(r=!1),!this.zones[e])return null;for(var s=null,a=Math.pow(50,2),u=this.zones[e],h=0;h<u.groups.length;h++)for(var c,l=n(u.groups[h]);!(c=l()).done;){var f=c.value;if(r&&(o.setFromCoplanarPoints(u.vertices[f.vertexIds[0]],u.vertices[f.vertexIds[1]],u.vertices[f.vertexIds[2]]),Math.abs(o.distanceToPoint(t))<.01&&i.isPointInPoly([u.vertices[f.vertexIds[0]],u.vertices[f.vertexIds[1]],u.vertices[f.vertexIds[2]]],t)))return h;var p=i.distanceToSquared(f.centroid,t);p<a&&(s=h,a=p)}return s}),c.prototype.clampStep=function(){var t,r,n=new e.Vector3,o=new e.Plane,i=new e.Triangle,s=new e.Vector3,a=new e.Vector3;return function(e,u,h,c,l,f){var p=this.zones[c].vertices,d=this.zones[c].groups[l],v=[h],g={};g[h.id]=0,t=void 0,a.set(0,0,0),r=Infinity,o.setFromCoplanarPoints(p[h.vertexIds[0]],p[h.vertexIds[1]],p[h.vertexIds[2]]),o.projectPoint(u,n),s.copy(n);for(var y=v.pop();y;y=v.pop()){i.set(p[y.vertexIds[0]],p[y.vertexIds[1]],p[y.vertexIds[2]]),i.closestPointToPoint(s,n),n.distanceToSquared(s)<r&&(t=y,a.copy(n),r=n.distanceToSquared(s));var b=g[y.id];if(!(b>2))for(var m=0;m<y.neighbours.length;m++){var M=d[y.neighbours[m]];M.id in g||(v.push(M),g[M.id]=b+1)}}return f.copy(a),t}}();var l={PLAYER:new e.Color(15631215).convertGammaToLinear(2.2).getHex(),TARGET:new e.Color(14469912).convertGammaToLinear(2.2).getHex(),PATH:new e.Color(41903).convertGammaToLinear(2.2).getHex(),WAYPOINT:new e.Color(41903).convertGammaToLinear(2.2).getHex(),CLAMPED_STEP:new e.Color(14472114).convertGammaToLinear(2.2).getHex(),CLOSEST_NODE:new e.Color(4417387).convertGammaToLinear(2.2).getHex()},f=function(r){var n,o;function i(){var t;return(t=r.call(this)||this)._playerMarker=new e.Mesh(new e.SphereBufferGeometry(.25,32,32),new e.MeshBasicMaterial({color:l.PLAYER})),t._targetMarker=new e.Mesh(new e.BoxBufferGeometry(.3,.3,.3),new e.MeshBasicMaterial({color:l.TARGET})),t._nodeMarker=new e.Mesh(new e.BoxBufferGeometry(.1,.8,.1),new e.MeshBasicMaterial({color:l.CLOSEST_NODE})),t._stepMarker=new e.Mesh(new e.BoxBufferGeometry(.1,1,.1),new e.MeshBasicMaterial({color:l.CLAMPED_STEP})),t._pathMarker=new e.Object3D,t._pathLineMaterial=new e.LineBasicMaterial({color:l.PATH,linewidth:2}),t._pathPointMaterial=new e.MeshBasicMaterial({color:l.WAYPOINT}),t._pathPointGeometry=new e.SphereBufferGeometry(.08),t._markers=[t._playerMarker,t._targetMarker,t._nodeMarker,t._stepMarker,t._pathMarker],t._markers.forEach(function(e){e.visible=!1,t.add(e)}),t}o=r,(n=i).prototype=Object.create(o.prototype),n.prototype.constructor=n,t(n,o);var s=i.prototype;return s.setPath=function(t){for(;this._pathMarker.children.length;)this._pathMarker.children[0].visible=!1,this._pathMarker.remove(this._pathMarker.children[0]);t=[this._playerMarker.position].concat(t);var r=new e.BufferGeometry;r.setAttribute("position",new e.BufferAttribute(new Float32Array(3*t.length),3));for(var n=0;n<t.length;n++)r.attributes.position.setXYZ(n,t[n].x,t[n].y+.2,t[n].z);this._pathMarker.add(new e.Line(r,this._pathLineMaterial));for(var o=0;o<t.length-1;o++){var i=new e.Mesh(this._pathPointGeometry,this._pathPointMaterial);i.position.copy(t[o]),i.position.y+=.2,this._pathMarker.add(i)}return this._pathMarker.visible=!0,this},s.setPlayerPosition=function(e){return this._playerMarker.position.copy(e),this._playerMarker.visible=!0,this},s.setTargetPosition=function(e){return this._targetMarker.position.copy(e),this._targetMarker.visible=!0,this},s.setNodePosition=function(e){return this._nodeMarker.position.copy(e),this._nodeMarker.visible=!0,this},s.setStepPosition=function(e){return this._stepMarker.position.copy(e),this._stepMarker.visible=!0,this},s.reset=function(){for(;this._pathMarker.children.length;)this._pathMarker.children[0].visible=!1,this._pathMarker.remove(this._pathMarker.children[0]);return this._markers.forEach(function(e){e.visible=!1}),this},i}(e.Object3D);exports.Pathfinding=c,exports.PathfindingHelper=f; | ||
//# sourceMappingURL=three-pathfinding.js.map |
@@ -1,2 +0,2 @@ | ||
import{BufferAttribute as t,Vector3 as e,Plane as r,Triangle as n,Color as s,Object3D as o,Mesh as i,SphereBufferGeometry as h,MeshBasicMaterial as a,BoxBufferGeometry as c,LineBasicMaterial as u,BufferGeometry as l,Line as p}from"three";class d{static roundNumber(t,e){const r=Math.pow(10,e);return Math.round(t*r)/r}static sample(t){return t[Math.floor(Math.random()*t.length)]}static distanceToSquared(t,e){var r=t.x-e.x,n=t.y-e.y,s=t.z-e.z;return r*r+n*n+s*s}static isPointInPoly(t,e){for(var r=!1,n=-1,s=t.length,o=s-1;++n<s;o=n)(t[n].z<=e.z&&e.z<t[o].z||t[o].z<=e.z&&e.z<t[n].z)&&e.x<(t[o].x-t[n].x)*(e.z-t[n].z)/(t[o].z-t[n].z)+t[n].x&&(r=!r);return r}static isVectorInPolygon(t,e,r){var n=1e5,s=-1e5,o=[];return e.vertexIds.forEach(t=>{n=Math.min(r[t].y,n),s=Math.max(r[t].y,s),o.push(r[t])}),!!(t.y<s+.5&&t.y>n-.5&&this.isPointInPoly(o,t))}static triarea2(t,e,r){return(r.x-t.x)*(e.z-t.z)-(e.x-t.x)*(r.z-t.z)}static vequal(t,e){return this.distanceToSquared(t,e)<1e-5}static mergeVertices(e,r=1e-4){r=Math.max(r,Number.EPSILON);for(var n={},s=e.getIndex(),o=e.getAttribute("position"),i=s?s.count:o.count,h=0,a=Object.keys(e.attributes),c={},u={},l=[],p=["getX","getY","getZ","getW"],d=0,g=a.length;d<g;d++)c[A=a[d]]=[],(M=e.morphAttributes[A])&&(u[A]=new Array(M.length).fill().map(()=>[]));var f=Math.log10(1/r),v=Math.pow(10,f);for(d=0;d<i;d++){var b=s?s.getX(d):d,m="",w=0;for(g=a.length;w<g;w++)for(var x=(_=e.getAttribute(A=a[w])).itemSize,y=0;y<x;y++)m+=~~(_[p[y]](b)*v)+",";if(m in n)l.push(n[m]);else{for(w=0,g=a.length;w<g;w++){var _=e.getAttribute(A=a[w]),M=e.morphAttributes[A],z=(x=_.itemSize,c[A]),P=u[A];for(y=0;y<x;y++){var k=p[y];if(z.push(_[k](b)),M)for(var I=0,E=M.length;I<E;I++)P[I].push(M[I][k](b))}}n[m]=h,l.push(h),h++}}const T=e.clone();for(d=0,g=a.length;d<g;d++){var A,S=e.getAttribute(A=a[d]),N=new S.array.constructor(c[A]);if(_=new t(N,S.itemSize,S.normalized),T.setAttribute(A,_),A in u)for(w=0;w<u[A].length;w++){var G=e.morphAttributes[A][w],L=(N=new G.array.constructor(u[A][w]),new t(N,G.itemSize,G.normalized));T.morphAttributes[A][w]=L}}return T.setIndex(l),T}}class g{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),r=this.content.pop();e!==this.content.length-1&&(this.content[e]=r,this.scoreFunction(r)<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 r=(t+1>>1)-1,n=this.content[r];if(!(this.scoreFunction(e)<this.scoreFunction(n)))break;this.content[r]=e,this.content[t]=n,t=r}}bubbleUp(t){const e=this.content.length,r=this.content[t],n=this.scoreFunction(r);for(;;){const s=t+1<<1,o=s-1;let i,h=null;if(o<e&&(i=this.scoreFunction(this.content[o]),i<n&&(h=o)),s<e&&this.scoreFunction(this.content[s])<(null===h?n:i)&&(h=s),null===h)break;this.content[t]=this.content[h],this.content[h]=r,t=h}}}class f{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 r,n,s,o=0,i=0,h=0;r=t[0].left,n=t[0].left,s=t[0].right,e.push(r);for(let a=1;a<t.length;a++){const c=t[a].left,u=t[a].right;if(d.triarea2(r,s,u)<=0){if(!(d.vequal(r,s)||d.triarea2(r,n,u)>0)){e.push(n),r=n,o=i,n=r,s=r,i=o,h=o,a=o;continue}s=u,h=a}if(d.triarea2(r,n,c)>=0){if(!(d.vequal(r,n)||d.triarea2(r,s,c)<0)){e.push(s),r=s,o=h,n=r,s=r,i=o,h=o,a=o;continue}n=c,i=a}}return 0!==e.length&&d.vequal(e[e.length-1],t[t.length-1].left)||e.push(t[t.length-1].left),this.path=e,e}}class v{constructor(){this.zones={}}static createZone(t){return class{static buildZone(t){const r=this._buildNavigationMesh(t),n={};r.vertices.forEach(t=>{t.x=d.roundNumber(t.x,2),t.y=d.roundNumber(t.y,2),t.z=d.roundNumber(t.z,2)}),n.vertices=r.vertices;const s=this._buildPolygonGroups(r);return n.groups=new Array(s.length),s.forEach((t,r)=>{const s=new Map;t.forEach((t,e)=>{s.set(t,e)});const o=new Array(t.length);t.forEach((t,r)=>{const i=[];t.neighbours.forEach(t=>i.push(s.get(t)));const h=[];t.neighbours.forEach(e=>h.push(this._getSharedVerticesInOrder(t,e)));const a=new e(0,0,0);a.add(n.vertices[t.vertexIds[0]]),a.add(n.vertices[t.vertexIds[1]]),a.add(n.vertices[t.vertexIds[2]]),a.divideScalar(3),a.x=d.roundNumber(a.x,2),a.y=d.roundNumber(a.y,2),a.z=d.roundNumber(a.z,2),o[r]={id:r,neighbours:i,vertexIds:t.vertexIds,centroid:a,portals:h}}),n.groups[r]=o}),n}static _buildNavigationMesh(t){return t=d.mergeVertices(t),this._buildPolygonsFromGeometry(t)}static _buildPolygonGroups(t){const e=[],r=function t(e){e.neighbours.forEach(r=>{void 0===r.group&&(r.group=e.group,t(r))})};return t.polygons.forEach(t=>{void 0!==t.group?e[t.group].push(t):(t.group=e.length,r(t),e.push([t]))}),e}static _buildPolygonNeighbours(t,e){const r=new Set,n=e[t.vertexIds[1]],s=e[t.vertexIds[2]];return e[t.vertexIds[0]].forEach(e=>{e!==t&&(n.includes(e)||s.includes(e))&&r.add(e)}),n.forEach(e=>{e!==t&&s.includes(e)&&r.add(e)}),r}static _buildPolygonsFromGeometry(t){const r=[],n=[],s=t.attributes.position,o=t.index,i=[];for(let t=0;t<s.count;t++)n.push((new e).fromBufferAttribute(s,t)),i[t]=[];for(let e=0;e<t.index.count;e+=3){const t=o.getX(e),n=o.getX(e+1),s=o.getX(e+2),h={vertexIds:[t,n,s],neighbours:null};r.push(h),i[t].push(h),i[n].push(h),i[s].push(h)}return r.forEach(t=>{t.neighbours=this._buildPolygonNeighbours(t,i)}),{polygons:r,vertices:n}}static _getSharedVerticesInOrder(t,e){const r=t.vertexIds,n=r[0],s=r[1],o=r[2],i=e.vertexIds,h=i.includes(n),a=i.includes(s),c=i.includes(o);return h&&a&&c?Array.from(r):h&&a?[n,s]:a&&c?[s,o]:h&&c?[o,n]:(console.warn("Error processing navigation mesh neighbors; neighbors with <2 shared vertices found."),[])}}.buildZone(t)}setZoneData(t,e){this.zones[t]=e}getRandomNode(t,r,n,s){if(!this.zones[t])return new e;n=n||null,s=s||0;const o=[];return this.zones[t].groups[r].forEach(t=>{n&&s?d.distanceToSquared(n,t.centroid)<s*s&&o.push(t.centroid):o.push(t.centroid)}),d.sample(o)||new e}getClosestNode(t,e,r,n=!1){const s=this.zones[e].vertices;let o=null,i=Infinity;return this.zones[e].groups[r].forEach(e=>{const r=d.distanceToSquared(e.centroid,t);r<i&&(!n||d.isVectorInPolygon(t,e,s))&&(o=e,i=r)}),o}findPath(t,r,n,s){const o=this.zones[n].groups[s],i=this.zones[n].vertices,h=this.getClosestNode(t,n,s,!0),a=this.getClosestNode(r,n,s,!0);if(!h||!a)return null;const c=class{static init(t){for(let e=0;e<t.length;e++){const r=t[e];r.f=0,r.g=0,r.h=0,r.cost=1,r.visited=!1,r.closed=!1,r.parent=null}}static cleanUp(t){for(let e=0;e<t.length;e++){const r=t[e];delete r.f,delete r.g,delete r.h,delete r.cost,delete r.visited,delete r.closed,delete r.parent}}static heap(){return new g(function(t){return t.f})}static search(t,e,r){this.init(t);const n=this.heap();for(n.push(e);n.size()>0;){const e=n.pop();if(e===r){let t=e;const r=[];for(;t.parent;)r.push(t),t=t.parent;return this.cleanUp(r),r.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,h=o.visited;if(!h||i<o.g){if(o.visited=!0,o.parent=e,!o.centroid||!r.centroid)throw new Error("Unexpected state");o.h=o.h||this.heuristic(o.centroid,r.centroid),o.g=i,o.f=o.g+o.h,h?n.rescoreElement(o):n.push(o)}}}return[]}static heuristic(t,e){return d.distanceToSquared(t,e)}static neighbours(t,e){const r=[];for(let n=0;n<e.neighbours.length;n++)r.push(t[e.neighbours[n]]);return r}}.search(o,h,a),u=function(t,e){for(var r=0;r<t.neighbours.length;r++)if(t.neighbours[r]===e.id)return t.portals[r]},l=new f;l.push(t);for(let t=0;t<c.length;t++){const e=c[t],r=c[t+1];if(r){const t=u(e,r);l.push(i[t[0]],i[t[1]])}}l.push(r),l.stringPull();const p=l.path.map(t=>new e(t.x,t.y,t.z));return p.shift(),p}}v.prototype.getGroup=function(){const t=new r;return function(e,r,n=!1){if(!this.zones[e])return null;let s=null,o=Math.pow(50,2);const i=this.zones[e];for(let e=0;e<i.groups.length;e++){const h=i.groups[e];for(const a of h){if(n&&(t.setFromCoplanarPoints(i.vertices[a.vertexIds[0]],i.vertices[a.vertexIds[1]],i.vertices[a.vertexIds[2]]),Math.abs(t.distanceToPoint(r))<.01)&&d.isPointInPoly([i.vertices[a.vertexIds[0]],i.vertices[a.vertexIds[1]],i.vertices[a.vertexIds[2]]],r))return e;const h=d.distanceToSquared(a.centroid,r);h<o&&(s=e,o=h)}}return s}}(),v.prototype.clampStep=function(){const t=new e,s=new r,o=new n,i=new e;let h,a,c=new e;return function(e,r,n,u,l,p){const d=this.zones[u].vertices,g=this.zones[u].groups[l],f=[n],v={};v[n.id]=0,h=void 0,c.set(0,0,0),a=Infinity,s.setFromCoplanarPoints(d[n.vertexIds[0]],d[n.vertexIds[1]],d[n.vertexIds[2]]),s.projectPoint(r,t),i.copy(t);for(let e=f.pop();e;e=f.pop()){o.set(d[e.vertexIds[0]],d[e.vertexIds[1]],d[e.vertexIds[2]]),o.closestPointToPoint(i,t),t.distanceToSquared(i)<a&&(h=e,c.copy(t),a=t.distanceToSquared(i));const r=v[e.id];if(!(r>2))for(let t=0;t<e.neighbours.length;t++){const n=g[e.neighbours[t]];n.id in v||(f.push(n),v[n.id]=r+1)}}return p.copy(c),h}}();const b={PLAYER:new s(15631215).convertGammaToLinear(2.2).getHex(),TARGET:new s(14469912).convertGammaToLinear(2.2).getHex(),PATH:new s(41903).convertGammaToLinear(2.2).getHex(),WAYPOINT:new s(41903).convertGammaToLinear(2.2).getHex(),CLAMPED_STEP:new s(14472114).convertGammaToLinear(2.2).getHex(),CLOSEST_NODE:new s(4417387).convertGammaToLinear(2.2).getHex()};class m extends o{constructor(){super(),this._playerMarker=new i(new h(.25,32,32),new a({color:b.PLAYER})),this._targetMarker=new i(new c(.3,.3,.3),new a({color:b.TARGET})),this._nodeMarker=new i(new c(.1,.8,.1),new a({color:b.CLOSEST_NODE})),this._stepMarker=new i(new c(.1,1,.1),new a({color:b.CLAMPED_STEP})),this._pathMarker=new o,this._pathLineMaterial=new u({color:b.PATH,linewidth:2}),this._pathPointMaterial=new a({color:b.WAYPOINT}),this._pathPointGeometry=new h(.08),this._markers=[this._playerMarker,this._targetMarker,this._nodeMarker,this._stepMarker,this._pathMarker],this._markers.forEach(t=>{t.visible=!1,this.add(t)})}setPath(e){for(;this._pathMarker.children.length;)this._pathMarker.children[0].visible=!1,this._pathMarker.remove(this._pathMarker.children[0]);e=[this._playerMarker.position].concat(e);const r=new l;r.setAttribute("position",new t(new Float32Array(3*e.length),3));for(let t=0;t<e.length;t++)r.attributes.position.setXYZ(t,e[t].x,e[t].y+.2,e[t].z);this._pathMarker.add(new p(r,this._pathLineMaterial));for(let t=0;t<e.length-1;t++){const r=new i(this._pathPointGeometry,this._pathPointMaterial);r.position.copy(e[t]),r.position.y+=.2,this._pathMarker.add(r)}return this._pathMarker.visible=!0,this}setPlayerPosition(t){return this._playerMarker.position.copy(t),this._playerMarker.visible=!0,this}setTargetPosition(t){return this._targetMarker.position.copy(t),this._targetMarker.visible=!0,this}setNodePosition(t){return this._nodeMarker.position.copy(t),this._nodeMarker.visible=!0,this}setStepPosition(t){return this._stepMarker.position.copy(t),this._stepMarker.visible=!0,this}reset(){for(;this._pathMarker.children.length;)this._pathMarker.children[0].visible=!1,this._pathMarker.remove(this._pathMarker.children[0]);return this._markers.forEach(t=>{t.visible=!1}),this}}export{v as Pathfinding,m as PathfindingHelper}; | ||
import{BufferAttribute as t,BufferGeometry as e,Vector3 as r,Plane as n,Triangle as s,Color as o,Object3D as i,Mesh as h,SphereBufferGeometry as c,MeshBasicMaterial as a,BoxBufferGeometry as u,LineBasicMaterial as l,Line as p}from"three";class d{static roundNumber(t,e){const r=Math.pow(10,e);return Math.round(t*r)/r}static sample(t){return t[Math.floor(Math.random()*t.length)]}static distanceToSquared(t,e){var r=t.x-e.x,n=t.y-e.y,s=t.z-e.z;return r*r+n*n+s*s}static isPointInPoly(t,e){for(var r=!1,n=-1,s=t.length,o=s-1;++n<s;o=n)(t[n].z<=e.z&&e.z<t[o].z||t[o].z<=e.z&&e.z<t[n].z)&&e.x<(t[o].x-t[n].x)*(e.z-t[n].z)/(t[o].z-t[n].z)+t[n].x&&(r=!r);return r}static isVectorInPolygon(t,e,r){var n=1e5,s=-1e5,o=[];return e.vertexIds.forEach(t=>{n=Math.min(r[t].y,n),s=Math.max(r[t].y,s),o.push(r[t])}),!!(t.y<s+.5&&t.y>n-.5&&this.isPointInPoly(o,t))}static triarea2(t,e,r){return(r.x-t.x)*(e.z-t.z)-(e.x-t.x)*(r.z-t.z)}static vequal(t,e){return this.distanceToSquared(t,e)<1e-5}static mergeVertices(r,n=1e-4){n=Math.max(n,Number.EPSILON);for(var s={},o=r.getIndex(),i=r.getAttribute("position"),h=o?o.count:i.count,c=0,a=[],u=[],l=Math.log10(1/n),p=Math.pow(10,l),d=0;d<h;d++){var g=o?o.getX(d):d,f="";f+=~~(i.getX(g)*p)+",",f+=~~(i.getY(g)*p)+",",(f+=~~(i.getZ(g)*p)+",")in s?a.push(s[f]):(u.push(i.getX(g)),u.push(i.getY(g)),u.push(i.getZ(g)),s[f]=c,a.push(c),c++)}const v=new t(new Float32Array(u),i.itemSize,i.normalized),b=new e;return b.setAttribute("position",v),b.setIndex(a),b}}class g{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),r=this.content.pop();e!==this.content.length-1&&(this.content[e]=r,this.scoreFunction(r)<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 r=(t+1>>1)-1,n=this.content[r];if(!(this.scoreFunction(e)<this.scoreFunction(n)))break;this.content[r]=e,this.content[t]=n,t=r}}bubbleUp(t){const e=this.content.length,r=this.content[t],n=this.scoreFunction(r);for(;;){const s=t+1<<1,o=s-1;let i,h=null;if(o<e&&(i=this.scoreFunction(this.content[o]),i<n&&(h=o)),s<e&&this.scoreFunction(this.content[s])<(null===h?n:i)&&(h=s),null===h)break;this.content[t]=this.content[h],this.content[h]=r,t=h}}}class f{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 r,n,s,o=0,i=0,h=0;r=t[0].left,n=t[0].left,s=t[0].right,e.push(r);for(let c=1;c<t.length;c++){const a=t[c].left,u=t[c].right;if(d.triarea2(r,s,u)<=0){if(!(d.vequal(r,s)||d.triarea2(r,n,u)>0)){e.push(n),r=n,o=i,n=r,s=r,i=o,h=o,c=o;continue}s=u,h=c}if(d.triarea2(r,n,a)>=0){if(!(d.vequal(r,n)||d.triarea2(r,s,a)<0)){e.push(s),r=s,o=h,n=r,s=r,i=o,h=o,c=o;continue}n=a,i=c}}return 0!==e.length&&d.vequal(e[e.length-1],t[t.length-1].left)||e.push(t[t.length-1].left),this.path=e,e}}class v{constructor(){this.zones={}}static createZone(t,e=1e-4){return class{static buildZone(t,e){const n=this._buildNavigationMesh(t,e),s={};n.vertices.forEach(t=>{t.x=d.roundNumber(t.x,2),t.y=d.roundNumber(t.y,2),t.z=d.roundNumber(t.z,2)}),s.vertices=n.vertices;const o=this._buildPolygonGroups(n);return s.groups=new Array(o.length),o.forEach((t,e)=>{const n=new Map;t.forEach((t,e)=>{n.set(t,e)});const o=new Array(t.length);t.forEach((t,e)=>{const i=[];t.neighbours.forEach(t=>i.push(n.get(t)));const h=[];t.neighbours.forEach(e=>h.push(this._getSharedVerticesInOrder(t,e)));const c=new r(0,0,0);c.add(s.vertices[t.vertexIds[0]]),c.add(s.vertices[t.vertexIds[1]]),c.add(s.vertices[t.vertexIds[2]]),c.divideScalar(3),c.x=d.roundNumber(c.x,2),c.y=d.roundNumber(c.y,2),c.z=d.roundNumber(c.z,2),o[e]={id:e,neighbours:i,vertexIds:t.vertexIds,centroid:c,portals:h}}),s.groups[e]=o}),s}static _buildNavigationMesh(t,e){return t=d.mergeVertices(t,e),this._buildPolygonsFromGeometry(t)}static _buildPolygonGroups(t){const e=[],r=function t(e){e.neighbours.forEach(r=>{void 0===r.group&&(r.group=e.group,t(r))})};return t.polygons.forEach(t=>{void 0!==t.group?e[t.group].push(t):(t.group=e.length,r(t),e.push([t]))}),e}static _buildPolygonNeighbours(t,e){const r=new Set,n=e[t.vertexIds[1]],s=e[t.vertexIds[2]];return e[t.vertexIds[0]].forEach(e=>{e!==t&&(n.includes(e)||s.includes(e))&&r.add(e)}),n.forEach(e=>{e!==t&&s.includes(e)&&r.add(e)}),r}static _buildPolygonsFromGeometry(t){const e=[],n=[],s=t.attributes.position,o=t.index,i=[];for(let t=0;t<s.count;t++)n.push((new r).fromBufferAttribute(s,t)),i[t]=[];for(let r=0;r<t.index.count;r+=3){const t=o.getX(r),n=o.getX(r+1),s=o.getX(r+2),h={vertexIds:[t,n,s],neighbours:null};e.push(h),i[t].push(h),i[n].push(h),i[s].push(h)}return e.forEach(t=>{t.neighbours=this._buildPolygonNeighbours(t,i)}),{polygons:e,vertices:n}}static _getSharedVerticesInOrder(t,e){const r=t.vertexIds,n=r[0],s=r[1],o=r[2],i=e.vertexIds,h=i.includes(n),c=i.includes(s),a=i.includes(o);return h&&c&&a?Array.from(r):h&&c?[n,s]:c&&a?[s,o]:h&&a?[o,n]:(console.warn("Error processing navigation mesh neighbors; neighbors with <2 shared vertices found."),[])}}.buildZone(t,e)}setZoneData(t,e){this.zones[t]=e}getRandomNode(t,e,n,s){if(!this.zones[t])return new r;n=n||null,s=s||0;const o=[];return this.zones[t].groups[e].forEach(t=>{n&&s?d.distanceToSquared(n,t.centroid)<s*s&&o.push(t.centroid):o.push(t.centroid)}),d.sample(o)||new r}getClosestNode(t,e,r,n=!1){const s=this.zones[e].vertices;let o=null,i=Infinity;return this.zones[e].groups[r].forEach(e=>{const r=d.distanceToSquared(e.centroid,t);r<i&&(!n||d.isVectorInPolygon(t,e,s))&&(o=e,i=r)}),o}findPath(t,e,n,s){const o=this.zones[n].groups[s],i=this.zones[n].vertices,h=this.getClosestNode(t,n,s,!0),c=this.getClosestNode(e,n,s,!0);if(!h||!c)return null;const a=class{static init(t){for(let e=0;e<t.length;e++){const r=t[e];r.f=0,r.g=0,r.h=0,r.cost=1,r.visited=!1,r.closed=!1,r.parent=null}}static cleanUp(t){for(let e=0;e<t.length;e++){const r=t[e];delete r.f,delete r.g,delete r.h,delete r.cost,delete r.visited,delete r.closed,delete r.parent}}static heap(){return new g(function(t){return t.f})}static search(t,e,r){this.init(t);const n=this.heap();for(n.push(e);n.size()>0;){const e=n.pop();if(e===r){let t=e;const r=[];for(;t.parent;)r.push(t),t=t.parent;return this.cleanUp(r),r.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,h=o.visited;if(!h||i<o.g){if(o.visited=!0,o.parent=e,!o.centroid||!r.centroid)throw new Error("Unexpected state");o.h=o.h||this.heuristic(o.centroid,r.centroid),o.g=i,o.f=o.g+o.h,h?n.rescoreElement(o):n.push(o)}}}return[]}static heuristic(t,e){return d.distanceToSquared(t,e)}static neighbours(t,e){const r=[];for(let n=0;n<e.neighbours.length;n++)r.push(t[e.neighbours[n]]);return r}}.search(o,h,c),u=function(t,e){for(var r=0;r<t.neighbours.length;r++)if(t.neighbours[r]===e.id)return t.portals[r]},l=new f;l.push(t);for(let t=0;t<a.length;t++){const e=a[t],r=a[t+1];if(r){const t=u(e,r);l.push(i[t[0]],i[t[1]])}}l.push(e),l.stringPull();const p=l.path.map(t=>new r(t.x,t.y,t.z));return p.shift(),p}}v.prototype.getGroup=function(){const t=new n;return function(e,r,n=!1){if(!this.zones[e])return null;let s=null,o=Math.pow(50,2);const i=this.zones[e];for(let e=0;e<i.groups.length;e++){const h=i.groups[e];for(const c of h){if(n&&(t.setFromCoplanarPoints(i.vertices[c.vertexIds[0]],i.vertices[c.vertexIds[1]],i.vertices[c.vertexIds[2]]),Math.abs(t.distanceToPoint(r))<.01)&&d.isPointInPoly([i.vertices[c.vertexIds[0]],i.vertices[c.vertexIds[1]],i.vertices[c.vertexIds[2]]],r))return e;const h=d.distanceToSquared(c.centroid,r);h<o&&(s=e,o=h)}}return s}}(),v.prototype.clampStep=function(){const t=new r,e=new n,o=new s,i=new r;let h,c,a=new r;return function(r,n,s,u,l,p){const d=this.zones[u].vertices,g=this.zones[u].groups[l],f=[s],v={};v[s.id]=0,h=void 0,a.set(0,0,0),c=Infinity,e.setFromCoplanarPoints(d[s.vertexIds[0]],d[s.vertexIds[1]],d[s.vertexIds[2]]),e.projectPoint(n,t),i.copy(t);for(let e=f.pop();e;e=f.pop()){o.set(d[e.vertexIds[0]],d[e.vertexIds[1]],d[e.vertexIds[2]]),o.closestPointToPoint(i,t),t.distanceToSquared(i)<c&&(h=e,a.copy(t),c=t.distanceToSquared(i));const r=v[e.id];if(!(r>2))for(let t=0;t<e.neighbours.length;t++){const n=g[e.neighbours[t]];n.id in v||(f.push(n),v[n.id]=r+1)}}return p.copy(a),h}}();const b={PLAYER:new o(15631215).convertGammaToLinear(2.2).getHex(),TARGET:new o(14469912).convertGammaToLinear(2.2).getHex(),PATH:new o(41903).convertGammaToLinear(2.2).getHex(),WAYPOINT:new o(41903).convertGammaToLinear(2.2).getHex(),CLAMPED_STEP:new o(14472114).convertGammaToLinear(2.2).getHex(),CLOSEST_NODE:new o(4417387).convertGammaToLinear(2.2).getHex()};class w extends i{constructor(){super(),this._playerMarker=new h(new c(.25,32,32),new a({color:b.PLAYER})),this._targetMarker=new h(new u(.3,.3,.3),new a({color:b.TARGET})),this._nodeMarker=new h(new u(.1,.8,.1),new a({color:b.CLOSEST_NODE})),this._stepMarker=new h(new u(.1,1,.1),new a({color:b.CLAMPED_STEP})),this._pathMarker=new i,this._pathLineMaterial=new l({color:b.PATH,linewidth:2}),this._pathPointMaterial=new a({color:b.WAYPOINT}),this._pathPointGeometry=new c(.08),this._markers=[this._playerMarker,this._targetMarker,this._nodeMarker,this._stepMarker,this._pathMarker],this._markers.forEach(t=>{t.visible=!1,this.add(t)})}setPath(r){for(;this._pathMarker.children.length;)this._pathMarker.children[0].visible=!1,this._pathMarker.remove(this._pathMarker.children[0]);r=[this._playerMarker.position].concat(r);const n=new e;n.setAttribute("position",new t(new Float32Array(3*r.length),3));for(let t=0;t<r.length;t++)n.attributes.position.setXYZ(t,r[t].x,r[t].y+.2,r[t].z);this._pathMarker.add(new p(n,this._pathLineMaterial));for(let t=0;t<r.length-1;t++){const e=new h(this._pathPointGeometry,this._pathPointMaterial);e.position.copy(r[t]),e.position.y+=.2,this._pathMarker.add(e)}return this._pathMarker.visible=!0,this}setPlayerPosition(t){return this._playerMarker.position.copy(t),this._playerMarker.visible=!0,this}setTargetPosition(t){return this._targetMarker.position.copy(t),this._targetMarker.visible=!0,this}setNodePosition(t){return this._nodeMarker.position.copy(t),this._nodeMarker.visible=!0,this}setStepPosition(t){return this._stepMarker.position.copy(t),this._stepMarker.visible=!0,this}reset(){for(;this._pathMarker.children.length;)this._pathMarker.children[0].visible=!1,this._pathMarker.remove(this._pathMarker.children[0]);return this._markers.forEach(t=>{t.visible=!1}),this}}export{v as Pathfinding,w as PathfindingHelper}; | ||
//# sourceMappingURL=three-pathfinding.modern.js.map |
@@ -1,2 +0,2 @@ | ||
import{BufferAttribute as t,Vector3 as e,Plane as r,Triangle as n,Color as o,BufferGeometry as i,Line as s,Mesh as a,SphereBufferGeometry as u,MeshBasicMaterial as h,BoxBufferGeometry as c,Object3D as l,LineBasicMaterial as f}from"three";var p,v=function(){function e(){}return e.roundNumber=function(t,e){var r=Math.pow(10,e);return Math.round(t*r)/r},e.sample=function(t){return t[Math.floor(Math.random()*t.length)]},e.distanceToSquared=function(t,e){var r=t.x-e.x,n=t.y-e.y,o=t.z-e.z;return r*r+n*n+o*o},e.isPointInPoly=function(t,e){for(var r=!1,n=-1,o=t.length,i=o-1;++n<o;i=n)(t[n].z<=e.z&&e.z<t[i].z||t[i].z<=e.z&&e.z<t[n].z)&&e.x<(t[i].x-t[n].x)*(e.z-t[n].z)/(t[i].z-t[n].z)+t[n].x&&(r=!r);return r},e.isVectorInPolygon=function(t,e,r){var n=1e5,o=-1e5,i=[];return e.vertexIds.forEach(function(t){n=Math.min(r[t].y,n),o=Math.max(r[t].y,o),i.push(r[t])}),!!(t.y<o+.5&&t.y>n-.5&&this.isPointInPoly(i,t))},e.triarea2=function(t,e,r){return(r.x-t.x)*(e.z-t.z)-(e.x-t.x)*(r.z-t.z)},e.vequal=function(t,e){return this.distanceToSquared(t,e)<1e-5},e.mergeVertices=function(e,r){void 0===r&&(r=1e-4),r=Math.max(r,Number.EPSILON);for(var n={},o=e.getIndex(),i=e.getAttribute("position"),s=o?o.count:i.count,a=0,u=Object.keys(e.attributes),h={},c={},l=[],f=["getX","getY","getZ","getW"],p=0,v=u.length;p<v;p++)h[A=u[p]]=[],(M=e.morphAttributes[A])&&(c[A]=new Array(M.length).fill().map(function(){return[]}));var d=Math.log10(1/r),g=Math.pow(10,d);for(p=0;p<s;p++){var b=o?o.getX(p):p,y="",m=0;for(v=u.length;m<v;m++)for(var w=(_=e.getAttribute(A=u[m])).itemSize,x=0;x<w;x++)y+=~~(_[f[x]](b)*g)+",";if(y in n)l.push(n[y]);else{for(m=0,v=u.length;m<v;m++){var _=e.getAttribute(A=u[m]),M=e.morphAttributes[A],z=(w=_.itemSize,h[A]),P=c[A];for(x=0;x<w;x++){var k=f[x];if(z.push(_[k](b)),M)for(var I=0,E=M.length;I<E;I++)P[I].push(M[I][k](b))}}n[y]=a,l.push(a),a++}}var T=e.clone();for(p=0,v=u.length;p<v;p++){var A,S=e.getAttribute(A=u[p]),N=new S.array.constructor(h[A]);if(_=new t(N,S.itemSize,S.normalized),T.setAttribute(A,_),A in c)for(m=0;m<c[A].length;m++){var G=e.morphAttributes[A][m],L=(N=new G.array.constructor(c[A][m]),new t(N,G.itemSize,G.normalized));T.morphAttributes[A][m]=L}}return T.setIndex(l),T},e}(),d=function(){function t(t){this.content=[],this.scoreFunction=t}var e=t.prototype;return e.push=function(t){this.content.push(t),this.sinkDown(this.content.length-1)},e.pop=function(){var t=this.content[0],e=this.content.pop();return this.content.length>0&&(this.content[0]=e,this.bubbleUp(0)),t},e.remove=function(t){var e=this.content.indexOf(t),r=this.content.pop();e!==this.content.length-1&&(this.content[e]=r,this.scoreFunction(r)<this.scoreFunction(t)?this.sinkDown(e):this.bubbleUp(e))},e.size=function(){return this.content.length},e.rescoreElement=function(t){this.sinkDown(this.content.indexOf(t))},e.sinkDown=function(t){for(var e=this.content[t];t>0;){var r=(t+1>>1)-1,n=this.content[r];if(!(this.scoreFunction(e)<this.scoreFunction(n)))break;this.content[r]=e,this.content[t]=n,t=r}},e.bubbleUp=function(t){for(var e=this.content.length,r=this.content[t],n=this.scoreFunction(r);;){var o=t+1<<1,i=o-1,s=null,a=void 0;if(i<e&&(a=this.scoreFunction(this.content[i]))<n&&(s=i),o<e&&this.scoreFunction(this.content[o])<(null===s?n:a)&&(s=o),null===s)break;this.content[t]=this.content[s],this.content[s]=r,t=s}},t}(),g=function(){function t(){}return t.init=function(t){for(var e=0;e<t.length;e++){var r=t[e];r.f=0,r.g=0,r.h=0,r.cost=1,r.visited=!1,r.closed=!1,r.parent=null}},t.cleanUp=function(t){for(var e=0;e<t.length;e++){var r=t[e];delete r.f,delete r.g,delete r.h,delete r.cost,delete r.visited,delete r.closed,delete r.parent}},t.heap=function(){return new d(function(t){return t.f})},t.search=function(t,e,r){this.init(t);var n=this.heap();for(n.push(e);n.size()>0;){var o=n.pop();if(o===r){for(var i=o,s=[];i.parent;)s.push(i),i=i.parent;return this.cleanUp(s),s.reverse()}o.closed=!0;for(var a=this.neighbours(t,o),u=0,h=a.length;u<h;u++){var c=a[u];if(!c.closed){var l=o.g+c.cost,f=c.visited;if(!f||l<c.g){if(c.visited=!0,c.parent=o,!c.centroid||!r.centroid)throw new Error("Unexpected state");c.h=c.h||this.heuristic(c.centroid,r.centroid),c.g=l,c.f=c.g+c.h,f?n.rescoreElement(c):n.push(c)}}}}return[]},t.heuristic=function(t,e){return v.distanceToSquared(t,e)},t.neighbours=function(t,e){for(var r=[],n=0;n<e.neighbours.length;n++)r.push(t[e.neighbours[n]]);return r},t}(),b=function(){function t(){}return t.buildZone=function(t){var r=this,n=this._buildNavigationMesh(t),o={};n.vertices.forEach(function(t){t.x=v.roundNumber(t.x,2),t.y=v.roundNumber(t.y,2),t.z=v.roundNumber(t.z,2)}),o.vertices=n.vertices;var i=this._buildPolygonGroups(n);return o.groups=new Array(i.length),i.forEach(function(t,n){var i=new Map;t.forEach(function(t,e){i.set(t,e)});var s=new Array(t.length);t.forEach(function(t,n){var a=[];t.neighbours.forEach(function(t){return a.push(i.get(t))});var u=[];t.neighbours.forEach(function(e){return u.push(r._getSharedVerticesInOrder(t,e))});var h=new e(0,0,0);h.add(o.vertices[t.vertexIds[0]]),h.add(o.vertices[t.vertexIds[1]]),h.add(o.vertices[t.vertexIds[2]]),h.divideScalar(3),h.x=v.roundNumber(h.x,2),h.y=v.roundNumber(h.y,2),h.z=v.roundNumber(h.z,2),s[n]={id:n,neighbours:a,vertexIds:t.vertexIds,centroid:h,portals:u}}),o.groups[n]=s}),o},t._buildNavigationMesh=function(t){return t=v.mergeVertices(t),this._buildPolygonsFromGeometry(t)},t._buildPolygonGroups=function(t){var e=[],r=function t(e){e.neighbours.forEach(function(r){void 0===r.group&&(r.group=e.group,t(r))})};return t.polygons.forEach(function(t){void 0!==t.group?e[t.group].push(t):(t.group=e.length,r(t),e.push([t]))}),e},t._buildPolygonNeighbours=function(t,e){var r=new Set,n=e[t.vertexIds[1]],o=e[t.vertexIds[2]];return e[t.vertexIds[0]].forEach(function(e){e!==t&&(n.includes(e)||o.includes(e))&&r.add(e)}),n.forEach(function(e){e!==t&&o.includes(e)&&r.add(e)}),r},t._buildPolygonsFromGeometry=function(t){for(var r=this,n=[],o=[],i=t.attributes.position,s=t.index,a=[],u=0;u<i.count;u++)o.push((new e).fromBufferAttribute(i,u)),a[u]=[];for(var h=0;h<t.index.count;h+=3){var c=s.getX(h),l=s.getX(h+1),f=s.getX(h+2),p={vertexIds:[c,l,f],neighbours:null};n.push(p),a[c].push(p),a[l].push(p),a[f].push(p)}return n.forEach(function(t){t.neighbours=r._buildPolygonNeighbours(t,a)}),{polygons:n,vertices:o}},t._getSharedVerticesInOrder=function(t,e){var r=t.vertexIds,n=r[0],o=r[1],i=r[2],s=e.vertexIds,a=s.includes(n),u=s.includes(o),h=s.includes(i);return a&&u&&h?Array.from(r):a&&u?[n,o]:u&&h?[o,i]:a&&h?[i,n]:(console.warn("Error processing navigation mesh neighbors; neighbors with <2 shared vertices found."),[])},t}(),y=function(){function t(){this.portals=[]}var e=t.prototype;return e.push=function(t,e){void 0===e&&(e=t),this.portals.push({left:t,right:e})},e.stringPull=function(){var t,e,r,n=this.portals,o=[],i=0,s=0,a=0;e=n[0].left,r=n[0].right,o.push(t=n[0].left);for(var u=1;u<n.length;u++){var h=n[u].left,c=n[u].right;if(v.triarea2(t,r,c)<=0){if(!(v.vequal(t,r)||v.triarea2(t,e,c)>0)){o.push(e),e=t=e,r=t,s=i=s,a=i,u=i;continue}r=c,a=u}if(v.triarea2(t,e,h)>=0){if(!(v.vequal(t,e)||v.triarea2(t,r,h)<0)){o.push(r),e=t=r,r=t,s=i=a,a=i,u=i;continue}e=h,s=u}}return 0!==o.length&&v.vequal(o[o.length-1],n[n.length-1].left)||o.push(n[n.length-1].left),this.path=o,o},t}(),m=function(){function t(){this.zones={}}t.createZone=function(t){return b.buildZone(t)};var r=t.prototype;return r.setZoneData=function(t,e){this.zones[t]=e},r.getRandomNode=function(t,r,n,o){if(!this.zones[t])return new e;n=n||null,o=o||0;var i=[];return this.zones[t].groups[r].forEach(function(t){n&&o?v.distanceToSquared(n,t.centroid)<o*o&&i.push(t.centroid):i.push(t.centroid)}),v.sample(i)||new e},r.getClosestNode=function(t,e,r,n){void 0===n&&(n=!1);var o=this.zones[e].vertices,i=null,s=Infinity;return this.zones[e].groups[r].forEach(function(e){var r=v.distanceToSquared(e.centroid,t);r<s&&(!n||v.isVectorInPolygon(t,e,o))&&(i=e,s=r)}),i},r.findPath=function(t,r,n,o){var i=this.zones[n].groups[o],s=this.zones[n].vertices,a=this.getClosestNode(t,n,o,!0),u=this.getClosestNode(r,n,o,!0);if(!a||!u)return null;var h=g.search(i,a,u),c=function(t,e){for(var r=0;r<t.neighbours.length;r++)if(t.neighbours[r]===e.id)return t.portals[r]},l=new y;l.push(t);for(var f=0;f<h.length;f++){var p=h[f+1];if(p){var v=c(h[f],p);l.push(s[v[0]],s[v[1]])}}l.push(r),l.stringPull();var d=l.path.map(function(t){return new e(t.x,t.y,t.z)});return d.shift(),d},t}();m.prototype.getGroup=(p=new r,function(t,e,r){if(void 0===r&&(r=!1),!this.zones[t])return null;for(var n=null,o=Math.pow(50,2),i=this.zones[t],s=0;s<i.groups.length;s++){var a=i.groups[s],u=Array.isArray(a),h=0;for(a=u?a:a[Symbol.iterator]();;){var c;if(u){if(h>=a.length)break;c=a[h++]}else{if((h=a.next()).done)break;c=h.value}var l=c;if(r&&(p.setFromCoplanarPoints(i.vertices[l.vertexIds[0]],i.vertices[l.vertexIds[1]],i.vertices[l.vertexIds[2]]),Math.abs(p.distanceToPoint(e))<.01&&v.isPointInPoly([i.vertices[l.vertexIds[0]],i.vertices[l.vertexIds[1]],i.vertices[l.vertexIds[2]]],e)))return s;var f=v.distanceToSquared(l.centroid,e);f<o&&(n=s,o=f)}}return n}),m.prototype.clampStep=function(){var t,o,i=new e,s=new r,a=new n,u=new e,h=new e;return function(e,r,n,c,l,f){var p=this.zones[c].vertices,v=this.zones[c].groups[l],d=[n],g={};g[n.id]=0,t=void 0,h.set(0,0,0),o=Infinity,s.setFromCoplanarPoints(p[n.vertexIds[0]],p[n.vertexIds[1]],p[n.vertexIds[2]]),s.projectPoint(r,i),u.copy(i);for(var b=d.pop();b;b=d.pop()){a.set(p[b.vertexIds[0]],p[b.vertexIds[1]],p[b.vertexIds[2]]),a.closestPointToPoint(u,i),i.distanceToSquared(u)<o&&(t=b,h.copy(i),o=i.distanceToSquared(u));var y=g[b.id];if(!(y>2))for(var m=0;m<b.neighbours.length;m++){var w=v[b.neighbours[m]];w.id in g||(d.push(w),g[w.id]=y+1)}}return f.copy(h),t}}();var w={PLAYER:new o(15631215).convertGammaToLinear(2.2).getHex(),TARGET:new o(14469912).convertGammaToLinear(2.2).getHex(),PATH:new o(41903).convertGammaToLinear(2.2).getHex(),WAYPOINT:new o(41903).convertGammaToLinear(2.2).getHex(),CLAMPED_STEP:new o(14472114).convertGammaToLinear(2.2).getHex(),CLOSEST_NODE:new o(4417387).convertGammaToLinear(2.2).getHex()},x=function(e){var r,n;function o(){var t;return(t=e.call(this)||this)._playerMarker=new a(new u(.25,32,32),new h({color:w.PLAYER})),t._targetMarker=new a(new c(.3,.3,.3),new h({color:w.TARGET})),t._nodeMarker=new a(new c(.1,.8,.1),new h({color:w.CLOSEST_NODE})),t._stepMarker=new a(new c(.1,1,.1),new h({color:w.CLAMPED_STEP})),t._pathMarker=new l,t._pathLineMaterial=new f({color:w.PATH,linewidth:2}),t._pathPointMaterial=new h({color:w.WAYPOINT}),t._pathPointGeometry=new u(.08),t._markers=[t._playerMarker,t._targetMarker,t._nodeMarker,t._stepMarker,t._pathMarker],t._markers.forEach(function(e){e.visible=!1,t.add(e)}),t}n=e,(r=o).prototype=Object.create(n.prototype),r.prototype.constructor=r,r.__proto__=n;var p=o.prototype;return p.setPath=function(e){for(;this._pathMarker.children.length;)this._pathMarker.children[0].visible=!1,this._pathMarker.remove(this._pathMarker.children[0]);e=[this._playerMarker.position].concat(e);var r=new i;r.setAttribute("position",new t(new Float32Array(3*e.length),3));for(var n=0;n<e.length;n++)r.attributes.position.setXYZ(n,e[n].x,e[n].y+.2,e[n].z);this._pathMarker.add(new s(r,this._pathLineMaterial));for(var o=0;o<e.length-1;o++){var u=new a(this._pathPointGeometry,this._pathPointMaterial);u.position.copy(e[o]),u.position.y+=.2,this._pathMarker.add(u)}return this._pathMarker.visible=!0,this},p.setPlayerPosition=function(t){return this._playerMarker.position.copy(t),this._playerMarker.visible=!0,this},p.setTargetPosition=function(t){return this._targetMarker.position.copy(t),this._targetMarker.visible=!0,this},p.setNodePosition=function(t){return this._nodeMarker.position.copy(t),this._nodeMarker.visible=!0,this},p.setStepPosition=function(t){return this._stepMarker.position.copy(t),this._stepMarker.visible=!0,this},p.reset=function(){for(;this._pathMarker.children.length;)this._pathMarker.children[0].visible=!1,this._pathMarker.remove(this._pathMarker.children[0]);return this._markers.forEach(function(t){t.visible=!1}),this},o}(l);export{m as Pathfinding,x as PathfindingHelper}; | ||
import{BufferAttribute as t,BufferGeometry as e,Vector3 as r,Plane as n,Triangle as o,Color as i,Line as s,Mesh as a,SphereBufferGeometry as u,MeshBasicMaterial as h,BoxBufferGeometry as c,Object3D as l,LineBasicMaterial as p}from"three";function f(t,e){return(f=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function d(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=new Array(e);r<e;r++)n[r]=t[r];return n}function v(t,e){var r;if("undefined"==typeof Symbol||null==t[Symbol.iterator]){if(Array.isArray(t)||(r=function(t,e){if(t){if("string"==typeof t)return d(t,e);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?d(t,e):void 0}}(t))||e&&t&&"number"==typeof t.length){r&&(t=r);var n=0;return function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}return(r=t[Symbol.iterator]()).next.bind(r)}var g,b=function(){function r(){}return r.roundNumber=function(t,e){var r=Math.pow(10,e);return Math.round(t*r)/r},r.sample=function(t){return t[Math.floor(Math.random()*t.length)]},r.distanceToSquared=function(t,e){var r=t.x-e.x,n=t.y-e.y,o=t.z-e.z;return r*r+n*n+o*o},r.isPointInPoly=function(t,e){for(var r=!1,n=-1,o=t.length,i=o-1;++n<o;i=n)(t[n].z<=e.z&&e.z<t[i].z||t[i].z<=e.z&&e.z<t[n].z)&&e.x<(t[i].x-t[n].x)*(e.z-t[n].z)/(t[i].z-t[n].z)+t[n].x&&(r=!r);return r},r.isVectorInPolygon=function(t,e,r){var n=1e5,o=-1e5,i=[];return e.vertexIds.forEach(function(t){n=Math.min(r[t].y,n),o=Math.max(r[t].y,o),i.push(r[t])}),!!(t.y<o+.5&&t.y>n-.5&&this.isPointInPoly(i,t))},r.triarea2=function(t,e,r){return(r.x-t.x)*(e.z-t.z)-(e.x-t.x)*(r.z-t.z)},r.vequal=function(t,e){return this.distanceToSquared(t,e)<1e-5},r.mergeVertices=function(r,n){void 0===n&&(n=1e-4),n=Math.max(n,Number.EPSILON);for(var o={},i=r.getIndex(),s=r.getAttribute("position"),a=i?i.count:s.count,u=0,h=[],c=[],l=Math.log10(1/n),p=Math.pow(10,l),f=0;f<a;f++){var d=i?i.getX(f):f,v="";v+=~~(s.getX(d)*p)+",",v+=~~(s.getY(d)*p)+",",(v+=~~(s.getZ(d)*p)+",")in o?h.push(o[v]):(c.push(s.getX(d)),c.push(s.getY(d)),c.push(s.getZ(d)),o[v]=u,h.push(u),u++)}var g=new t(new Float32Array(c),s.itemSize,s.normalized),b=new e;return b.setAttribute("position",g),b.setIndex(h),b},r}(),y=function(){function t(t){this.content=[],this.scoreFunction=t}var e=t.prototype;return e.push=function(t){this.content.push(t),this.sinkDown(this.content.length-1)},e.pop=function(){var t=this.content[0],e=this.content.pop();return this.content.length>0&&(this.content[0]=e,this.bubbleUp(0)),t},e.remove=function(t){var e=this.content.indexOf(t),r=this.content.pop();e!==this.content.length-1&&(this.content[e]=r,this.scoreFunction(r)<this.scoreFunction(t)?this.sinkDown(e):this.bubbleUp(e))},e.size=function(){return this.content.length},e.rescoreElement=function(t){this.sinkDown(this.content.indexOf(t))},e.sinkDown=function(t){for(var e=this.content[t];t>0;){var r=(t+1>>1)-1,n=this.content[r];if(!(this.scoreFunction(e)<this.scoreFunction(n)))break;this.content[r]=e,this.content[t]=n,t=r}},e.bubbleUp=function(t){for(var e=this.content.length,r=this.content[t],n=this.scoreFunction(r);;){var o=t+1<<1,i=o-1,s=null,a=void 0;if(i<e&&(a=this.scoreFunction(this.content[i]))<n&&(s=i),o<e&&this.scoreFunction(this.content[o])<(null===s?n:a)&&(s=o),null===s)break;this.content[t]=this.content[s],this.content[s]=r,t=s}},t}(),m=function(){function t(){}return t.init=function(t){for(var e=0;e<t.length;e++){var r=t[e];r.f=0,r.g=0,r.h=0,r.cost=1,r.visited=!1,r.closed=!1,r.parent=null}},t.cleanUp=function(t){for(var e=0;e<t.length;e++){var r=t[e];delete r.f,delete r.g,delete r.h,delete r.cost,delete r.visited,delete r.closed,delete r.parent}},t.heap=function(){return new y(function(t){return t.f})},t.search=function(t,e,r){this.init(t);var n=this.heap();for(n.push(e);n.size()>0;){var o=n.pop();if(o===r){for(var i=o,s=[];i.parent;)s.push(i),i=i.parent;return this.cleanUp(s),s.reverse()}o.closed=!0;for(var a=this.neighbours(t,o),u=0,h=a.length;u<h;u++){var c=a[u];if(!c.closed){var l=o.g+c.cost,p=c.visited;if(!p||l<c.g){if(c.visited=!0,c.parent=o,!c.centroid||!r.centroid)throw new Error("Unexpected state");c.h=c.h||this.heuristic(c.centroid,r.centroid),c.g=l,c.f=c.g+c.h,p?n.rescoreElement(c):n.push(c)}}}}return[]},t.heuristic=function(t,e){return b.distanceToSquared(t,e)},t.neighbours=function(t,e){for(var r=[],n=0;n<e.neighbours.length;n++)r.push(t[e.neighbours[n]]);return r},t}(),w=function(){function t(){}return t.buildZone=function(t,e){var n=this,o=this._buildNavigationMesh(t,e),i={};o.vertices.forEach(function(t){t.x=b.roundNumber(t.x,2),t.y=b.roundNumber(t.y,2),t.z=b.roundNumber(t.z,2)}),i.vertices=o.vertices;var s=this._buildPolygonGroups(o);return i.groups=new Array(s.length),s.forEach(function(t,e){var o=new Map;t.forEach(function(t,e){o.set(t,e)});var s=new Array(t.length);t.forEach(function(t,e){var a=[];t.neighbours.forEach(function(t){return a.push(o.get(t))});var u=[];t.neighbours.forEach(function(e){return u.push(n._getSharedVerticesInOrder(t,e))});var h=new r(0,0,0);h.add(i.vertices[t.vertexIds[0]]),h.add(i.vertices[t.vertexIds[1]]),h.add(i.vertices[t.vertexIds[2]]),h.divideScalar(3),h.x=b.roundNumber(h.x,2),h.y=b.roundNumber(h.y,2),h.z=b.roundNumber(h.z,2),s[e]={id:e,neighbours:a,vertexIds:t.vertexIds,centroid:h,portals:u}}),i.groups[e]=s}),i},t._buildNavigationMesh=function(t,e){return t=b.mergeVertices(t,e),this._buildPolygonsFromGeometry(t)},t._buildPolygonGroups=function(t){var e=[],r=function t(e){e.neighbours.forEach(function(r){void 0===r.group&&(r.group=e.group,t(r))})};return t.polygons.forEach(function(t){void 0!==t.group?e[t.group].push(t):(t.group=e.length,r(t),e.push([t]))}),e},t._buildPolygonNeighbours=function(t,e){var r=new Set,n=e[t.vertexIds[1]],o=e[t.vertexIds[2]];return e[t.vertexIds[0]].forEach(function(e){e!==t&&(n.includes(e)||o.includes(e))&&r.add(e)}),n.forEach(function(e){e!==t&&o.includes(e)&&r.add(e)}),r},t._buildPolygonsFromGeometry=function(t){for(var e=this,n=[],o=[],i=t.attributes.position,s=t.index,a=[],u=0;u<i.count;u++)o.push((new r).fromBufferAttribute(i,u)),a[u]=[];for(var h=0;h<t.index.count;h+=3){var c=s.getX(h),l=s.getX(h+1),p=s.getX(h+2),f={vertexIds:[c,l,p],neighbours:null};n.push(f),a[c].push(f),a[l].push(f),a[p].push(f)}return n.forEach(function(t){t.neighbours=e._buildPolygonNeighbours(t,a)}),{polygons:n,vertices:o}},t._getSharedVerticesInOrder=function(t,e){var r=t.vertexIds,n=r[0],o=r[1],i=r[2],s=e.vertexIds,a=s.includes(n),u=s.includes(o),h=s.includes(i);return a&&u&&h?Array.from(r):a&&u?[n,o]:u&&h?[o,i]:a&&h?[i,n]:(console.warn("Error processing navigation mesh neighbors; neighbors with <2 shared vertices found."),[])},t}(),x=function(){function t(){this.portals=[]}var e=t.prototype;return e.push=function(t,e){void 0===e&&(e=t),this.portals.push({left:t,right:e})},e.stringPull=function(){var t,e,r,n=this.portals,o=[],i=0,s=0,a=0;e=n[0].left,r=n[0].right,o.push(t=n[0].left);for(var u=1;u<n.length;u++){var h=n[u].left,c=n[u].right;if(b.triarea2(t,r,c)<=0){if(!(b.vequal(t,r)||b.triarea2(t,e,c)>0)){o.push(e),e=t=e,r=t,s=i=s,a=i,u=i;continue}r=c,a=u}if(b.triarea2(t,e,h)>=0){if(!(b.vequal(t,e)||b.triarea2(t,r,h)<0)){o.push(r),e=t=r,r=t,s=i=a,a=i,u=i;continue}e=h,s=u}}return 0!==o.length&&b.vequal(o[o.length-1],n[n.length-1].left)||o.push(n[n.length-1].left),this.path=o,o},t}(),_=function(){function t(){this.zones={}}t.createZone=function(t,e){return void 0===e&&(e=1e-4),w.buildZone(t,e)};var e=t.prototype;return e.setZoneData=function(t,e){this.zones[t]=e},e.getRandomNode=function(t,e,n,o){if(!this.zones[t])return new r;n=n||null,o=o||0;var i=[];return this.zones[t].groups[e].forEach(function(t){n&&o?b.distanceToSquared(n,t.centroid)<o*o&&i.push(t.centroid):i.push(t.centroid)}),b.sample(i)||new r},e.getClosestNode=function(t,e,r,n){void 0===n&&(n=!1);var o=this.zones[e].vertices,i=null,s=Infinity;return this.zones[e].groups[r].forEach(function(e){var r=b.distanceToSquared(e.centroid,t);r<s&&(!n||b.isVectorInPolygon(t,e,o))&&(i=e,s=r)}),i},e.findPath=function(t,e,n,o){var i=this.zones[n].groups[o],s=this.zones[n].vertices,a=this.getClosestNode(t,n,o,!0),u=this.getClosestNode(e,n,o,!0);if(!a||!u)return null;var h=m.search(i,a,u),c=function(t,e){for(var r=0;r<t.neighbours.length;r++)if(t.neighbours[r]===e.id)return t.portals[r]},l=new x;l.push(t);for(var p=0;p<h.length;p++){var f=h[p+1];if(f){var d=c(h[p],f);l.push(s[d[0]],s[d[1]])}}l.push(e),l.stringPull();var v=l.path.map(function(t){return new r(t.x,t.y,t.z)});return v.shift(),v},t}();_.prototype.getGroup=(g=new n,function(t,e,r){if(void 0===r&&(r=!1),!this.zones[t])return null;for(var n=null,o=Math.pow(50,2),i=this.zones[t],s=0;s<i.groups.length;s++)for(var a,u=v(i.groups[s]);!(a=u()).done;){var h=a.value;if(r&&(g.setFromCoplanarPoints(i.vertices[h.vertexIds[0]],i.vertices[h.vertexIds[1]],i.vertices[h.vertexIds[2]]),Math.abs(g.distanceToPoint(e))<.01&&b.isPointInPoly([i.vertices[h.vertexIds[0]],i.vertices[h.vertexIds[1]],i.vertices[h.vertexIds[2]]],e)))return s;var c=b.distanceToSquared(h.centroid,e);c<o&&(n=s,o=c)}return n}),_.prototype.clampStep=function(){var t,e,i=new r,s=new n,a=new o,u=new r,h=new r;return function(r,n,o,c,l,p){var f=this.zones[c].vertices,d=this.zones[c].groups[l],v=[o],g={};g[o.id]=0,t=void 0,h.set(0,0,0),e=Infinity,s.setFromCoplanarPoints(f[o.vertexIds[0]],f[o.vertexIds[1]],f[o.vertexIds[2]]),s.projectPoint(n,i),u.copy(i);for(var b=v.pop();b;b=v.pop()){a.set(f[b.vertexIds[0]],f[b.vertexIds[1]],f[b.vertexIds[2]]),a.closestPointToPoint(u,i),i.distanceToSquared(u)<e&&(t=b,h.copy(i),e=i.distanceToSquared(u));var y=g[b.id];if(!(y>2))for(var m=0;m<b.neighbours.length;m++){var w=d[b.neighbours[m]];w.id in g||(v.push(w),g[w.id]=y+1)}}return p.copy(h),t}}();var M={PLAYER:new i(15631215).convertGammaToLinear(2.2).getHex(),TARGET:new i(14469912).convertGammaToLinear(2.2).getHex(),PATH:new i(41903).convertGammaToLinear(2.2).getHex(),WAYPOINT:new i(41903).convertGammaToLinear(2.2).getHex(),CLAMPED_STEP:new i(14472114).convertGammaToLinear(2.2).getHex(),CLOSEST_NODE:new i(4417387).convertGammaToLinear(2.2).getHex()},P=function(r){var n,o;function i(){var t;return(t=r.call(this)||this)._playerMarker=new a(new u(.25,32,32),new h({color:M.PLAYER})),t._targetMarker=new a(new c(.3,.3,.3),new h({color:M.TARGET})),t._nodeMarker=new a(new c(.1,.8,.1),new h({color:M.CLOSEST_NODE})),t._stepMarker=new a(new c(.1,1,.1),new h({color:M.CLAMPED_STEP})),t._pathMarker=new l,t._pathLineMaterial=new p({color:M.PATH,linewidth:2}),t._pathPointMaterial=new h({color:M.WAYPOINT}),t._pathPointGeometry=new u(.08),t._markers=[t._playerMarker,t._targetMarker,t._nodeMarker,t._stepMarker,t._pathMarker],t._markers.forEach(function(e){e.visible=!1,t.add(e)}),t}o=r,(n=i).prototype=Object.create(o.prototype),n.prototype.constructor=n,f(n,o);var d=i.prototype;return d.setPath=function(r){for(;this._pathMarker.children.length;)this._pathMarker.children[0].visible=!1,this._pathMarker.remove(this._pathMarker.children[0]);r=[this._playerMarker.position].concat(r);var n=new e;n.setAttribute("position",new t(new Float32Array(3*r.length),3));for(var o=0;o<r.length;o++)n.attributes.position.setXYZ(o,r[o].x,r[o].y+.2,r[o].z);this._pathMarker.add(new s(n,this._pathLineMaterial));for(var i=0;i<r.length-1;i++){var u=new a(this._pathPointGeometry,this._pathPointMaterial);u.position.copy(r[i]),u.position.y+=.2,this._pathMarker.add(u)}return this._pathMarker.visible=!0,this},d.setPlayerPosition=function(t){return this._playerMarker.position.copy(t),this._playerMarker.visible=!0,this},d.setTargetPosition=function(t){return this._targetMarker.position.copy(t),this._targetMarker.visible=!0,this},d.setNodePosition=function(t){return this._nodeMarker.position.copy(t),this._nodeMarker.visible=!0,this},d.setStepPosition=function(t){return this._stepMarker.position.copy(t),this._stepMarker.visible=!0,this},d.reset=function(){for(;this._pathMarker.children.length;)this._pathMarker.children[0].visible=!1,this._pathMarker.remove(this._pathMarker.children[0]);return this._markers.forEach(function(t){t.visible=!1}),this},i}(l);export{_ as Pathfinding,P as PathfindingHelper}; | ||
//# sourceMappingURL=three-pathfinding.module.js.map |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("three")):"function"==typeof define&&define.amd?define(["exports","three"],t):t((e||self).threePathfinding={},e.THREE)}(this,function(e,t){var r,n=function(){function e(){}return e.roundNumber=function(e,t){var r=Math.pow(10,t);return Math.round(e*r)/r},e.sample=function(e){return e[Math.floor(Math.random()*e.length)]},e.distanceToSquared=function(e,t){var r=e.x-t.x,n=e.y-t.y,o=e.z-t.z;return r*r+n*n+o*o},e.isPointInPoly=function(e,t){for(var r=!1,n=-1,o=e.length,i=o-1;++n<o;i=n)(e[n].z<=t.z&&t.z<e[i].z||e[i].z<=t.z&&t.z<e[n].z)&&t.x<(e[i].x-e[n].x)*(t.z-e[n].z)/(e[i].z-e[n].z)+e[n].x&&(r=!r);return r},e.isVectorInPolygon=function(e,t,r){var n=1e5,o=-1e5,i=[];return t.vertexIds.forEach(function(e){n=Math.min(r[e].y,n),o=Math.max(r[e].y,o),i.push(r[e])}),!!(e.y<o+.5&&e.y>n-.5&&this.isPointInPoly(i,e))},e.triarea2=function(e,t,r){return(r.x-e.x)*(t.z-e.z)-(t.x-e.x)*(r.z-e.z)},e.vequal=function(e,t){return this.distanceToSquared(e,t)<1e-5},e.mergeVertices=function(e,r){void 0===r&&(r=1e-4),r=Math.max(r,Number.EPSILON);for(var n={},o=e.getIndex(),i=e.getAttribute("position"),s=o?o.count:i.count,a=0,u=Object.keys(e.attributes),h={},c={},f=[],l=["getX","getY","getZ","getW"],p=0,d=u.length;p<d;p++)h[A=u[p]]=[],(_=e.morphAttributes[A])&&(c[A]=new Array(_.length).fill().map(function(){return[]}));var v=Math.log10(1/r),g=Math.pow(10,v);for(p=0;p<s;p++){var b=o?o.getX(p):p,y="",m=0;for(d=u.length;m<d;m++)for(var M=(w=e.getAttribute(A=u[m])).itemSize,x=0;x<M;x++)y+=~~(w[l[x]](b)*g)+",";if(y in n)f.push(n[y]);else{for(m=0,d=u.length;m<d;m++){var w=e.getAttribute(A=u[m]),_=e.morphAttributes[A],P=(M=w.itemSize,h[A]),z=c[A];for(x=0;x<M;x++){var k=l[x];if(P.push(w[k](b)),_)for(var I=0,E=_.length;I<E;I++)z[I].push(_[I][k](b))}}n[y]=a,f.push(a),a++}}var T=e.clone();for(p=0,d=u.length;p<d;p++){var A,S=e.getAttribute(A=u[p]),N=new S.array.constructor(h[A]);if(w=new t.BufferAttribute(N,S.itemSize,S.normalized),T.setAttribute(A,w),A in c)for(m=0;m<c[A].length;m++){var G=e.morphAttributes[A][m],B=(N=new G.array.constructor(c[A][m]),new t.BufferAttribute(N,G.itemSize,G.normalized));T.morphAttributes[A][m]=B}}return T.setIndex(f),T},e}(),o=function(){function e(e){this.content=[],this.scoreFunction=e}var t=e.prototype;return t.push=function(e){this.content.push(e),this.sinkDown(this.content.length-1)},t.pop=function(){var e=this.content[0],t=this.content.pop();return this.content.length>0&&(this.content[0]=t,this.bubbleUp(0)),e},t.remove=function(e){var t=this.content.indexOf(e),r=this.content.pop();t!==this.content.length-1&&(this.content[t]=r,this.scoreFunction(r)<this.scoreFunction(e)?this.sinkDown(t):this.bubbleUp(t))},t.size=function(){return this.content.length},t.rescoreElement=function(e){this.sinkDown(this.content.indexOf(e))},t.sinkDown=function(e){for(var t=this.content[e];e>0;){var r=(e+1>>1)-1,n=this.content[r];if(!(this.scoreFunction(t)<this.scoreFunction(n)))break;this.content[r]=t,this.content[e]=n,e=r}},t.bubbleUp=function(e){for(var t=this.content.length,r=this.content[e],n=this.scoreFunction(r);;){var o=e+1<<1,i=o-1,s=null,a=void 0;if(i<t&&(a=this.scoreFunction(this.content[i]))<n&&(s=i),o<t&&this.scoreFunction(this.content[o])<(null===s?n:a)&&(s=o),null===s)break;this.content[e]=this.content[s],this.content[s]=r,e=s}},e}(),i=function(){function e(){}return e.init=function(e){for(var t=0;t<e.length;t++){var r=e[t];r.f=0,r.g=0,r.h=0,r.cost=1,r.visited=!1,r.closed=!1,r.parent=null}},e.cleanUp=function(e){for(var t=0;t<e.length;t++){var r=e[t];delete r.f,delete r.g,delete r.h,delete r.cost,delete r.visited,delete r.closed,delete r.parent}},e.heap=function(){return new o(function(e){return e.f})},e.search=function(e,t,r){this.init(e);var n=this.heap();for(n.push(t);n.size()>0;){var o=n.pop();if(o===r){for(var i=o,s=[];i.parent;)s.push(i),i=i.parent;return this.cleanUp(s),s.reverse()}o.closed=!0;for(var a=this.neighbours(e,o),u=0,h=a.length;u<h;u++){var c=a[u];if(!c.closed){var f=o.g+c.cost,l=c.visited;if(!l||f<c.g){if(c.visited=!0,c.parent=o,!c.centroid||!r.centroid)throw new Error("Unexpected state");c.h=c.h||this.heuristic(c.centroid,r.centroid),c.g=f,c.f=c.g+c.h,l?n.rescoreElement(c):n.push(c)}}}}return[]},e.heuristic=function(e,t){return n.distanceToSquared(e,t)},e.neighbours=function(e,t){for(var r=[],n=0;n<t.neighbours.length;n++)r.push(e[t.neighbours[n]]);return r},e}(),s=function(){function e(){}return e.buildZone=function(e){var r=this,o=this._buildNavigationMesh(e),i={};o.vertices.forEach(function(e){e.x=n.roundNumber(e.x,2),e.y=n.roundNumber(e.y,2),e.z=n.roundNumber(e.z,2)}),i.vertices=o.vertices;var s=this._buildPolygonGroups(o);return i.groups=new Array(s.length),s.forEach(function(e,o){var s=new Map;e.forEach(function(e,t){s.set(e,t)});var a=new Array(e.length);e.forEach(function(e,o){var u=[];e.neighbours.forEach(function(e){return u.push(s.get(e))});var h=[];e.neighbours.forEach(function(t){return h.push(r._getSharedVerticesInOrder(e,t))});var c=new t.Vector3(0,0,0);c.add(i.vertices[e.vertexIds[0]]),c.add(i.vertices[e.vertexIds[1]]),c.add(i.vertices[e.vertexIds[2]]),c.divideScalar(3),c.x=n.roundNumber(c.x,2),c.y=n.roundNumber(c.y,2),c.z=n.roundNumber(c.z,2),a[o]={id:o,neighbours:u,vertexIds:e.vertexIds,centroid:c,portals:h}}),i.groups[o]=a}),i},e._buildNavigationMesh=function(e){return e=n.mergeVertices(e),this._buildPolygonsFromGeometry(e)},e._buildPolygonGroups=function(e){var t=[],r=function e(t){t.neighbours.forEach(function(r){void 0===r.group&&(r.group=t.group,e(r))})};return e.polygons.forEach(function(e){void 0!==e.group?t[e.group].push(e):(e.group=t.length,r(e),t.push([e]))}),t},e._buildPolygonNeighbours=function(e,t){var r=new Set,n=t[e.vertexIds[1]],o=t[e.vertexIds[2]];return t[e.vertexIds[0]].forEach(function(t){t!==e&&(n.includes(t)||o.includes(t))&&r.add(t)}),n.forEach(function(t){t!==e&&o.includes(t)&&r.add(t)}),r},e._buildPolygonsFromGeometry=function(e){for(var r=this,n=[],o=[],i=e.attributes.position,s=e.index,a=[],u=0;u<i.count;u++)o.push((new t.Vector3).fromBufferAttribute(i,u)),a[u]=[];for(var h=0;h<e.index.count;h+=3){var c=s.getX(h),f=s.getX(h+1),l=s.getX(h+2),p={vertexIds:[c,f,l],neighbours:null};n.push(p),a[c].push(p),a[f].push(p),a[l].push(p)}return n.forEach(function(e){e.neighbours=r._buildPolygonNeighbours(e,a)}),{polygons:n,vertices:o}},e._getSharedVerticesInOrder=function(e,t){var r=e.vertexIds,n=r[0],o=r[1],i=r[2],s=t.vertexIds,a=s.includes(n),u=s.includes(o),h=s.includes(i);return a&&u&&h?Array.from(r):a&&u?[n,o]:u&&h?[o,i]:a&&h?[i,n]:(console.warn("Error processing navigation mesh neighbors; neighbors with <2 shared vertices found."),[])},e}(),a=function(){function e(){this.portals=[]}var t=e.prototype;return t.push=function(e,t){void 0===t&&(t=e),this.portals.push({left:e,right:t})},t.stringPull=function(){var e,t,r,o=this.portals,i=[],s=0,a=0,u=0;t=o[0].left,r=o[0].right,i.push(e=o[0].left);for(var h=1;h<o.length;h++){var c=o[h].left,f=o[h].right;if(n.triarea2(e,r,f)<=0){if(!(n.vequal(e,r)||n.triarea2(e,t,f)>0)){i.push(t),t=e=t,r=e,a=s=a,u=s,h=s;continue}r=f,u=h}if(n.triarea2(e,t,c)>=0){if(!(n.vequal(e,t)||n.triarea2(e,r,c)<0)){i.push(r),t=e=r,r=e,a=s=u,u=s,h=s;continue}t=c,a=h}}return 0!==i.length&&n.vequal(i[i.length-1],o[o.length-1].left)||i.push(o[o.length-1].left),this.path=i,i},e}(),u=function(){function e(){this.zones={}}e.createZone=function(e){return s.buildZone(e)};var r=e.prototype;return r.setZoneData=function(e,t){this.zones[e]=t},r.getRandomNode=function(e,r,o,i){if(!this.zones[e])return new t.Vector3;o=o||null,i=i||0;var s=[];return this.zones[e].groups[r].forEach(function(e){o&&i?n.distanceToSquared(o,e.centroid)<i*i&&s.push(e.centroid):s.push(e.centroid)}),n.sample(s)||new t.Vector3},r.getClosestNode=function(e,t,r,o){void 0===o&&(o=!1);var i=this.zones[t].vertices,s=null,a=Infinity;return this.zones[t].groups[r].forEach(function(t){var r=n.distanceToSquared(t.centroid,e);r<a&&(!o||n.isVectorInPolygon(e,t,i))&&(s=t,a=r)}),s},r.findPath=function(e,r,n,o){var s=this.zones[n].groups[o],u=this.zones[n].vertices,h=this.getClosestNode(e,n,o,!0),c=this.getClosestNode(r,n,o,!0);if(!h||!c)return null;var f=i.search(s,h,c),l=function(e,t){for(var r=0;r<e.neighbours.length;r++)if(e.neighbours[r]===t.id)return e.portals[r]},p=new a;p.push(e);for(var d=0;d<f.length;d++){var v=f[d+1];if(v){var g=l(f[d],v);p.push(u[g[0]],u[g[1]])}}p.push(r),p.stringPull();var b=p.path.map(function(e){return new t.Vector3(e.x,e.y,e.z)});return b.shift(),b},e}();u.prototype.getGroup=(r=new t.Plane,function(e,t,o){if(void 0===o&&(o=!1),!this.zones[e])return null;for(var i=null,s=Math.pow(50,2),a=this.zones[e],u=0;u<a.groups.length;u++){var h=a.groups[u],c=Array.isArray(h),f=0;for(h=c?h:h[Symbol.iterator]();;){var l;if(c){if(f>=h.length)break;l=h[f++]}else{if((f=h.next()).done)break;l=f.value}var p=l;if(o&&(r.setFromCoplanarPoints(a.vertices[p.vertexIds[0]],a.vertices[p.vertexIds[1]],a.vertices[p.vertexIds[2]]),Math.abs(r.distanceToPoint(t))<.01&&n.isPointInPoly([a.vertices[p.vertexIds[0]],a.vertices[p.vertexIds[1]],a.vertices[p.vertexIds[2]]],t)))return u;var d=n.distanceToSquared(p.centroid,t);d<s&&(i=u,s=d)}}return i}),u.prototype.clampStep=function(){var e,r,n=new t.Vector3,o=new t.Plane,i=new t.Triangle,s=new t.Vector3,a=new t.Vector3;return function(t,u,h,c,f,l){var p=this.zones[c].vertices,d=this.zones[c].groups[f],v=[h],g={};g[h.id]=0,e=void 0,a.set(0,0,0),r=Infinity,o.setFromCoplanarPoints(p[h.vertexIds[0]],p[h.vertexIds[1]],p[h.vertexIds[2]]),o.projectPoint(u,n),s.copy(n);for(var b=v.pop();b;b=v.pop()){i.set(p[b.vertexIds[0]],p[b.vertexIds[1]],p[b.vertexIds[2]]),i.closestPointToPoint(s,n),n.distanceToSquared(s)<r&&(e=b,a.copy(n),r=n.distanceToSquared(s));var y=g[b.id];if(!(y>2))for(var m=0;m<b.neighbours.length;m++){var M=d[b.neighbours[m]];M.id in g||(v.push(M),g[M.id]=y+1)}}return l.copy(a),e}}();var h={PLAYER:new t.Color(15631215).convertGammaToLinear(2.2).getHex(),TARGET:new t.Color(14469912).convertGammaToLinear(2.2).getHex(),PATH:new t.Color(41903).convertGammaToLinear(2.2).getHex(),WAYPOINT:new t.Color(41903).convertGammaToLinear(2.2).getHex(),CLAMPED_STEP:new t.Color(14472114).convertGammaToLinear(2.2).getHex(),CLOSEST_NODE:new t.Color(4417387).convertGammaToLinear(2.2).getHex()},c=function(e){var r,n;function o(){var r;return(r=e.call(this)||this)._playerMarker=new t.Mesh(new t.SphereBufferGeometry(.25,32,32),new t.MeshBasicMaterial({color:h.PLAYER})),r._targetMarker=new t.Mesh(new t.BoxBufferGeometry(.3,.3,.3),new t.MeshBasicMaterial({color:h.TARGET})),r._nodeMarker=new t.Mesh(new t.BoxBufferGeometry(.1,.8,.1),new t.MeshBasicMaterial({color:h.CLOSEST_NODE})),r._stepMarker=new t.Mesh(new t.BoxBufferGeometry(.1,1,.1),new t.MeshBasicMaterial({color:h.CLAMPED_STEP})),r._pathMarker=new t.Object3D,r._pathLineMaterial=new t.LineBasicMaterial({color:h.PATH,linewidth:2}),r._pathPointMaterial=new t.MeshBasicMaterial({color:h.WAYPOINT}),r._pathPointGeometry=new t.SphereBufferGeometry(.08),r._markers=[r._playerMarker,r._targetMarker,r._nodeMarker,r._stepMarker,r._pathMarker],r._markers.forEach(function(e){e.visible=!1,r.add(e)}),r}n=e,(r=o).prototype=Object.create(n.prototype),r.prototype.constructor=r,r.__proto__=n;var i=o.prototype;return i.setPath=function(e){for(;this._pathMarker.children.length;)this._pathMarker.children[0].visible=!1,this._pathMarker.remove(this._pathMarker.children[0]);e=[this._playerMarker.position].concat(e);var r=new t.BufferGeometry;r.setAttribute("position",new t.BufferAttribute(new Float32Array(3*e.length),3));for(var n=0;n<e.length;n++)r.attributes.position.setXYZ(n,e[n].x,e[n].y+.2,e[n].z);this._pathMarker.add(new t.Line(r,this._pathLineMaterial));for(var o=0;o<e.length-1;o++){var i=new t.Mesh(this._pathPointGeometry,this._pathPointMaterial);i.position.copy(e[o]),i.position.y+=.2,this._pathMarker.add(i)}return this._pathMarker.visible=!0,this},i.setPlayerPosition=function(e){return this._playerMarker.position.copy(e),this._playerMarker.visible=!0,this},i.setTargetPosition=function(e){return this._targetMarker.position.copy(e),this._targetMarker.visible=!0,this},i.setNodePosition=function(e){return this._nodeMarker.position.copy(e),this._nodeMarker.visible=!0,this},i.setStepPosition=function(e){return this._stepMarker.position.copy(e),this._stepMarker.visible=!0,this},i.reset=function(){for(;this._pathMarker.children.length;)this._pathMarker.children[0].visible=!1,this._pathMarker.remove(this._pathMarker.children[0]);return this._markers.forEach(function(e){e.visible=!1}),this},o}(t.Object3D);e.Pathfinding=u,e.PathfindingHelper=c}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("three")):"function"==typeof define&&define.amd?define(["exports","three"],t):t((e||self).threePathfinding={},e.THREE)}(this,function(e,t){function r(e,t){return(r=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function n(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function o(e,t){var r;if("undefined"==typeof Symbol||null==e[Symbol.iterator]){if(Array.isArray(e)||(r=function(e,t){if(e){if("string"==typeof e)return n(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?n(e,t):void 0}}(e))||t&&e&&"number"==typeof e.length){r&&(e=r);var o=0;return function(){return o>=e.length?{done:!0}:{done:!1,value:e[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}return(r=e[Symbol.iterator]()).next.bind(r)}var i,s=function(){function e(){}return e.roundNumber=function(e,t){var r=Math.pow(10,t);return Math.round(e*r)/r},e.sample=function(e){return e[Math.floor(Math.random()*e.length)]},e.distanceToSquared=function(e,t){var r=e.x-t.x,n=e.y-t.y,o=e.z-t.z;return r*r+n*n+o*o},e.isPointInPoly=function(e,t){for(var r=!1,n=-1,o=e.length,i=o-1;++n<o;i=n)(e[n].z<=t.z&&t.z<e[i].z||e[i].z<=t.z&&t.z<e[n].z)&&t.x<(e[i].x-e[n].x)*(t.z-e[n].z)/(e[i].z-e[n].z)+e[n].x&&(r=!r);return r},e.isVectorInPolygon=function(e,t,r){var n=1e5,o=-1e5,i=[];return t.vertexIds.forEach(function(e){n=Math.min(r[e].y,n),o=Math.max(r[e].y,o),i.push(r[e])}),!!(e.y<o+.5&&e.y>n-.5&&this.isPointInPoly(i,e))},e.triarea2=function(e,t,r){return(r.x-e.x)*(t.z-e.z)-(t.x-e.x)*(r.z-e.z)},e.vequal=function(e,t){return this.distanceToSquared(e,t)<1e-5},e.mergeVertices=function(e,r){void 0===r&&(r=1e-4),r=Math.max(r,Number.EPSILON);for(var n={},o=e.getIndex(),i=e.getAttribute("position"),s=o?o.count:i.count,a=0,u=[],h=[],c=Math.log10(1/r),l=Math.pow(10,c),f=0;f<s;f++){var p=o?o.getX(f):f,d="";d+=~~(i.getX(p)*l)+",",d+=~~(i.getY(p)*l)+",",(d+=~~(i.getZ(p)*l)+",")in n?u.push(n[d]):(h.push(i.getX(p)),h.push(i.getY(p)),h.push(i.getZ(p)),n[d]=a,u.push(a),a++)}var v=new t.BufferAttribute(new Float32Array(h),i.itemSize,i.normalized),g=new t.BufferGeometry;return g.setAttribute("position",v),g.setIndex(u),g},e}(),a=function(){function e(e){this.content=[],this.scoreFunction=e}var t=e.prototype;return t.push=function(e){this.content.push(e),this.sinkDown(this.content.length-1)},t.pop=function(){var e=this.content[0],t=this.content.pop();return this.content.length>0&&(this.content[0]=t,this.bubbleUp(0)),e},t.remove=function(e){var t=this.content.indexOf(e),r=this.content.pop();t!==this.content.length-1&&(this.content[t]=r,this.scoreFunction(r)<this.scoreFunction(e)?this.sinkDown(t):this.bubbleUp(t))},t.size=function(){return this.content.length},t.rescoreElement=function(e){this.sinkDown(this.content.indexOf(e))},t.sinkDown=function(e){for(var t=this.content[e];e>0;){var r=(e+1>>1)-1,n=this.content[r];if(!(this.scoreFunction(t)<this.scoreFunction(n)))break;this.content[r]=t,this.content[e]=n,e=r}},t.bubbleUp=function(e){for(var t=this.content.length,r=this.content[e],n=this.scoreFunction(r);;){var o=e+1<<1,i=o-1,s=null,a=void 0;if(i<t&&(a=this.scoreFunction(this.content[i]))<n&&(s=i),o<t&&this.scoreFunction(this.content[o])<(null===s?n:a)&&(s=o),null===s)break;this.content[e]=this.content[s],this.content[s]=r,e=s}},e}(),u=function(){function e(){}return e.init=function(e){for(var t=0;t<e.length;t++){var r=e[t];r.f=0,r.g=0,r.h=0,r.cost=1,r.visited=!1,r.closed=!1,r.parent=null}},e.cleanUp=function(e){for(var t=0;t<e.length;t++){var r=e[t];delete r.f,delete r.g,delete r.h,delete r.cost,delete r.visited,delete r.closed,delete r.parent}},e.heap=function(){return new a(function(e){return e.f})},e.search=function(e,t,r){this.init(e);var n=this.heap();for(n.push(t);n.size()>0;){var o=n.pop();if(o===r){for(var i=o,s=[];i.parent;)s.push(i),i=i.parent;return this.cleanUp(s),s.reverse()}o.closed=!0;for(var a=this.neighbours(e,o),u=0,h=a.length;u<h;u++){var c=a[u];if(!c.closed){var l=o.g+c.cost,f=c.visited;if(!f||l<c.g){if(c.visited=!0,c.parent=o,!c.centroid||!r.centroid)throw new Error("Unexpected state");c.h=c.h||this.heuristic(c.centroid,r.centroid),c.g=l,c.f=c.g+c.h,f?n.rescoreElement(c):n.push(c)}}}}return[]},e.heuristic=function(e,t){return s.distanceToSquared(e,t)},e.neighbours=function(e,t){for(var r=[],n=0;n<t.neighbours.length;n++)r.push(e[t.neighbours[n]]);return r},e}(),h=function(){function e(){}return e.buildZone=function(e,r){var n=this,o=this._buildNavigationMesh(e,r),i={};o.vertices.forEach(function(e){e.x=s.roundNumber(e.x,2),e.y=s.roundNumber(e.y,2),e.z=s.roundNumber(e.z,2)}),i.vertices=o.vertices;var a=this._buildPolygonGroups(o);return i.groups=new Array(a.length),a.forEach(function(e,r){var o=new Map;e.forEach(function(e,t){o.set(e,t)});var a=new Array(e.length);e.forEach(function(e,r){var u=[];e.neighbours.forEach(function(e){return u.push(o.get(e))});var h=[];e.neighbours.forEach(function(t){return h.push(n._getSharedVerticesInOrder(e,t))});var c=new t.Vector3(0,0,0);c.add(i.vertices[e.vertexIds[0]]),c.add(i.vertices[e.vertexIds[1]]),c.add(i.vertices[e.vertexIds[2]]),c.divideScalar(3),c.x=s.roundNumber(c.x,2),c.y=s.roundNumber(c.y,2),c.z=s.roundNumber(c.z,2),a[r]={id:r,neighbours:u,vertexIds:e.vertexIds,centroid:c,portals:h}}),i.groups[r]=a}),i},e._buildNavigationMesh=function(e,t){return e=s.mergeVertices(e,t),this._buildPolygonsFromGeometry(e)},e._buildPolygonGroups=function(e){var t=[],r=function e(t){t.neighbours.forEach(function(r){void 0===r.group&&(r.group=t.group,e(r))})};return e.polygons.forEach(function(e){void 0!==e.group?t[e.group].push(e):(e.group=t.length,r(e),t.push([e]))}),t},e._buildPolygonNeighbours=function(e,t){var r=new Set,n=t[e.vertexIds[1]],o=t[e.vertexIds[2]];return t[e.vertexIds[0]].forEach(function(t){t!==e&&(n.includes(t)||o.includes(t))&&r.add(t)}),n.forEach(function(t){t!==e&&o.includes(t)&&r.add(t)}),r},e._buildPolygonsFromGeometry=function(e){for(var r=this,n=[],o=[],i=e.attributes.position,s=e.index,a=[],u=0;u<i.count;u++)o.push((new t.Vector3).fromBufferAttribute(i,u)),a[u]=[];for(var h=0;h<e.index.count;h+=3){var c=s.getX(h),l=s.getX(h+1),f=s.getX(h+2),p={vertexIds:[c,l,f],neighbours:null};n.push(p),a[c].push(p),a[l].push(p),a[f].push(p)}return n.forEach(function(e){e.neighbours=r._buildPolygonNeighbours(e,a)}),{polygons:n,vertices:o}},e._getSharedVerticesInOrder=function(e,t){var r=e.vertexIds,n=r[0],o=r[1],i=r[2],s=t.vertexIds,a=s.includes(n),u=s.includes(o),h=s.includes(i);return a&&u&&h?Array.from(r):a&&u?[n,o]:u&&h?[o,i]:a&&h?[i,n]:(console.warn("Error processing navigation mesh neighbors; neighbors with <2 shared vertices found."),[])},e}(),c=function(){function e(){this.portals=[]}var t=e.prototype;return t.push=function(e,t){void 0===t&&(t=e),this.portals.push({left:e,right:t})},t.stringPull=function(){var e,t,r,n=this.portals,o=[],i=0,a=0,u=0;t=n[0].left,r=n[0].right,o.push(e=n[0].left);for(var h=1;h<n.length;h++){var c=n[h].left,l=n[h].right;if(s.triarea2(e,r,l)<=0){if(!(s.vequal(e,r)||s.triarea2(e,t,l)>0)){o.push(t),t=e=t,r=e,a=i=a,u=i,h=i;continue}r=l,u=h}if(s.triarea2(e,t,c)>=0){if(!(s.vequal(e,t)||s.triarea2(e,r,c)<0)){o.push(r),t=e=r,r=e,a=i=u,u=i,h=i;continue}t=c,a=h}}return 0!==o.length&&s.vequal(o[o.length-1],n[n.length-1].left)||o.push(n[n.length-1].left),this.path=o,o},e}(),l=function(){function e(){this.zones={}}e.createZone=function(e,t){return void 0===t&&(t=1e-4),h.buildZone(e,t)};var r=e.prototype;return r.setZoneData=function(e,t){this.zones[e]=t},r.getRandomNode=function(e,r,n,o){if(!this.zones[e])return new t.Vector3;n=n||null,o=o||0;var i=[];return this.zones[e].groups[r].forEach(function(e){n&&o?s.distanceToSquared(n,e.centroid)<o*o&&i.push(e.centroid):i.push(e.centroid)}),s.sample(i)||new t.Vector3},r.getClosestNode=function(e,t,r,n){void 0===n&&(n=!1);var o=this.zones[t].vertices,i=null,a=Infinity;return this.zones[t].groups[r].forEach(function(t){var r=s.distanceToSquared(t.centroid,e);r<a&&(!n||s.isVectorInPolygon(e,t,o))&&(i=t,a=r)}),i},r.findPath=function(e,r,n,o){var i=this.zones[n].groups[o],s=this.zones[n].vertices,a=this.getClosestNode(e,n,o,!0),h=this.getClosestNode(r,n,o,!0);if(!a||!h)return null;var l=u.search(i,a,h),f=function(e,t){for(var r=0;r<e.neighbours.length;r++)if(e.neighbours[r]===t.id)return e.portals[r]},p=new c;p.push(e);for(var d=0;d<l.length;d++){var v=l[d+1];if(v){var g=f(l[d],v);p.push(s[g[0]],s[g[1]])}}p.push(r),p.stringPull();var y=p.path.map(function(e){return new t.Vector3(e.x,e.y,e.z)});return y.shift(),y},e}();l.prototype.getGroup=(i=new t.Plane,function(e,t,r){if(void 0===r&&(r=!1),!this.zones[e])return null;for(var n=null,a=Math.pow(50,2),u=this.zones[e],h=0;h<u.groups.length;h++)for(var c,l=o(u.groups[h]);!(c=l()).done;){var f=c.value;if(r&&(i.setFromCoplanarPoints(u.vertices[f.vertexIds[0]],u.vertices[f.vertexIds[1]],u.vertices[f.vertexIds[2]]),Math.abs(i.distanceToPoint(t))<.01&&s.isPointInPoly([u.vertices[f.vertexIds[0]],u.vertices[f.vertexIds[1]],u.vertices[f.vertexIds[2]]],t)))return h;var p=s.distanceToSquared(f.centroid,t);p<a&&(n=h,a=p)}return n}),l.prototype.clampStep=function(){var e,r,n=new t.Vector3,o=new t.Plane,i=new t.Triangle,s=new t.Vector3,a=new t.Vector3;return function(t,u,h,c,l,f){var p=this.zones[c].vertices,d=this.zones[c].groups[l],v=[h],g={};g[h.id]=0,e=void 0,a.set(0,0,0),r=Infinity,o.setFromCoplanarPoints(p[h.vertexIds[0]],p[h.vertexIds[1]],p[h.vertexIds[2]]),o.projectPoint(u,n),s.copy(n);for(var y=v.pop();y;y=v.pop()){i.set(p[y.vertexIds[0]],p[y.vertexIds[1]],p[y.vertexIds[2]]),i.closestPointToPoint(s,n),n.distanceToSquared(s)<r&&(e=y,a.copy(n),r=n.distanceToSquared(s));var b=g[y.id];if(!(b>2))for(var m=0;m<y.neighbours.length;m++){var M=d[y.neighbours[m]];M.id in g||(v.push(M),g[M.id]=b+1)}}return f.copy(a),e}}();var f={PLAYER:new t.Color(15631215).convertGammaToLinear(2.2).getHex(),TARGET:new t.Color(14469912).convertGammaToLinear(2.2).getHex(),PATH:new t.Color(41903).convertGammaToLinear(2.2).getHex(),WAYPOINT:new t.Color(41903).convertGammaToLinear(2.2).getHex(),CLAMPED_STEP:new t.Color(14472114).convertGammaToLinear(2.2).getHex(),CLOSEST_NODE:new t.Color(4417387).convertGammaToLinear(2.2).getHex()},p=function(e){var n,o;function i(){var r;return(r=e.call(this)||this)._playerMarker=new t.Mesh(new t.SphereBufferGeometry(.25,32,32),new t.MeshBasicMaterial({color:f.PLAYER})),r._targetMarker=new t.Mesh(new t.BoxBufferGeometry(.3,.3,.3),new t.MeshBasicMaterial({color:f.TARGET})),r._nodeMarker=new t.Mesh(new t.BoxBufferGeometry(.1,.8,.1),new t.MeshBasicMaterial({color:f.CLOSEST_NODE})),r._stepMarker=new t.Mesh(new t.BoxBufferGeometry(.1,1,.1),new t.MeshBasicMaterial({color:f.CLAMPED_STEP})),r._pathMarker=new t.Object3D,r._pathLineMaterial=new t.LineBasicMaterial({color:f.PATH,linewidth:2}),r._pathPointMaterial=new t.MeshBasicMaterial({color:f.WAYPOINT}),r._pathPointGeometry=new t.SphereBufferGeometry(.08),r._markers=[r._playerMarker,r._targetMarker,r._nodeMarker,r._stepMarker,r._pathMarker],r._markers.forEach(function(e){e.visible=!1,r.add(e)}),r}o=e,(n=i).prototype=Object.create(o.prototype),n.prototype.constructor=n,r(n,o);var s=i.prototype;return s.setPath=function(e){for(;this._pathMarker.children.length;)this._pathMarker.children[0].visible=!1,this._pathMarker.remove(this._pathMarker.children[0]);e=[this._playerMarker.position].concat(e);var r=new t.BufferGeometry;r.setAttribute("position",new t.BufferAttribute(new Float32Array(3*e.length),3));for(var n=0;n<e.length;n++)r.attributes.position.setXYZ(n,e[n].x,e[n].y+.2,e[n].z);this._pathMarker.add(new t.Line(r,this._pathLineMaterial));for(var o=0;o<e.length-1;o++){var i=new t.Mesh(this._pathPointGeometry,this._pathPointMaterial);i.position.copy(e[o]),i.position.y+=.2,this._pathMarker.add(i)}return this._pathMarker.visible=!0,this},s.setPlayerPosition=function(e){return this._playerMarker.position.copy(e),this._playerMarker.visible=!0,this},s.setTargetPosition=function(e){return this._targetMarker.position.copy(e),this._targetMarker.visible=!0,this},s.setNodePosition=function(e){return this._nodeMarker.position.copy(e),this._nodeMarker.visible=!0,this},s.setStepPosition=function(e){return this._stepMarker.position.copy(e),this._stepMarker.visible=!0,this},s.reset=function(){for(;this._pathMarker.children.length;)this._pathMarker.children[0].visible=!1,this._pathMarker.remove(this._pathMarker.children[0]);return this._markers.forEach(function(e){e.visible=!1}),this},i}(t.Object3D);e.Pathfinding=l,e.PathfindingHelper=p}); | ||
//# sourceMappingURL=three-pathfinding.umd.js.map |
{ | ||
"name": "three-pathfinding", | ||
"version": "0.14.0", | ||
"version": "0.14.1", | ||
"description": "Navigation mesh toolkit for three.js, based on PatrolJS", | ||
@@ -50,4 +50,4 @@ "author": "Don McCurdy <dm@donmccurdy.com>", | ||
"devDependencies": { | ||
"concurrently": "^3.6.0", | ||
"documentation": "^12.2.0", | ||
"concurrently": "^6.0.0", | ||
"documentation": "^13.1.1", | ||
"microbundle": "^0.13.0", | ||
@@ -57,4 +57,4 @@ "replace-between": "0.0.8", | ||
"tap-spec": "^5.0.0", | ||
"tape": "^4.9.1", | ||
"three": "^0.124.0", | ||
"tape": "^5.2.2", | ||
"three": "^0.126.1", | ||
"uglify-js": "^3.3.3" | ||
@@ -61,0 +61,0 @@ }, |
@@ -9,7 +9,8 @@ import { Vector3 } from 'three'; | ||
* @param {BufferGeometry} geometry | ||
* @param {number} tolerance | ||
* @return {Zone} | ||
*/ | ||
static buildZone (geometry) { | ||
static buildZone (geometry, tolerance) { | ||
const navMesh = this._buildNavigationMesh(geometry); | ||
const navMesh = this._buildNavigationMesh(geometry, tolerance); | ||
@@ -76,4 +77,4 @@ const zone = {}; | ||
*/ | ||
static _buildNavigationMesh (geometry) { | ||
geometry = Utils.mergeVertices(geometry); | ||
static _buildNavigationMesh (geometry, tolerance) { | ||
geometry = Utils.mergeVertices(geometry, tolerance); | ||
return this._buildPolygonsFromGeometry(geometry); | ||
@@ -80,0 +81,0 @@ } |
@@ -23,6 +23,7 @@ import { | ||
* @param {BufferGeometry} geometry | ||
* @param {number} tolerance Vertex welding tolerance. | ||
* @return {Zone} | ||
*/ | ||
static createZone (geometry) { | ||
return Builder.buildZone(geometry); | ||
static createZone (geometry, tolerance = 1e-4) { | ||
return Builder.buildZone(geometry, tolerance); | ||
} | ||
@@ -29,0 +30,0 @@ |
123
src/Utils.js
@@ -1,2 +0,2 @@ | ||
import { BufferAttribute } from 'three'; | ||
import { BufferAttribute, BufferGeometry } from 'three'; | ||
@@ -68,4 +68,4 @@ class Utils { | ||
/** | ||
* Copied from BufferGeometryUtils.mergeVertices, because importing ES modules | ||
* from a sub-directory makes Node.js (as of v14) angry. | ||
* Modified version of BufferGeometryUtils.mergeVertices, ignoring vertex | ||
* attributes other than position. | ||
* | ||
@@ -87,31 +87,12 @@ * @param {THREE.BufferGeometry} geometry | ||
// next value for triangle indices | ||
// Next value for triangle indices. | ||
var nextIndex = 0; | ||
// attributes and new attribute arrays | ||
var attributeNames = Object.keys( geometry.attributes ); | ||
var attrArrays = {}; | ||
var morphAttrsArrays = {}; | ||
var newIndices = []; | ||
var getters = [ 'getX', 'getY', 'getZ', 'getW' ]; | ||
var newPositions = []; | ||
// initialize the arrays | ||
for ( var i = 0, l = attributeNames.length; i < l; i ++ ) { | ||
var name = attributeNames[ i ]; | ||
attrArrays[ name ] = []; | ||
var morphAttr = geometry.morphAttributes[ name ]; | ||
if ( morphAttr ) { | ||
morphAttrsArrays[ name ] = new Array( morphAttr.length ).fill().map( () => [] ); | ||
} | ||
} | ||
// convert the error tolerance to an amount of decimal places to truncate to | ||
// Convert the error tolerance to an amount of decimal places to truncate to. | ||
var decimalShift = Math.log10( 1 / tolerance ); | ||
var shiftMultiplier = Math.pow( 10, decimalShift ); | ||
for ( var i = 0; i < vertexCount; i ++ ) { | ||
@@ -121,21 +102,12 @@ | ||
// Generate a hash for the vertex attributes at the current index 'i' | ||
// Generate a hash for the vertex attributes at the current index 'i'. | ||
var hash = ''; | ||
for ( var j = 0, l = attributeNames.length; j < l; j ++ ) { | ||
var name = attributeNames[ j ]; | ||
var attribute = geometry.getAttribute( name ); | ||
var itemSize = attribute.itemSize; | ||
// Double tilde truncates the decimal value. | ||
hash += `${ ~ ~ ( positions.getX( index ) * shiftMultiplier ) },`; | ||
hash += `${ ~ ~ ( positions.getY( index ) * shiftMultiplier ) },`; | ||
hash += `${ ~ ~ ( positions.getZ( index ) * shiftMultiplier ) },`; | ||
for ( var k = 0; k < itemSize; k ++ ) { | ||
// double tilde truncates the decimal value | ||
hash += `${ ~ ~ ( attribute[ getters[ k ] ]( index ) * shiftMultiplier ) },`; | ||
} | ||
} | ||
// Add another reference to the vertex if it's already | ||
// used by another index | ||
// used by another index. | ||
if ( hash in hashToIndex ) { | ||
@@ -147,31 +119,6 @@ | ||
// copy data to the new index in the attribute arrays | ||
for ( var j = 0, l = attributeNames.length; j < l; j ++ ) { | ||
newPositions.push( positions.getX( index ) ); | ||
newPositions.push( positions.getY( index ) ); | ||
newPositions.push( positions.getZ( index ) ); | ||
var name = attributeNames[ j ]; | ||
var attribute = geometry.getAttribute( name ); | ||
var morphAttr = geometry.morphAttributes[ name ]; | ||
var itemSize = attribute.itemSize; | ||
var newarray = attrArrays[ name ]; | ||
var newMorphArrays = morphAttrsArrays[ name ]; | ||
for ( var k = 0; k < itemSize; k ++ ) { | ||
var getterFunc = getters[ k ]; | ||
newarray.push( attribute[ getterFunc ]( index ) ); | ||
if ( morphAttr ) { | ||
for ( var m = 0, ml = morphAttr.length; m < ml; m ++ ) { | ||
newMorphArrays[ m ].push( morphAttr[ m ][ getterFunc ]( index ) ); | ||
} | ||
} | ||
} | ||
} | ||
hashToIndex[ hash ] = nextIndex; | ||
@@ -185,34 +132,12 @@ newIndices.push( nextIndex ); | ||
// Generate typed arrays from new attribute arrays and update | ||
// the attributeBuffers | ||
const result = geometry.clone(); | ||
for ( var i = 0, l = attributeNames.length; i < l; i ++ ) { | ||
// Construct merged BufferGeometry. | ||
var name = attributeNames[ i ]; | ||
var oldAttribute = geometry.getAttribute( name ); | ||
const positionAttribute = new BufferAttribute( | ||
new Float32Array( newPositions ), | ||
positions.itemSize, | ||
positions.normalized | ||
); | ||
var buffer = new oldAttribute.array.constructor( attrArrays[ name ] ); | ||
var attribute = new BufferAttribute( buffer, oldAttribute.itemSize, oldAttribute.normalized ); | ||
result.setAttribute( name, attribute ); | ||
// Update the attribute arrays | ||
if ( name in morphAttrsArrays ) { | ||
for ( var j = 0; j < morphAttrsArrays[ name ].length; j ++ ) { | ||
var oldMorphAttribute = geometry.morphAttributes[ name ][ j ]; | ||
var buffer = new oldMorphAttribute.array.constructor( morphAttrsArrays[ name ][ j ] ); | ||
var morphAttribute = new BufferAttribute( buffer, oldMorphAttribute.itemSize, oldMorphAttribute.normalized ); | ||
result.morphAttributes[ name ][ j ] = morphAttribute; | ||
} | ||
} | ||
} | ||
// indices | ||
const result = new BufferGeometry(); | ||
result.setAttribute( 'position', positionAttribute ); | ||
result.setIndex( newIndices ); | ||
@@ -219,0 +144,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
302913
19
1165
1