yuka
Version:
JavaScript Game AI library
25 lines (24 loc) • 123 kB
JavaScript
/**
* The MIT License
*
* Copyright © 2022 Yuka authors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).YUKA={})}(this,(function(t){"use strict";class e{constructor(t,e,s,i,r){this.sender=t,this.receiver=e,this.message=s,this.delay=i,this.data=r}toJSON(){return{type:this.constructor.name,sender:this.sender.uuid,receiver:this.receiver.uuid,message:this.message,delay:this.delay,data:this.data}}fromJSON(t){return this.sender=t.sender,this.receiver=t.receiver,this.message=t.message,this.delay=t.delay,this.data=t.data,this}resolveReferences(t){return this.sender=t.get(this.sender),this.receiver=t.get(this.receiver),this}}class s{static setLevel(t){i=t}static log(...t){i<=s.LEVEL.LOG&&console.log(...t)}static warn(...t){i<=s.LEVEL.WARN&&console.warn(...t)}static error(...t){i<=s.LEVEL.ERROR&&console.error(...t)}}s.LEVEL=Object.freeze({LOG:0,WARN:1,ERROR:2,SILENT:3});let i=s.LEVEL.WARN;class r{constructor(){this.delayedTelegrams=new Array}deliver(t){const e=t.receiver;return!1===e.handleMessage(t)&&s.warn("YUKA.MessageDispatcher: Message not handled by receiver: %o",e),this}dispatch(t,s,i,r,n){const o=new e(t,s,i,r,n);return r<=0?this.deliver(o):this.delayedTelegrams.push(o),this}dispatchDelayedMessages(t){let e=this.delayedTelegrams.length;for(;e--;){const s=this.delayedTelegrams[e];s.delay-=t,s.delay<=0&&(this.deliver(s),this.delayedTelegrams.pop())}return this}clear(){return this.delayedTelegrams.length=0,this}toJSON(){const t={type:this.constructor.name,delayedTelegrams:new Array};for(let e=0,s=this.delayedTelegrams.length;e<s;e++){const s=this.delayedTelegrams[e];t.delayedTelegrams.push(s.toJSON())}return t}fromJSON(t){this.clear();const s=t.delayedTelegrams;for(let t=0,i=s.length;t<i;t++){const i=s[t],r=(new e).fromJSON(i);this.delayedTelegrams.push(r)}return this}resolveReferences(t){const e=this.delayedTelegrams;for(let s=0,i=e.length;s<i;s++){e[s].resolveReferences(t)}return this}}const n=new Array;for(let t=0;t<256;t++)n[t]=(t<16?"0":"")+t.toString(16);class o{static area(t,e,s){return(s.x-t.x)*(e.z-t.z)-(e.x-t.x)*(s.z-t.z)}static argmax(t){const e=Math.max(...t),s=[];for(let i=0,r=t.length;i<r;i++)t[i]===e&&s.push(i);return s}static choice(t,e=null){const s=Math.random();if(null===e)return t[Math.floor(Math.random()*t.length)];{let i=0;const r=t.map(((t,s)=>(i+=e[s],i))).findIndex((t=>t>=s));return t[r]}}static clamp(t,e,s){return Math.max(e,Math.min(s,t))}static generateUUID(){const t=4294967295*Math.random()|0,e=4294967295*Math.random()|0,s=4294967295*Math.random()|0,i=4294967295*Math.random()|0;return(n[255&t]+n[t>>8&255]+n[t>>16&255]+n[t>>24&255]+"-"+n[255&e]+n[e>>8&255]+"-"+n[e>>16&15|64]+n[e>>24&255]+"-"+n[63&s|128]+n[s>>8&255]+"-"+n[s>>16&255]+n[s>>24&255]+n[255&i]+n[i>>8&255]+n[i>>16&255]+n[i>>24&255]).toUpperCase()}static randFloat(t,e){return t+Math.random()*(e-t)}static randInt(t,e){return t+Math.floor(Math.random()*(e-t+1))}}class h{constructor(t=0,e=0,s=0){this.x=t,this.y=e,this.z=s}set(t,e,s){return this.x=t,this.y=e,this.z=s,this}copy(t){return this.x=t.x,this.y=t.y,this.z=t.z,this}clone(){return(new this.constructor).copy(this)}add(t){return this.x+=t.x,this.y+=t.y,this.z+=t.z,this}addScalar(t){return this.x+=t,this.y+=t,this.z+=t,this}addVectors(t,e){return this.x=t.x+e.x,this.y=t.y+e.y,this.z=t.z+e.z,this}sub(t){return this.x-=t.x,this.y-=t.y,this.z-=t.z,this}subScalar(t){return this.x-=t,this.y-=t,this.z-=t,this}subVectors(t,e){return this.x=t.x-e.x,this.y=t.y-e.y,this.z=t.z-e.z,this}multiply(t){return this.x*=t.x,this.y*=t.y,this.z*=t.z,this}multiplyScalar(t){return this.x*=t,this.y*=t,this.z*=t,this}multiplyVectors(t,e){return this.x=t.x*e.x,this.y=t.y*e.y,this.z=t.z*e.z,this}divide(t){return this.x/=t.x,this.y/=t.y,this.z/=t.z,this}divideScalar(t){return this.x/=t,this.y/=t,this.z/=t,this}divideVectors(t,e){return this.x=t.x/e.x,this.y=t.y/e.y,this.z=t.z/e.z,this}reflect(t){return this.sub(a.copy(t).multiplyScalar(2*this.dot(t)))}clamp(t,e){return this.x=Math.max(t.x,Math.min(e.x,this.x)),this.y=Math.max(t.y,Math.min(e.y,this.y)),this.z=Math.max(t.z,Math.min(e.z,this.z)),this}min(t){return this.x=Math.min(this.x,t.x),this.y=Math.min(this.y,t.y),this.z=Math.min(this.z,t.z),this}max(t){return this.x=Math.max(this.x,t.x),this.y=Math.max(this.y,t.y),this.z=Math.max(this.z,t.z),this}dot(t){return this.x*t.x+this.y*t.y+this.z*t.z}cross(t){const e=this.x,s=this.y,i=this.z;return this.x=s*t.z-i*t.y,this.y=i*t.x-e*t.z,this.z=e*t.y-s*t.x,this}crossVectors(t,e){const s=t.x,i=t.y,r=t.z,n=e.x,o=e.y,h=e.z;return this.x=i*h-r*o,this.y=r*n-s*h,this.z=s*o-i*n,this}angleTo(t){const e=Math.sqrt(this.squaredLength()*t.squaredLength());if(0===e)return 0;const s=this.dot(t)/e;return Math.acos(o.clamp(s,-1,1))}length(){return Math.sqrt(this.squaredLength())}squaredLength(){return this.dot(this)}manhattanLength(){return Math.abs(this.x)+Math.abs(this.y)+Math.abs(this.z)}distanceTo(t){return Math.sqrt(this.squaredDistanceTo(t))}squaredDistanceTo(t){const e=this.x-t.x,s=this.y-t.y,i=this.z-t.z;return e*e+s*s+i*i}manhattanDistanceTo(t){const e=this.x-t.x,s=this.y-t.y,i=this.z-t.z;return Math.abs(e)+Math.abs(s)+Math.abs(i)}normalize(){return this.divideScalar(this.length()||1)}applyMatrix4(t){const e=this.x,s=this.y,i=this.z,r=t.elements,n=1/(r[3]*e+r[7]*s+r[11]*i+r[15]);return this.x=(r[0]*e+r[4]*s+r[8]*i+r[12])*n,this.y=(r[1]*e+r[5]*s+r[9]*i+r[13])*n,this.z=(r[2]*e+r[6]*s+r[10]*i+r[14])*n,this}applyRotation(t){const e=this.x,s=this.y,i=this.z,r=t.x,n=t.y,o=t.z,h=t.w,a=h*e+n*i-o*s,l=h*s+o*e-r*i,c=h*i+r*s-n*e,u=-r*e-n*s-o*i;return this.x=a*h+u*-r+l*-o-c*-n,this.y=l*h+u*-n+c*-r-a*-o,this.z=c*h+u*-o+a*-n-l*-r,this}extractPositionFromMatrix(t){const e=t.elements;return this.x=e[12],this.y=e[13],this.z=e[14],this}transformDirection(t){const e=this.x,s=this.y,i=this.z,r=t.elements;return this.x=r[0]*e+r[4]*s+r[8]*i,this.y=r[1]*e+r[5]*s+r[9]*i,this.z=r[2]*e+r[6]*s+r[10]*i,this.normalize()}fromMatrix3Column(t,e){return this.fromArray(t.elements,3*e)}fromMatrix4Column(t,e){return this.fromArray(t.elements,4*e)}fromSpherical(t,e,s){const i=Math.sin(e)*t;return this.x=i*Math.sin(s),this.y=Math.cos(e)*t,this.z=i*Math.cos(s),this}fromArray(t,e=0){return this.x=t[e+0],this.y=t[e+1],this.z=t[e+2],this}toArray(t,e=0){return t[e+0]=this.x,t[e+1]=this.y,t[e+2]=this.z,t}equals(t){return t.x===this.x&&t.y===this.y&&t.z===this.z}}const a=new h,l=new h(0,1,0),c=new h,u=new h,d=new h,p=new h,g=[2,2,1],m=[1,0,0];class f{constructor(){this.elements=[1,0,0,0,1,0,0,0,1]}set(t,e,s,i,r,n,o,h,a){const l=this.elements;return l[0]=t,l[3]=e,l[6]=s,l[1]=i,l[4]=r,l[7]=n,l[2]=o,l[5]=h,l[8]=a,this}copy(t){const e=this.elements,s=t.elements;return e[0]=s[0],e[1]=s[1],e[2]=s[2],e[3]=s[3],e[4]=s[4],e[5]=s[5],e[6]=s[6],e[7]=s[7],e[8]=s[8],this}clone(){return(new this.constructor).copy(this)}identity(){return this.set(1,0,0,0,1,0,0,0,1),this}multiply(t){return this.multiplyMatrices(this,t)}premultiply(t){return this.multiplyMatrices(t,this)}multiplyMatrices(t,e){const s=t.elements,i=e.elements,r=this.elements,n=s[0],o=s[3],h=s[6],a=s[1],l=s[4],c=s[7],u=s[2],d=s[5],p=s[8],g=i[0],m=i[3],f=i[6],y=i[1],x=i[4],w=i[7],S=i[2],v=i[5],b=i[8];return r[0]=n*g+o*y+h*S,r[3]=n*m+o*x+h*v,r[6]=n*f+o*w+h*b,r[1]=a*g+l*y+c*S,r[4]=a*m+l*x+c*v,r[7]=a*f+l*w+c*b,r[2]=u*g+d*y+p*S,r[5]=u*m+d*x+p*v,r[8]=u*f+d*w+p*b,this}multiplyScalar(t){const e=this.elements;return e[0]*=t,e[3]*=t,e[6]*=t,e[1]*=t,e[4]*=t,e[7]*=t,e[2]*=t,e[5]*=t,e[8]*=t,this}extractBasis(t,e,s){return t.fromMatrix3Column(this,0),e.fromMatrix3Column(this,1),s.fromMatrix3Column(this,2),this}makeBasis(t,e,s){return this.set(t.x,e.x,s.x,t.y,e.y,s.y,t.z,e.z,s.z),this}lookAt(t,e,s){return c.crossVectors(s,t).normalize(),u.crossVectors(l,e).normalize(),0===u.squaredLength()&&(p.copy(e).addScalar(Number.EPSILON),u.crossVectors(l,p).normalize()),d.crossVectors(e,u).normalize(),y.makeBasis(u,d,e),x.makeBasis(c,s,t),this.multiplyMatrices(y,x.transpose()),this}transpose(){const t=this.elements;let e;return e=t[1],t[1]=t[3],t[3]=e,e=t[2],t[2]=t[6],t[6]=e,e=t[5],t[5]=t[7],t[7]=e,this}getElementIndex(t,e){return 3*t+e}frobeniusNorm(){const t=this.elements;let e=0;for(let s=0;s<9;s++)e+=t[s]*t[s];return Math.sqrt(e)}offDiagonalFrobeniusNorm(){const t=this.elements;let e=0;for(let s=0;s<3;s++){const i=t[this.getElementIndex(g[s],m[s])];e+=2*i*i}return Math.sqrt(e)}eigenDecomposition(t){let e=0,s=0;t.unitary.identity(),t.diagonal.copy(this);const i=t.unitary,r=t.diagonal,n=Number.EPSILON*r.frobeniusNorm();for(;s<10&&r.offDiagonalFrobeniusNorm()>n;)r.shurDecomposition(y),x.copy(y).transpose(),r.multiply(y),r.premultiply(x),i.multiply(y),++e>2&&(s++,e=0);return t}shurDecomposition(t){let e=0,s=1;const i=this.elements;for(let t=0;t<3;t++){const r=Math.abs(i[this.getElementIndex(g[t],m[t])]);r>e&&(e=r,s=t)}let r=1,n=0;const o=m[s],h=g[s];if(Math.abs(i[this.getElementIndex(h,o)])>Number.EPSILON){const t=(i[this.getElementIndex(h,h)]-i[this.getElementIndex(o,o)])/2/i[this.getElementIndex(h,o)];let e;e=t<0?-1/(-t+Math.sqrt(1+t*t)):1/(t+Math.sqrt(1+t*t)),r=1/Math.sqrt(1+e*e),n=e*r}return t.identity(),t.elements[this.getElementIndex(o,o)]=r,t.elements[this.getElementIndex(h,h)]=r,t.elements[this.getElementIndex(h,o)]=n,t.elements[this.getElementIndex(o,h)]=-n,t}fromQuaternion(t){const e=this.elements,s=t.x,i=t.y,r=t.z,n=t.w,o=s+s,h=i+i,a=r+r,l=s*o,c=s*h,u=s*a,d=i*h,p=i*a,g=r*a,m=n*o,f=n*h,y=n*a;return e[0]=1-(d+g),e[3]=c-y,e[6]=u+f,e[1]=c+y,e[4]=1-(l+g),e[7]=p-m,e[2]=u-f,e[5]=p+m,e[8]=1-(l+d),this}fromMatrix4(t){const e=this.elements,s=t.elements;return e[0]=s[0],e[1]=s[1],e[2]=s[2],e[3]=s[4],e[4]=s[5],e[5]=s[6],e[6]=s[8],e[7]=s[9],e[8]=s[10],this}fromArray(t,e=0){const s=this.elements;for(let i=0;i<9;i++)s[i]=t[i+e];return this}toArray(t,e=0){const s=this.elements;return t[e+0]=s[0],t[e+1]=s[1],t[e+2]=s[2],t[e+3]=s[3],t[e+4]=s[4],t[e+5]=s[5],t[e+6]=s[6],t[e+7]=s[7],t[e+8]=s[8],t}equals(t){const e=this.elements,s=t.elements;for(let t=0;t<9;t++)if(e[t]!==s[t])return!1;return!0}}const y=new f,x=new f,w=new f,S=new h;class v{constructor(t=0,e=0,s=0,i=1){this.x=t,this.y=e,this.z=s,this.w=i}set(t,e,s,i){return this.x=t,this.y=e,this.z=s,this.w=i,this}copy(t){return this.x=t.x,this.y=t.y,this.z=t.z,this.w=t.w,this}clone(){return(new this.constructor).copy(this)}inverse(){return this.conjugate().normalize()}conjugate(){return this.x*=-1,this.y*=-1,this.z*=-1,this}dot(t){return this.x*t.x+this.y*t.y+this.z*t.z+this.w*t.w}length(){return Math.sqrt(this.squaredLength())}squaredLength(){return this.dot(this)}normalize(){let t=this.length();return 0===t?(this.x=0,this.y=0,this.z=0,this.w=1):(t=1/t,this.x=this.x*t,this.y=this.y*t,this.z=this.z*t,this.w=this.w*t),this}multiply(t){return this.multiplyQuaternions(this,t)}premultiply(t){return this.multiplyQuaternions(t,this)}multiplyQuaternions(t,e){const s=t.x,i=t.y,r=t.z,n=t.w,o=e.x,h=e.y,a=e.z,l=e.w;return this.x=s*l+n*o+i*a-r*h,this.y=i*l+n*h+r*o-s*a,this.z=r*l+n*a+s*h-i*o,this.w=n*l-s*o-i*h-r*a,this}angleTo(t){return 2*Math.acos(Math.abs(o.clamp(this.dot(t),-1,1)))}rotateTo(t,e,s=1e-4){const i=this.angleTo(t);if(i<s)return!0;const r=Math.min(1,e/i);return this.slerp(t,r),!1}lookAt(t,e,s){w.lookAt(t,e,s),this.fromMatrix3(w)}slerp(t,e){if(0===e)return this;if(1===e)return this.copy(t);const s=this.x,i=this.y,r=this.z,n=this.w;let o=n*t.w+s*t.x+i*t.y+r*t.z;if(o<0?(this.w=-t.w,this.x=-t.x,this.y=-t.y,this.z=-t.z,o=-o):this.copy(t),o>=1)return this.w=n,this.x=s,this.y=i,this.z=r,this;const h=Math.sqrt(1-o*o);if(Math.abs(h)<.001)return this.w=.5*(n+this.w),this.x=.5*(s+this.x),this.y=.5*(i+this.y),this.z=.5*(r+this.z),this;const a=Math.atan2(h,o),l=Math.sin((1-e)*a)/h,c=Math.sin(e*a)/h;return this.w=n*l+this.w*c,this.x=s*l+this.x*c,this.y=i*l+this.y*c,this.z=r*l+this.z*c,this}extractRotationFromMatrix(t){const e=w.elements,s=t.elements,i=1/S.fromMatrix4Column(t,0).length(),r=1/S.fromMatrix4Column(t,1).length(),n=1/S.fromMatrix4Column(t,2).length();return e[0]=s[0]*i,e[1]=s[1]*i,e[2]=s[2]*i,e[3]=s[4]*r,e[4]=s[5]*r,e[5]=s[6]*r,e[6]=s[8]*n,e[7]=s[9]*n,e[8]=s[10]*n,this.fromMatrix3(w),this}fromEuler(t,e,s){const i=Math.cos(e/2),r=Math.cos(t/2),n=Math.cos(s/2),o=Math.sin(e/2),h=Math.sin(t/2),a=Math.sin(s/2);return this.w=i*r*n+o*h*a,this.x=i*h*n+o*r*a,this.y=o*r*n-i*h*a,this.z=i*r*a-o*h*n,this}toEuler(t){const e=-2*(this.y*this.z-this.x*this.w);return Math.abs(e)>.9999?(t.x=.5*Math.PI*e,t.y=Math.atan2(this.x*this.z+this.w*this.y,.5-this.x*this.x-this.y*this.y),t.z=0):(t.x=Math.asin(e),t.y=Math.atan2(this.x*this.z+this.w*this.y,.5-this.x*this.x-this.y*this.y),t.z=Math.atan2(this.x*this.y+this.w*this.z,.5-this.x*this.x-this.z*this.z)),t}fromMatrix3(t){const e=t.elements,s=e[0],i=e[3],r=e[6],n=e[1],o=e[4],h=e[7],a=e[2],l=e[5],c=e[8],u=s+o+c;if(u>0){let t=.5/Math.sqrt(u+1);this.w=.25/t,this.x=(l-h)*t,this.y=(r-a)*t,this.z=(n-i)*t}else if(s>o&&s>c){let t=2*Math.sqrt(1+s-o-c);this.w=(l-h)/t,this.x=.25*t,this.y=(i+n)/t,this.z=(r+a)/t}else if(o>c){let t=2*Math.sqrt(1+o-s-c);this.w=(r-a)/t,this.x=(i+n)/t,this.y=.25*t,this.z=(h+l)/t}else{let t=2*Math.sqrt(1+c-s-o);this.w=(n-i)/t,this.x=(r+a)/t,this.y=(h+l)/t,this.z=.25*t}return this}fromArray(t,e=0){return this.x=t[e+0],this.y=t[e+1],this.z=t[e+2],this.w=t[e+3],this}toArray(t,e=0){return t[e+0]=this.x,t[e+1]=this.y,t[e+2]=this.z,t[e+3]=this.w,t}equals(t){return t.x===this.x&&t.y===this.y&&t.z===this.z&&t.w===this.w}}class b{constructor(){this.elements=[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]}set(t,e,s,i,r,n,o,h,a,l,c,u,d,p,g,m){const f=this.elements;return f[0]=t,f[4]=e,f[8]=s,f[12]=i,f[1]=r,f[5]=n,f[9]=o,f[13]=h,f[2]=a,f[6]=l,f[10]=c,f[14]=u,f[3]=d,f[7]=p,f[11]=g,f[15]=m,this}copy(t){const e=this.elements,s=t.elements;return e[0]=s[0],e[1]=s[1],e[2]=s[2],e[3]=s[3],e[4]=s[4],e[5]=s[5],e[6]=s[6],e[7]=s[7],e[8]=s[8],e[9]=s[9],e[10]=s[10],e[11]=s[11],e[12]=s[12],e[13]=s[13],e[14]=s[14],e[15]=s[15],this}clone(){return(new this.constructor).copy(this)}identity(){return this.set(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1),this}multiply(t){return this.multiplyMatrices(this,t)}premultiply(t){return this.multiplyMatrices(t,this)}multiplyMatrices(t,e){const s=t.elements,i=e.elements,r=this.elements,n=s[0],o=s[4],h=s[8],a=s[12],l=s[1],c=s[5],u=s[9],d=s[13],p=s[2],g=s[6],m=s[10],f=s[14],y=s[3],x=s[7],w=s[11],S=s[15],v=i[0],b=i[4],_=i[8],M=i[12],z=i[1],O=i[5],A=i[9],N=i[13],T=i[2],J=i[6],D=i[10],P=i[14],E=i[3],R=i[7],F=i[11],k=i[15];return r[0]=n*v+o*z+h*T+a*E,r[4]=n*b+o*O+h*J+a*R,r[8]=n*_+o*A+h*D+a*F,r[12]=n*M+o*N+h*P+a*k,r[1]=l*v+c*z+u*T+d*E,r[5]=l*b+c*O+u*J+d*R,r[9]=l*_+c*A+u*D+d*F,r[13]=l*M+c*N+u*P+d*k,r[2]=p*v+g*z+m*T+f*E,r[6]=p*b+g*O+m*J+f*R,r[10]=p*_+g*A+m*D+f*F,r[14]=p*M+g*N+m*P+f*k,r[3]=y*v+x*z+w*T+S*E,r[7]=y*b+x*O+w*J+S*R,r[11]=y*_+x*A+w*D+S*F,r[15]=y*M+x*N+w*P+S*k,this}multiplyScalar(t){const e=this.elements;return e[0]*=t,e[4]*=t,e[8]*=t,e[12]*=t,e[1]*=t,e[5]*=t,e[9]*=t,e[13]*=t,e[2]*=t,e[6]*=t,e[10]*=t,e[14]*=t,e[3]*=t,e[7]*=t,e[11]*=t,e[15]*=t,this}extractBasis(t,e,s){return t.fromMatrix4Column(this,0),e.fromMatrix4Column(this,1),s.fromMatrix4Column(this,2),this}makeBasis(t,e,s){return this.set(t.x,e.x,s.x,0,t.y,e.y,s.y,0,t.z,e.z,s.z,0,0,0,0,1),this}compose(t,e,s){return this.fromQuaternion(e),this.scale(s),this.setPosition(t),this}scale(t){const e=this.elements,s=t.x,i=t.y,r=t.z;return e[0]*=s,e[4]*=i,e[8]*=r,e[1]*=s,e[5]*=i,e[9]*=r,e[2]*=s,e[6]*=i,e[10]*=r,e[3]*=s,e[7]*=i,e[11]*=r,this}setPosition(t){const e=this.elements;return e[12]=t.x,e[13]=t.y,e[14]=t.z,this}transpose(){const t=this.elements;let e;return e=t[1],t[1]=t[4],t[4]=e,e=t[2],t[2]=t[8],t[8]=e,e=t[6],t[6]=t[9],t[9]=e,e=t[3],t[3]=t[12],t[12]=e,e=t[7],t[7]=t[13],t[13]=e,e=t[11],t[11]=t[14],t[14]=e,this}getInverse(t){const e=this.elements,s=t.elements,i=e[0],r=e[1],n=e[2],o=e[3],h=e[4],a=e[5],l=e[6],c=e[7],u=e[8],d=e[9],p=e[10],g=e[11],m=e[12],f=e[13],y=e[14],x=e[15],w=d*y*c-f*p*c+f*l*g-a*y*g-d*l*x+a*p*x,S=m*p*c-u*y*c-m*l*g+h*y*g+u*l*x-h*p*x,v=u*f*c-m*d*c+m*a*g-h*f*g-u*a*x+h*d*x,b=m*d*l-u*f*l-m*a*p+h*f*p+u*a*y-h*d*y,_=i*w+r*S+n*v+o*b;if(0===_)return t.set(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);const M=1/_;return s[0]=w*M,s[1]=(f*p*o-d*y*o-f*n*g+r*y*g+d*n*x-r*p*x)*M,s[2]=(a*y*o-f*l*o+f*n*c-r*y*c-a*n*x+r*l*x)*M,s[3]=(d*l*o-a*p*o-d*n*c+r*p*c+a*n*g-r*l*g)*M,s[4]=S*M,s[5]=(u*y*o-m*p*o+m*n*g-i*y*g-u*n*x+i*p*x)*M,s[6]=(m*l*o-h*y*o-m*n*c+i*y*c+h*n*x-i*l*x)*M,s[7]=(h*p*o-u*l*o+u*n*c-i*p*c-h*n*g+i*l*g)*M,s[8]=v*M,s[9]=(m*d*o-u*f*o-m*r*g+i*f*g+u*r*x-i*d*x)*M,s[10]=(h*f*o-m*a*o+m*r*c-i*f*c-h*r*x+i*a*x)*M,s[11]=(u*a*o-h*d*o-u*r*c+i*d*c+h*r*g-i*a*g)*M,s[12]=b*M,s[13]=(u*f*n-m*d*n+m*r*p-i*f*p-u*r*y+i*d*y)*M,s[14]=(m*a*n-h*f*n-m*r*l+i*f*l+h*r*y-i*a*y)*M,s[15]=(h*d*n-u*a*n+u*r*l-i*d*l-h*r*p+i*a*p)*M,t}getMaxScale(){const t=this.elements,e=t[0]*t[0]+t[1]*t[1]+t[2]*t[2],s=t[4]*t[4]+t[5]*t[5]+t[6]*t[6],i=t[8]*t[8]+t[9]*t[9]+t[10]*t[10];return Math.sqrt(Math.max(e,s,i))}fromQuaternion(t){const e=this.elements,s=t.x,i=t.y,r=t.z,n=t.w,o=s+s,h=i+i,a=r+r,l=s*o,c=s*h,u=s*a,d=i*h,p=i*a,g=r*a,m=n*o,f=n*h,y=n*a;return e[0]=1-(d+g),e[4]=c-y,e[8]=u+f,e[1]=c+y,e[5]=1-(l+g),e[9]=p-m,e[2]=u-f,e[6]=p+m,e[10]=1-(l+d),e[3]=0,e[7]=0,e[11]=0,e[12]=0,e[13]=0,e[14]=0,e[15]=1,this}fromMatrix3(t){const e=this.elements,s=t.elements;return e[0]=s[0],e[1]=s[1],e[2]=s[2],e[3]=0,e[4]=s[3],e[5]=s[4],e[6]=s[5],e[7]=0,e[8]=s[6],e[9]=s[7],e[10]=s[8],e[11]=0,e[12]=0,e[13]=0,e[14]=0,e[15]=1,this}fromArray(t,e=0){const s=this.elements;for(let i=0;i<16;i++)s[i]=t[i+e];return this}toArray(t,e=0){const s=this.elements;return t[e+0]=s[0],t[e+1]=s[1],t[e+2]=s[2],t[e+3]=s[3],t[e+4]=s[4],t[e+5]=s[5],t[e+6]=s[6],t[e+7]=s[7],t[e+8]=s[8],t[e+9]=s[9],t[e+10]=s[10],t[e+11]=s[11],t[e+12]=s[12],t[e+13]=s[13],t[e+14]=s[14],t[e+15]=s[15],t}equals(t){const e=this.elements,s=t.elements;for(let t=0;t<16;t++)if(e[t]!==s[t])return!1;return!0}}const _=new v,M=new h,z=new h,O=new v;class A{constructor(){this.name="",this.active=!0,this.children=new Array,this.parent=null,this.neighbors=new Array,this.neighborhoodRadius=1,this.updateNeighborhood=!1,this.position=new h,this.rotation=new v,this.scale=new h(1,1,1),this.forward=new h(0,0,1),this.up=new h(0,1,0),this.boundingRadius=0,this.maxTurnRate=Math.PI,this.canActivateTrigger=!0,this.manager=null,this._localMatrix=new b,this._worldMatrix=new b,this._cache={position:new h,rotation:new v,scale:new h(1,1,1)},this._renderComponent=null,this._renderComponentCallback=null,this._started=!1,this._uuid=null,this._worldMatrixDirty=!1}get worldMatrix(){return this._updateWorldMatrix(),this._worldMatrix}get uuid(){return null===this._uuid&&(this._uuid=o.generateUUID()),this._uuid}start(){return this}update(){return this}add(t){return null!==t.parent&&t.parent.remove(t),this.children.push(t),t.parent=this,this}remove(t){const e=this.children.indexOf(t);return this.children.splice(e,1),t.parent=null,this}getDirection(t){return t.copy(this.forward).applyRotation(this.rotation).normalize()}lookAt(t){const e=this.parent;return null!==e?(this.getWorldPosition(z),M.subVectors(t,z).normalize(),this.rotation.lookAt(this.forward,M,this.up),O.extractRotationFromMatrix(e.worldMatrix).inverse(),this.rotation.premultiply(O)):(M.subVectors(t,this.position).normalize(),this.rotation.lookAt(this.forward,M,this.up)),this}rotateTo(t,e,s=1e-4){const i=this.parent;return null!==i?(this.getWorldPosition(z),M.subVectors(t,z).normalize(),_.lookAt(this.forward,M,this.up),O.extractRotationFromMatrix(i.worldMatrix).inverse(),_.premultiply(O)):(M.subVectors(t,this.position).normalize(),_.lookAt(this.forward,M,this.up)),this.rotation.rotateTo(_,this.maxTurnRate*e,s)}getWorldDirection(t){return O.extractRotationFromMatrix(this.worldMatrix),t.copy(this.forward).applyRotation(O).normalize()}getWorldPosition(t){return t.extractPositionFromMatrix(this.worldMatrix)}setRenderComponent(t,e){return this._renderComponent=t,this._renderComponentCallback=e,this}handleMessage(){return!1}lineOfSightTest(){return null}sendMessage(t,e,i=0,r=null){return null!==this.manager?this.manager.sendMessage(this,t,e,i,r):s.error("YUKA.GameEntity: The game entity must be added to a manager in order to send a message."),this}toJSON(){return{type:this.constructor.name,uuid:this.uuid,name:this.name,active:this.active,children:N(this.children),parent:null!==this.parent?this.parent.uuid:null,neighbors:N(this.neighbors),neighborhoodRadius:this.neighborhoodRadius,updateNeighborhood:this.updateNeighborhood,position:this.position.toArray(new Array),rotation:this.rotation.toArray(new Array),scale:this.scale.toArray(new Array),forward:this.forward.toArray(new Array),up:this.up.toArray(new Array),boundingRadius:this.boundingRadius,maxTurnRate:this.maxTurnRate,canActivateTrigger:this.canActivateTrigger,worldMatrix:this.worldMatrix.toArray(new Array),_localMatrix:this._localMatrix.toArray(new Array),_cache:{position:this._cache.position.toArray(new Array),rotation:this._cache.rotation.toArray(new Array),scale:this._cache.scale.toArray(new Array)},_started:this._started}}fromJSON(t){return this.name=t.name,this.active=t.active,this.neighborhoodRadius=t.neighborhoodRadius,this.updateNeighborhood=t.updateNeighborhood,this.position.fromArray(t.position),this.rotation.fromArray(t.rotation),this.scale.fromArray(t.scale),this.forward.fromArray(t.forward),this.up.fromArray(t.up),this.boundingRadius=t.boundingRadius,this.maxTurnRate=t.maxTurnRate,this.canActivateTrigger=t.canActivateTrigger,this.children=t.children.slice(),this.neighbors=t.neighbors.slice(),this.parent=t.parent,this._localMatrix.fromArray(t._localMatrix),this._worldMatrix.fromArray(t.worldMatrix),this._cache.position.fromArray(t._cache.position),this._cache.rotation.fromArray(t._cache.rotation),this._cache.scale.fromArray(t._cache.scale),this._started=t._started,this._uuid=t.uuid,this}resolveReferences(t){const e=this.neighbors;for(let s=0,i=e.length;s<i;s++)e[s]=t.get(e[s]);const s=this.children;for(let e=0,i=s.length;e<i;e++)s[e]=t.get(s[e]);return this.parent=t.get(this.parent)||null,this}_updateMatrix(){const t=this._cache;t.position.equals(this.position)&&t.rotation.equals(this.rotation)&&t.scale.equals(this.scale)||(this._localMatrix.compose(this.position,this.rotation,this.scale),t.position.copy(this.position),t.rotation.copy(this.rotation),t.scale.copy(this.scale),this._worldMatrixDirty=!0)}_updateWorldMatrix(){const t=this.parent;if(null!==t&&t._updateWorldMatrix(),this._updateMatrix(),!0===this._worldMatrixDirty){null===t?this._worldMatrix.copy(this._localMatrix):this._worldMatrix.multiplyMatrices(this.parent._worldMatrix,this._localMatrix),this._worldMatrixDirty=!1;const e=this.children;for(let t=0,s=e.length;t<s;t++){e[t]._worldMatrixDirty=!0}}}updateWorldMatrix(){return console.warn("GameEntity: .updateWorldMatrix() has been removed. World matrices are automatically updated on access."),this}}function N(t){const e=new Array;for(let s=0,i=t.length;s<i;s++)e.push(t[s].uuid);return e}const T=new h,J=new h;class D extends A{constructor(){super(),this.velocity=new h,this.maxSpeed=1,this.updateOrientation=!0}update(t){return this.getSpeedSquared()>this.maxSpeed*this.maxSpeed&&(this.velocity.normalize(),this.velocity.multiplyScalar(this.maxSpeed)),T.copy(this.velocity).multiplyScalar(t),J.copy(this.position).add(T),this.updateOrientation&&this.getSpeedSquared()>1e-8&&this.lookAt(J),this.position.copy(J),this}getSpeed(){return this.velocity.length()}getSpeedSquared(){return this.velocity.squaredLength()}toJSON(){const t=super.toJSON();return t.velocity=this.velocity.toArray(new Array),t.maxSpeed=this.maxSpeed,t.updateOrientation=this.updateOrientation,t}fromJSON(t){return super.fromJSON(t),this.velocity.fromArray(t.velocity),this.maxSpeed=t.maxSpeed,this.updateOrientation=t.updateOrientation,this}}class P{constructor(){this.active=!0,this.weight=1}calculate(){}toJSON(){return{type:this.constructor.name,active:this.active,weight:this.weight}}fromJSON(t){return this.active=t.active,this.weight=t.weight,this}resolveReferences(){}}const E=new h,R=new h;class F extends P{constructor(){super()}calculate(t,e){E.set(0,0,0);const s=t.neighbors;for(let t=0,e=s.length;t<e;t++){s[t].getDirection(R),E.add(R)}return s.length>0&&(E.divideScalar(s.length),t.getDirection(R),e.subVectors(E,R)),e}}const k=new h,V=new h;class C extends P{constructor(t=new h,e=3,s=0){super(),this.target=t,this.deceleration=e,this.tolerance=s}calculate(t,e){const s=this.target,i=this.deceleration;V.subVectors(s,t.position);const r=V.length();if(r>this.tolerance){let e=r/i;e=Math.min(e,t.maxSpeed),k.copy(V).multiplyScalar(e/r)}else k.set(0,0,0);return e.subVectors(k,t.velocity)}toJSON(){const t=super.toJSON();return t.target=this.target.toArray(new Array),t.deceleration=this.deceleration,t}fromJSON(t){return super.fromJSON(t),this.target.fromArray(t.target),this.deceleration=t.deceleration,this}}const B=new h;class q extends P{constructor(t=new h){super(),this.target=t}calculate(t,e){const s=this.target;return B.subVectors(s,t.position).normalize(),B.multiplyScalar(t.maxSpeed),e.subVectors(B,t.velocity)}toJSON(){const t=super.toJSON();return t.target=this.target.toArray(new Array),t}fromJSON(t){return super.fromJSON(t),this.target.fromArray(t.target),this}}const I=new h;class U extends P{constructor(){super(),this._seek=new q}calculate(t,e){I.set(0,0,0);const s=t.neighbors;for(let t=0,e=s.length;t<e;t++){const e=s[t];I.add(e.position)}return s.length>0&&(I.divideScalar(s.length),this._seek.target=I,this._seek.calculate(t,e),e.normalize()),e}}const L=new h;class Y extends P{constructor(t=new h,e=10){super(),this.target=t,this.panicDistance=e}calculate(t,e){const s=this.target;return t.position.squaredDistanceTo(s)<=this.panicDistance*this.panicDistance&&(L.subVectors(t.position,s).normalize(),0===L.squaredLength()&&L.set(0,0,1),L.multiplyScalar(t.maxSpeed),e.subVectors(L,t.velocity)),e}toJSON(){const t=super.toJSON();return t.target=this.target.toArray(new Array),t.panicDistance=this.panicDistance,t}fromJSON(t){return super.fromJSON(t),this.target.fromArray(t.target),this.panicDistance=t.panicDistance,this}}const H=new h,W=new h,j=new h;class K extends P{constructor(t=null,e=10,s=1){super(),this.pursuer=t,this.panicDistance=e,this.predictionFactor=s,this._flee=new Y}calculate(t,e){const s=this.pursuer;H.subVectors(s.position,t.position);let i=H.length()/(t.maxSpeed+s.getSpeed());return i*=this.predictionFactor,W.copy(s.velocity).multiplyScalar(i),j.addVectors(s.position,W),this._flee.target=j,this._flee.panicDistance=this.panicDistance,this._flee.calculate(t,e),e}toJSON(){const t=super.toJSON();return t.pursuer=this.pursuer?this.pursuer.uuid:null,t.panicDistance=this.panicDistance,t.predictionFactor=this.predictionFactor,t}fromJSON(t){return super.fromJSON(t),this.pursuer=t.pursuer,this.panicDistance=t.panicDistance,this.predictionFactor=t.predictionFactor,this}resolveReferences(t){this.pursuer=t.get(this.pursuer)||null}}class G{constructor(){this.loop=!1,this._waypoints=new Array,this._index=0}add(t){return this._waypoints.push(t),this}clear(){return this._waypoints.length=0,this._index=0,this}current(){return this._waypoints[this._index]}finished(){const t=this._waypoints.length-1;return!0!==this.loop&&this._index===t}advance(){return this._index++,this._index===this._waypoints.length&&(!0===this.loop?this._index=0:this._index--),this}toJSON(){const t={type:this.constructor.name,loop:this.loop,_waypoints:new Array,_index:this._index},e=this._waypoints;for(let s=0,i=e.length;s<i;s++){const i=e[s];t._waypoints.push(i.toArray(new Array))}return t}fromJSON(t){this.loop=t.loop,this._index=t._index;const e=t._waypoints;for(let t=0,s=e.length;t<s;t++){const s=e[t];this._waypoints.push((new h).fromArray(s))}return this}}class Z extends P{constructor(t=new G,e=1){super(),this.path=t,this.nextWaypointDistance=e,this._arrive=new C,this._seek=new q}calculate(t,e){const s=this.path;s.current().squaredDistanceTo(t.position)<this.nextWaypointDistance*this.nextWaypointDistance&&s.advance();const i=s.current();return!0===s.finished()?(this._arrive.target=i,this._arrive.calculate(t,e)):(this._seek.target=i,this._seek.calculate(t,e)),e}toJSON(){const t=super.toJSON();return t.path=this.path.toJSON(),t.nextWaypointDistance=this.nextWaypointDistance,t}fromJSON(t){return super.fromJSON(t),this.path.fromJSON(t.path),this.nextWaypointDistance=t.nextWaypointDistance,this}}const X=new h,Q=new h,$=new h,tt=new h;class et extends P{constructor(t=null,e=null,s=3){super(),this.entity1=t,this.entity2=e,this.deceleration=s,this._arrive=new C}calculate(t,e){const s=this.entity1,i=this.entity2;X.addVectors(s.position,i.position).multiplyScalar(.5);const r=t.position.distanceTo(X)/t.maxSpeed;return Q.copy(s.velocity).multiplyScalar(r),$.addVectors(s.position,Q),Q.copy(i.velocity).multiplyScalar(r),tt.addVectors(i.position,Q),X.addVectors($,tt).multiplyScalar(.5),this._arrive.deceleration=this.deceleration,this._arrive.target=X,this._arrive.calculate(t,e),e}toJSON(){const t=super.toJSON();return t.entity1=this.entity1?this.entity1.uuid:null,t.entity2=this.entity2?this.entity2.uuid:null,t.deceleration=this.deceleration,t}fromJSON(t){return super.fromJSON(t),this.entity1=t.entity1,this.entity2=t.entity2,this.deceleration=t.deceleration,this}resolveReferences(t){this.entity1=t.get(this.entity1)||null,this.entity2=t.get(this.entity2)||null}}const st=new h,it=new h,rt=new h,nt=[new h,new h,new h,new h,new h,new h,new h,new h];class ot{constructor(t=new h,e=new h){this.min=t,this.max=e}set(t,e){return this.min=t,this.max=e,this}copy(t){return this.min.copy(t.min),this.max.copy(t.max),this}clone(){return(new this.constructor).copy(this)}clampPoint(t,e){return e.copy(t).clamp(this.min,this.max),e}containsPoint(t){return!(t.x<this.min.x||t.x>this.max.x||t.y<this.min.y||t.y>this.max.y||t.z<this.min.z||t.z>this.max.z)}expand(t){return this.min.min(t),this.max.max(t),this}getCenter(t){return t.addVectors(this.min,this.max).multiplyScalar(.5)}getSize(t){return t.subVectors(this.max,this.min)}intersectsAABB(t){return!(t.max.x<this.min.x||t.min.x>this.max.x||t.max.y<this.min.y||t.min.y>this.max.y||t.max.z<this.min.z||t.min.z>this.max.z)}intersectsBoundingSphere(t){return this.clampPoint(t.center,st),st.squaredDistanceTo(t.center)<=t.radius*t.radius}intersectsPlane(t){const e=t.normal;this.getCenter(it),rt.subVectors(this.max,it);const s=rt.x*Math.abs(e.x)+rt.y*Math.abs(e.y)+rt.z*Math.abs(e.z),i=t.distanceToPoint(it);return Math.abs(i)<=s}getNormalFromSurfacePoint(t,e){let s;e.set(0,0,0);let i=1/0;return this.getCenter(it),this.getSize(rt),st.copy(t).sub(it),s=Math.abs(rt.x-Math.abs(st.x)),s<i&&(i=s,e.set(1*Math.sign(st.x),0,0)),s=Math.abs(rt.y-Math.abs(st.y)),s<i&&(i=s,e.set(0,1*Math.sign(st.y),0)),s=Math.abs(rt.z-Math.abs(st.z)),s<i&&e.set(0,0,1*Math.sign(st.z)),e}fromCenterAndSize(t,e){return st.copy(e).multiplyScalar(.5),this.min.copy(t).sub(st),this.max.copy(t).add(st),this}fromPoints(t){this.min.set(1/0,1/0,1/0),this.max.set(-1/0,-1/0,-1/0);for(let e=0,s=t.length;e<s;e++)this.expand(t[e]);return this}applyMatrix4(t){const e=this.min,s=this.max;return nt[0].set(e.x,e.y,e.z).applyMatrix4(t),nt[1].set(e.x,e.y,s.z).applyMatrix4(t),nt[2].set(e.x,s.y,e.z).applyMatrix4(t),nt[3].set(e.x,s.y,s.z).applyMatrix4(t),nt[4].set(s.x,e.y,e.z).applyMatrix4(t),nt[5].set(s.x,e.y,s.z).applyMatrix4(t),nt[6].set(s.x,s.y,e.z).applyMatrix4(t),nt[7].set(s.x,s.y,s.z).applyMatrix4(t),this.fromPoints(nt)}equals(t){return t.min.equals(this.min)&&t.max.equals(this.max)}toJSON(){return{type:this.constructor.name,min:this.min.toArray(new Array),max:this.max.toArray(new Array)}}fromJSON(t){return this.min.fromArray(t.min),this.max.fromArray(t.max),this}}const ht=new ot;class at{constructor(t=new h,e=0){this.center=t,this.radius=e}set(t,e){return this.center=t,this.radius=e,this}copy(t){return this.center.copy(t.center),this.radius=t.radius,this}clone(){return(new this.constructor).copy(this)}clampPoint(t,e){e.copy(t);return this.center.squaredDistanceTo(t)>this.radius*this.radius&&(e.sub(this.center).normalize(),e.multiplyScalar(this.radius).add(this.center)),e}containsPoint(t){return t.squaredDistanceTo(this.center)<=this.radius*this.radius}intersectsBoundingSphere(t){const e=this.radius+t.radius;return t.center.squaredDistanceTo(this.center)<=e*e}intersectsPlane(t){return Math.abs(t.distanceToPoint(this.center))<=this.radius}getNormalFromSurfacePoint(t,e){return e.subVectors(t,this.center).normalize()}fromPoints(t){return ht.fromPoints(t),ht.getCenter(this.center),this.radius=this.center.distanceTo(ht.max),this}applyMatrix4(t){return this.center.applyMatrix4(t),this.radius=this.radius*t.getMaxScale(),this}equals(t){return t.center.equals(this.center)&&t.radius===this.radius}toJSON(){return{type:this.constructor.name,center:this.center.toArray(new Array),radius:this.radius}}fromJSON(t){return this.center.fromArray(t.center),this.radius=t.radius,this}}const lt=new h,ct=new h,ut=new h,dt=new h,pt=new h,gt=new b,mt=new b,ft=new ot;class yt{constructor(t=new h,e=new h){this.origin=t,this.direction=e}set(t,e){return this.origin=t,this.direction=e,this}copy(t){return this.origin.copy(t.origin),this.direction.copy(t.direction),this}clone(){return(new this.constructor).copy(this)}at(t,e){return e.copy(this.direction).multiplyScalar(t).add(this.origin)}intersectBoundingSphere(t,e){lt.subVectors(t.center,this.origin);const s=lt.dot(this.direction),i=lt.dot(lt)-s*s,r=t.radius*t.radius;if(i>r)return null;const n=Math.sqrt(r-i),o=s-n,h=s+n;return o<0&&h<0?null:o<0?this.at(h,e):this.at(o,e)}intersectsBoundingSphere(t){const e=new h;let s;const i=e.subVectors(t.center,this.origin).dot(this.direction);return i<0?s=this.origin.squaredDistanceTo(t.center):(e.copy(this.direction).multiplyScalar(i).add(this.origin),s=e.squaredDistanceTo(t.center)),s<=t.radius*t.radius}intersectAABB(t,e){let s,i,r,n,o,h;const a=1/this.direction.x,l=1/this.direction.y,c=1/this.direction.z,u=this.origin;return a>=0?(s=(t.min.x-u.x)*a,i=(t.max.x-u.x)*a):(s=(t.max.x-u.x)*a,i=(t.min.x-u.x)*a),l>=0?(r=(t.min.y-u.y)*l,n=(t.max.y-u.y)*l):(r=(t.max.y-u.y)*l,n=(t.min.y-u.y)*l),s>n||r>i?null:((r>s||s!=s)&&(s=r),(n<i||i!=i)&&(i=n),c>=0?(o=(t.min.z-u.z)*c,h=(t.max.z-u.z)*c):(o=(t.max.z-u.z)*c,h=(t.min.z-u.z)*c),s>h||o>i?null:((o>s||s!=s)&&(s=o),(h<i||i!=i)&&(i=h),i<0?null:this.at(s>=0?s:i,e)))}intersectsAABB(t){return null!==this.intersectAABB(t,lt)}intersectPlane(t,e){let s;const i=t.normal.dot(this.direction);if(0===i){if(0!==t.distanceToPoint(this.origin))return null;s=0}else s=-(this.origin.dot(t.normal)+t.constant)/i;return s>=0?this.at(s,e):null}intersectsPlane(t){const e=t.distanceToPoint(this.origin);if(0===e)return!0;return t.normal.dot(this.direction)*e<0}intersectOBB(t,e){return t.getSize(pt),ft.fromCenterAndSize(lt.set(0,0,0),pt),gt.fromMatrix3(t.rotation),gt.setPosition(t.center),xt.copy(this).applyMatrix4(gt.getInverse(mt)),xt.intersectAABB(ft,e)?e.applyMatrix4(gt):null}intersectsOBB(t){return null!==this.intersectOBB(t,lt)}intersectConvexHull(t,e){const s=t.faces;let i=-1/0,r=1/0;for(let t=0,e=s.length;t<e;t++){const e=s[t].plane,n=e.distanceToPoint(this.origin),o=e.normal.dot(this.direction);if(n>0&&o>=0)return null;const h=0!==o?-n/o:0;if(!(h<=0)&&(o>0?r=Math.min(h,r):i=Math.max(h,i),i>r))return null}return i!==-1/0?this.at(i,e):this.at(r,e),e}intersectsConvexHull(t){return null!==this.intersectConvexHull(t,lt)}intersectTriangle(t,e,s){const i=t.a,r=t.b,n=t.c;ct.subVectors(r,i),ut.subVectors(n,i),dt.crossVectors(ct,ut);let o,h=this.direction.dot(dt);if(h>0){if(e)return null;o=1}else{if(!(h<0))return null;o=-1,h=-h}lt.subVectors(this.origin,i);const a=o*this.direction.dot(ut.crossVectors(lt,ut));if(a<0)return null;const l=o*this.direction.dot(ct.cross(lt));if(l<0)return null;if(a+l>h)return null;const c=-o*lt.dot(dt);return c<0?null:this.at(c/h,s)}intersectBVH(t,e){return t.root.intersectRay(this,e)}intersectsBVH(t){return t.root.intersectsRay(this)}applyMatrix4(t){return this.origin.applyMatrix4(t),this.direction.transformDirection(t),this}equals(t){return t.origin.equals(this.origin)&&t.direction.equals(this.direction)}}const xt=new yt,wt=new b,St=new h,vt=new h,bt=new h,_t=new at,Mt=new yt(new h(0,0,0),new h(0,0,1));class zt extends P{constructor(t=new Array){super(),this.obstacles=t,this.brakingWeight=.2,this.dBoxMinLength=4}calculate(t,e){const s=this.obstacles;let i=null,r=1/0;const n=this.dBoxMinLength+t.getSpeed()/t.maxSpeed*this.dBoxMinLength;t.worldMatrix.getInverse(wt);for(let e=0,o=s.length;e<o;e++){const o=s[e];if(o!==t&&(St.copy(o.position).applyMatrix4(wt),St.z>0&&Math.abs(St.z)<n)){const e=o.boundingRadius+t.boundingRadius;Math.abs(St.x)<e&&(_t.center.copy(St),_t.radius=e,Mt.intersectBoundingSphere(_t,bt),bt.z<r&&(r=bt.z,i=o,vt.copy(St)))}}if(null!==i){const s=1+(n-vt.z)/n;e.x=(i.boundingRadius-vt.x)*s,e.z=(i.boundingRadius-vt.z)*this.brakingWeight,e.applyRotation(t.rotation)}return e}toJSON(){const t=super.toJSON();t.obstacles=new Array,t.brakingWeight=this.brakingWeight,t.dBoxMinLength=this.dBoxMinLength;for(let e=0,s=this.obstacles.length;e<s;e++)t.obstacles.push(this.obstacles[e].uuid);return t}fromJSON(t){return super.fromJSON(t),this.obstacles=t.obstacles,this.brakingWeight=t.brakingWeight,this.dBoxMinLength=t.dBoxMinLength,this}resolveReferences(t){const e=this.obstacles;for(let s=0,i=e.length;s<i;s++)e[s]=t.get(e[s])}}const Ot=new h,At=new h,Nt=new h,Tt=new h;class Jt extends P{constructor(t=null,e=new h){super(),this.leader=t,this.offset=e,this._arrive=new C,this._arrive.deceleration=1.5}calculate(t,e){const s=this.leader,i=this.offset;Ot.copy(i).applyMatrix4(s.worldMatrix),At.subVectors(Ot,t.position);const r=At.length()/(t.maxSpeed+s.getSpeed());return Nt.copy(s.velocity).multiplyScalar(r),Tt.addVectors(Ot,Nt),this._arrive.target=Tt,this._arrive.calculate(t,e),e}toJSON(){const t=super.toJSON();return t.leader=this.leader?this.leader.uuid:null,t.offset=this.offset,t}fromJSON(t){return super.fromJSON(t),this.leader=t.leader,this.offset=t.offset,this}resolveReferences(t){this.leader=t.get(this.leader)||null}}const Dt=new h,Pt=new h,Et=new h,Rt=new h,Ft=new h;class kt extends P{constructor(t=null,e=1){super(),this.evader=t,this.predictionFactor=e,this._seek=new q}calculate(t,e){const s=this.evader;Dt.subVectors(s.position,t.position),t.getDirection(Pt),s.getDirection(Et);const i=Dt.dot(Pt)>0,r=Pt.dot(Et)<-.95;if(!0===i&&!0===r)return this._seek.target=s.position,this._seek.calculate(t,e),e;let n=Dt.length()/(t.maxSpeed+s.getSpeed());return n*=this.predictionFactor,Rt.copy(s.velocity).multiplyScalar(n),Ft.addVectors(s.position,Rt),this._seek.target=Ft,this._seek.calculate(t,e),e}toJSON(){const t=super.toJSON();return t.evader=this.evader?this.evader.uuid:null,t.predictionFactor=this.predictionFactor,t}fromJSON(t){return super.fromJSON(t),this.evader=t.evader,this.predictionFactor=t.predictionFactor,this}resolveReferences(t){this.evader=t.get(this.evader)||null}}const Vt=new h;class Ct extends P{constructor(){super()}calculate(t,e){const s=t.neighbors;for(let i=0,r=s.length;i<r;i++){const r=s[i];Vt.subVectors(t.position,r.position);let n=Vt.length();0===n&&(n=1e-4),Vt.normalize().divideScalar(n),e.add(Vt)}return e}}const Bt=new h,qt=new h;class It extends P{constructor(t=1,e=5,s=5){super(),this.radius=t,this.distance=e,this.jitter=s,this._targetLocal=new h,function(t,e){const s=Math.random()*Math.PI*2;e.x=t*Math.cos(s),e.z=t*Math.sin(s)}(this.radius,this._targetLocal)}calculate(t,e,s){const i=this.jitter*s;return qt.x=o.randFloat(-1,1)*i,qt.z=o.randFloat(-1,1)*i,this._targetLocal.add(qt),this._targetLocal.normalize(),this._targetLocal.multiplyScalar(this.radius),Bt.copy(this._targetLocal),Bt.z+=this.distance,Bt.applyMatrix4(t.worldMatrix),e.subVectors(Bt,t.position),e}toJSON(){const t=super.toJSON();return t.radius=this.radius,t.distance=this.distance,t.jitter=this.jitter,t._targetLocal=this._targetLocal.toArray(new Array),t}fromJSON(t){return super.fromJSON(t),this.radius=t.radius,this.distance=t.distance,this.jitter=t.jitter,this._targetLocal.fromArray(t._targetLocal),this}}const Ut=new h;class Lt{constructor(t){this.vehicle=t,this.behaviors=new Array,this._steeringForce=new h,this._typesMap=new Map}add(t){return this.behaviors.push(t),this}remove(t){const e=this.behaviors.indexOf(t);return this.behaviors.splice(e,1),this}clear(){return this.behaviors.length=0,this}calculate(t,e){return this._calculateByOrder(t),e.copy(this._steeringForce)}_accumulate(t){const e=this._steeringForce.length(),s=this.vehicle.maxForce-e;if(s<=0)return!1;return t.length()>s&&t.normalize().multiplyScalar(s),this._steeringForce.add(t),!0}_calculateByOrder(t){const e=this.behaviors;this._steeringForce.set(0,0,0);for(let s=0,i=e.length;s<i;s++){const i=e[s];if(!0===i.active&&(Ut.set(0,0,0),i.calculate(this.vehicle,Ut,t),Ut.multiplyScalar(i.weight),!1===this._accumulate(Ut)))return}}toJSON(){const t={type:"SteeringManager",behaviors:new Array},e=this.behaviors;for(let s=0,i=e.length;s<i;s++){const i=e[s];t.behaviors.push(i.toJSON())}return t}fromJSON(t){this.clear();const e=t.behaviors;for(let t=0,i=e.length;t<i;t++){const i=e[t],r=i.type;let n;switch(r){case"SteeringBehavior":n=(new P).fromJSON(i);break;case"AlignmentBehavior":n=(new F).fromJSON(i);break;case"ArriveBehavior":n=(new C).fromJSON(i);break;case"CohesionBehavior":n=(new U).fromJSON(i);break;case"EvadeBehavior":n=(new K).fromJSON(i);break;case"FleeBehavior":n=(new Y).fromJSON(i);break;case"FollowPathBehavior":n=(new Z).fromJSON(i);break;case"InterposeBehavior":n=(new et).fromJSON(i);break;case"ObstacleAvoidanceBehavior":n=(new zt).fromJSON(i);break;case"OffsetPursuitBehavior":n=(new Jt).fromJSON(i);break;case"PursuitBehavior":n=(new kt).fromJSON(i);break;case"SeekBehavior":n=(new q).fromJSON(i);break;case"SeparationBehavior":n=(new Ct).fromJSON(i);break;case"WanderBehavior":n=(new It).fromJSON(i);break;default:const t=this._typesMap.get(r);if(void 0===t){s.warn("YUKA.SteeringManager: Unsupported steering behavior type:",r);continue}n=(new t).fromJSON(i)}this.add(n)}return this}registerType(t,e){return this._typesMap.set(t,e),this}resolveReferences(t){const e=this.behaviors;for(let s=0,i=e.length;s<i;s++){e[s].resolveReferences(t)}return this}}class Yt{constructor(t=10){this.count=t,this._history=new Array,this._slot=0;for(let t=0;t<this.count;t++)this._history[t]=new h}calculate(t,e){e.set(0,0,0),this._slot===this.count&&(this._slot=0),this._history[this._slot].copy(t),this._slot++;for(let t=0;t<this.count;t++)e.add(this._history[t]);return e.divideScalar(this.count),e}toJSON(){const t={type:this.constructor.name,count:this.count,_history:new Array,_slot:this._slot},e=this._history;for(let s=0,i=e.length;s<i;s++){const i=e[s];t._history.push(i.toArray(new Array))}return t}fromJSON(t){this.count=t.count,this._slot=t._slot;const e=t._history;this._history.length=0;for(let t=0,s=e.length;t<s;t++){const s=e[t];this._history.push((new h).fromArray(s))}return this}}const Ht=new h,Wt=new h,jt=new h,Kt=new h,Gt=new h;class Zt extends D{constructor(){super(),this.mass=1,this.maxForce=100,this.steering=new Lt(this),this.smoother=null}update(t){return this.steering.calculate(t,Ht),jt.copy(Ht).divideScalar(this.mass),this.velocity.add(jt.multiplyScalar(t)),this.getSpeedSquared()>this.maxSpeed*this.maxSpeed&&(this.velocity.normalize(),this.velocity.multiplyScalar(this.maxSpeed)),Wt.copy(this.velocity).multiplyScalar(t),Kt.copy(this.position).add(Wt),!0===this.updateOrientation&&null===this.smoother&&this.getSpeedSquared()>1e-8&&this.lookAt(Kt),this.position.copy(Kt),!0===this.updateOrientation&&null!==this.smoother&&(this.smoother.calculate(this.velocity,Gt),Wt.copy(Gt).multiplyScalar(t),Kt.copy(this.position).add(Wt),this.lookAt(Kt)),this}toJSON(){const t=super.toJSON();return t.mass=this.mass,t.maxForce=this.maxForce,t.steering=this.steering.toJSON(),t.smoother=this.smoother?this.smoother.toJSON():null,t}fromJSON(t){return super.fromJSON(t),this.mass=t.mass,this.maxForce=t.maxForce,this.steering=new Lt(this).fromJSON(t.steering),this.smoother=t.smoother?(new Yt).fromJSON(t.smoother):null,this}resolveReferences(t){super.resolveReferences(t),this.steering.resolveReferences(t)}}class Xt{touching(){return!1}update(){return this}toJSON(){return{type:this.constructor.name}}fromJSON(){return this}}const Qt=new at,$t=new h;class te extends Xt{constructor(t=new h){super(),this.size=t,this._aabb=new ot}touching(t){return Qt.set(t.position,t.boundingRadius),this._aabb.intersectsBoundingSphere(Qt)}update(t){return t.getWorldPosition($t),this._aabb.fromCenterAndSize($t,this.size),this}toJSON(){const t=super.toJSON();return t.size=this.size.toArray(new Array),t}fromJSON(t){return super.fromJSON(t),this.size.fromArray(t.size),this}}const ee=new at;class se extends Xt{constructor(t=0){super(),this.radius=t,this._boundingSphere=new at}touching(t){return t.getWorldPosition(ee.center),ee.radius=t.boundingRadius,this._boundingSphere.intersectsBoundingSphere(ee)}update(t){return t.getWorldPosition(this._boundingSphere.center),this._boundingSphere.radius=this.radius,this}toJSON(){const t=super.toJSON();return t.radius=this.radius,t}fromJSON(t){return super.fromJSON(t),this.radius=t.radius,this}}class ie extends A{constructor(t=new Xt){super(),this.region=t,this.canActivateTrigger=!1,this._typesMap=new Map}check(t){return!0===this.region.touching(t)&&this.execute(t),this}execute(){}updateRegion(){return this.region.update(this),this}toJSON(){const t=super.toJSON();return t.region=this.region.toJSON(),t}fromJSON(t){super.fromJSON(t);const e=t.region;let i=e.type;switch(i){case"TriggerRegion":this.region=(new Xt).fromJSON(e);break;case"RectangularTriggerRegion":this.region=(new te).fromJSON(e);break;case"SphericalTriggerRegion":this.region=(new se).fromJSON(e);break;default:const t=this._typesMap.get(i);void 0!==t?this.region=(new t).fromJSON(e):s.warn("YUKA.Trigger: Unsupported trigger region type:",e.type)}return this}registerType(t,e){return this._typesMap.set(t,e),this}}const re=new Array;const ne=new h,oe=new h,he=new h;class ae{constructor(t=new h(0,0,1),e=0){this.normal=t,this.constant=e}set(t,e){return this.normal=t,this.constant=e,this}copy(t){return this.normal.copy(t.normal),this.constant=t.constant,this}clone(){return(new this.constructor).copy(this)}distanceToPoint(t){return this.normal.dot(t)+this.constant}fromNormalAndCoplanarPoint(t,e){return this.normal.copy(t),this.constant=-e.dot(this.normal),this}fromCoplanarPoints(t,e,s){return ne.subVectors(s,e).cross(oe.subVectors(t,e)).normalize(),this.fromNormalAndCoplanarPoint(ne,t),this}intersectPlane(t,e){he.crossVectors(this.normal,t.normal);const s=he.dot(he);return 0===s?null:(ne.copy(t.normal).multiplyScalar(this.constant),oe.copy(this.normal).multiplyScalar(t.constant),e.crossVectors(ne.sub(oe),he).divideScalar(s),e)}intersectsPlane(t){const e=this.normal.dot(t.normal);return 1!==Math.abs(e)}projectPoint(t,e){return ne.copy(this.normal).multiplyScalar(this.distanceToPoint(t)),e.subVectors(t,ne),e}equals(t){return t.normal.equals(this.normal)&&t.constant===this.constant}}const le=new at,ce={a:new h,b:new h,c:new h},ue=new yt,de=new ae,pe=new b,ge=new h,me={a:new h,b:new h,c:new h};class fe{constructor(t=new Float32Array,e=null){this.vertices=t,this.indices=e,this.backfaceCulling=!0,this.aabb=new ot,this.boundingSphere=new at,this.computeBoundingVolume()}computeBoundingVolume(){const t=this.vertices,e=new h,s=this.aabb,i=this.boundingSphere;s.min.set(1/0,1/0,1/0),s.max.set(-1/0,-1/0,-1/0);for(let i=0,r=t.length;i<r;i+=3)e.x=t[i],e.y=t[i+1],e.z=t[i+2],s.expand(e);return s.getCenter(i.center),i.radius=i.center.distanceTo(s.max),this}intersectRay(t,e,s,i,r=null){if(le.copy(this.boundingSphere).applyMatrix4(e),t.intersectsBoundingSphere(le)&&(e.getInverse(pe),ue.copy(t).applyMatrix4(pe),ue.intersectsAABB(this.aabb))){const t=this.vertices,n=this.indices;let o=1/0,h=!1;if(null===n){for(let e=0,r=t.length;e<r;e+=9)if(ce.a.set(t[e],t[e+1],t[e+2]),ce.b.set(t[e+3],t[e+4],t[e+5]),ce.c.set(t[e+6],t[e+7],t[e+8]),null!==ue.intersectTriangle(ce,this.backfaceCulling,i)){if(!s){h=!0;break}{const t=i.squaredDistanceTo(ue.origin);t<o&&(o=t,ge.copy(i),me.a.copy(ce.a),me.b.copy(ce.b),me.c.copy(ce.c),h=!0)}}}else for(let e=0,r=n.length;e<r;e+=3){const r=n[e],a=n[e+1],l=n[e+2],c=3;if(ce.a.set(t[r*c],t[r*c+1],t[r*c+2]),ce.b.set(t[a*c],t[a*c+1],t[a*c+2]),ce.c.set(t[l*c],t[l*c+1],t[l*c+2]),null!==ue.intersectTriangle(ce,this.backfaceCulling,i)){if(!s){h=!0;break}{const t=i.squaredDistanceTo(ue.origin);t<o&&(o=t,ge.copy(i),me.a.copy(ce.a),me.b.copy(ce.b),me.c.copy(ce.c),h=!0)}}}if(h)return s&&(i.copy(ge),ce.a.copy(me.a),ce.b.copy(me.b),ce.c.copy(me.c)),i.applyMatrix4(e),null!==r&&(de.fromCoplanarPoints(ce.a,ce.b,ce.c),r.copy(de.normal),r.transformDirection(e)),i}return null}toTriangleSoup(){const t=this.indices;if(t){const e=this.vertices,s=new Float32Array(3*t.length);for(let i=0,r=t.length;i<r;i++){const r=t[i],n=3;s[i*n]=e[r*n],s[i*n+1]=e[r*n+1],s[i*n+2]=e[r*n+2]}retur