UNPKG

babylon-navigation-mesh

Version:

A toolkit to move on navigation mesh with BABYLONJS

2 lines 1.45 MB
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Navigation=f()}})(function(){var define,module,exports;return function(){function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}return e}()({1:[function(require,module,exports){"use strict";var Class=require("abitbol");var BABYLON=require("babylonjs");var BinaryHeap=require("./BinaryHeap.js");var Astar=Class.$extend({init:function init(graph){for(var x=0;x<graph.length;x++){var node=graph[x];node.f=0;node.g=0;node.h=0;node.cost=1;node.visited=false;node.closed=false;node.parent=null}},cleanUp:function cleanUp(graph){for(var x=0;x<graph.length;x++){var node=graph[x];delete node.f;delete node.g;delete node.h;delete node.cost;delete node.visited;delete node.closed;delete node.parent}},heap:function heap(){return new BinaryHeap(function(node){return node.f})},search:function search(graph,start,end){this.init(graph);var openHeap=this.heap();openHeap.push(start);while(openHeap.size()>0){var currentNode=openHeap.pop();if(currentNode===end){var curr=currentNode;var ret=[];while(curr.parent){ret.push(curr);curr=curr.parent}this.cleanUp(ret);return ret.reverse()}currentNode.closed=true;var neighbours=this.neighbours(graph,currentNode);for(var i=0,il=neighbours.length;i<il;i++){var neighbour=neighbours[i];if(neighbour.closed){continue}var gScore=currentNode.g+neighbour.cost;var beenVisited=neighbour.visited;if(!beenVisited||gScore<neighbour.g){neighbour.visited=true;neighbour.parent=currentNode;if(!neighbour.centroid||!end.centroid)debugger;neighbour.h=neighbour.h||this.heuristic(neighbour.centroid,end.centroid);neighbour.g=gScore;neighbour.f=neighbour.g+neighbour.h;if(!beenVisited){openHeap.push(neighbour)}else{openHeap.rescoreElement(neighbour)}}}}return[]},heuristic:function heuristic(pos1,pos2){return BABYLON.Vector3.DistanceSquared(pos1,pos2)},neighbours:function neighbours(graph,node){var ret=[];for(var e=0;e<node.neighbours.length;e++){ret.push(graph[node.neighbours[e]])}return ret}});module.exports=Astar},{"./BinaryHeap.js":2,abitbol:5,babylonjs:7}],2:[function(require,module,exports){"use strict";var Class=require("abitbol");var BinaryHeap=Class.$extend({__init__:function __init__(scoreFunction){this.content=[];this.scoreFunction=scoreFunction||new function(){}},push:function push(element){this.content.push(element);this.sinkDown(this.content.length-1)},pop:function pop(){var result=this.content[0];var end=this.content.pop();if(this.content.length>0){this.content[0]=end;this.bubbleUp(0)}return result},remove:function remove(node){var i=this.content.indexOf(node);var end=this.content.pop();if(i!==this.content.length-1){this.content[i]=end;if(this.scoreFunction(end)<this.scoreFunction(node)){this.sinkDown(i)}else{this.bubbleUp(i)}}},size:function size(){return this.content.length},rescoreElement:function rescoreElement(node){this.sinkDown(this.content.indexOf(node))},sinkDown:function sinkDown(n){var element=this.content[n];while(n>0){var parentN=(n+1>>1)-1,parent=this.content[parentN];if(this.scoreFunction(element)<this.scoreFunction(parent)){this.content[parentN]=element;this.content[n]=parent;n=parentN}else{break}}},bubbleUp:function bubbleUp(n){var length=this.content.length,element=this.content[n],elemScore=this.scoreFunction(element);while(true){var child2N=n+1<<1,child1N=child2N-1;var swap=null;if(child1N<length){var child1=this.content[child1N],child1Score=this.scoreFunction(child1);if(child1Score<elemScore)swap=child1N}if(child2N<length){var child2=this.content[child2N],child2Score=this.scoreFunction(child2);if(child2Score<(swap===null?elemScore:child1Score)){swap=child2N}}if(swap!==null){this.content[n]=this.content[swap];this.content[swap]=element;n=swap}else{break}}}});module.exports=BinaryHeap},{abitbol:5}],3:[function(require,module,exports){"use strict";var Class=require("abitbol");var BABYLON=require("babylonjs");var Channel=Class.$extend({__init__:function __init__(){this.portals=[]},push:function push(p1,p2){if(p2===undefined)p2=p1;this.portals.push({left:p1,right:p2})},_vequal:function _vequal(a,b){return BABYLON.Vector3.DistanceSquared(a,b)<1e-5},_triarea2:function _triarea2(a,b,c){var ax=b.x-a.x;var az=b.z-a.z;var bx=c.x-a.x;var bz=c.z-a.z;return bx*az-ax*bz},stringPull:function stringPull(){var portals=this.portals;var pts=[];var portalApex,portalLeft,portalRight;var apexIndex=0,leftIndex=0,rightIndex=0;portalApex=portals[0].left;portalLeft=portals[0].left;portalRight=portals[0].right;pts.push(portalApex);for(var i=1;i<portals.length;i++){var left=portals[i].left;var right=portals[i].right;if(this._triarea2(portalApex,portalRight,right)>=0){if(this._vequal(portalApex,portalRight)||this._triarea2(portalApex,portalLeft,right)<0){portalRight=right;rightIndex=i}else{pts.push(portalLeft);portalApex=portalLeft;apexIndex=leftIndex;portalLeft=portalApex;portalRight=portalApex;leftIndex=apexIndex;rightIndex=apexIndex;i=apexIndex;continue}}if(this._triarea2(portalApex,portalLeft,left)<=0){if(this._vequal(portalApex,portalLeft)||this._triarea2(portalApex,portalRight,left)>0){portalLeft=left;leftIndex=i}else{pts.push(portalRight);portalApex=portalRight;apexIndex=rightIndex;portalLeft=portalApex;portalRight=portalApex;leftIndex=apexIndex;rightIndex=apexIndex;i=apexIndex;continue}}}if(pts.length===0||!this._vequal(pts[pts.length-1],portals[portals.length-1].left)){pts.push(portals[portals.length-1].left)}this.path=pts;return pts}});module.exports=Channel},{abitbol:5,babylonjs:7}],4:[function(require,module,exports){"use strict";var Class=require("abitbol");var _=require("lodash");var Astar=require("./Astar.js");var Channel=require("./Channel.js");var BABYLON=require("babylonjs");var Navigation=Class.$extend({__init__:function __init__(){this.zoneNodes={};this.astar=new Astar;this.yTolerance=1},buildNodes:function buildNodes(mesh){var navigationMesh=this._buildNavigationMesh(mesh.geometry);var zoneNodes=this._groupNavMesh(navigationMesh);return zoneNodes},setZoneData:function setZoneData(zone,data){this.zoneNodes[zone]=data},setHeightTolerance:function setHeightTolerance(tolerance){this.yTolerance=tolerance},getGroup:function getGroup(zone,position){if(!this.zoneNodes[zone]){return null}var closestNodeGroup=null;var distance=Infinity;_.each(this.zoneNodes[zone].groups,function(group,index){_.each(group,function(node){var measuredDistance=BABYLON.Vector3.DistanceSquared(node.centroid,position);if(measuredDistance<distance){closestNodeGroup=index;distance=measuredDistance}})});return closestNodeGroup},getRandomNode:function getRandomNode(zone,group,nearPosition,nearRange){if(!this.zoneNodes[zone])return new BABYLON.Vector3;nearPosition=nearPosition||null;nearRange=nearRange||0;var candidates=[];var polygons=this.zoneNodes[zone].groups[group];_.each(polygons,function(p){if(nearPosition&&nearRange){if(BABYLON.Vector3.DistanceSquared(nearPosition,p.centroid)<nearRange*nearRange){candidates.push(p.centroid)}}else{candidates.push(p.centroid)}});return _.sample(candidates)||new BABYLON.Vector3},projectOnNavmesh:function projectOnNavmesh(position,zone,group){var allNodes=this.zoneNodes[zone].groups[group];var vertices=this.zoneNodes[zone].vertices;var closestNode=null;var distance=Infinity;var finalProj=null,proj=null,node=null,measuredDistance=0;for(var i=0;i<allNodes.length;i++){node=allNodes[i];proj=this._getProjectionOnNode(position,node,vertices);measuredDistance=BABYLON.Vector3.DistanceSquared(proj,position);if(measuredDistance<distance){distance=measuredDistance;finalProj=proj;closestNode=node}}return finalProj},_projectPointOnPlane:function _projectPointOnPlane(point,plane){var coef=BABYLON.Vector3.Dot(point,plane.normal)+plane.d;var proj=point.subtract(plane.normal.scale(coef));return proj},_getProjectionOnNode:function _getProjectionOnNode(position,node,vertices){var A=this.getVectorFrom(vertices,node.vertexIds[0]);var B=this.getVectorFrom(vertices,node.vertexIds[1]);var C=this.getVectorFrom(vertices,node.vertexIds[2]);var u=B.subtract(A);var v=C.subtract(A);var n=BABYLON.Vector3.Cross(u,v).normalize();var plane={normal:n,d:-BABYLON.Vector3.Dot(A,n)};var p=this._projectPointOnPlane(position,plane);var barycentric=function barycentric(p,a,b,c){var ret={};var v0=c.subtract(a),v1=b.subtract(a),v2=p.subtract(a);var d00=BABYLON.Vector3.Dot(v0,v0);var d01=BABYLON.Vector3.Dot(v0,v1);var d02=BABYLON.Vector3.Dot(v0,v2);var d11=BABYLON.Vector3.Dot(v1,v1);var d12=BABYLON.Vector3.Dot(v1,v2);var denom=d00*d11-d01*d01;ret.u=(d11*d02-d01*d12)/denom;ret.v=(d00*d12-d01*d02)/denom;ret.w=1-ret.u-ret.v;return ret};var bary=barycentric(p,A,B,C);bary.u=Math.min(Math.max(bary.u,0),1);bary.v=Math.min(Math.max(bary.v,0),1);if(bary.u+bary.v>=1){var sum=bary.u+bary.v;bary.u/=sum;bary.v/=sum}var proj=A.add(B.subtract(A).scale(bary.v).add(C.subtract(A).scale(bary.u)));return proj},findPath:function findPath(startPosition,targetPosition,zone,group){var allNodes=this.zoneNodes[zone].groups[group];var vertices=this.zoneNodes[zone].vertices;var startingNode=null;for(var i=0;i<allNodes.length;i++){if(this._isVectorInPolygon(startPosition,allNodes[i],vertices)){startingNode=allNodes[i];break}}var endNode=null;for(var i=0;i<allNodes.length;i++){if(this._isVectorInPolygon(targetPosition,allNodes[i],vertices)){endNode=allNodes[i];break}}if(!startingNode||!endNode){return null}if(startingNode.id!=endNode.id){var paths=this.astar.search(allNodes,startingNode,endNode)}else{vectors=[];vectors.push(new BABYLON.Vector3(targetPosition.x,targetPosition.y,targetPosition.z));return vectors}var getPortalFromTo=function getPortalFromTo(a,b){for(var i=0;i<a.neighbours.length;i++){if(a.neighbours[i]===b.id){return a.portals[i]}}};var channel=new Channel;channel.push(startPosition);for(var i=0;i<paths.length;i++){var polygon=paths[i];var nextPolygon=paths[i+1];if(nextPolygon){var portals=getPortalFromTo(polygon,nextPolygon);channel.push(this.getVectorFrom(vertices,portals[0]),this.getVectorFrom(vertices,portals[1]))}}channel.push(targetPosition);channel.stringPull();var vectors=[];channel.path.forEach(function(c){var vec=new BABYLON.Vector3(c.x,c.y,c.z);vectors.push(vec)});vectors.shift();return vectors},_isPointInPoly:function _isPointInPoly(poly,pt){for(var c=false,i=-1,l=poly.length,j=l-1;++i<l;j=i){(poly[i].z<=pt.z&&pt.z<poly[j].z||poly[j].z<=pt.z&&pt.z<poly[i].z)&&pt.x<(poly[j].x-poly[i].x)*(pt.z-poly[i].z)/(poly[j].z-poly[i].z)+poly[i].x&&(c=!c)}return c},_isVectorInPolygon:function _isVectorInPolygon(vector,polygon,vertices){var lowestPoint=1e5;var highestPoint=-1e5;var polygonVertices=[];_.each(polygon.vertexIds,function(vId){var point=this.getVectorFrom(vertices,vId);lowestPoint=Math.min(point.y,lowestPoint);highestPoint=Math.max(point.y,highestPoint);polygonVertices.push(point)}.bind(this));if(vector.y<highestPoint+this.yTolerance&&vector.y>lowestPoint-this.yTolerance&&this._isPointInPoly(polygonVertices,vector)){return true}return false},_computeCentroids:function _computeCentroids(geometry){var centroids=[];var indices=geometry.getIndices();var vertices=geometry.getVerticesData(BABYLON.VertexBuffer.PositionKind);var c=new BABYLON.Vector3(0,0,0);for(var f=0;f<indices.length;f+=3){var p1=this.getVectorFrom(vertices,indices[f]);var p2=this.getVectorFrom(vertices,indices[f+1]);var p3=this.getVectorFrom(vertices,indices[f+2]);c.copyFromFloats(0,0,0);c.addInPlace(p1);c.addInPlace(p2);c.addInPlace(p3);c.scaleInPlace(1/3);centroids.push(c.clone())}geometry.centroids=centroids},_roundNumber:function _roundNumber(number,decimals){var newnumber=new Number(number+"").toFixed(parseInt(decimals));return parseFloat(newnumber)},_mergeVertexIds:function _mergeVertexIds(aList,bList){var sharedVertices=[];aList.forEach(function(vId){if(_.includes(bList,vId)){sharedVertices.push(vId)}});if(sharedVertices.length<2)return[];if(_.includes(sharedVertices,aList[0])&&_.includes(sharedVertices,aList[aList.length-1])){aList.push(aList.shift())}if(_.includes(sharedVertices,bList[0])&&_.includes(sharedVertices,bList[bList.length-1])){bList.push(bList.shift())}sharedVertices=[];aList.forEach(function(vId){if(_.includes(bList,vId)){sharedVertices.push(vId)}});var clockwiseMostSharedVertex=sharedVertices[1];var counterClockwiseMostSharedVertex=sharedVertices[0];var cList=_.clone(aList);while(cList[0]!==clockwiseMostSharedVertex){cList.push(cList.shift())}var c=0;var temp=_.clone(bList);while(temp[0]!==counterClockwiseMostSharedVertex){temp.push(temp.shift());if(c++>10)break}temp.shift();temp.pop();cList=cList.concat(temp);return cList},_setPolygonCentroid:function _setPolygonCentroid(polygon,navigationMesh){var sum=new BABYLON.Vector3(0,0,0);var vertices=navigationMesh.vertices;_.each(polygon.vertexIds,function(vId){sum.x+=vertices[vId*3];sum.y+=vertices[vId*3+1];sum.z+=vertices[vId*3+2]});sum.scaleInPlace(1/polygon.vertexIds.length);polygon.centroid.copyFrom(sum)},getVectorFrom:function getVectorFrom(vertices,id,_vector){if(_vector){_vector.copyFromFloats(vertices[id*3],vertices[id*3+1],vertices[id*3+2]);return _vector}return new BABYLON.Vector3(vertices[id*3],vertices[id*3+1],vertices[id*3+2])},_cleanPolygon:function _cleanPolygon(polygon,navigationMesh){var newVertexIds=[];var vertices=navigationMesh.vertices;for(var i=0;i<polygon.vertexIds.length;i++){var vertex=this.getVectorFrom(vertices,polygon.vertexIds[i]);var nextVertexId,previousVertexId;var nextVertex,previousVertex;if(i===0){nextVertexId=polygon.vertexIds[1];previousVertexId=polygon.vertexIds[polygon.vertexIds.length-1]}else if(i===polygon.vertexIds.length-1){nextVertexId=polygon.vertexIds[0];previousVertexId=polygon.vertexIds[polygon.vertexIds.length-2]}else{nextVertexId=polygon.vertexIds[i+1];previousVertexId=polygon.vertexIds[i-1]}nextVertex=this.getVectorFrom(vertices,nextVertexId);previousVertex=this.getVectorFrom(vertices,previousVertexId);var a=nextVertex.clone().sub(vertex);var b=previousVertex.clone().sub(vertex);var angle=a.angleTo(b);if(angle>Math.PI-.01&&angle<Math.PI+.01){var goodNeighbours=[];polygon.neighbours.forEach(function(neighbour){if(!_.includes(neighbour.vertexIds,polygon.vertexIds[i])){goodNeighbours.push(neighbour)}});polygon.neighbours=goodNeighbours}else{newVertexIds.push(polygon.vertexIds[i])}}polygon.vertexIds=newVertexIds;this._setPolygonCentroid(polygon,navigationMesh)},_isConvex:function _isConvex(polygon,navigationMesh){var vertices=navigationMesh.vertices;if(polygon.vertexIds.length<3)return false;var convex=true;var total=0;var results=[];for(var i=0;i<polygon.vertexIds.length;i++){var vertex=this.getVectorFrom(vertices,polygon.vertexIds[i]);var nextVertex,previousVertex;if(i===0){nextVertex=this.getVectorFrom(vertices,polygon.vertexIds[1]);previousVertex=this.getVectorFrom(vertices,polygon.vertexIds[polygon.vertexIds.length-1])}else if(i===polygon.vertexIds.length-1){nextVertex=this.getVectorFrom(vertices,polygon.vertexIds[0]);previousVertex=this.getVectorFrom(vertices,polygon.vertexIds[polygon.vertexIds.length-2])}else{nextVertex=this.getVectorFrom(vertices,polygon.vertexIds[i+1]);previousVertex=this.getVectorFrom(vertices,polygon.vertexIds[i-1])}var a=nextVertex.clone().sub(vertex);var b=previousVertex.clone().sub(vertex);var angle=a.angleTo(b);total+=angle;if(angle===Math.PI||angle===0)return false;var r=BABYLON.Vector3.Cross(a,b).y;results.push(r)}results.forEach(function(r){if(r===0)convex=false});if(results[0]>0){results.forEach(function(r){if(r<0)convex=false})}else{results.forEach(function(r){if(r>0)convex=false})}return convex},_buildPolygonGroups:function _buildPolygonGroups(navigationMesh){var polygons=navigationMesh.polygons;var polygonGroups=[];var groupCount=0;var spreadGroupId=function spreadGroupId(polygon){_.each(polygon.neighbours,function(neighbour){if(_.isUndefined(neighbour.group)){neighbour.group=polygon.group;spreadGroupId(neighbour)}})};_.each(polygons,function(polygon){if(_.isUndefined(polygon.group)){polygon.group=groupCount++;spreadGroupId(polygon)}if(!polygonGroups[polygon.group])polygonGroups[polygon.group]=[];polygonGroups[polygon.group].push(polygon)});console.log("Groups built: ",polygonGroups.length);return polygonGroups},_array_intersect:function _array_intersect(){var i,shortest,nShortest,n,len,ret=[],obj={},nOthers;nOthers=arguments.length-1;nShortest=arguments[0].length;shortest=0;for(i=0;i<=nOthers;i++){n=arguments[i].length;if(n<nShortest){shortest=i;nShortest=n}}for(i=0;i<=nOthers;i++){n=i===shortest?0:i||shortest;len=arguments[n].length;for(var j=0;j<len;j++){var elem=arguments[n][j];if(obj[elem]===i-1){if(i===nOthers){ret.push(elem);obj[elem]=0}else{obj[elem]=i}}else if(i===0){obj[elem]=0}}}return ret},_buildPolygonNeighbours:function _buildPolygonNeighbours(polygon,navigationMesh){polygon.neighbours=[];for(var i=0,len=navigationMesh.polygons.length;i<len;i++){if(polygon===navigationMesh.polygons[i])continue;if(BABYLON.Vector3.DistanceSquared(polygon.centroid,navigationMesh.polygons[i].centroid)>100*100)continue;var matches=this._array_intersect(polygon.vertexIds,navigationMesh.polygons[i].vertexIds);if(matches.length>=2){polygon.neighbours.push(navigationMesh.polygons[i])}}},_buildPolygonsFromGeometry:function _buildPolygonsFromGeometry(geometry){var polygons=[];var vertices=geometry.getVerticesData(BABYLON.VertexBuffer.PositionKind);var indices=geometry.getIndices();var polygonId=1;console.log("Vertices:",vertices.length/3,"polygons:",indices.length/3);for(var i=0;i<indices.length;i+=3){var a=this.getVectorFrom(vertices,indices[i]);var b=this.getVectorFrom(vertices,indices[i+1]);var c=this.getVectorFrom(vertices,indices[i+2]);var normal=BABYLON.Vector3.Cross(b.subtract(a),b.subtract(c)).normalize();polygons.push({id:polygonId++,vertexIds:[indices[i],indices[i+1],indices[i+2]],centroid:geometry.centroids[i/3],normal:normal,neighbours:[]})}var navigationMesh={polygons:polygons,vertices:vertices};_.each(polygons,function(polygon){this._buildPolygonNeighbours(polygon,navigationMesh)}.bind(this));return navigationMesh},_cleanNavigationMesh:function _cleanNavigationMesh(navigationMesh){var polygons=navigationMesh.polygons;var vertices=navigationMesh.vertices;var up=new BABYLON.Vector3(0,1,0);polygons=_.filter(polygons,function(polygon){var angle=Math.acos(BABYLON.Vector3.Dot(up,polygon.normal));return angle<Math.PI/4});var newPolygons=[];_.each(polygons,function(polygon){if(polygon.toBeDeleted)return;var keepLooking=true;while(keepLooking){keepLooking=false;_.each(polygon.neighbours,function(otherPolygon){if(polygon===otherPolygon)return;if(Math.acos(BABYLON.Vector3.Dot(polygon.normal,otherPolygon.normal))<.01){var testPolygon={vertexIds:this._mergeVertexIds(polygon.vertexIds,otherPolygon.vertexIds),neighbours:polygon.neighbours,normal:polygon.normal.clone(),centroid:polygon.centroid.clone()};this._cleanPolygon(testPolygon,navigationMesh);if(this._isConvex(testPolygon,navigationMesh)){otherPolygon.toBeDeleted=true;_.each(otherPolygon.neighbours,function(otherPolygonNeighbour){otherPolygonNeighbour.neighbours=_.without(otherPolygonNeighbour.neighbours,otherPolygon);if(otherPolygonNeighbour!==polygon){otherPolygonNeighbour.neighbours.push(polygon)}else{polygon.neighbours=polygon.neighbours.concat(otherPolygon.neighbours);polygon.neighbours=_.uniq(polygon.neighbours);polygon.neighbours=_.without(polygon.neighbours,polygon)}});polygon.vertexIds=this._mergeVertexIds(polygon.vertexIds,otherPolygon.vertexIds);this._cleanPolygon(polygon,navigationMesh);keepLooking=true}}}.bind(this))}if(!polygon.toBeDeleted){newPolygons.push(polygon)}});var isUsed=function isUsed(vId){var contains=false;_.each(newPolygons,function(p){if(!contains&&_.includes(p.vertexIds,vId)){contains=true}});return contains};for(var i=0;i<vertices.length;i++){if(!isUsed(i)){_.each(newPolygons,function(p){for(var j=0;j<p.vertexIds.length;j++){if(p.vertexIds[j]>i){p.vertexIds[j]--}}});vertices.splice(i,1);i--}}navigationMesh.polygons=newPolygons;navigationMesh.vertices=vertices},_buildNavigationMesh:function _buildNavigationMesh(geometry){this._computeCentroids(geometry);this._mergeVertices(geometry);var navigationMesh=this._buildPolygonsFromGeometry(geometry);return navigationMesh},_mergeVertices:function _mergeVertices(geometry){var verticesMap={};var unique=[],changes=[];var v,key;var precisionPoints=4;var precision=Math.pow(10,precisionPoints);var indices;var ind=geometry.getIndices(),vert=geometry.getVerticesData(BABYLON.VertexBuffer.PositionKind);for(var i=0;i<vert.length;i+=3){v=new BABYLON.Vector3(vert[i],vert[i+1],vert[i+2]);key=Math.round(v.x*precision)+"_"+Math.round(v.y*precision)+"_"+Math.round(v.z*precision);if(verticesMap[key]===undefined){verticesMap[key]=i/3;unique.push(v.clone());changes[i/3]=unique.length-1}else{changes[i/3]=changes[verticesMap[key]]}}var faceIndicesToRemove=[];for(i=0;i<ind.length;i+=3){ind[i]=changes[ind[i]];ind[i+1]=changes[ind[i+1]];ind[i+2]=changes[ind[i+2]];indices=[ind[i],ind[i+1],ind[i+2]];var dupIndex=-1;for(var n=0;n<3;n++){if(indices[n]===indices[(n+1)%3]){dupIndex=n;faceIndicesToRemove.push(i);break}}}for(i=faceIndicesToRemove.length-1;i>=0;i--){var idx=faceIndicesToRemove[i];ind.splice(idx,3)}var diff=vert.length/3-unique.length;vert=[];for(i=0;i<unique.length;i++){vert.push(unique[i].x,unique[i].y,unique[i].z)}geometry.setIndices(ind);geometry.setVerticesData(BABYLON.VertexBuffer.PositionKind,vert);return diff},_getSharedVerticesInOrder:function _getSharedVerticesInOrder(a,b){var aList=a.vertexIds;var bList=b.vertexIds;var sharedVertices=[];_.each(aList,function(vId){if(_.includes(bList,vId)){sharedVertices.push(vId)}});if(sharedVertices.length<2)return[];if(_.includes(sharedVertices,aList[0])&&_.includes(sharedVertices,aList[aList.length-1])){aList.push(aList.shift())}if(_.includes(sharedVertices,bList[0])&&_.includes(sharedVertices,bList[bList.length-1])){bList.push(bList.shift())}sharedVertices=[];_.each(aList,function(vId){if(_.includes(bList,vId)){sharedVertices.push(vId)}});return sharedVertices},_groupNavMesh:function _groupNavMesh(navigationMesh){var saveObj={};_.each(navigationMesh.vertices,function(v){v=this._roundNumber(v,2)}.bind(this));saveObj.vertices=navigationMesh.vertices;var groups=this._buildPolygonGroups(navigationMesh);saveObj.groups=[];var findPolygonIndex=function findPolygonIndex(group,p){for(var i=0;i<group.length;i++){if(p===group[i])return i}};_.each(groups,function(group){var newGroup=[];_.each(group,function(p){var neighbours=[];_.each(p.neighbours,function(n){neighbours.push(findPolygonIndex(group,n))});var portals=[];_.each(p.neighbours,function(n){portals.push(this._getSharedVerticesInOrder(p,n))}.bind(this));p.centroid.x=this._roundNumber(p.centroid.x,2);p.centroid.y=this._roundNumber(p.centroid.y,2);p.centroid.z=this._roundNumber(p.centroid.z,2);newGroup.push({id:findPolygonIndex(group,p),neighbours:neighbours,vertexIds:p.vertexIds,centroid:p.centroid,portals:portals})}.bind(this));saveObj.groups.push(newGroup)}.bind(this));return saveObj}});module.exports=Navigation},{"./Astar.js":1,"./Channel.js":3,abitbol:5,babylonjs:7,lodash:8}],5:[function(require,module,exports){"use strict";var extractAnnotations=require("./annotation.js");var _disableConstructor=false;function inherit(SuperClass){_disableConstructor=true;var __class__=new SuperClass;_disableConstructor=false;return __class__}function usesSpecialProperty(fn){return Boolean(fn.toString().match(/.*(\$super|\$name|\$computedPropertyName).*/))}var Class=function(){};Object.defineProperty(Class,"$class",{enumerable:false,value:Class});Object.defineProperty(Class,"$map",{enumerable:false,value:{attributes:{},methods:{},computedProperties:{}}});Object.defineProperty(Class,"$extend",{enumerable:false,value:function(properties){var _superClass=this;var _classMap=JSON.parse(JSON.stringify(_superClass.$map));var __class__=function(){if(_disableConstructor){return}Object.defineProperty(this,"$class",{enumerable:false,value:__class__});Object.defineProperty(this,"$map",{enumerable:false,value:_classMap});Object.defineProperty(this,"$data",{enumerable:false,value:{}});for(var property in _classMap.computedProperties){Object.defineProperty(this,property,{enumerable:true,configurable:false,get:_classMap.computedProperties[property].get!==undefined?function(accessorName){return function(){return this[accessorName].apply(this,arguments)}}(_classMap.computedProperties[property].get):undefined,set:_classMap.computedProperties[property].set!==undefined?function(mutatorName){return function(){return this[mutatorName].apply(this,arguments)}}(_classMap.computedProperties[property].set):undefined})}for(var method in _classMap.methods){this[method]=this[method].bind(this)}if(this.__init__){this.__init__.apply(this,arguments)}return this};__class__.prototype=inherit(this.$class);properties=properties||{};var property;var computedPropertyName;var annotations;var i;var mixin;if(properties.__include__){for(i=properties.__include__.length-1;i>=0;i--){mixin=properties.__include__[i];for(property in mixin){if(property=="__classvars__"){continue}else if(properties[property]===undefined){properties[property]=mixin[property]}}if(mixin.__classvars__){if(!properties.__classvars__){properties.__classvars__={}}for(property in mixin.__classvars__){if(properties.__classvars__[property]===undefined){properties.__classvars__[property]=mixin.__classvars__[property]}}}}}for(property in properties||{}){if(property=="__include__"||property=="__classvars__"){continue}if(typeof properties[property]=="function"){computedPropertyName=undefined;_classMap.methods[property]={annotations:{}};if(property.indexOf("get")===0){computedPropertyName=property.slice(3,4).toLowerCase()+property.slice(4,property.length);if(!_classMap.computedProperties[computedPropertyName]){_classMap.computedProperties[computedPropertyName]={annotations:{}}}_classMap.computedProperties[computedPropertyName].get=property}else if(property.indexOf("set")===0){computedPropertyName=property.slice(3,4).toLowerCase()+property.slice(4,property.length);if(!_classMap.computedProperties[computedPropertyName]){_classMap.computedProperties[computedPropertyName]={annotations:{}}}_classMap.computedProperties[computedPropertyName].set=property}else if(property.indexOf("has")===0){computedPropertyName=property.slice(3,4).toLowerCase()+property.slice(4,property.length);if(!_classMap.computedProperties[computedPropertyName]){_classMap.computedProperties[computedPropertyName]={annotations:{}}}_classMap.computedProperties[computedPropertyName].get=property}else if(property.indexOf("is")===0){computedPropertyName=property.slice(2,3).toLowerCase()+property.slice(3,property.length);if(!_classMap.computedProperties[computedPropertyName]){_classMap.computedProperties[computedPropertyName]={annotations:{}}}_classMap.computedProperties[computedPropertyName].get=property}annotations=extractAnnotations(properties[property]);for(var annotation in annotations){_classMap.methods[property].annotations[annotation]=annotations[annotation];if(computedPropertyName){_classMap.computedProperties[computedPropertyName].annotations[annotation]=annotations[annotation]}}if(usesSpecialProperty(properties[property])){__class__.prototype[property]=function(method,propertyName,computedPropertyName){return function(){var _oldSuper=this.$super;var _oldName=this.$name;var _oldComputedPropertyName=this.$computedPropertyName;this.$super=_superClass.prototype[propertyName];this.$name=propertyName;this.$computedPropertyName=computedPropertyName;try{return method.apply(this,arguments)}finally{if(_oldSuper){this.$super=_oldSuper}else{delete this.$super}if(_oldName){this.$name=_oldName}else{delete this.$name}if(_oldComputedPropertyName){this.$computedPropertyName=_oldComputedPropertyName}else{delete this.$computedPropertyName}}}}(properties[property],property,computedPropertyName)}else{__class__.prototype[property]=properties[property]}}else{_classMap.attributes[property]=true;__class__.prototype[property]=properties[property]}}var scStaticProps=Object.getOwnPropertyNames(_superClass);scStaticProps=scStaticProps.filter(function(value){return["caller","callee","arguments","$class","$extend","$map"].indexOf(value)==-1});for(i=0;i<scStaticProps.length;i++){if(__class__[scStaticProps[i]]===undefined){__class__[scStaticProps[i]]=_superClass[scStaticProps[i]]}}if(properties.__classvars__){for(property in properties.__classvars__){__class__[property]=properties.__classvars__[property]}}Object.defineProperty(__class__,"$class",{enumerable:false,value:__class__});Object.defineProperty(__class__,"$extend",{enumerable:false,value:Class.$extend});Object.defineProperty(__class__,"$map",{enumerable:false,value:_classMap});return __class__}});module.exports=Class},{"./annotation.js":6}],6:[function(require,module,exports){"use strict";function cleanJs(js){var c;var p=0;for(var i=0;i<js.length;i++){c=js[i];if(c=="("){++p}else if(c==")"){--p}else if(c=="{"&&p===0){js=js.slice(i+1);break}}js=js.replace(/\/\*(.|\r|\n)*?\*\//g,"");js=js.replace(/\/\/.*?\r?\n/g,"\n");js=js.replace(/\s*\r?\n\s*/g,"");return js}function extractStrings(js){var strings=[];var instr=false;var inesc=false;var quote;var buff;var c;for(var i=0;i<js.length;i++){c=js[i];if(!instr){if(c=='"'||c=="'"){instr=true;inesc=false;quote=c;buff=""}else if([" "," ","\n","\r",";"].indexOf(c)>-1){continue}else{break}}else{if(!inesc){if(c=="\\"){inesc=true}else if(c==quote){strings.push(buff);instr=false}else{buff+=c}}else{if(c=="\\"){buff+="\\"}else if(c=="n"){buff+="\n"}else if(c=="r"){buff+="\r"}else if(c=="t"){buff+="\t"}else if(c==quote){buff+=quote}else{buff+="\\"+c}inesc=false}}}return strings}function autoCast(value){if(value=="true"){return true}else if(value=="false"){return false}else if(value=="null"){return null}else if(value=="undefined"){return undefined}else if(value.match(/^([0-9]+\.?|[0-9]*\.[0-9]+)$/)){return parseFloat(value)}else{return value}}function extractAnnotations(func){var js=cleanJs(func.toString());var strings=extractStrings(js);var annotations={};var string;var key;var value;for(var i=0;i<strings.length;i++){string=strings[i].trim();if(string.indexOf("@")!==0){continue}key=string.slice(1,string.indexOf(" ")>-1?string.indexOf(" "):string.length);value=true;if(string.indexOf(" ")>-1){value=string.slice(string.indexOf(" ")+1,string.length);value=value.trim();value=autoCast(value)}annotations[key]=value}return annotations}module.exports=extractAnnotations},{}],7:[function(require,module,exports){var __decorate=this&&this.__decorate||function(e,t,i,r){var n,o=arguments.length,s=o<3?t:null===r?r=Object.getOwnPropertyDescriptor(t,i):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(e,t,i,r);else for(var a=e.length-1;a>=0;a--)(n=e[a])&&(s=(o<3?n(s):o>3?n(t,i,s):n(t,i))||s);return o>3&&s&&Object.defineProperty(t,i,s),s},__extends=this&&this.__extends||function(e,t){function i(){this.constructor=e}for(var r in t)t.hasOwnProperty(r)&&(e[r]=t[r]);i.prototype=t.prototype,e.prototype=new i},BABYLON;!function(e){e.ToGammaSpace=1/2.2,e.ToLinearSpace=2.2,e.Epsilon=.001;var t=function(){function e(){}return e.WithinEpsilon=function(e,t,i){void 0===i&&(i=1.401298e-45);var r=e-t;return-i<=r&&r<=i},e.ToHex=function(e){var t=e.toString(16);return e<=15?("0"+t).toUpperCase():t.toUpperCase()},e.Sign=function(e){return e=+e,0===e||isNaN(e)?e:e>0?1:-1},e.Clamp=function(e,t,i){return void 0===t&&(t=0),void 0===i&&(i=1),Math.min(i,Math.max(t,e))},e}();e.MathTools=t;var i=function(){ function i(e,t,i){void 0===e&&(e=0),void 0===t&&(t=0),void 0===i&&(i=0),this.r=e,this.g=t,this.b=i}return i.prototype.toString=function(){return"{R: "+this.r+" G:"+this.g+" B:"+this.b+"}"},i.prototype.getClassName=function(){return"Color3"},i.prototype.getHashCode=function(){var e=this.r||0;return e=397*e^(this.g||0),e=397*e^(this.b||0)},i.prototype.toArray=function(e,t){return void 0===t&&(t=0),e[t]=this.r,e[t+1]=this.g,e[t+2]=this.b,this},i.prototype.toColor4=function(e){return void 0===e&&(e=1),new r(this.r,this.g,this.b,e)},i.prototype.asArray=function(){var e=[];return this.toArray(e,0),e},i.prototype.toLuminance=function(){return.3*this.r+.59*this.g+.11*this.b},i.prototype.multiply=function(e){return new i(this.r*e.r,this.g*e.g,this.b*e.b)},i.prototype.multiplyToRef=function(e,t){return t.r=this.r*e.r,t.g=this.g*e.g,t.b=this.b*e.b,this},i.prototype.equals=function(e){return e&&this.r===e.r&&this.g===e.g&&this.b===e.b},i.prototype.equalsFloats=function(e,t,i){return this.r===e&&this.g===t&&this.b===i},i.prototype.scale=function(e){return new i(this.r*e,this.g*e,this.b*e)},i.prototype.scaleToRef=function(e,t){return t.r=this.r*e,t.g=this.g*e,t.b=this.b*e,this},i.prototype.add=function(e){return new i(this.r+e.r,this.g+e.g,this.b+e.b)},i.prototype.addToRef=function(e,t){return t.r=this.r+e.r,t.g=this.g+e.g,t.b=this.b+e.b,this},i.prototype.subtract=function(e){return new i(this.r-e.r,this.g-e.g,this.b-e.b)},i.prototype.subtractToRef=function(e,t){return t.r=this.r-e.r,t.g=this.g-e.g,t.b=this.b-e.b,this},i.prototype.clone=function(){return new i(this.r,this.g,this.b)},i.prototype.copyFrom=function(e){return this.r=e.r,this.g=e.g,this.b=e.b,this},i.prototype.copyFromFloats=function(e,t,i){return this.r=e,this.g=t,this.b=i,this},i.prototype.toHexString=function(){var e=255*this.r|0,i=255*this.g|0,r=255*this.b|0;return"#"+t.ToHex(e)+t.ToHex(i)+t.ToHex(r)},i.prototype.toLinearSpace=function(){var e=new i;return this.toLinearSpaceToRef(e),e},i.prototype.toLinearSpaceToRef=function(t){return t.r=Math.pow(this.r,e.ToLinearSpace),t.g=Math.pow(this.g,e.ToLinearSpace),t.b=Math.pow(this.b,e.ToLinearSpace),this},i.prototype.toGammaSpace=function(){var e=new i;return this.toGammaSpaceToRef(e),e},i.prototype.toGammaSpaceToRef=function(t){return t.r=Math.pow(this.r,e.ToGammaSpace),t.g=Math.pow(this.g,e.ToGammaSpace),t.b=Math.pow(this.b,e.ToGammaSpace),this},i.FromHexString=function(e){if("#"!==e.substring(0,1)||7!==e.length)return new i(0,0,0);var t=parseInt(e.substring(1,3),16),r=parseInt(e.substring(3,5),16),n=parseInt(e.substring(5,7),16);return i.FromInts(t,r,n)},i.FromArray=function(e,t){return void 0===t&&(t=0),new i(e[t],e[t+1],e[t+2])},i.FromInts=function(e,t,r){return new i(e/255,t/255,r/255)},i.Lerp=function(e,t,r){var n=e.r+(t.r-e.r)*r,o=e.g+(t.g-e.g)*r,s=e.b+(t.b-e.b)*r;return new i(n,o,s)},i.Red=function(){return new i(1,0,0)},i.Green=function(){return new i(0,1,0)},i.Blue=function(){return new i(0,0,1)},i.Black=function(){return new i(0,0,0)},i.White=function(){return new i(1,1,1)},i.Purple=function(){return new i(.5,0,.5)},i.Magenta=function(){return new i(1,0,1)},i.Yellow=function(){return new i(1,1,0)},i.Gray=function(){return new i(.5,.5,.5)},i}();e.Color3=i;var r=function(){function e(e,t,i,r){this.r=e,this.g=t,this.b=i,this.a=r}return e.prototype.addInPlace=function(e){return this.r+=e.r,this.g+=e.g,this.b+=e.b,this.a+=e.a,this},e.prototype.asArray=function(){var e=[];return this.toArray(e,0),e},e.prototype.toArray=function(e,t){return void 0===t&&(t=0),e[t]=this.r,e[t+1]=this.g,e[t+2]=this.b,e[t+3]=this.a,this},e.prototype.add=function(t){return new e(this.r+t.r,this.g+t.g,this.b+t.b,this.a+t.a)},e.prototype.subtract=function(t){return new e(this.r-t.r,this.g-t.g,this.b-t.b,this.a-t.a)},e.prototype.subtractToRef=function(e,t){return t.r=this.r-e.r,t.g=this.g-e.g,t.b=this.b-e.b,t.a=this.a-e.a,this},e.prototype.scale=function(t){return new e(this.r*t,this.g*t,this.b*t,this.a*t)},e.prototype.scaleToRef=function(e,t){return t.r=this.r*e,t.g=this.g*e,t.b=this.b*e,t.a=this.a*e,this},e.prototype.multiply=function(t){return new e(this.r*t.r,this.g*t.g,this.b*t.b,this.a*t.a)},e.prototype.multiplyToRef=function(e,t){return t.r=this.r*e.r,t.g=this.g*e.g,t.b=this.b*e.b,t.a=this.a*e.a,t},e.prototype.toString=function(){return"{R: "+this.r+" G:"+this.g+" B:"+this.b+" A:"+this.a+"}"},e.prototype.getClassName=function(){return"Color4"},e.prototype.getHashCode=function(){var e=this.r||0;return e=397*e^(this.g||0),e=397*e^(this.b||0),e=397*e^(this.a||0)},e.prototype.clone=function(){return new e(this.r,this.g,this.b,this.a)},e.prototype.copyFrom=function(e){return this.r=e.r,this.g=e.g,this.b=e.b,this.a=e.a,this},e.prototype.toHexString=function(){var e=255*this.r|0,i=255*this.g|0,r=255*this.b|0,n=255*this.a|0;return"#"+t.ToHex(e)+t.ToHex(i)+t.ToHex(r)+t.ToHex(n)},e.FromHexString=function(t){if("#"!==t.substring(0,1)||9!==t.length)return new e(0,0,0,0);var i=parseInt(t.substring(1,3),16),r=parseInt(t.substring(3,5),16),n=parseInt(t.substring(5,7),16),o=parseInt(t.substring(7,9),16);return e.FromInts(i,r,n,o)},e.Lerp=function(t,i,r){var n=new e(0,0,0,0);return e.LerpToRef(t,i,r,n),n},e.LerpToRef=function(e,t,i,r){r.r=e.r+(t.r-e.r)*i,r.g=e.g+(t.g-e.g)*i,r.b=e.b+(t.b-e.b)*i,r.a=e.a+(t.a-e.a)*i},e.FromArray=function(t,i){return void 0===i&&(i=0),new e(t[i],t[i+1],t[i+2],t[i+3])},e.FromInts=function(t,i,r,n){return new e(t/255,i/255,r/255,n/255)},e.CheckColors4=function(e,t){if(e.length===3*t){for(var i=[],r=0;r<e.length;r+=3){var n=r/3*4;i[n]=e[r],i[n+1]=e[r+1],i[n+2]=e[r+2],i[n+3]=1}return i}return e},e}();e.Color4=r;var n=function(){function i(e,t){this.x=e,this.y=t}return i.prototype.toString=function(){return"{X: "+this.x+" Y:"+this.y+"}"},i.prototype.getClassName=function(){return"Vector2"},i.prototype.getHashCode=function(){var e=this.x||0;return e=397*e^(this.y||0)},i.prototype.toArray=function(e,t){return void 0===t&&(t=0),e[t]=this.x,e[t+1]=this.y,this},i.prototype.asArray=function(){var e=[];return this.toArray(e,0),e},i.prototype.copyFrom=function(e){return this.x=e.x,this.y=e.y,this},i.prototype.copyFromFloats=function(e,t){return this.x=e,this.y=t,this},i.prototype.add=function(e){return new i(this.x+e.x,this.y+e.y)},i.prototype.addToRef=function(e,t){return t.x=this.x+e.x,t.y=this.y+e.y,this},i.prototype.addInPlace=function(e){return this.x+=e.x,this.y+=e.y,this},i.prototype.addVector3=function(e){return new i(this.x+e.x,this.y+e.y)},i.prototype.subtract=function(e){return new i(this.x-e.x,this.y-e.y)},i.prototype.subtractToRef=function(e,t){return t.x=this.x-e.x,t.y=this.y-e.y,this},i.prototype.subtractInPlace=function(e){return this.x-=e.x,this.y-=e.y,this},i.prototype.multiplyInPlace=function(e){return this.x*=e.x,this.y*=e.y,this},i.prototype.multiply=function(e){return new i(this.x*e.x,this.y*e.y)},i.prototype.multiplyToRef=function(e,t){return t.x=this.x*e.x,t.y=this.y*e.y,this},i.prototype.multiplyByFloats=function(e,t){return new i(this.x*e,this.y*t)},i.prototype.divide=function(e){return new i(this.x/e.x,this.y/e.y)},i.prototype.divideToRef=function(e,t){return t.x=this.x/e.x,t.y=this.y/e.y,this},i.prototype.negate=function(){return new i(-this.x,-this.y)},i.prototype.scaleInPlace=function(e){return this.x*=e,this.y*=e,this},i.prototype.scale=function(e){return new i(this.x*e,this.y*e)},i.prototype.equals=function(e){return e&&this.x===e.x&&this.y===e.y},i.prototype.equalsWithEpsilon=function(i,r){return void 0===r&&(r=e.Epsilon),i&&t.WithinEpsilon(this.x,i.x,r)&&t.WithinEpsilon(this.y,i.y,r)},i.prototype.length=function(){return Math.sqrt(this.x*this.x+this.y*this.y)},i.prototype.lengthSquared=function(){return this.x*this.x+this.y*this.y},i.prototype.normalize=function(){var e=this.length();if(0===e)return this;var t=1/e;return this.x*=t,this.y*=t,this},i.prototype.clone=function(){return new i(this.x,this.y)},i.Zero=function(){return new i(0,0)},i.FromArray=function(e,t){return void 0===t&&(t=0),new i(e[t],e[t+1])},i.FromArrayToRef=function(e,t,i){i.x=e[t],i.y=e[t+1]},i.CatmullRom=function(e,t,r,n,o){var s=o*o,a=o*s,h=.5*(2*t.x+(-e.x+r.x)*o+(2*e.x-5*t.x+4*r.x-n.x)*s+(-e.x+3*t.x-3*r.x+n.x)*a),c=.5*(2*t.y+(-e.y+r.y)*o+(2*e.y-5*t.y+4*r.y-n.y)*s+(-e.y+3*t.y-3*r.y+n.y)*a);return new i(h,c)},i.Clamp=function(e,t,r){var n=e.x;n=n>r.x?r.x:n,n=n<t.x?t.x:n;var o=e.y;return o=o>r.y?r.y:o,o=o<t.y?t.y:o,new i(n,o)},i.Hermite=function(e,t,r,n,o){var s=o*o,a=o*s,h=2*a-3*s+1,c=-2*a+3*s,l=a-2*s+o,u=a-s,d=e.x*h+r.x*c+t.x*l+n.x*u,f=e.y*h+r.y*c+t.y*l+n.y*u;return new i(d,f)},i.Lerp=function(e,t,r){var n=e.x+(t.x-e.x)*r,o=e.y+(t.y-e.y)*r;return new i(n,o)},i.Dot=function(e,t){return e.x*t.x+e.y*t.y},i.Normalize=function(e){var t=e.clone();return t.normalize(),t},i.Minimize=function(e,t){var r=e.x<t.x?e.x:t.x,n=e.y<t.y?e.y:t.y;return new i(r,n)},i.Maximize=function(e,t){var r=e.x>t.x?e.x:t.x,n=e.y>t.y?e.y:t.y;return new i(r,n)},i.Transform=function(e,t){var r=i.Zero();return i.TransformToRef(e,t,r),r},i.TransformToRef=function(e,t,i){var r=e.x*t.m[0]+e.y*t.m[4]+t.m[12],n=e.x*t.m[1]+e.y*t.m[5]+t.m[13];i.x=r,i.y=n},i.PointInTriangle=function(e,t,i,r){var n=.5*(-i.y*r.x+t.y*(-i.x+r.x)+t.x*(i.y-r.y)+i.x*r.y),o=n<0?-1:1,s=(t.y*r.x-t.x*r.y+(r.y-t.y)*e.x+(t.x-r.x)*e.y)*o,a=(t.x*i.y-t.y*i.x+(t.y-i.y)*e.x+(i.x-t.x)*e.y)*o;return s>0&&a>0&&s+a<2*n*o},i.Distance=function(e,t){return Math.sqrt(i.DistanceSquared(e,t))},i.DistanceSquared=function(e,t){var i=e.x-t.x,r=e.y-t.y;return i*i+r*r},i.Center=function(e,t){var i=e.add(t);return i.scaleInPlace(.5),i},i.DistanceOfPointFromSegment=function(e,t,r){var n=i.DistanceSquared(t,r);if(0===n)return i.Distance(e,t);var o=r.subtract(t),s=Math.max(0,Math.min(1,i.Dot(e.subtract(t),o)/n)),a=t.add(o.multiplyByFloats(s,s));return i.Distance(e,a)},i}();e.Vector2=n;var o=function(){function i(e,t,i){this.x=e,this.y=t,this.z=i}return i.prototype.toString=function(){return"{X: "+this.x+" Y:"+this.y+" Z:"+this.z+"}"},i.prototype.getClassName=function(){return"Vector3"},i.prototype.getHashCode=function(){var e=this.x||0;return e=397*e^(this.y||0),e=397*e^(this.z||0)},i.prototype.asArray=function(){var e=[];return this.toArray(e,0),e},i.prototype.toArray=function(e,t){return void 0===t&&(t=0),e[t]=this.x,e[t+1]=this.y,e[t+2]=this.z,this},i.prototype.toQuaternion=function(){var e=new h(0,0,0,1),t=Math.cos(.5*(this.x+this.z)),i=Math.sin(.5*(this.x+this.z)),r=Math.cos(.5*(this.z-this.x)),n=Math.sin(.5*(this.z-this.x)),o=Math.cos(.5*this.y),s=Math.sin(.5*this.y);return e.x=r*s,e.y=-n*s,e.z=i*o,e.w=t*o,e},i.prototype.addInPlace=function(e){return this.x+=e.x,this.y+=e.y,this.z+=e.z,this},i.prototype.add=function(e){return new i(this.x+e.x,this.y+e.y,this.z+e.z)},i.prototype.addToRef=function(e,t){return t.x=this.x+e.x,t.y=this.y+e.y,t.z=this.z+e.z,this},i.prototype.subtractInPlace=function(e){return this.x-=e.x,this.y-=e.y,this.z-=e.z,this},i.prototype.subtract=function(e){return new i(this.x-e.x,this.y-e.y,this.z-e.z)},i.prototype.subtractToRef=function(e,t){return t.x=this.x-e.x,t.y=this.y-e.y,t.z=this.z-e.z,this},i.prototype.subtractFromFloats=function(e,t,r){return new i(this.x-e,this.y-t,this.z-r)},i.prototype.subtractFromFloatsToRef=function(e,t,i,r){return r.x=this.x-e,r.y=this.y-t,r.z=this.z-i,this},i.prototype.negate=function(){return new i(-this.x,-this.y,-this.z)},i.prototype.scaleInPlace=function(e){return this.x*=e,this.y*=e,this.z*=e,this},i.prototype.scale=function(e){return new i(this.x*e,this.y*e,this.z*e)},i.prototype.scaleToRef=function(e,t){t.x=this.x*e,t.y=this.y*e,t.z=this.z*e},i.prototype.equals=function(e){return e&&this.x===e.x&&this.y===e.y&&this.z===e.z},i.prototype.equalsWithEpsilon=function(i,r){return void 0===r&&(r=e.Epsilon),i&&t.WithinEpsilon(this.x,i.x,r)&&t.WithinEpsilon(this.y,i.y,r)&&t.WithinEpsilon(this.z,i.z,r)},i.prototype.equalsToFloats=function(e,t,i){return this.x===e&&this.y===t&&this.z===i},i.prototype.multiplyInPlace=function(e){return this.x*=e.x,this.y*=e.y,this.z*=e.z,this},i.prototype.multiply=function(e){return new i(this.x*e.x,this.y*e.y,this.z*e.z)},i.prototype.multiplyToRef=function(e,t){return t.x=this.x*e.x,t.y=this.y*e.y,t.z=this.z*e.z,this},i.prototype.multiplyByFloats=function(e,t,r){return new i(this.x*e,this.y*t,this.z*r)},i.prototype.divide=function(e){return new i(this.x/e.x,this.y/e.y,this.z/e.z)},i.prototype.divideToRef=function(e,t){return t.x=this.x/e.x,t.y=this.y/e.y,t.z=this.z/e.z,this},i.prototype.MinimizeInPlace=function(e){return e.x<this.x&&(this.x=e.x),e.y<this.y&&(this.y=e.y),e.z<this.z&&(this.z=e.z),this},i.prototype.MaximizeInPlace=function(e){return e.x>this.x&&(this.x=e.x),e.y>this.y&&(this.y=e.y),e.z>this.z&&(this.z=e.z),this},i.prototype.length=function(){return Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z)},i.prototype.lengthSquared=function(){return this.x*this.x+this.y*this.y+this.z*this.z},i.prototype.normalize=function(){var e=this.length();if(0===e||1===e)return this;var t=1/e;return this.x*=t,this.y*=t,this.z*=t,this},i.prototype.clone=function(){return new i(this.x,this.y,this.z)},i.prototype.copyFrom=function(e){return this.x=e.x,this.y=e.y,this.z=e.z,this},i.prototype.copyFromFloats=function(e,t,i){return this.x=e,this.y=t,this.z=i,this},i.GetClipFactor=function(e,t,r,n){var o=i.Dot(e,r)-n,s=i.Dot(t,r)-n,a=o/(o-s);return a},i.FromArray=function(e,t){return t||(t=0),new i(e[t],e[t+1],e[t+2])},i.FromFloatArray=function(e,t){return t||(t=0),new i(e[t],e[t+1],e[t+2])},i.FromArrayToRef=function(e,t,i){i.x=e[t],i.y=e[t+1],i.z=e[t+2]},i.FromFloatArrayToRef=function(e,t,i){i.x=e[t],i.y=e[t+1],i.z=e[t+2]},i.FromFloatsToRef=function(e,t,i,r){r.x=e,r.y=t,r.z=i},i.Zero=function(){return new i(0,0,0)},i.Up=function(){return new i(0,1,0)},i.Forward=function(){return new i(0,0,1)},i.Right=function(){return new i(1,0,0)},i.Left=function(){return new i(-1,0,0)},i.TransformCoordinates=function(e,t){var r=i.Zero();return i.TransformCoordinatesToRef(e,t,r),r},i.TransformCoordinatesToRef=function(e,t,i){var r=e.x*t.m[0]+e.y*t.m[4]+e.z*t.m[8]+t.m[12],n=e.x*t.m[1]+e.y*t.m[5]+e.z*t.m[9]+t.m[13],o=e.x*t.m[2]+e.y*t.m[6]+e.z*t.m[10]+t.m[14],s=e.x*t.m[3]+e.y*t.m[7]+e.z*t.m[11]+t.m[15];i.x=r/s,i.y=n/s,i.z=o/s},i.TransformCoordinatesFromFloatsToRef=function(e,t,i,r,n){var o=e*r.m[0]+t*r.m[4]+i*r.m[8]+r.m[12],s=e*r.m[1]+t*r.m[5]+i*r.m[9]+r.m[13],a=e*r.m[2]+t*r.m[6]+i*r.m[10]+r.m[14],h=e*r.m[3]+t*r.m[7]+i*r.m[11]+r.m[15];n.x=o/h,n.y=s/h,n.z=a/h},i.TransformNormal=function(e,t){var r=i.Zero();return i.TransformNormalToRef(e,t,r),r},i.TransformNormalToRef=function(e,t,i){var r=e.x*t.m[0]+e.y*t.m[4]+e.z*t.m[8],n=e.x*t.m[1]+e.y*t.m[5]+e.z*t.m[9],o=e.x*t.m[2]+e.y*t.m[6]+e.z*t.m[10];i.x=r,i.y=n,i.z=o},i.TransformNormalFromFloatsToRef=function(e,t,i,r,n){n.x=e*r.m[0]+t*r.m[4]+i*r.m[8],n.y=e*r.m[1]+t*r.m[5]+i*r.m[9],n.z=e*r.m[2]+t*r.m[6]+i*r.m[10]},i.CatmullRom=function(e,t,r,n,o){var s=o*o,a=o*s,h=.5*(2*t.x+(-e.x+r.x)*o+(2*e.x-5*t.x+4*r.x-n.x)*s+(-e.x+3*t.x-3*r.x+n.x)*a),c=.5*(2*t.y+(-e.y+r.y)*o+(2*e.y-5*t.y+4*r.y-n.y)*s+(-e.y+3*t.y-3*r.y+n.y)*a),l=.5*(2*t.z+(-e.z+r.z)*o+(2*e.z-5*t.z+4*r.z-n.z)*s+(-e.z+3*t.z-3*r.z+n.z)*a);return new i(h,c,l)},i.Clamp=function(e,t,r){var n=e.x;n=n>r.x?r.x:n,n=n<t.x?t.x:n;var o=e.y;o=o>r.y?r.y:o,o=o<t.y?t.y:o;var s=e.z;return s=s>r.z?r.z:s,s=s<t.z?t.z:s,new i(n,o,s)},i.Hermite=function(e,t,r,n,o){var s=o*o,a=o*s,h=2*a-3*s+1,c=-2*a+3*s,l=a-2*s+o,u=a-s,d=e.x*h+r.x*c+t.x*l+n.x*u,f=e.y*h+r.y*c+t.y*l+n.y*u,p=e.z*h+r.z*c+t.z*l+n.z*u;return new i(d,f,p)},i.Lerp=function(e,t,r){var n=e.x+(t.x-e.x)*r,o=e.y+(t.y-e.y)*r,s=e.z+(t.z-e.z)*r;return new i(n,o,s)},i.Dot=function(e,t){return e.x*t.x+e.y*t.y+e.z*t.z},i.Cross=function(e,t){var r=i.Zero();return i.CrossToRef(e,t,r),r},i.CrossToRef=function(e,t,i){P.Vector3[0].x=e.y*t.z-e.z*t.y,P.Vector3[0].y=e.z*t.x-e.x*t.z,P.Vector3[0].z=e.x*t.y-e.y*t.x,i.copyFrom(P.Vector3[0])},i.Normalize=function(e){var t=i.Zero();return i.NormalizeToRef(e,t),t},i.NormalizeToRef=function(e,t){t.copyFrom(e),t.normalize()},i.Project=function(e,t,r,n){var o=n.width,s=n.height,a=n.x,h=n.y,l=i._viewportMatrixCache?i._viewportMatrixCache:i._viewportMatrixCache=new c;c.FromValuesToRef(o/2,0,0,0,0,-s/2,0,0,0,0,1,0,a+o/2,s/2+h,0,1,l);var u=i._matrixCache?i._matrixCache:i._matrixCache=new c;return t.multiplyToRef(r,u),u.multiplyToRef(l,u),i.TransformCoordinates(e,u)},i.UnprojectFromTransform=function(e,r,n,o,s){var a=i._matrixCache?i._matrixCache:i._matrixCache=new c;o.multiplyToRef(s,a),a.invert(),e.x=e.x/r*2-1,e.y=-(e.y/n*2-1);var h=i.TransformCoordinates(e,a),l=e.x*a.m[3]+e.y*a.m[7]+e.z*a.m[11]+a.m[15];return t.WithinEpsilon(l,1)&&(h=h.scale(1/l)),h},i.Unproject=function(e,r,n,o,s,a){var h=i._matrixCache?i._matrixCache:i._matrixCache=new c;o.multiplyToRef(s,h),h.multiplyToRef(a,h),h.invert();var l=new i(e.x/r*2-1,-(e.y/n*2-1),e.z),u=i.TransformCoordinates(l,h),d=l.x*h.m[3]+l.y*h.m[7]+l.z*h.m[11]+h.m[15];return t.WithinEpsilon(d,1)&&(u=u.scale(1/d)),u},i.Minimize=function(e,t){var i=e.clone();return i.MinimizeInPlace(t),i},i.Maximize=function(e,t){var i=e.clone();return i.MaximizeInPlace(t),i},i.Distance=function(e,t){return Math.sqrt(i.DistanceSquared(e,t))},i.DistanceSquared=function(e,t){var i=e.x-t.x,r=e.y-t.y,n=e.z-t.z;return i*i+r*r+n*n},i.Center=function(e,t){var i=e.add(t);return i.scaleInPlace(.5),i},i.RotationFromAxis=function(e,t,r){var n=i.Zero();return i.RotationFromAxisToRef(e,t,r,n),n},i.RotationFromAxisToRef=function(r,n,o,s){var a=r.normalize(),h=o.normalize(),c=f.X,l=f.Y,u=0,d=0,p=0,m=0,_=0,g=0,v=0,y=-1,x=0,b=P.Vector3[0],T=0,A=P.Vector3[1];t.WithinEpsilon(h.z,0,e.Epsilon)?g=1:t.WithinEpsilon(h.x,0,e.Epsilon)?m=1:(v=h.z/h.x,m=-v*Math.sqrt(1/(1+v*v)),g=Math.sqrt(1/(1+v*v))),A.x=m,A.y=_,A.z=g,A.normalize(),i.CrossToRef(a,A,b),b.normalize(),i.Dot(h,b)<0&&(y=1),T=i.Dot(a,A),T=Math.min(1,Math.max(-1,T)),p=Math.acos(T)*y,i.Dot(A,c)<0&&(p=Math.PI+p,A=A.scaleInPlace(-1),x++);var E=P.Vector3[2],M=P.Vector3[3];m=0,_=0,g=0,y=-1,t.WithinEpsilon(h.z,0,e.Epsilon)?m=1:(v=A.z/A.x,m=-v*Math.sqrt(1/(1+v*v)),g=Math.sqrt(1/(1+v*v))),E.x=m,E.y=_,E.z=g,E.normalize(),i