@xeokit/xeokit-sdk 2.4.0-alpha-68 → 2.4.0-alpha-69
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});class e{constructor(e,t){this.items=e||[],this._lastUniqueId=(t||0)+1}addItem(){let e;if(2===arguments.length){const t=arguments[0];if(e=arguments[1],this.items[t])throw"ID clash: '"+t+"'";return this.items[t]=e,t}for(e=arguments[0]||{};;){const t=this._lastUniqueId++;if(!this.items[t])return this.items[t]=e,t}}removeItem(e){const t=this.items[e];return delete this.items[e],t}}const t=new e;class s{constructor(e){this.id=e,this.parentItem=null,this.groups=[],this.menuElement=null,this.shown=!1,this.mouseOver=0}}class n{constructor(){this.items=[]}}class i{constructor(e,t,s,n,i){this.id=e,this.getTitle=t,this.doAction=s,this.getEnabled=n,this.getShown=i,this.itemElement=null,this.subMenu=null,this.enabled=!0}}class a{constructor(e,t,s){this.id=s&&s.id?s.id:e,this.viewer=t,this._subIdMap=null,this._subIdEvents=null,this._eventSubs=null,this._eventSubsNum=null,this._events=null,this._eventCallDepth=0,t.addPlugin(this)}fire(e,t,s){this._events||(this._events={}),this._eventSubs||(this._eventSubs={},this._eventSubsNum={}),!0!==s&&(this._events[e]=t||!0);const n=this._eventSubs[e];let i;if(n)for(const s in n)n.hasOwnProperty(s)&&(i=n[s],this._eventCallDepth++,this._eventCallDepth<300?i.callback.call(i.scope,t):this.error("fire: potential stack overflow from recursive event '"+e+"' - dropping this event"),this._eventCallDepth--)}on(t,s,n){this._events||(this._events={}),this._subIdMap||(this._subIdMap=new e),this._subIdEvents||(this._subIdEvents={}),this._eventSubs||(this._eventSubs={}),this._eventSubsNum||(this._eventSubsNum={});let i=this._eventSubs[t];i?this._eventSubsNum[t]++:(i={},this._eventSubs[t]=i,this._eventSubsNum[t]=1);const a=this._subIdMap.addItem();i[a]={callback:s,scope:n||this},this._subIdEvents[a]=t;const r=this._events[t];return void 0!==r&&s.call(n||this,r),a}off(e){if(null==e)return;if(!this._subIdEvents)return;const t=this._subIdEvents[e];if(t){delete this._subIdEvents[e];const s=this._eventSubs[t];s&&(delete s[e],this._eventSubsNum[t]--),this._subIdMap.removeItem(e)}}once(e,t,s){const n=this,i=this.on(e,(function(e){n.off(i),t.call(s||this,e)}),s)}hasSubs(e){return this._eventSubsNum&&this._eventSubsNum[e]>0}log(e){console.log(`[xeokit plugin ${this.id}]: ${e}`)}warn(e){console.warn(`[xeokit plugin ${this.id}]: ${e}`)}error(e){console.error(`[xeokit plugin ${this.id}]: ${e}`)}send(e,t){}destroy(){this.viewer.removePlugin(this)}}let r=!0,l=r?Float64Array:Float32Array;const o=new l(16),c=new l(16),u=new l(4),h={setDoublePrecisionEnabled(e){r=e,l=r?Float64Array:Float32Array},getDoublePrecisionEnabled:()=>r,MIN_DOUBLE:-Number.MAX_SAFE_INTEGER,MAX_DOUBLE:Number.MAX_SAFE_INTEGER,MAX_INT:1e7,DEGTORAD:.0174532925,RADTODEG:57.295779513,unglobalizeObjectId(e,t){const s=t.indexOf("#");return s===e.length&&t.startsWith(e)?t.substring(s+1):t},globalizeObjectId:(e,t)=>e+"#"+t,safeInv(e){const t=1/e;return isNaN(t)||!isFinite(t)?1:t},vec2:e=>new l(e||2),vec3:e=>new l(e||3),vec4:e=>new l(e||4),mat3:e=>new l(e||9),mat3ToMat4:(e,t=new l(16))=>(t[0]=e[0],t[1]=e[1],t[2]=e[2],t[3]=0,t[4]=e[3],t[5]=e[4],t[6]=e[5],t[7]=0,t[8]=e[6],t[9]=e[7],t[10]=e[8],t[11]=0,t[12]=0,t[13]=0,t[14]=0,t[15]=1,t),mat4:e=>new l(e||16),mat4ToMat3(e,t){},doublesToFloats(e,t,s){const n=new l(2);for(let i=0,a=e.length;i<a;i++)h.splitDouble(e[i],n),t[i]=n[0],s[i]=n[1]},splitDouble(e,t){const s=l.from([e])[0],n=e-s;t[0]=s,t[1]=n},createUUID:(()=>{const e=[];for(let t=0;t<256;t++)e[t]=(t<16?"0":"")+t.toString(16);return()=>{const t=4294967295*Math.random()|0,s=4294967295*Math.random()|0,n=4294967295*Math.random()|0,i=4294967295*Math.random()|0;return`${e[255&t]+e[t>>8&255]+e[t>>16&255]+e[t>>24&255]}-${e[255&s]}${e[s>>8&255]}-${e[s>>16&15|64]}${e[s>>24&255]}-${e[63&n|128]}${e[n>>8&255]}-${e[n>>16&255]}${e[n>>24&255]}${e[255&i]}${e[i>>8&255]}${e[i>>16&255]}${e[i>>24&255]}`}})(),clamp:(e,t,s)=>Math.max(t,Math.min(s,e)),fmod(e,t){if(e<t)return console.error("math.fmod : Attempting to find modulus within negative range - would be infinite loop - ignoring"),e;for(;t<=e;)e-=t;return e},compareVec3:(e,t)=>e[0]===t[0]&&e[1]===t[1]&&e[2]===t[2],negateVec3:(e,t)=>(t||(t=e),t[0]=-e[0],t[1]=-e[1],t[2]=-e[2],t),negateVec4:(e,t)=>(t||(t=e),t[0]=-e[0],t[1]=-e[1],t[2]=-e[2],t[3]=-e[3],t),addVec4:(e,t,s)=>(s||(s=e),s[0]=e[0]+t[0],s[1]=e[1]+t[1],s[2]=e[2]+t[2],s[3]=e[3]+t[3],s),addVec4Scalar:(e,t,s)=>(s||(s=e),s[0]=e[0]+t,s[1]=e[1]+t,s[2]=e[2]+t,s[3]=e[3]+t,s),addVec3:(e,t,s)=>(s||(s=e),s[0]=e[0]+t[0],s[1]=e[1]+t[1],s[2]=e[2]+t[2],s),addVec3Scalar:(e,t,s)=>(s||(s=e),s[0]=e[0]+t,s[1]=e[1]+t,s[2]=e[2]+t,s),subVec4:(e,t,s)=>(s||(s=e),s[0]=e[0]-t[0],s[1]=e[1]-t[1],s[2]=e[2]-t[2],s[3]=e[3]-t[3],s),subVec3:(e,t,s)=>(s||(s=e),s[0]=e[0]-t[0],s[1]=e[1]-t[1],s[2]=e[2]-t[2],s),subVec2:(e,t,s)=>(s||(s=e),s[0]=e[0]-t[0],s[1]=e[1]-t[1],s),geometricMeanVec2(...e){const t=new l(e[0]);for(let s=1;s<e.length;s++)t[0]+=e[s][0],t[1]+=e[s][1];return t[0]/=e.length,t[1]/=e.length,t},subVec4Scalar:(e,t,s)=>(s||(s=e),s[0]=e[0]-t,s[1]=e[1]-t,s[2]=e[2]-t,s[3]=e[3]-t,s),subScalarVec4:(e,t,s)=>(s||(s=e),s[0]=t-e[0],s[1]=t-e[1],s[2]=t-e[2],s[3]=t-e[3],s),mulVec4:(e,t,s)=>(s||(s=e),s[0]=e[0]*t[0],s[1]=e[1]*t[1],s[2]=e[2]*t[2],s[3]=e[3]*t[3],s),mulVec4Scalar:(e,t,s)=>(s||(s=e),s[0]=e[0]*t,s[1]=e[1]*t,s[2]=e[2]*t,s[3]=e[3]*t,s),mulVec3Scalar:(e,t,s)=>(s||(s=e),s[0]=e[0]*t,s[1]=e[1]*t,s[2]=e[2]*t,s),mulVec2Scalar:(e,t,s)=>(s||(s=e),s[0]=e[0]*t,s[1]=e[1]*t,s),divVec3:(e,t,s)=>(s||(s=e),s[0]=e[0]/t[0],s[1]=e[1]/t[1],s[2]=e[2]/t[2],s),divVec4:(e,t,s)=>(s||(s=e),s[0]=e[0]/t[0],s[1]=e[1]/t[1],s[2]=e[2]/t[2],s[3]=e[3]/t[3],s),divScalarVec3:(e,t,s)=>(s||(s=t),s[0]=e/t[0],s[1]=e/t[1],s[2]=e/t[2],s),divVec3Scalar:(e,t,s)=>(s||(s=e),s[0]=e[0]/t,s[1]=e[1]/t,s[2]=e[2]/t,s),divVec4Scalar:(e,t,s)=>(s||(s=e),s[0]=e[0]/t,s[1]=e[1]/t,s[2]=e[2]/t,s[3]=e[3]/t,s),divScalarVec4:(e,t,s)=>(s||(s=t),s[0]=e/t[0],s[1]=e/t[1],s[2]=e/t[2],s[3]=e/t[3],s),dotVec4:(e,t)=>e[0]*t[0]+e[1]*t[1]+e[2]*t[2]+e[3]*t[3],cross3Vec4(e,t){const s=e[0],n=e[1],i=e[2],a=t[0],r=t[1],l=t[2];return[n*l-i*r,i*a-s*l,s*r-n*a,0]},cross3Vec3(e,t,s){s||(s=e);const n=e[0],i=e[1],a=e[2],r=t[0],l=t[1],o=t[2];return s[0]=i*o-a*l,s[1]=a*r-n*o,s[2]=n*l-i*r,s},sqLenVec4:e=>h.dotVec4(e,e),lenVec4:e=>Math.sqrt(h.sqLenVec4(e)),dotVec3:(e,t)=>e[0]*t[0]+e[1]*t[1]+e[2]*t[2],dotVec2:(e,t)=>e[0]*t[0]+e[1]*t[1],sqLenVec3:e=>h.dotVec3(e,e),sqLenVec2:e=>h.dotVec2(e,e),lenVec3:e=>Math.sqrt(h.sqLenVec3(e)),distVec3:(()=>{const e=new l(3);return(t,s)=>h.lenVec3(h.subVec3(t,s,e))})(),lenVec2:e=>Math.sqrt(h.sqLenVec2(e)),distVec2:(()=>{const e=new l(2);return(t,s)=>h.lenVec2(h.subVec2(t,s,e))})(),rcpVec3:(e,t)=>h.divScalarVec3(1,e,t),normalizeVec4(e,t){const s=1/h.lenVec4(e);return h.mulVec4Scalar(e,s,t)},normalizeVec3(e,t){const s=1/h.lenVec3(e);return h.mulVec3Scalar(e,s,t)},normalizeVec2(e,t){const s=1/h.lenVec2(e);return h.mulVec2Scalar(e,s,t)},angleVec3(e,t){let s=h.dotVec3(e,t)/Math.sqrt(h.sqLenVec3(e)*h.sqLenVec3(t));return s=s<-1?-1:s>1?1:s,Math.acos(s)},vec3FromMat4Scale:(()=>{const e=new l(3);return(t,s)=>(e[0]=t[0],e[1]=t[1],e[2]=t[2],s[0]=h.lenVec3(e),e[0]=t[4],e[1]=t[5],e[2]=t[6],s[1]=h.lenVec3(e),e[0]=t[8],e[1]=t[9],e[2]=t[10],s[2]=h.lenVec3(e),s)})(),vecToArray:(()=>{function e(e){return Math.round(1e5*e)/1e5}return t=>{for(let s=0,n=(t=Array.prototype.slice.call(t)).length;s<n;s++)t[s]=e(t[s]);return t}})(),xyzArrayToObject:e=>({x:e[0],y:e[1],z:e[2]}),xyzObjectToArray:(e,t)=>((t=t||h.vec3())[0]=e.x,t[1]=e.y,t[2]=e.z,t),dupMat4:e=>e.slice(0,16),mat4To3:e=>[e[0],e[1],e[2],e[4],e[5],e[6],e[8],e[9],e[10]],m4s:e=>[e,e,e,e,e,e,e,e,e,e,e,e,e,e,e,e],setMat4ToZeroes:()=>h.m4s(0),setMat4ToOnes:()=>h.m4s(1),diagonalMat4v:e=>new l([e[0],0,0,0,0,e[1],0,0,0,0,e[2],0,0,0,0,e[3]]),diagonalMat4c:(e,t,s,n)=>h.diagonalMat4v([e,t,s,n]),diagonalMat4s:e=>h.diagonalMat4c(e,e,e,e),identityMat4:(e=new l(16))=>(e[0]=1,e[1]=0,e[2]=0,e[3]=0,e[4]=0,e[5]=1,e[6]=0,e[7]=0,e[8]=0,e[9]=0,e[10]=1,e[11]=0,e[12]=0,e[13]=0,e[14]=0,e[15]=1,e),identityMat3:(e=new l(9))=>(e[0]=1,e[1]=0,e[2]=0,e[3]=0,e[4]=1,e[5]=0,e[6]=0,e[7]=0,e[8]=1,e),isIdentityMat4:e=>1===e[0]&&0===e[1]&&0===e[2]&&0===e[3]&&0===e[4]&&1===e[5]&&0===e[6]&&0===e[7]&&0===e[8]&&0===e[9]&&1===e[10]&&0===e[11]&&0===e[12]&&0===e[13]&&0===e[14]&&1===e[15],negateMat4:(e,t)=>(t||(t=e),t[0]=-e[0],t[1]=-e[1],t[2]=-e[2],t[3]=-e[3],t[4]=-e[4],t[5]=-e[5],t[6]=-e[6],t[7]=-e[7],t[8]=-e[8],t[9]=-e[9],t[10]=-e[10],t[11]=-e[11],t[12]=-e[12],t[13]=-e[13],t[14]=-e[14],t[15]=-e[15],t),addMat4:(e,t,s)=>(s||(s=e),s[0]=e[0]+t[0],s[1]=e[1]+t[1],s[2]=e[2]+t[2],s[3]=e[3]+t[3],s[4]=e[4]+t[4],s[5]=e[5]+t[5],s[6]=e[6]+t[6],s[7]=e[7]+t[7],s[8]=e[8]+t[8],s[9]=e[9]+t[9],s[10]=e[10]+t[10],s[11]=e[11]+t[11],s[12]=e[12]+t[12],s[13]=e[13]+t[13],s[14]=e[14]+t[14],s[15]=e[15]+t[15],s),addMat4Scalar:(e,t,s)=>(s||(s=e),s[0]=e[0]+t,s[1]=e[1]+t,s[2]=e[2]+t,s[3]=e[3]+t,s[4]=e[4]+t,s[5]=e[5]+t,s[6]=e[6]+t,s[7]=e[7]+t,s[8]=e[8]+t,s[9]=e[9]+t,s[10]=e[10]+t,s[11]=e[11]+t,s[12]=e[12]+t,s[13]=e[13]+t,s[14]=e[14]+t,s[15]=e[15]+t,s),addScalarMat4:(e,t,s)=>h.addMat4Scalar(t,e,s),subMat4:(e,t,s)=>(s||(s=e),s[0]=e[0]-t[0],s[1]=e[1]-t[1],s[2]=e[2]-t[2],s[3]=e[3]-t[3],s[4]=e[4]-t[4],s[5]=e[5]-t[5],s[6]=e[6]-t[6],s[7]=e[7]-t[7],s[8]=e[8]-t[8],s[9]=e[9]-t[9],s[10]=e[10]-t[10],s[11]=e[11]-t[11],s[12]=e[12]-t[12],s[13]=e[13]-t[13],s[14]=e[14]-t[14],s[15]=e[15]-t[15],s),subMat4Scalar:(e,t,s)=>(s||(s=e),s[0]=e[0]-t,s[1]=e[1]-t,s[2]=e[2]-t,s[3]=e[3]-t,s[4]=e[4]-t,s[5]=e[5]-t,s[6]=e[6]-t,s[7]=e[7]-t,s[8]=e[8]-t,s[9]=e[9]-t,s[10]=e[10]-t,s[11]=e[11]-t,s[12]=e[12]-t,s[13]=e[13]-t,s[14]=e[14]-t,s[15]=e[15]-t,s),subScalarMat4:(e,t,s)=>(s||(s=t),s[0]=e-t[0],s[1]=e-t[1],s[2]=e-t[2],s[3]=e-t[3],s[4]=e-t[4],s[5]=e-t[5],s[6]=e-t[6],s[7]=e-t[7],s[8]=e-t[8],s[9]=e-t[9],s[10]=e-t[10],s[11]=e-t[11],s[12]=e-t[12],s[13]=e-t[13],s[14]=e-t[14],s[15]=e-t[15],s),mulMat4(e,t,s){s||(s=e);const n=e[0],i=e[1],a=e[2],r=e[3],l=e[4],o=e[5],c=e[6],u=e[7],h=e[8],p=e[9],A=e[10],d=e[11],f=e[12],I=e[13],y=e[14],m=e[15],v=t[0],w=t[1],g=t[2],T=t[3],E=t[4],b=t[5],D=t[6],P=t[7],R=t[8],C=t[9],_=t[10],B=t[11],O=t[12],S=t[13],N=t[14],x=t[15];return s[0]=v*n+w*l+g*h+T*f,s[1]=v*i+w*o+g*p+T*I,s[2]=v*a+w*c+g*A+T*y,s[3]=v*r+w*u+g*d+T*m,s[4]=E*n+b*l+D*h+P*f,s[5]=E*i+b*o+D*p+P*I,s[6]=E*a+b*c+D*A+P*y,s[7]=E*r+b*u+D*d+P*m,s[8]=R*n+C*l+_*h+B*f,s[9]=R*i+C*o+_*p+B*I,s[10]=R*a+C*c+_*A+B*y,s[11]=R*r+C*u+_*d+B*m,s[12]=O*n+S*l+N*h+x*f,s[13]=O*i+S*o+N*p+x*I,s[14]=O*a+S*c+N*A+x*y,s[15]=O*r+S*u+N*d+x*m,s},mulMat3(e,t,s){s||(s=new l(9));const n=e[0],i=e[3],a=e[6],r=e[1],o=e[4],c=e[7],u=e[2],h=e[5],p=e[8],A=t[0],d=t[3],f=t[6],I=t[1],y=t[4],m=t[7],v=t[2],w=t[5],g=t[8];return s[0]=n*A+i*I+a*v,s[3]=n*d+i*y+a*w,s[6]=n*f+i*m+a*g,s[1]=r*A+o*I+c*v,s[4]=r*d+o*y+c*w,s[7]=r*f+o*m+c*g,s[2]=u*A+h*I+p*v,s[5]=u*d+h*y+p*w,s[8]=u*f+h*m+p*g,s},mulMat4Scalar:(e,t,s)=>(s||(s=e),s[0]=e[0]*t,s[1]=e[1]*t,s[2]=e[2]*t,s[3]=e[3]*t,s[4]=e[4]*t,s[5]=e[5]*t,s[6]=e[6]*t,s[7]=e[7]*t,s[8]=e[8]*t,s[9]=e[9]*t,s[10]=e[10]*t,s[11]=e[11]*t,s[12]=e[12]*t,s[13]=e[13]*t,s[14]=e[14]*t,s[15]=e[15]*t,s),mulMat4v4(e,t,s=h.vec4()){const n=t[0],i=t[1],a=t[2],r=t[3];return s[0]=e[0]*n+e[4]*i+e[8]*a+e[12]*r,s[1]=e[1]*n+e[5]*i+e[9]*a+e[13]*r,s[2]=e[2]*n+e[6]*i+e[10]*a+e[14]*r,s[3]=e[3]*n+e[7]*i+e[11]*a+e[15]*r,s},transposeMat4(e,t){const s=e[4],n=e[14],i=e[8],a=e[13],r=e[12],l=e[9];if(!t||e===t){const t=e[1],o=e[2],c=e[3],u=e[6],h=e[7],p=e[11];return e[1]=s,e[2]=i,e[3]=r,e[4]=t,e[6]=l,e[7]=a,e[8]=o,e[9]=u,e[11]=n,e[12]=c,e[13]=h,e[14]=p,e}return t[0]=e[0],t[1]=s,t[2]=i,t[3]=r,t[4]=e[1],t[5]=e[5],t[6]=l,t[7]=a,t[8]=e[2],t[9]=e[6],t[10]=e[10],t[11]=n,t[12]=e[3],t[13]=e[7],t[14]=e[11],t[15]=e[15],t},transposeMat3(e,t){if(t===e){const s=e[1],n=e[2],i=e[5];t[1]=e[3],t[2]=e[6],t[3]=s,t[5]=e[7],t[6]=n,t[7]=i}else t[0]=e[0],t[1]=e[3],t[2]=e[6],t[3]=e[1],t[4]=e[4],t[5]=e[7],t[6]=e[2],t[7]=e[5],t[8]=e[8];return t},determinantMat4(e){const t=e[0],s=e[1],n=e[2],i=e[3],a=e[4],r=e[5],l=e[6],o=e[7],c=e[8],u=e[9],h=e[10],p=e[11],A=e[12],d=e[13],f=e[14],I=e[15];return A*u*l*i-c*d*l*i-A*r*h*i+a*d*h*i+c*r*f*i-a*u*f*i-A*u*n*o+c*d*n*o+A*s*h*o-t*d*h*o-c*s*f*o+t*u*f*o+A*r*n*p-a*d*n*p-A*s*l*p+t*d*l*p+a*s*f*p-t*r*f*p-c*r*n*I+a*u*n*I+c*s*l*I-t*u*l*I-a*s*h*I+t*r*h*I},inverseMat4(e,t){t||(t=e);const s=e[0],n=e[1],i=e[2],a=e[3],r=e[4],l=e[5],o=e[6],c=e[7],u=e[8],h=e[9],p=e[10],A=e[11],d=e[12],f=e[13],I=e[14],y=e[15],m=s*l-n*r,v=s*o-i*r,w=s*c-a*r,g=n*o-i*l,T=n*c-a*l,E=i*c-a*o,b=u*f-h*d,D=u*I-p*d,P=u*y-A*d,R=h*I-p*f,C=h*y-A*f,_=p*y-A*I,B=1/(m*_-v*C+w*R+g*P-T*D+E*b);return t[0]=(l*_-o*C+c*R)*B,t[1]=(-n*_+i*C-a*R)*B,t[2]=(f*E-I*T+y*g)*B,t[3]=(-h*E+p*T-A*g)*B,t[4]=(-r*_+o*P-c*D)*B,t[5]=(s*_-i*P+a*D)*B,t[6]=(-d*E+I*w-y*v)*B,t[7]=(u*E-p*w+A*v)*B,t[8]=(r*C-l*P+c*b)*B,t[9]=(-s*C+n*P-a*b)*B,t[10]=(d*T-f*w+y*m)*B,t[11]=(-u*T+h*w-A*m)*B,t[12]=(-r*R+l*D-o*b)*B,t[13]=(s*R-n*D+i*b)*B,t[14]=(-d*g+f*v-I*m)*B,t[15]=(u*g-h*v+p*m)*B,t},traceMat4:e=>e[0]+e[5]+e[10]+e[15],translationMat4v(e,t){const s=t||h.identityMat4();return s[12]=e[0],s[13]=e[1],s[14]=e[2],s},translationMat3v(e,t){const s=t||h.identityMat3();return s[6]=e[0],s[7]=e[1],s},translationMat4c:(()=>{const e=new l(3);return(t,s,n,i)=>(e[0]=t,e[1]=s,e[2]=n,h.translationMat4v(e,i))})(),translationMat4s:(e,t)=>h.translationMat4c(e,e,e,t),translateMat4v:(e,t)=>h.translateMat4c(e[0],e[1],e[2],t),translateMat4c(e,t,s,n){const i=n[3];n[0]+=i*e,n[1]+=i*t,n[2]+=i*s;const a=n[7];n[4]+=a*e,n[5]+=a*t,n[6]+=a*s;const r=n[11];n[8]+=r*e,n[9]+=r*t,n[10]+=r*s;const l=n[15];return n[12]+=l*e,n[13]+=l*t,n[14]+=l*s,n},setMat4Translation:(e,t,s)=>(s[0]=e[0],s[1]=e[1],s[2]=e[2],s[3]=e[3],s[4]=e[4],s[5]=e[5],s[6]=e[6],s[7]=e[7],s[8]=e[8],s[9]=e[9],s[10]=e[10],s[11]=e[11],s[12]=t[0],s[13]=t[1],s[14]=t[2],s[15]=e[15],s),rotationMat4v(e,t,s){const n=h.normalizeVec4([t[0],t[1],t[2],0],[]),i=Math.sin(e),a=Math.cos(e),r=1-a,l=n[0],o=n[1],c=n[2];let u,p,A,d,f,I;return u=l*o,p=o*c,A=c*l,d=l*i,f=o*i,I=c*i,(s=s||h.mat4())[0]=r*l*l+a,s[1]=r*u+I,s[2]=r*A-f,s[3]=0,s[4]=r*u-I,s[5]=r*o*o+a,s[6]=r*p+d,s[7]=0,s[8]=r*A+f,s[9]=r*p-d,s[10]=r*c*c+a,s[11]=0,s[12]=0,s[13]=0,s[14]=0,s[15]=1,s},rotationMat4c:(e,t,s,n,i)=>h.rotationMat4v(e,[t,s,n],i),scalingMat4v:(e,t=h.identityMat4())=>(t[0]=e[0],t[5]=e[1],t[10]=e[2],t),scalingMat3v:(e,t=h.identityMat3())=>(t[0]=e[0],t[4]=e[1],t),scalingMat4c:(()=>{const e=new l(3);return(t,s,n,i)=>(e[0]=t,e[1]=s,e[2]=n,h.scalingMat4v(e,i))})(),scaleMat4c:(e,t,s,n)=>(n[0]*=e,n[4]*=t,n[8]*=s,n[1]*=e,n[5]*=t,n[9]*=s,n[2]*=e,n[6]*=t,n[10]*=s,n[3]*=e,n[7]*=t,n[11]*=s,n),scaleMat4v(e,t){const s=e[0],n=e[1],i=e[2];return t[0]*=s,t[4]*=n,t[8]*=i,t[1]*=s,t[5]*=n,t[9]*=i,t[2]*=s,t[6]*=n,t[10]*=i,t[3]*=s,t[7]*=n,t[11]*=i,t},scalingMat4s:e=>h.scalingMat4c(e,e,e),rotationTranslationMat4(e,t,s=h.mat4()){const n=e[0],i=e[1],a=e[2],r=e[3],l=n+n,o=i+i,c=a+a,u=n*l,p=n*o,A=n*c,d=i*o,f=i*c,I=a*c,y=r*l,m=r*o,v=r*c;return s[0]=1-(d+I),s[1]=p+v,s[2]=A-m,s[3]=0,s[4]=p-v,s[5]=1-(u+I),s[6]=f+y,s[7]=0,s[8]=A+m,s[9]=f-y,s[10]=1-(u+d),s[11]=0,s[12]=t[0],s[13]=t[1],s[14]=t[2],s[15]=1,s},mat4ToEuler(e,t,s=h.vec4()){const n=h.clamp,i=e[0],a=e[4],r=e[8],l=e[1],o=e[5],c=e[9],u=e[2],p=e[6],A=e[10];return"XYZ"===t?(s[1]=Math.asin(n(r,-1,1)),Math.abs(r)<.99999?(s[0]=Math.atan2(-c,A),s[2]=Math.atan2(-a,i)):(s[0]=Math.atan2(p,o),s[2]=0)):"YXZ"===t?(s[0]=Math.asin(-n(c,-1,1)),Math.abs(c)<.99999?(s[1]=Math.atan2(r,A),s[2]=Math.atan2(l,o)):(s[1]=Math.atan2(-u,i),s[2]=0)):"ZXY"===t?(s[0]=Math.asin(n(p,-1,1)),Math.abs(p)<.99999?(s[1]=Math.atan2(-u,A),s[2]=Math.atan2(-a,o)):(s[1]=0,s[2]=Math.atan2(l,i))):"ZYX"===t?(s[1]=Math.asin(-n(u,-1,1)),Math.abs(u)<.99999?(s[0]=Math.atan2(p,A),s[2]=Math.atan2(l,i)):(s[0]=0,s[2]=Math.atan2(-a,o))):"YZX"===t?(s[2]=Math.asin(n(l,-1,1)),Math.abs(l)<.99999?(s[0]=Math.atan2(-c,o),s[1]=Math.atan2(-u,i)):(s[0]=0,s[1]=Math.atan2(r,A))):"XZY"===t&&(s[2]=Math.asin(-n(a,-1,1)),Math.abs(a)<.99999?(s[0]=Math.atan2(p,o),s[1]=Math.atan2(r,i)):(s[0]=Math.atan2(-c,A),s[1]=0)),s},composeMat4:(e,t,s,n=h.mat4())=>(h.quaternionToRotationMat4(t,n),h.scaleMat4v(s,n),h.translateMat4v(e,n),n),decomposeMat4:(()=>{const e=new l(3),t=new l(16);return function(s,n,i,a){e[0]=s[0],e[1]=s[1],e[2]=s[2];let r=h.lenVec3(e);e[0]=s[4],e[1]=s[5],e[2]=s[6];const l=h.lenVec3(e);e[8]=s[8],e[9]=s[9],e[10]=s[10];const o=h.lenVec3(e);h.determinantMat4(s)<0&&(r=-r),n[0]=s[12],n[1]=s[13],n[2]=s[14],t.set(s);const c=1/r,u=1/l,p=1/o;return t[0]*=c,t[1]*=c,t[2]*=c,t[4]*=u,t[5]*=u,t[6]*=u,t[8]*=p,t[9]*=p,t[10]*=p,h.mat4ToQuaternion(t,i),a[0]=r,a[1]=l,a[2]=o,this}})(),getColMat4(e,t){const s=4*t;return[e[s],e[s+1],e[s+2],e[s+3]]},setRowMat4(e,t,s){e[t]=s[0],e[t+4]=s[1],e[t+8]=s[2],e[t+12]=s[3]},lookAtMat4v(e,t,s,n){n||(n=h.mat4());const i=e[0],a=e[1],r=e[2],l=s[0],o=s[1],c=s[2],u=t[0],p=t[1],A=t[2];if(i===u&&a===p&&r===A)return h.identityMat4();let d,f,I,y,m,v,w,g,T,E;return d=i-u,f=a-p,I=r-A,E=1/Math.sqrt(d*d+f*f+I*I),d*=E,f*=E,I*=E,y=o*I-c*f,m=c*d-l*I,v=l*f-o*d,E=Math.sqrt(y*y+m*m+v*v),E?(E=1/E,y*=E,m*=E,v*=E):(y=0,m=0,v=0),w=f*v-I*m,g=I*y-d*v,T=d*m-f*y,E=Math.sqrt(w*w+g*g+T*T),E?(E=1/E,w*=E,g*=E,T*=E):(w=0,g=0,T=0),n[0]=y,n[1]=w,n[2]=d,n[3]=0,n[4]=m,n[5]=g,n[6]=f,n[7]=0,n[8]=v,n[9]=T,n[10]=I,n[11]=0,n[12]=-(y*i+m*a+v*r),n[13]=-(w*i+g*a+T*r),n[14]=-(d*i+f*a+I*r),n[15]=1,n},lookAtMat4c:(e,t,s,n,i,a,r,l,o)=>h.lookAtMat4v([e,t,s],[n,i,a],[r,l,o],[]),orthoMat4c(e,t,s,n,i,a,r){r||(r=h.mat4());const l=t-e,o=n-s,c=a-i;return r[0]=2/l,r[1]=0,r[2]=0,r[3]=0,r[4]=0,r[5]=2/o,r[6]=0,r[7]=0,r[8]=0,r[9]=0,r[10]=-2/c,r[11]=0,r[12]=-(e+t)/l,r[13]=-(n+s)/o,r[14]=-(a+i)/c,r[15]=1,r},frustumMat4v(e,t,s){s||(s=h.mat4());const n=[e[0],e[1],e[2],0],i=[t[0],t[1],t[2],0];h.addVec4(i,n,o),h.subVec4(i,n,c);const a=2*n[2],r=c[0],l=c[1],u=c[2];return s[0]=a/r,s[1]=0,s[2]=0,s[3]=0,s[4]=0,s[5]=a/l,s[6]=0,s[7]=0,s[8]=o[0]/r,s[9]=o[1]/l,s[10]=-o[2]/u,s[11]=-1,s[12]=0,s[13]=0,s[14]=-a*i[2]/u,s[15]=0,s},frustumMat4(e,t,s,n,i,a,r){r||(r=h.mat4());const l=t-e,o=n-s,c=a-i;return r[0]=2*i/l,r[1]=0,r[2]=0,r[3]=0,r[4]=0,r[5]=2*i/o,r[6]=0,r[7]=0,r[8]=(t+e)/l,r[9]=(n+s)/o,r[10]=-(a+i)/c,r[11]=-1,r[12]=0,r[13]=0,r[14]=-a*i*2/c,r[15]=0,r},perspectiveMat4(e,t,s,n,i){const a=[],r=[];return a[2]=s,r[2]=n,r[1]=a[2]*Math.tan(e/2),a[1]=-r[1],r[0]=r[1]*t,a[0]=-r[0],h.frustumMat4v(a,r,i)},compareMat4:(e,t)=>e[0]===t[0]&&e[1]===t[1]&&e[2]===t[2]&&e[3]===t[3]&&e[4]===t[4]&&e[5]===t[5]&&e[6]===t[6]&&e[7]===t[7]&&e[8]===t[8]&&e[9]===t[9]&&e[10]===t[10]&&e[11]===t[11]&&e[12]===t[12]&&e[13]===t[13]&&e[14]===t[14]&&e[15]===t[15],transformPoint3(e,t,s=h.vec3()){const n=t[0],i=t[1],a=t[2];return s[0]=e[0]*n+e[4]*i+e[8]*a+e[12],s[1]=e[1]*n+e[5]*i+e[9]*a+e[13],s[2]=e[2]*n+e[6]*i+e[10]*a+e[14],s},transformPoint4:(e,t,s=h.vec4())=>(s[0]=e[0]*t[0]+e[4]*t[1]+e[8]*t[2]+e[12]*t[3],s[1]=e[1]*t[0]+e[5]*t[1]+e[9]*t[2]+e[13]*t[3],s[2]=e[2]*t[0]+e[6]*t[1]+e[10]*t[2]+e[14]*t[3],s[3]=e[3]*t[0]+e[7]*t[1]+e[11]*t[2]+e[15]*t[3],s),transformPoints3(e,t,s){const n=s||[],i=t.length;let a,r,l,o;const c=e[0],u=e[1],h=e[2],p=e[3],A=e[4],d=e[5],f=e[6],I=e[7],y=e[8],m=e[9],v=e[10],w=e[11],g=e[12],T=e[13],E=e[14],b=e[15];let D;for(let e=0;e<i;++e)o=t[e],a=o[0],r=o[1],l=o[2],D=n[e]||(n[e]=[0,0,0]),D[0]=c*a+A*r+y*l+g,D[1]=u*a+d*r+m*l+T,D[2]=h*a+f*r+v*l+E,D[3]=p*a+I*r+w*l+b;return n.length=i,n},transformPositions3(e,t,s=t){let n;const i=t.length;let a,r,l;const o=e[0],c=e[1],u=e[2];e[3];const h=e[4],p=e[5],A=e[6];e[7];const d=e[8],f=e[9],I=e[10];e[11];const y=e[12],m=e[13],v=e[14];for(e[15],n=0;n<i;n+=3)a=t[n+0],r=t[n+1],l=t[n+2],s[n+0]=o*a+h*r+d*l+y,s[n+1]=c*a+p*r+f*l+m,s[n+2]=u*a+A*r+I*l+v;return s},transformPositions4(e,t,s=t){let n;const i=t.length;let a,r,l;const o=e[0],c=e[1],u=e[2],h=e[3],p=e[4],A=e[5],d=e[6],f=e[7],I=e[8],y=e[9],m=e[10],v=e[11],w=e[12],g=e[13],T=e[14],E=e[15];for(n=0;n<i;n+=4)a=t[n+0],r=t[n+1],l=t[n+2],s[n+0]=o*a+p*r+I*l+w,s[n+1]=c*a+A*r+y*l+g,s[n+2]=u*a+d*r+m*l+T,s[n+3]=h*a+f*r+v*l+E;return s},transformVec3(e,t,s){const n=t[0],i=t[1],a=t[2];return(s=s||this.vec3())[0]=e[0]*n+e[4]*i+e[8]*a,s[1]=e[1]*n+e[5]*i+e[9]*a,s[2]=e[2]*n+e[6]*i+e[10]*a,s},transformVec4(e,t,s){const n=t[0],i=t[1],a=t[2],r=t[3];return(s=s||h.vec4())[0]=e[0]*n+e[4]*i+e[8]*a+e[12]*r,s[1]=e[1]*n+e[5]*i+e[9]*a+e[13]*r,s[2]=e[2]*n+e[6]*i+e[10]*a+e[14]*r,s[3]=e[3]*n+e[7]*i+e[11]*a+e[15]*r,s},rotateVec2(e,t,s,n=e){const i=Math.cos(s),a=Math.sin(s),r=e[0]-t[0],l=e[1]-t[1];return n[0]=r*i-l*a+t[0],n[1]=r*a+l*i+t[1],e},rotateVec3X(e,t,s,n){const i=[],a=[];return i[0]=e[0]-t[0],i[1]=e[1]-t[1],i[2]=e[2]-t[2],a[0]=i[0],a[1]=i[1]*Math.cos(s)-i[2]*Math.sin(s),a[2]=i[1]*Math.sin(s)+i[2]*Math.cos(s),n[0]=a[0]+t[0],n[1]=a[1]+t[1],n[2]=a[2]+t[2],n},rotateVec3Y(e,t,s,n){const i=[],a=[];return i[0]=e[0]-t[0],i[1]=e[1]-t[1],i[2]=e[2]-t[2],a[0]=i[2]*Math.sin(s)+i[0]*Math.cos(s),a[1]=i[1],a[2]=i[2]*Math.cos(s)-i[0]*Math.sin(s),n[0]=a[0]+t[0],n[1]=a[1]+t[1],n[2]=a[2]+t[2],n},rotateVec3Z(e,t,s,n){const i=[],a=[];return i[0]=e[0]-t[0],i[1]=e[1]-t[1],i[2]=e[2]-t[2],a[0]=i[0]*Math.cos(s)-i[1]*Math.sin(s),a[1]=i[0]*Math.sin(s)+i[1]*Math.cos(s),a[2]=i[2],n[0]=a[0]+t[0],n[1]=a[1]+t[1],n[2]=a[2]+t[2],n},projectVec4(e,t){const s=1/e[3];return(t=t||h.vec2())[0]=e[0]*s,t[1]=e[1]*s,t},unprojectVec3:(()=>{const e=new l(16),t=new l(16),s=new l(16);return function(n,i,a,r){return this.transformVec3(this.mulMat4(this.inverseMat4(i,e),this.inverseMat4(a,t),s),n,r)}})(),lerpVec3(e,t,s,n,i,a){const r=a||h.vec3(),l=(e-t)/(s-t);return r[0]=n[0]+l*(i[0]-n[0]),r[1]=n[1]+l*(i[1]-n[1]),r[2]=n[2]+l*(i[2]-n[2]),r},lerpMat4(e,t,s,n,i,a){const r=a||h.mat4(),l=(e-t)/(s-t);return r[0]=n[0]+l*(i[0]-n[0]),r[1]=n[1]+l*(i[1]-n[1]),r[2]=n[2]+l*(i[2]-n[2]),r[3]=n[3]+l*(i[3]-n[3]),r[4]=n[4]+l*(i[4]-n[4]),r[5]=n[5]+l*(i[5]-n[5]),r[6]=n[6]+l*(i[6]-n[6]),r[7]=n[7]+l*(i[7]-n[7]),r[8]=n[8]+l*(i[8]-n[8]),r[9]=n[9]+l*(i[9]-n[9]),r[10]=n[10]+l*(i[10]-n[10]),r[11]=n[11]+l*(i[11]-n[11]),r[12]=n[12]+l*(i[12]-n[12]),r[13]=n[13]+l*(i[13]-n[13]),r[14]=n[14]+l*(i[14]-n[14]),r[15]=n[15]+l*(i[15]-n[15]),r},flatten(e){const t=[];let s,n,i,a,r;for(s=0,n=e.length;s<n;s++)for(r=e[s],i=0,a=r.length;i<a;i++)t.push(r[i]);return t},identityQuaternion:(e=h.vec4())=>(e[0]=0,e[1]=0,e[2]=0,e[3]=1,e),eulerToQuaternion(e,t,s=h.vec4()){const n=e[0]*h.DEGTORAD/2,i=e[1]*h.DEGTORAD/2,a=e[2]*h.DEGTORAD/2,r=Math.cos(n),l=Math.cos(i),o=Math.cos(a),c=Math.sin(n),u=Math.sin(i),p=Math.sin(a);return"XYZ"===t?(s[0]=c*l*o+r*u*p,s[1]=r*u*o-c*l*p,s[2]=r*l*p+c*u*o,s[3]=r*l*o-c*u*p):"YXZ"===t?(s[0]=c*l*o+r*u*p,s[1]=r*u*o-c*l*p,s[2]=r*l*p-c*u*o,s[3]=r*l*o+c*u*p):"ZXY"===t?(s[0]=c*l*o-r*u*p,s[1]=r*u*o+c*l*p,s[2]=r*l*p+c*u*o,s[3]=r*l*o-c*u*p):"ZYX"===t?(s[0]=c*l*o-r*u*p,s[1]=r*u*o+c*l*p,s[2]=r*l*p-c*u*o,s[3]=r*l*o+c*u*p):"YZX"===t?(s[0]=c*l*o+r*u*p,s[1]=r*u*o+c*l*p,s[2]=r*l*p-c*u*o,s[3]=r*l*o-c*u*p):"XZY"===t&&(s[0]=c*l*o-r*u*p,s[1]=r*u*o-c*l*p,s[2]=r*l*p+c*u*o,s[3]=r*l*o+c*u*p),s},mat4ToQuaternion(e,t=h.vec4()){const s=e[0],n=e[4],i=e[8],a=e[1],r=e[5],l=e[9],o=e[2],c=e[6],u=e[10];let p;const A=s+r+u;return A>0?(p=.5/Math.sqrt(A+1),t[3]=.25/p,t[0]=(c-l)*p,t[1]=(i-o)*p,t[2]=(a-n)*p):s>r&&s>u?(p=2*Math.sqrt(1+s-r-u),t[3]=(c-l)/p,t[0]=.25*p,t[1]=(n+a)/p,t[2]=(i+o)/p):r>u?(p=2*Math.sqrt(1+r-s-u),t[3]=(i-o)/p,t[0]=(n+a)/p,t[1]=.25*p,t[2]=(l+c)/p):(p=2*Math.sqrt(1+u-s-r),t[3]=(a-n)/p,t[0]=(i+o)/p,t[1]=(l+c)/p,t[2]=.25*p),t},vec3PairToQuaternion(e,t,s=h.vec4()){const n=Math.sqrt(h.dotVec3(e,e)*h.dotVec3(t,t));let i=n+h.dotVec3(e,t);return i<1e-8*n?(i=0,Math.abs(e[0])>Math.abs(e[2])?(s[0]=-e[1],s[1]=e[0],s[2]=0):(s[0]=0,s[1]=-e[2],s[2]=e[1])):h.cross3Vec3(e,t,s),s[3]=i,h.normalizeQuaternion(s)},angleAxisToQuaternion(e,t=h.vec4()){const s=e[3]/2,n=Math.sin(s);return t[0]=n*e[0],t[1]=n*e[1],t[2]=n*e[2],t[3]=Math.cos(s),t},quaternionToEuler:(()=>{const e=new l(16);return(t,s,n)=>(n=n||h.vec3(),h.quaternionToRotationMat4(t,e),h.mat4ToEuler(e,s,n),n)})(),mulQuaternions(e,t,s=h.vec4()){const n=e[0],i=e[1],a=e[2],r=e[3],l=t[0],o=t[1],c=t[2],u=t[3];return s[0]=r*l+n*u+i*c-a*o,s[1]=r*o+i*u+a*l-n*c,s[2]=r*c+a*u+n*o-i*l,s[3]=r*u-n*l-i*o-a*c,s},vec3ApplyQuaternion(e,t,s=h.vec3()){const n=t[0],i=t[1],a=t[2],r=e[0],l=e[1],o=e[2],c=e[3],u=c*n+l*a-o*i,p=c*i+o*n-r*a,A=c*a+r*i-l*n,d=-r*n-l*i-o*a;return s[0]=u*c+d*-r+p*-o-A*-l,s[1]=p*c+d*-l+A*-r-u*-o,s[2]=A*c+d*-o+u*-l-p*-r,s},quaternionToMat4(e,t){t=h.identityMat4(t);const s=e[0],n=e[1],i=e[2],a=e[3],r=2*s,l=2*n,o=2*i,c=r*a,u=l*a,p=o*a,A=r*s,d=l*s,f=o*s,I=l*n,y=o*n,m=o*i;return t[0]=1-(I+m),t[1]=d+p,t[2]=f-u,t[4]=d-p,t[5]=1-(A+m),t[6]=y+c,t[8]=f+u,t[9]=y-c,t[10]=1-(A+I),t},quaternionToRotationMat4(e,t){const s=e[0],n=e[1],i=e[2],a=e[3],r=s+s,l=n+n,o=i+i,c=s*r,u=s*l,h=s*o,p=n*l,A=n*o,d=i*o,f=a*r,I=a*l,y=a*o;return t[0]=1-(p+d),t[4]=u-y,t[8]=h+I,t[1]=u+y,t[5]=1-(c+d),t[9]=A-f,t[2]=h-I,t[6]=A+f,t[10]=1-(c+p),t[3]=0,t[7]=0,t[11]=0,t[12]=0,t[13]=0,t[14]=0,t[15]=1,t},normalizeQuaternion(e,t=e){const s=h.lenVec4([e[0],e[1],e[2],e[3]]);return t[0]=e[0]/s,t[1]=e[1]/s,t[2]=e[2]/s,t[3]=e[3]/s,t},conjugateQuaternion:(e,t=e)=>(t[0]=-e[0],t[1]=-e[1],t[2]=-e[2],t[3]=e[3],t),inverseQuaternion:(e,t)=>h.normalizeQuaternion(h.conjugateQuaternion(e,t)),quaternionToAngleAxis(e,t=h.vec4()){const s=(e=h.normalizeQuaternion(e,u))[3],n=2*Math.acos(s),i=Math.sqrt(1-s*s);return i<.001?(t[0]=e[0],t[1]=e[1],t[2]=e[2]):(t[0]=e[0]/i,t[1]=e[1]/i,t[2]=e[2]/i),t[3]=n,t},AABB3:e=>new l(e||6),AABB2:e=>new l(e||4),OBB3:e=>new l(e||32),OBB2:e=>new l(e||16),Sphere3:(e,t,s,n)=>new l([e,t,s,n]),transformOBB3(e,t,s=t){let n;const i=t.length;let a,r,l;const o=e[0],c=e[1],u=e[2],h=e[3],p=e[4],A=e[5],d=e[6],f=e[7],I=e[8],y=e[9],m=e[10],v=e[11],w=e[12],g=e[13],T=e[14],E=e[15];for(n=0;n<i;n+=4)a=t[n+0],r=t[n+1],l=t[n+2],s[n+0]=o*a+p*r+I*l+w,s[n+1]=c*a+A*r+y*l+g,s[n+2]=u*a+d*r+m*l+T,s[n+3]=h*a+f*r+v*l+E;return s},containsAABB3:function(e,t){return e[0]<=t[0]&&t[3]<=e[3]&&e[1]<=t[1]&&t[4]<=e[4]&&e[2]<=t[2]&&t[5]<=e[5]},getAABB3Diag:(()=>{const e=new l(3),t=new l(3),s=new l(3);return n=>(e[0]=n[0],e[1]=n[1],e[2]=n[2],t[0]=n[3],t[1]=n[4],t[2]=n[5],h.subVec3(t,e,s),Math.abs(h.lenVec3(s)))})(),getAABB3DiagPoint:(()=>{const e=new l(3),t=new l(3),s=new l(3);return(n,i)=>{e[0]=n[0],e[1]=n[1],e[2]=n[2],t[0]=n[3],t[1]=n[4],t[2]=n[5];const a=h.subVec3(t,e,s),r=i[0]-n[0],l=n[3]-i[0],o=i[1]-n[1],c=n[4]-i[1],u=i[2]-n[2],p=n[5]-i[2];return a[0]+=r>l?r:l,a[1]+=o>c?o:c,a[2]+=u>p?u:p,Math.abs(h.lenVec3(a))}})(),getAABB3Area:e=>(e[3]-e[0])*(e[4]-e[1])*(e[5]-e[2]),getAABB3Center(e,t){const s=t||h.vec3();return s[0]=(e[0]+e[3])/2,s[1]=(e[1]+e[4])/2,s[2]=(e[2]+e[5])/2,s},getAABB2Center(e,t){const s=t||h.vec2();return s[0]=(e[2]+e[0])/2,s[1]=(e[3]+e[1])/2,s},collapseAABB3:(e=h.AABB3())=>(e[0]=h.MAX_DOUBLE,e[1]=h.MAX_DOUBLE,e[2]=h.MAX_DOUBLE,e[3]=h.MIN_DOUBLE,e[4]=h.MIN_DOUBLE,e[5]=h.MIN_DOUBLE,e),AABB3ToOBB3:(e,t=h.OBB3())=>(t[0]=e[0],t[1]=e[1],t[2]=e[2],t[3]=1,t[4]=e[3],t[5]=e[1],t[6]=e[2],t[7]=1,t[8]=e[3],t[9]=e[4],t[10]=e[2],t[11]=1,t[12]=e[0],t[13]=e[4],t[14]=e[2],t[15]=1,t[16]=e[0],t[17]=e[1],t[18]=e[5],t[19]=1,t[20]=e[3],t[21]=e[1],t[22]=e[5],t[23]=1,t[24]=e[3],t[25]=e[4],t[26]=e[5],t[27]=1,t[28]=e[0],t[29]=e[4],t[30]=e[5],t[31]=1,t),positions3ToAABB3:(()=>{const e=new l(3);return(t,s,n)=>{s=s||h.AABB3();let i,a,r,l=h.MAX_DOUBLE,o=h.MAX_DOUBLE,c=h.MAX_DOUBLE,u=h.MIN_DOUBLE,p=h.MIN_DOUBLE,A=h.MIN_DOUBLE;for(let s=0,d=t.length;s<d;s+=3)n?(e[0]=t[s+0],e[1]=t[s+1],e[2]=t[s+2],h.decompressPosition(e,n,e),i=e[0],a=e[1],r=e[2]):(i=t[s+0],a=t[s+1],r=t[s+2]),i<l&&(l=i),a<o&&(o=a),r<c&&(c=r),i>u&&(u=i),a>p&&(p=a),r>A&&(A=r);return s[0]=l,s[1]=o,s[2]=c,s[3]=u,s[4]=p,s[5]=A,s}})(),OBB3ToAABB3(e,t=h.AABB3()){let s,n,i,a=h.MAX_DOUBLE,r=h.MAX_DOUBLE,l=h.MAX_DOUBLE,o=h.MIN_DOUBLE,c=h.MIN_DOUBLE,u=h.MIN_DOUBLE;for(let t=0,h=e.length;t<h;t+=4)s=e[t+0],n=e[t+1],i=e[t+2],s<a&&(a=s),n<r&&(r=n),i<l&&(l=i),s>o&&(o=s),n>c&&(c=n),i>u&&(u=i);return t[0]=a,t[1]=r,t[2]=l,t[3]=o,t[4]=c,t[5]=u,t},points3ToAABB3(e,t=h.AABB3()){let s,n,i,a=h.MAX_DOUBLE,r=h.MAX_DOUBLE,l=h.MAX_DOUBLE,o=h.MIN_DOUBLE,c=h.MIN_DOUBLE,u=h.MIN_DOUBLE;for(let t=0,h=e.length;t<h;t++)s=e[t][0],n=e[t][1],i=e[t][2],s<a&&(a=s),n<r&&(r=n),i<l&&(l=i),s>o&&(o=s),n>c&&(c=n),i>u&&(u=i);return t[0]=a,t[1]=r,t[2]=l,t[3]=o,t[4]=c,t[5]=u,t},points3ToSphere3:(()=>{const e=new l(3);return(t,s)=>{s=s||h.vec4();let n,i=0,a=0,r=0;const l=t.length;for(n=0;n<l;n++)i+=t[n][0],a+=t[n][1],r+=t[n][2];s[0]=i/l,s[1]=a/l,s[2]=r/l;let o,c=0;for(n=0;n<l;n++)o=Math.abs(h.lenVec3(h.subVec3(t[n],s,e))),o>c&&(c=o);return s[3]=c,s}})(),positions3ToSphere3:(()=>{const e=new l(3),t=new l(3);return(s,n)=>{n=n||h.vec4();let i,a=0,r=0,l=0;const o=s.length;let c=0;for(i=0;i<o;i+=3)a+=s[i],r+=s[i+1],l+=s[i+2];const u=o/3;let p;for(n[0]=a/u,n[1]=r/u,n[2]=l/u,i=0;i<o;i+=3)e[0]=s[i],e[1]=s[i+1],e[2]=s[i+2],p=Math.abs(h.lenVec3(h.subVec3(e,n,t))),p>c&&(c=p);return n[3]=c,n}})(),OBB3ToSphere3:(()=>{const e=new l(3),t=new l(3);return(s,n)=>{n=n||h.vec4();let i,a=0,r=0,l=0;const o=s.length,c=o/4;for(i=0;i<o;i+=4)a+=s[i+0],r+=s[i+1],l+=s[i+2];n[0]=a/c,n[1]=r/c,n[2]=l/c;let u,p=0;for(i=0;i<o;i+=4)e[0]=s[i+0],e[1]=s[i+1],e[2]=s[i+2],u=Math.abs(h.lenVec3(h.subVec3(e,n,t))),u>p&&(p=u);return n[3]=p,n}})(),getSphere3Center:(e,t=h.vec3())=>(t[0]=e[0],t[1]=e[1],t[2]=e[2],t),getPositionsCenter(e,t=h.vec3()){let s=0,n=0,i=0;for(var a=0,r=e.length;a<r;a+=3)s+=e[a+0],n+=e[a+1],i+=e[a+2];const l=e.length/3;return t[0]=s/l,t[1]=n/l,t[2]=i/l,t},expandAABB3:(e,t)=>(e[0]>t[0]&&(e[0]=t[0]),e[1]>t[1]&&(e[1]=t[1]),e[2]>t[2]&&(e[2]=t[2]),e[3]<t[3]&&(e[3]=t[3]),e[4]<t[4]&&(e[4]=t[4]),e[5]<t[5]&&(e[5]=t[5]),e),expandAABB3Point3:(e,t)=>(e[0]>t[0]&&(e[0]=t[0]),e[1]>t[1]&&(e[1]=t[1]),e[2]>t[2]&&(e[2]=t[2]),e[3]<t[0]&&(e[3]=t[0]),e[4]<t[1]&&(e[4]=t[1]),e[5]<t[2]&&(e[5]=t[2]),e),expandAABB3Points3(e,t){for(var s,n,i,a=0,r=t.length;a<r;a+=3)s=t[a],n=t[a+1],i=t[a+2],e[0]>s&&(e[0]=s),e[1]>n&&(e[1]=n),e[2]>i&&(e[2]=i),e[3]<s&&(e[3]=s),e[4]<n&&(e[4]=n),e[5]<i&&(e[5]=i);return e},collapseAABB2:(e=h.AABB2())=>(e[0]=h.MAX_DOUBLE,e[1]=h.MAX_DOUBLE,e[2]=h.MIN_DOUBLE,e[3]=h.MIN_DOUBLE,e),point3AABB3Intersect:(e,t)=>e[0]>t[0]||e[3]<t[0]||e[1]>t[1]||e[4]<t[1]||e[2]>t[2]||e[5]<t[2],planeAABB3Intersect(e,t,s){let n,i;e[0]>0?(n=e[0]*s[0],i=e[0]*s[3]):(n=e[0]*s[3],i=e[0]*s[0]),e[1]>0?(n+=e[1]*s[1],i+=e[1]*s[4]):(n+=e[1]*s[4],i+=e[1]*s[1]),e[2]>0?(n+=e[2]*s[2],i+=e[2]*s[5]):(n+=e[2]*s[5],i+=e[2]*s[2]);if(n<=-t&&i<=-t)return-1;return n>=-t&&i>=-t?1:0},OBB3ToAABB2(e,t=h.AABB2()){let s,n,i,a,r=h.MAX_DOUBLE,l=h.MAX_DOUBLE,o=h.MIN_DOUBLE,c=h.MIN_DOUBLE;for(let t=0,u=e.length;t<u;t+=4)s=e[t+0],n=e[t+1],i=e[t+3]||1,a=1/i,s*=a,n*=a,s<r&&(r=s),n<l&&(l=n),s>o&&(o=s),n>c&&(c=n);return t[0]=r,t[1]=l,t[2]=o,t[3]=c,t},expandAABB2:(e,t)=>(e[0]>t[0]&&(e[0]=t[0]),e[1]>t[1]&&(e[1]=t[1]),e[2]<t[2]&&(e[2]=t[2]),e[3]<t[3]&&(e[3]=t[3]),e),expandAABB2Point2:(e,t)=>(e[0]>t[0]&&(e[0]=t[0]),e[1]>t[1]&&(e[1]=t[1]),e[2]<t[0]&&(e[2]=t[0]),e[3]<t[1]&&(e[3]=t[1]),e),AABB2ToCanvas(e,t,s,n=e){const i=.5*(e[0]+1),a=.5*(e[1]+1),r=.5*(e[2]+1),l=.5*(e[3]+1);return n[0]=Math.floor(i*t),n[1]=s-Math.floor(l*s),n[2]=Math.floor(r*t),n[3]=s-Math.floor(a*s),n},tangentQuadraticBezier:(e,t,s,n)=>2*(1-e)*(s-t)+2*e*(n-s),tangentQuadraticBezier3:(e,t,s,n,i)=>-3*t*(1-e)*(1-e)+3*s*(1-e)*(1-e)-6*e*s*(1-e)+6*e*n*(1-e)-3*e*e*n+3*e*e*i,tangentSpline:e=>6*e*e-6*e+(3*e*e-4*e+1)+(-6*e*e+6*e)+(3*e*e-2*e),catmullRomInterpolate(e,t,s,n,i){const a=.5*(s-e),r=.5*(n-t),l=i*i;return(2*t-2*s+a+r)*(i*l)+(-3*t+3*s-2*a-r)*l+a*i+t},b2p0(e,t){const s=1-e;return s*s*t},b2p1:(e,t)=>2*(1-e)*e*t,b2p2:(e,t)=>e*e*t,b2(e,t,s,n){return this.b2p0(e,t)+this.b2p1(e,s)+this.b2p2(e,n)},b3p0(e,t){const s=1-e;return s*s*s*t},b3p1(e,t){const s=1-e;return 3*s*s*e*t},b3p2:(e,t)=>3*(1-e)*e*e*t,b3p3:(e,t)=>e*e*e*t,b3(e,t,s,n,i){return this.b3p0(e,t)+this.b3p1(e,s)+this.b3p2(e,n)+this.b3p3(e,i)},triangleNormal(e,t,s,n=h.vec3()){const i=t[0]-e[0],a=t[1]-e[1],r=t[2]-e[2],l=s[0]-e[0],o=s[1]-e[1],c=s[2]-e[2],u=a*c-r*o,p=r*l-i*c,A=i*o-a*l,d=Math.sqrt(u*u+p*p+A*A);return 0===d?(n[0]=0,n[1]=0,n[2]=0):(n[0]=u/d,n[1]=p/d,n[2]=A/d),n},rayTriangleIntersect:(()=>{const e=new l(3),t=new l(3),s=new l(3),n=new l(3),i=new l(3);return(a,r,l,o,c,u)=>{u=u||h.vec3();const p=h.subVec3(o,l,e),A=h.subVec3(c,l,t),d=h.cross3Vec3(r,A,s),f=h.dotVec3(p,d);if(f<1e-6)return null;const I=h.subVec3(a,l,n),y=h.dotVec3(I,d);if(y<0||y>f)return null;const m=h.cross3Vec3(I,p,i),v=h.dotVec3(r,m);if(v<0||y+v>f)return null;const w=h.dotVec3(A,m)/f;return u[0]=a[0]+w*r[0],u[1]=a[1]+w*r[1],u[2]=a[2]+w*r[2],u}})(),rayPlaneIntersect:(()=>{const e=new l(3),t=new l(3),s=new l(3),n=new l(3);return(i,a,r,l,o,c)=>{c=c||h.vec3(),a=h.normalizeVec3(a,e);const u=h.subVec3(l,r,t),p=h.subVec3(o,r,s),A=h.cross3Vec3(u,p,n);h.normalizeVec3(A,A);const d=-h.dotVec3(r,A),f=-(h.dotVec3(i,A)+d)/h.dotVec3(a,A);return c[0]=i[0]+f*a[0],c[1]=i[1]+f*a[1],c[2]=i[2]+f*a[2],c}})(),cartesianToBarycentric:(()=>{const e=new l(3),t=new l(3),s=new l(3);return(n,i,a,r,l)=>{const o=h.subVec3(r,i,e),c=h.subVec3(a,i,t),u=h.subVec3(n,i,s),p=h.dotVec3(o,o),A=h.dotVec3(o,c),d=h.dotVec3(o,u),f=h.dotVec3(c,c),I=h.dotVec3(c,u),y=p*f-A*A;if(0===y)return null;const m=1/y,v=(f*d-A*I)*m,w=(p*I-A*d)*m;return l[0]=1-v-w,l[1]=w,l[2]=v,l}})(),barycentricInsideTriangle(e){const t=e[1],s=e[2];return s>=0&&t>=0&&s+t<1},barycentricToCartesian(e,t,s,n,i=h.vec3()){const a=e[0],r=e[1],l=e[2];return i[0]=t[0]*a+s[0]*r+n[0]*l,i[1]=t[1]*a+s[1]*r+n[1]*l,i[2]=t[2]*a+s[2]*r+n[2]*l,i},mergeVertices(e,t,s,n){const i={},a=[],r=[],l=t?[]:null,o=s?[]:null,c=[];let u,h,p,A;const d=1e4;let f,I,y=0;for(f=0,I=e.length;f<I;f+=3)u=e[f],h=e[f+1],p=e[f+2],A=`${Math.round(u*d)}_${Math.round(h*d)}_${Math.round(p*d)}`,void 0===i[A]&&(i[A]=r.length/3,r.push(u),r.push(h),r.push(p),t&&(l.push(t[f]),l.push(t[f+1]),l.push(t[f+2])),s&&(o.push(s[y]),o.push(s[y+1]))),a[f/3]=i[A],y+=2;for(f=0,I=n.length;f<I;f++)c[f]=a[n[f]];const m={positions:r,indices:c};return l&&(m.normals=l),o&&(m.uv=o),m},buildNormals:(()=>{const e=new l(3),t=new l(3),s=new l(3),n=new l(3),i=new l(3),a=new l(3);return(r,l,o)=>{let c,u;const p=new Array(r.length/3);let A,d,f,I,y,m,v;for(c=0,u=l.length;c<u;c+=3){A=l[c],d=l[c+1],f=l[c+2],e[0]=r[3*A],e[1]=r[3*A+1],e[2]=r[3*A+2],t[0]=r[3*d],t[1]=r[3*d+1],t[2]=r[3*d+2],s[0]=r[3*f],s[1]=r[3*f+1],s[2]=r[3*f+2],h.subVec3(t,e,n),h.subVec3(s,e,i);const o=h.vec3();h.normalizeVec3(h.cross3Vec3(n,i,a),o),p[A]||(p[A]=[]),p[d]||(p[d]=[]),p[f]||(p[f]=[]),p[A].push(o),p[d].push(o),p[f].push(o)}for(o=o&&o.length===r.length?o:new Float32Array(r.length),c=0,u=p.length;c<u;c++){I=p[c].length,y=0,m=0,v=0;for(let e=0;e<I;e++)y+=p[c][e][0],m+=p[c][e][1],v+=p[c][e][2];o[3*c]=y/I,o[3*c+1]=m/I,o[3*c+2]=v/I}return o}})(),buildTangents:(()=>{const e=new l(3),t=new l(3),s=new l(3),n=new l(3),i=new l(3),a=new l(3),r=new l(3);return(l,o,c)=>{const u=new Float32Array(l.length);for(let p=0;p<o.length;p+=3){let A=o[p];const d=l.subarray(3*A,3*A+3),f=c.subarray(2*A,2*A+2);A=o[p+1];const I=l.subarray(3*A,3*A+3),y=c.subarray(2*A,2*A+2);A=o[p+2];const m=l.subarray(3*A,3*A+3),v=c.subarray(2*A,2*A+2),w=h.subVec3(I,d,e),g=h.subVec3(m,d,t),T=h.subVec2(y,f,s),E=h.subVec2(v,f,n),b=1/(T[0]*E[1]-T[1]*E[0]),D=h.mulVec3Scalar(h.subVec3(h.mulVec3Scalar(w,E[1],i),h.mulVec3Scalar(g,T[1],a),r),b,a);let P;for(let e=0;e<3;e++)P=3*o[p+e],u[P]+=D[0],u[P+1]+=D[1],u[P+2]+=D[2]}return u}})(),buildPickTriangles(e,t,s){const n=t.length,i=s?new Uint16Array(9*n):new Float32Array(9*n),a=new Uint8Array(12*n);let r,l,o,c,u,h,p=0,A=0,d=0;for(let s=0;s<n;s+=3)h=p>>24&255,u=p>>16&255,c=p>>8&255,o=255&p,l=t[s],r=3*l,i[A++]=e[r],i[A++]=e[r+1],i[A++]=e[r+2],a[d++]=o,a[d++]=c,a[d++]=u,a[d++]=h,l=t[s+1],r=3*l,i[A++]=e[r],i[A++]=e[r+1],i[A++]=e[r+2],a[d++]=o,a[d++]=c,a[d++]=u,a[d++]=h,l=t[s+2],r=3*l,i[A++]=e[r],i[A++]=e[r+1],i[A++]=e[r+2],a[d++]=o,a[d++]=c,a[d++]=u,a[d++]=h,p++;return{positions:i,colors:a}},faceToVertexNormals(e,t,s={}){const n=s.smoothNormalsAngleThreshold||20,i={},a=[],r={};let l,o,c,u,p;const A=1e4;let d,f,I,y,m,v;for(f=0,y=e.length;f<y;f+=3){d=f/3,o=e[f],c=e[f+1],u=e[f+2],p=`${Math.round(o*A)}_${Math.round(c*A)}_${Math.round(u*A)}`,void 0===i[p]?i[p]=[d]:i[p].push(d);const s=h.normalizeVec3([t[f],t[f+1],t[f+2]]);a[d]=s,l=h.vec4([s[0],s[1],s[2],1]),r[d]=l}for(p in i)if(i.hasOwnProperty(p)){const e=i[p],t=e.length;for(f=0;f<t;f++){const s=e[f];for(l=r[s],I=0;I<t;I++){if(f===I)continue;const t=e[I];m=a[s],v=a[t];Math.abs(h.angleVec3(m,v)/h.DEGTORAD)<n&&(l[0]+=v[0],l[1]+=v[1],l[2]+=v[2],l[3]+=1)}}}for(f=0,y=t.length;f<y;f+=3)l=r[f/3],t[f+0]=l[0]/l[3],t[f+1]=l[1]/l[3],t[f+2]=l[2]/l[3]},transformRay:(()=>{const e=new l(4),t=new l(4);return(s,n,i,a,r)=>{e[0]=n[0],e[1]=n[1],e[2]=n[2],e[3]=1,h.transformVec4(s,e,t),a[0]=t[0],a[1]=t[1],a[2]=t[2],e[0]=i[0],e[1]=i[1],e[2]=i[2],h.transformVec3(s,e,t),h.normalizeVec3(t),r[0]=t[0],r[1]=t[1],r[2]=t[2]}})(),canvasPosToWorldRay:(()=>{const e=new l(16),t=new l(16),s=new l(4),n=new l(4),i=new l(4),a=new l(4);return(r,l,o,c,u,p)=>{const A=h.mulMat4(o,l,e),d=h.inverseMat4(A,t),f=r.width,I=r.height,y=(c[0]-f/2)/(f/2),m=-(c[1]-I/2)/(I/2);s[0]=y,s[1]=m,s[2]=-1,s[3]=1,h.transformVec4(d,s,n),h.mulVec4Scalar(n,1/n[3]),i[0]=y,i[1]=m,i[2]=1,i[3]=1,h.transformVec4(d,i,a),h.mulVec4Scalar(a,1/a[3]),u[0]=a[0],u[1]=a[1],u[2]=a[2],h.subVec3(a,n,p),h.normalizeVec3(p)}})(),canvasPosToLocalRay:(()=>{const e=new l(3),t=new l(3);return(s,n,i,a,r,l,o)=>{h.canvasPosToWorldRay(s,n,i,r,e,t),h.worldRayToLocalRay(a,e,t,l,o)}})(),worldRayToLocalRay:(()=>{const e=new l(16),t=new l(4),s=new l(4);return(n,i,a,r,l)=>{const o=h.inverseMat4(n,e);t[0]=i[0],t[1]=i[1],t[2]=i[2],t[3]=1,h.transformVec4(o,t,s),r[0]=s[0],r[1]=s[1],r[2]=s[2],h.transformVec3(o,a,l)}})(),buildKDTree:(()=>{const e=new Float32Array;function t(s,n,i,a){const r=new l(6),o={triangles:null,left:null,right:null,leaf:!1,splitDim:0,aabb:r};let c,u;for(r[0]=r[1]=r[2]=Number.POSITIVE_INFINITY,r[3]=r[4]=r[5]=Number.NEGATIVE_INFINITY,c=0,u=s.length;c<u;++c){var h=3*s[c];for(let e=0;e<3;++e){const t=3*n[h+e];i[t]<r[0]&&(r[0]=i[t]),i[t]>r[3]&&(r[3]=i[t]),i[t+1]<r[1]&&(r[1]=i[t+1]),i[t+1]>r[4]&&(r[4]=i[t+1]),i[t+2]<r[2]&&(r[2]=i[t+2]),i[t+2]>r[5]&&(r[5]=i[t+2])}}if(s.length<20||a>10)return o.triangles=s,o.leaf=!0,o;e[0]=r[3]-r[0],e[1]=r[4]-r[1],e[2]=r[5]-r[2];let p=0;e[1]>e[p]&&(p=1),e[2]>e[p]&&(p=2),o.splitDim=p;const A=(r[p]+r[p+3])/2,d=new Array(s.length);let f=0;const I=new Array(s.length);let y=0;for(c=0,u=s.length;c<u;++c){const e=n[h=3*s[c]],t=3*n[h+1],a=3*n[h+2];i[3*e+p]<=A||i[t+p]<=A||i[a+p]<=A?d[f++]=s[c]:I[y++]=s[c]}return d.length=f,I.length=y,o.left=t(d,n,i,a+1),o.right=t(I,n,i,a+1),o}return(e,s)=>{const n=e.length/3,i=new Array(n);for(let e=0;e<n;++e)i[e]=e;return t(i,e,s,0)}})(),decompressPosition(e,t,s){(s=s||e)[0]=e[0]*t[0]+t[12],s[1]=e[1]*t[5]+t[13],s[2]=e[2]*t[10]+t[14]},decompressPositions(e,t,s=new Float32Array(e.length)){for(let n=0,i=e.length;n<i;n+=3)s[n+0]=e[n+0]*t[0]+t[12],s[n+1]=e[n+1]*t[5]+t[13],s[n+2]=e[n+2]*t[10]+t[14];return s},decompressUV(e,t,s){s[0]=e[0]*t[0]+t[6],s[1]=e[1]*t[4]+t[7]},decompressUVs(e,t,s=new Float32Array(e.length)){for(let n=0,i=e.length;n<i;n+=3)s[n+0]=e[n+0]*t[0]+t[6],s[n+1]=e[n+1]*t[4]+t[7];return s},octDecodeVec2(e,t){let s=e[0],n=e[1];s=(2*s+1)/255,n=(2*n+1)/255;const i=1-Math.abs(s)-Math.abs(n);i<0&&(s=(1-Math.abs(n))*(s>=0?1:-1),n=(1-Math.abs(s))*(n>=0?1:-1));const a=Math.sqrt(s*s+n*n+i*i);return t[0]=s/a,t[1]=n/a,t[2]=i/a,t},octDecodeVec2s(e,t){for(let s=0,n=0,i=e.length;s<i;s+=2){let i=e[s+0],a=e[s+1];i=(2*i+1)/255,a=(2*a+1)/255;const r=1-Math.abs(i)-Math.abs(a);r<0&&(i=(1-Math.abs(a))*(i>=0?1:-1),a=(1-Math.abs(i))*(a>=0?1:-1));const l=Math.sqrt(i*i+a*a+r*r);t[n+0]=i/l,t[n+1]=a/l,t[n+2]=r/l,n+=3}return t}};h.buildEdgeIndices=function(){const e=[],t=[],s=[],n=[],i=[];let a=0;const r=new Uint16Array(3),l=new Uint16Array(3),o=new Uint16Array(3),c=h.vec3(),u=h.vec3(),p=h.vec3(),A=h.vec3(),d=h.vec3(),f=h.vec3(),I=h.vec3();return function(y,m,v,w){!function(i,a){const r={};let l,o,c,u;const h=Math.pow(10,4);let p,A,d=0;for(p=0,A=i.length;p<A;p+=3)l=i[p],o=i[p+1],c=i[p+2],u=Math.round(l*h)+"_"+Math.round(o*h)+"_"+Math.round(c*h),void 0===r[u]&&(r[u]=d/3,e[d++]=l,e[d++]=o,e[d++]=c),t[p/3]=r[u];for(p=0,A=a.length;p<A;p++)n[p]=t[a[p]],s[n[p]]=a[p]}(y,m),function(t,s){a=0;for(let y=0,m=t;y<m;y+=3){const t=3*n[y],m=3*n[y+1],v=3*n[y+2];s?(r[0]=e[t],r[1]=e[t+1],r[2]=e[t+2],l[0]=e[m],l[1]=e[m+1],l[2]=e[m+2],o[0]=e[v],o[1]=e[v+1],o[2]=e[v+2],h.decompressPosition(r,s,c),h.decompressPosition(l,s,u),h.decompressPosition(o,s,p)):(c[0]=e[t],c[1]=e[t+1],c[2]=e[t+2],u[0]=e[m],u[1]=e[m+1],u[2]=e[m+2],p[0]=e[v],p[1]=e[v+1],p[2]=e[v+2]),h.subVec3(p,u,A),h.subVec3(c,u,d),h.cross3Vec3(A,d,f),h.normalizeVec3(f,I);const w=i[a]||(i[a]={normal:h.vec3()});w.normal[0]=I[0],w.normal[1]=I[1],w.normal[2]=I[2],a++}}(m.length,v);const g=[],T=Math.cos(h.DEGTORAD*w),E={};let b,D,P,R,C,_,B,O,S,N,x,L=!1;for(let e=0,t=m.length;e<t;e+=3){const t=e/3;for(let s=0;s<3;s++)b=n[e+s],D=n[e+(s+1)%3],P=Math.min(b,D),R=Math.max(b,D),C=P+","+R,void 0===E[C]?E[C]={index1:P,index2:R,face1:t,face2:void 0}:E[C].face2=t}for(C in E)_=E[C],void 0!==_.face2&&(B=i[_.face1].normal,O=i[_.face2].normal,S=h.dotVec3(B,O),S>T)||(N=s[_.index1],x=s[_.index2],(!L&&N>65535||x>65535)&&(L=!0),g.push(N),g.push(x));return L?new Uint32Array(g):new Uint16Array(g)}}();class p{constructor(){this._head=[],this._headLength=0,this._tail=[],this._index=0,this._length=0}get length(){return this._length}shift(){if(this._index>=this._headLength){const e=this._head;if(e.length=0,this._head=this._tail,this._tail=e,this._index=0,this._headLength=this._head.length,!this._headLength)return}const e=this._head[this._index];return this._index<0?delete this._head[this._index++]:this._head[this._index++]=void 0,this._length--,e}push(e){return this._length++,this._tail.push(e),this}unshift(e){return this._head[--this._index]=e,this._length++,this}}const A={build:{version:"0.8"},client:{browser:navigator&&navigator.userAgent?navigator.userAgent:"n/a"},components:{scenes:0,models:0,meshes:0,objects:0},memory:{meshes:0,positions:0,colors:0,normals:0,uvs:0,indices:0,textures:0,transforms:0,materials:0,programs:0},frame:{frameCount:0,fps:0,useProgram:0,bindTexture:0,bindArray:0,drawElements:0,drawArrays:0,tasksRun:0,tasksScheduled:0}};var d=[["0",10],["A",26],["a",26],["_",1],["$",1]].map((function(e){for(var t=[],s=e[0].charCodeAt(0),n=s+e[1],i=s;i<n;++i)t.push(i);return String.fromCharCode.apply(null,t)})).join("");function f(e,t){return(t&&4!==t?[0,6]:[0,6,12,18]).map((function(t){return d.substr(parseInt(e/(1<<t))%64,1)})).reverse().join("")}const I=function(){for(var e={},t=window.location.search.substring(1).split("&"),s=0;s<t.length;s++){var n=t[s].split("=");if(void 0===e[n[0]])e[n[0]]=decodeURIComponent(n[1]);else if("string"==typeof e[n[0]]){var i=[e[n[0]],decodeURIComponent(n[1])];e[n[0]]=i}else e[n[0]].push(decodeURIComponent(n[1]))}return e}();const y={xmlToJson:function e(t,s){if(t.nodeType===t.TEXT_NODE){var n=t.nodeValue;if(null===n.match(/^\s+$/))return n}else if(t.nodeType===t.ELEMENT_NODE||t.nodeType===t.DOCUMENT_NODE){var i={type:t.nodeName,children:[]};if(t.nodeType===t.ELEMENT_NODE)for(var a=0;a<t.attributes.length;a++){var r=t.attributes[a];i[s[r.nodeName]||r.nodeName]=r.nodeValue}for(var l=0;l<t.childNodes.length;l++){(a=e(t.childNodes[l],s))&&i.children.push(a)}return i}},clone:function(e){return JSON.parse(JSON.stringify(e))},compressGuid:function(e){var t=[0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30].map((function(t){return parseInt(e.substr(t,2),16)}));return f(t[0],2)+[1,4,7,10,13].map((function(e){return f((t[e]<<16)+(t[e+1]<<8)+t[e+2])})).join("")},findNodeOfType:function(e,t){var s=[],n=function(e){e.type===t&&s.push(e),(e.children||[]).forEach((function(e){n(e)}))};return n(e),s},timeout:function(e){return new Promise((function(t,s){setTimeout(t,e)}))},httpRequest:function(e){return new Promise((function(t,s){var n=new XMLHttpRequest;n.open(e.method||"GET",e.url,!0),n.onload=function(i){console.log(e.url,n.readyState,n.status),4===n.readyState&&(200===n.status?t(n.responseXML):s(n.statusText))},n.send(null)}))},loadJSON:function(e,t,s){var n=e=>{};t=t||n,s=s||n;var i=new XMLHttpRequest;i.overrideMimeType("application/json"),i.open("GET",e,!0),i.addEventListener("load",(function(e){var n=e.target.response;if(200===this.status){var i;try{i=JSON.parse(n)}catch(e){s(`utils.loadJSON(): Failed to parse JSON response - ${e}`)}t(i)}else if(0===this.status){console.warn("loadFile: HTTP Status 0 received.");try{t(JSON.parse(n))}catch(e){s(`utils.loadJSON(): Failed to parse JSON response - ${e}`)}}else s(e)}),!1),i.addEventListener("error",(function(e){s(e)}),!1),i.send(null)},loadArraybuffer:function(e,t,s){var n=e=>{};t=t||n,s=s||n;const i=e.match(/^data:(.*?)(;base64)?,(.*)$/);if(i){const e=!!i[2];var a=i[3];a=window.decodeURIComponent(a),e&&(a=window.atob(a));try{const e=new ArrayBuffer(a.length),s=new Uint8Array(e);for(var r=0;r<a.length;r++)s[r]=a.charCodeAt(r);window.setTimeout((function(){t(e)}),0)}catch(e){window.setTimeout((function(){s(e)}),0)}}else{const n=new XMLHttpRequest;n.open("GET",e,!0),n.responseType="arraybuffer",n.onreadystatechange=function(){4===n.readyState&&(200===n.status?t(n.response):s("loadArrayBuffer error : "+n.response))},n.send(null)}},queryString:I,isArray:function(e){return e&&!e.propertyIsEnumerable("length")&&"object"==typeof e&&"number"==typeof e.length},isString:function(e){return"string"==typeof e||e instanceof String},isNumeric:function(e){return!isNaN(parseFloat(e))&&isFinite(e)},isID:function(e){return y.isString(e)||y.isNumeric(e)},isSameComponent:function(e,t){return!(!e||!t)&&(y.isNumeric(e)||y.isString(e)?`${e}`:e.id)===(y.isNumeric(t)||y.isString(t)?`${t}`:t.id)},isFunction:function(e){return"function"==typeof e},isObject:function(e){const t={}.constructor;return!!e&&e.constructor===t},copy:function(e){return y.apply(e,{})},apply:function(e,t){for(const s in e)e.hasOwnProperty(s)&&(t[s]=e[s]);return t},apply2:function(e,t){for(const s in e)e.hasOwnProperty(s)&&void 0!==e[s]&&null!==e[s]&&(t[s]=e[s]);return t},applyIf:function(e,t){for(const s in e)e.hasOwnProperty(s)&&(void 0!==t[s]&&null!==t[s]||(t[s]=e[s]));return t},isEmptyObject:function(e){for(const t in e)if(e.hasOwnProperty(t))return!1;return!0},inQuotes:function(e){return y.isNumeric(e)?`${e}`:`'${e}'`},concat:function(e,t){const s=new e.constructor(e.length+t.length);return s.set(e),s.set(t,e.length),s},flattenParentChildHierarchy:function(e){var t=[];return function e(s){s.id=s.uuid,delete s.oid,t.push(s);var n=s.children;if(n)for(var i=0,a=n.length;i<a;i++){n[i].parent=s.id,e(n[i])}s.children=[]}(e),t}},m={},v=new e,w=new p,g={sceneId:null,time:null,startTime:null,prevTime:null,deltaTime:null},T=[];let E,b=0,D=0;const P=new function(){this.version="1.0.0",this.scenes={},this._superTypes={},this._addScene=function(e){if(e.id){if(P.scenes[e.id])return void console.error(`[ERROR] Scene ${y.inQuotes(e.id)} already exists`)}else e.id=v.addItem({});P.scenes[e.id]=e;const t=e.ticksPerOcclusionTest,s=e.ticksPerRender;m[e.id]={ticksPerOcclusionTest:t,ticksPerRender:s,renderCountdown:s},A.components.scenes++,e.once("destroyed",(()=>{v.removeItem(e.id),delete P.scenes[e.id],delete m[e.id],A.components.scenes--}))},this.clear=function(){let e;for(const t in P.scenes)P.scenes.hasOwnProperty(t)&&(e=P.scenes[t],"default.scene"===t?e.clear():(e.destroy(),delete P.scenes[e.id]))},this.scheduleTask=function(e,t){w.push(e),w.push(t)},this.runTasks=function(e=-1){let t,s,n=(new Date).getTime(),i=0;for(;w.length>0&&(e<0||n<e);)t=w.shift(),s=w.shift(),s?t.call(s):t(),n=(new Date).getTime(),i++;return i},this.getNumTasks=function(){return w.length}},R=function(){let e=Date.now();if(E=e-b,b>0&&E>0){var t=1e3/E;D+=t,T.push(t),T.length>=30&&(D-=T.shift()),A.frame.fps=Math.round(D/T.length)}!function(e){const t=P.runTasks(e+10),s=P.getNumTasks();A.frame.tasksRun=t,A.frame.tasksScheduled=s,A.frame.tasksBudget=10}(e),function(e){for(var t in g.time=e,P.scenes)if(P.scenes.hasOwnProperty(t)){var s=P.scenes[t];g.sceneId=t,g.startTime=s.startTime,g.deltaTime=null!=g.prevTime?g.time-g.prevTime:0,s.fire("tick",g,!0)}g.prevTime=e}(e),function(){const e=P.scenes,t=!1;let s,n,i,a,r;for(r in e)e.hasOwnProperty(r)&&(s=e[r],n=m[r],n||(n=m[r]={}),i=s.ticksPerOcclusionTest,n.ticksPerOcclusionTest!==i&&(n.ticksPerOcclusionTest=i,n.renderCountdown=i),--s.occlusionTestCountdown<=0&&(s.doOcclusionTest(),s.occlusionTestCountdown=i),a=s.ticksPerRender,n.ticksPerRender!==a&&(n.ticksPerRender=a,n.renderCountdown=a),0==--n.renderCountdown&&(s.render(t),n.renderCountdown=a))}(),b=e,void 0!==window.requestPostAnimationFrame?window.requestPostAnimationFrame(R):requestAnimationFrame(R)};void 0!==window.requestPostAnimationFrame?window.requestPostAnimationFrame(R):requestAnimationFrame(R);class C{get type(){return"Component"}get isComponent(){return!0}constructor(e=null,t={}){if(this.scene=null,"Scene"===this.type)this.scene=this,this.viewer=t.viewer;else{if("Scene"===e.type)this.scene=e;else{if(!(e instanceof C))throw"Invalid param: owner must be a Component";this.scene=e.scene}this._owner=e}this._dontClear=!!t.dontClear,this._renderer=this.scene._renderer,this.meta=t.meta||{},this.id=t.id,this.destroyed=!1,this._attached={},this._attachments=null,this._subIdMap=null,this._subIdEvents=null,this._eventSubs=null,this._eventSubsNum=null,this._events=null,this._eventCallDepth=0,this._ownedComponents=null,this!==this.scene&&this.scene._addComponent(this),this._updateScheduled=!1,e&&e._own(this)}glRedraw(){this._renderer&&(this._renderer.imageDirty(),this.castsShadow&&this._renderer.shadowsDirty())}glResort(){this._renderer&&this._renderer.needStateSort()}get owner(){return this._owner}isType(e){return this.type===e}fire(e,t,s){this._events||(this._events={}),this._eventSubs||(this._eventSubs={},this._eventSubsNum={}),!0!==s&&(this._events[e]=t||!0);const n=this._eventSubs[e];let i;if(n)for(const s in n)n.hasOwnProperty(s)&&(i=n[s],this._eventCallDepth++,this._eventCallDepth<300?i.callback.call(i.scope,t):this.error("fire: potential stack overflow from recursive event '"+e+"' - dropping this event"),this._eventCallDepth--)}on(t,s,n){this._events||(this._events={}),this._subIdMap||(this._subIdMap=new e),this._subIdEvents||(this._subIdEvents={}),this._eventSubs||(this._eventSubs={}),this._eventSubsNum||(this._eventSubsNum={});let i=this._eventSubs[t];i?this._eventSubsNum[t]++:(i={},this._eventSubs[t]=i,this._eventSubsNum[t]=1);const a=this._subIdMap.addItem();i[a]={callback:s,scope:n||this},this._subIdEvents[a]=t;const r=this._events[t];return void 0!==r&&s.call(n||this,r),a}off(e){if(null==e)return;if(!this._subIdEvents)return;const t=this._subIdEvents[e];if(t){delete this._subIdEvents[e];const s=this._eventSubs[t];s&&(delete s[e],this._eventSubsNum[t]--),this._subIdMap.removeItem(e)}}once(e,t,s){const n=this,i=this.on(e,(function(e){n.off(i),t.call(s||this,e)}),s)}hasSubs(e){return this._eventSubsNum&&this._eventSubsNum[e]>0}log(e){e="[LOG]"+this._message(e),window.console.log(e),this.scene.fire("log",e)}_message(e){return" ["+this.type+" "+y.inQuotes(this.id)+"]: "+e}warn(e){e="[WARN]"+this._message(e),window.console.warn(e),this.scene.fire("warn",e)}error(e){e="[ERROR]"+this._message(e),window.console.error(e),this.scene.fire("error",e)}_attach(e){const t=e.name;if(!t)return void this.error("Component 'name' expected");let s=e.component;const n=e.sceneDefault,i=e.sceneSingleton,a=e.type,r=e.on,l=!1!==e.recompiles;if(s&&(y.isNumeric(s)||y.isString(s))){const e=s;if(s=this.scene.components[e],!s)return void this.error("Component not found: "+y.inQuotes(e))}if(!s)if(!0===i){const e=this.scene.types[a];for(const t in e)if(e.hasOwnProperty){s=e[t];break}if(!s)return this.error("Scene has no default component for '"+t+"'"),null}else if(!0===n&&(s=this.scene[t],!s))return this.error("Scene has no default component for '"+t+"'"),null;if(s){if(s.scene.id!==this.scene.id)return void this.error("Not in same scene: "+s.type+" "+y.inQuotes(s.id));if(a&&!s.isType(a))return void this.error("Expected a "+a+" type or subtype: "+s.type+" "+y.inQuotes(s.id))}this._attachments||(this._attachments={});const o=this._attached[t];let c,u,h;if(o){if(s&&o.id===s.id)return;const e=this._attachments[o.id];for(c=e.subs,u=0,h=c.length;u<h;u++)o.off(c[u]);delete this._attached[t],delete this._attachments[o.id];const n=e.params.onDetached;n&&(y.isFunction(n)?n(o):n.scope?n.callback.call(n.scope,o):n.callback(o)),e.managingLifecycle&&o.destroy()}if(s){const n={params:e,component:s,subs:[],managingLifecycle:false};n.subs.push(s.once("destroyed",(function(){n.params.component=null,this._attach(n.params)}),this)),l&&n.subs.push(s.on("dirty",(function(){this.fire("dirty",this)}),this)),this._attached[t]=s,this._attachments[s.id]=n;const i=e.onAttached;if(i&&(y.isFunction(i)?i(s):i.scope?i.callback.call(i.scope,s):i.callback(s)),r){let e,t,i,a;for(e in r)if(r.hasOwnProperty(e)){if(t=r[e],y.isFunction(t)?(i=t,a=null):(i=t.callback,a=t.scope),!i)continue;n.subs.push(s.on(e,i,a))}}}return l&&this.fire("dirty",this),this.fire(t,s),s}_checkComponent(e,t){if(!t.isComponent){if(!y.isID(t))return void this.error("Expected a Component or ID");{const e=t;if(!(t=this.scene.components[e]))return void this.error("Component not found: "+e)}}if(e===t.type){if(t.scene.id===this.scene.id)return t;this.error("Not in same scene: "+t.type)}else this.error("Expected a "+e+" Component")}_checkComponent2(e,t){if(!t.isComponent){if(!y.isID(t))return void this.error("Expected a Component or ID");{const e=t;if(!(t=this.scene.components[e]))return void this.error("Component not found: "+e)}}if(t.scene.id===this.scene.id){for(var s=0,n=e.length;s<n;s++)if(e[s]===t.type)return t;return this.error("Expected component types: "+e),null}this.error("Not in same scene: "+t.type)}_own(e){this._ownedComponents||(this._ownedComponents={}),this._ownedComponents[e.id]||(this._ownedComponents[e.id]=e),e.once("destroyed",(()=>{delete this._ownedComponents[e.id]}),this)}_needUpdate(e){this._updateScheduled||(this._updateScheduled=!0,0===e?this._doUpdate():P.scheduleTask(this._doUpdate,this))}_doUpdate(){this._updateScheduled&&(this._updateScheduled=!1,this._update&&this._update())}_update(){}clear(){if(this._ownedComponents)for(var e in this._ownedComponents)if(this._ownedComponents.hasOwnProperty(e)){this._ownedComponents[e].destroy(),delete this._ownedComponents[e]}}destroy(){if(this.destroyed)return;let e,t,s,n,i,a;if(this.fire("destroyed",this.destroyed=!0),this._attachments)for(e in this._attachments)if(this._attachments.hasOwnProperty(e)){for(t=this._attachments[e],s=t.component,n=t.subs,i=0,a=n.length;i<a;i++)s.off(n[i]);t.managingLifecycle&&s.destroy()}if(this._ownedComponents)for(e in this._ownedComponents)this._ownedComponents.hasOwnProperty(e)&&(s=this._ownedComponents[e],s.destroy(),delete this._ownedComponents[e]);this.scene._removeComponent(this),this._attached={},this._attachments=null,this._subIdMap=null,this._subIdEvents=null,this._eventSubs=null,this._events=null,this._eventCallDepth=0,this._ownedComponents=null,this._updateScheduled=!1}}const _=h.vec3(),B=function(){const e=new Float64Array(16),t=new Float64Array(4),s=new Float64Array(4);return function(n,i,a){return a=a||e,t[0]=i[0],t[1]=i[1],t[2]=i[2],t[3]=1,h.transformVec4(n,t,s),h.setMat4Translation(n,s,a),a.slice()}}();function O(e,t,s){const n=Float32Array.from([e[0]])[0],i=e[0]-n,a=Float32Array.from([e[1]])[0],r=e[1]-a,l=Float32Array.from([e[2]])[0],o=e[2]-l;t[0]=n,t[1]=a,t[2]=l,s[0]=i,s[1]=r,s[2]=o}function S(e,t,s,n=1e3){const i=h.getPositionsCenter(e,_),a=Math.round(i[0]/n)*n,r=Math.round(i[1]/n)*n,l=Math.round(i[2]/n)*n;s[0]=a,s[1]=r,s[2]=l;const o=0!==s[0]||0!==s[1]||0!==s[2];if(o)for(let s=0,n=e.length;s<n;s+=3)t[s+0]=e[s+0]-a,t[s+1]=e[s+1]-r,t[s+2]=e[s+2]-l;return o}function N(e,t,s,n){const i=h.dotVec3(t,s)+e,a=h.normalizeVec3(t,_);return h.mulVec3Scalar(a,-i,n),n}const x=1,L=4,M=8,F=16,H=32,U=256,G=512,V=1024,j=2048,k=new Float32Array([0,0,0]),Q=new Uint16Array([0,0,0]);class W{constructor(e,t,s,n,i,a,r){this._isObject=t,this.scene=e.scene,this.model=e,this.meshes=n,this._numPrimitives=0;for(let e=0,t=this.meshes.length;e<t;e++){const t=this.meshes[e];t.parent=this,this._numPrimitives+=t.numPrimitives}this.id=s,this.originalSystemId=h.unglobalizeObjectId(e.id,s),this._flags=i,this._aabb=a,this._offsetAABB=h.AABB3(a),this._offset=h.vec3(),this._colorizeUpdated=!1,this._opacityUpdated=!1,this._lodCullable=!!r,this._culled=!1,this._culledVFC=!1,this._culledLOD=!1,this._isObject&&e.scene._registerObject(this)}get isEntity(){return!0}get isModel(){return!1}get isObject(){return this._isObject}get aabb(){return this._offsetAABB}get numPrimitives(){return this._numPrimitives}get numTriangles(){return this._numPrimitives}get visible(){return this._getFlag(x)}set visible(e){if(!!(this._flags&x)!==e){this._flags=e?this._flags|x:this._flags&~x;for(let e=0,t=this.meshes.length;e<t;e++)this.meshes[e]._setVisible(this._flags);this._isObject&&this.model.scene._objectVisibilityUpdated(this),this.model.glRedraw()}}get highlighted(){return this._getFlag(G)}set highlighted(e){if(!!(this._flags&G)!==e){this._flags=e?this._flags|G:this._flags&~G;for(var t=0,s=this.meshes.length;t<s;t++)this.meshes[t]._setHighlighted(this._flags);this._isObject&&this.model.scene._objectHighlightedUpdated(this),this.model.glRedraw()}}get xrayed(){return this._getFlag(U)}set xrayed(e){if(!!(this._flags&U)!==e){this._flags=e?this._flags|U:this._flags&~U;for(let e=0,t=this.meshes.length;e<t;e++)this.meshes[e]._setXRayed(this._flags);this._isObject&&this.model.scene._objectXRayedUpdated(this),this.model.glRedraw()}}get selected(){return this._getFlag(V)}set selected(e){if(!!(this._flags&V)!==e){this._flags=e?this._flags|V:this._flags&~V;for(let e=0,t=this.meshes.length;e<t;e++)this.meshes[e]._setSelected(this._flags);this._isObject&&this.model.scene._objectSelectedUpdated(this),this.model.glRedraw()}}get edges(){return this._getFlag(j)}set edges(e){if(!!(this._flags&j)!==e){this._flags=e?this._flags|j:this._flags&~j;for(var t=0,s=this.meshes.length;t<s;t++)this.meshes[t]._setEdges(this._flags);this.model.glRedraw()}}get culledVFC(){return!!this._culledVFC}set culledVFC(e){this._culledVFC=e,this._setCulled()}get culledLOD(){return!!this._culledLOD}set culledLOD(e){this._culledLOD=e,this._setCulled()}get culled(){return!!this._culled}set culled(e){this._culled=e,this._setCulled()}_setCulled(){let e=!!this._culled||!(!this._culledLOD||!this._lodCullable)||!!this._culledVFC;if(!!(this._flags&L)!==e){this._flags=e?this._flags|L:this._flags&~L;for(var t=0,s=this.meshes.length;t<s;t++)this.meshes[t]._setCulled(this._flags);this.model.glRedraw()}}get clippable(){return this._getFlag(F)}set clippable(e){if(!!(this._flags&F)!==e){this._flags=e?this._flags|F:this._flags&~F;for(var t=0,s=this.meshes.length;t<s;t++)this.meshes[t]._setClippable(this._flags);this.model.glRedraw()}}get collidable(){return this._getFlag(H)}set collidable(e){if(!!(this._flags&H)!==e){this._flags=e?this._flags|H:this._flags&~H;for(var t=0,s=this.meshes.length;t<s;t++)this.meshes[t]._setCollidable(this._flags)}}get pickable(){return this._getFlag(M)}set pickable(e){if(!!(this._flags&M)!==e){this._flags=e?this._flags|M:this._flags&~M;for(var t=0,s=this.meshes.length;t<s;t++)this.meshes[t]._setPickable(this._flags)}}get colorize(){if(0===this.meshes.length)return null;const e=this.meshes[0]._colorize;return k[0]=e[0]/255,k[1]=e[1]/255,k[2]=e[2]/255,k}set colorize(e){if(e){Q[0]=Math.floor(255*e[0]),Q[1]=Math.floor(255*e[1]),Q[2]=Math.floor(255*e[2]);for(let e=0,t=this.meshes.length;e<t;e++)this.meshes[e]._setColorize(Q)}else for(let e=0,t=this.meshes.length;e<t;e++)this.meshes[e]._setColorize(null);if(this._isObject){const t=!!e;this.scene._objectColorizeUpdated(this,t),this._colorizeUpdated=t}this.model.glRedraw()}get opacity(){return this.meshes.length>0?this.meshes[0]._colorize[3]/255:1}set opacity(e){if(0===this.meshes.length)return;const t=null!=e,s=this.meshes[0]._colorize[3];let n=255;if(t){if(e<0?e=0:e>1&&(e=1),n=Math.floor(255*e),s===n)return}else if(n=255,s===n)return;for(let e=0,t=this.meshes.length;e<t;e++)this.meshes[e]._setOpacity(n,this._flags);this._isObject&&(this.scene._objectOpacityUpdated(this,t),this._opacityUpdated=t),this.model.glRedraw()}get offset(){return this._offset}set offset(e){e?(this._offset[0]=e[0],this._offset[1]=e[1],this._offset[2]=e[2]):(this._offset[0]=0,this._offset[1]=0,this._offset[2]=0);for(let e=0,t=this.meshes.length;e<t;e++)this.meshes[e]._setOffset(this._offset);this._offsetAABB[0]=this._aabb[0]+this._offset[0],this._offsetAABB[1]=this._aabb[1]+this._offset[1],this._offsetAABB[2]=this._aabb[2]+this._offset[2],this._offsetAABB[3]=this._aabb[3]+this._offset[0],this._offsetAABB[4]=this._aabb[4]+this._offset[1],this._offsetAABB[5]=this._aabb[5]+this._offset[2],this.scene._aabbDirty=!0,this.scene._objectOffsetUpdated(this,e),this.model._aabbDirty=!0,this.model.glRedraw()}get saoEnabled(){return this.model.saoEnabled}getEachVertex(e){for(let t=0,s=this.meshes.length;t<s;t++)this.meshes[t].getEachVertex(e)}_getFlag(e){return!!(this._flags&e)}_finalize(){const e=this.model.scene;this._isObject&&(this.visible&&e._objectVisibilityUpdated(this),this.highlighted&&e._objectHighlightedUpdated(this),this.xrayed&&e._objectXRayedUpdated(this),this.selected&&e._objectSelectedUpdated(this));for(let e=0,t=this.meshes.length;e<t;e++)this.meshes[e]._finalize(this._flags)}_finalize2(){for(let e=0,t=this.meshes.length;e<t;e++)this.meshes[e]._finalize2()}_destroy(){const e=this.model.scene;this._isObject&&(e._deregisterObject(this),this.visible&&e._objectVisibilityUpdated(this,!1),this.xrayed&&e._objectXRayedUpdated(this),this.selected&&e._objectSelectedUpdated(this),this.highlighted&&e._objectHighlightedUpdated(this),this._opacityUpdated&&this.scene._objectColorizeUpdated(this,!1),this._opacityUpdated&&this.scene._objectOpacityUpdated(this,!1),this.scene._objectOffsetUpdated(this,!1));for(let e=0,t=this.meshes.length;e<t;e++)this.meshes[e]._destroy();e._aabbDirty=!0}}const z=h.vec4(),K=h.vec4();class Y extends C{constructor(e,t){super(e,t),this._entity=null,this._visible=null,this._worldPos=h.vec3(),this._origin=h.vec3(),this._rtcPos=h.vec3(),this._viewPos=h.vec3(),this._canvasPos=h.vec2(),this._occludable=!1,this._onCameraViewMatrix=this.scene.camera.on("matrix",(()=>{this._viewPosDirty=!0,this._needUpdate()})),this._onCameraProjMatrix=this.scene.camera.on("projMatrix",(()=>{this._canvasPosDirty=!0,this._needUpdate()})),this._onEntityDestroyed=null,this._onEntityModelDestroyed=null,this._renderer.addMarker(this),this.entity=t.entity,this.worldPos=t.worldPos,this.occludable=t.occludable}_update(){if(this._viewPosDirty&&(h.transformPoint3(this.scene.camera.viewMatrix,this._worldPos,this._viewPos),this._viewPosDirty=!1,this._canvasPosDirty=!0,this.fire("viewPos",this._viewPos)),this._canvasPosDirty){z.set(this._viewPos),z[3]=1,h.transformPoint4(this.scene.camera.projMatrix,z,K);const e=this.scene.canvas.boundary;this._canvasPos[0]=Math.floor((1+K[0]/K[3])*e[2]/2),this._canvasPos[1]=Math.floor((1-K[1]/K[3])*e[3]/2),this._canvasPosDirty=!1,this.fire("canvasPos",this._canvasPos)}}_setVisible(e){this._visible,this._visible=e,this.fire("visible",this._visible)}set entity(e){if(this._entity){if(this._entity===e)return;null!==this._onEntityDestroyed&&(this._entity.off(this._onEntityDestroyed),this._onEntityDestroyed=null),null!==this._onEntityModelDestroyed&&(this._entity.model.off(this._onEntityModelDestroyed),this._onEntityModelDestroyed=null)}this._entity=e,this._entity&&(this._entity instanceof W?this._onEntityModelDestroyed=this._entity.model.on("destroyed",(()=>{this._entity=null,this._onEntityModelDestroyed=null})):this._onEntityDestroyed=this._entity.on("destroyed",(()=>{this._entity=null,this._onEntityDestroyed=null}))),this.fire("entity",this._entity,!0)}get entity(){return this._entity}set occludable(e){(e=!!e)!==this._occludable&&(this._occludable=e)}get occludable(){return this._occludable}set worldPos(e){this._worldPos.set(e||[0,0,0]),O(this._worldPos,this._origin,this._rtcPos),this._occludable&&this._renderer.markerWorldPosUpdated(this),this._viewPosDirty=!0,this.fire("worldPos",this._worldPos),this._needUpdate()}get worldPos(){return this._worldPos}get origin(){return this._origin}get rtcPos(){return this._rtcPos}get viewPos(){return this._update(),this._viewPos}get canvasPos(){return this._update(),this._canvasPos}get visible(){return!!this._visible}destroy(){this.fire("destroyed",!0),this.scene.camera.off(this._onCameraViewMatrix),this.scene.camera.off(this._onCameraProjMatrix),this._entity&&(null!==this._onEntityDestroyed&&this._entity.off(this._onEntityDestroyed),null!==this._onEntityModelDestroyed&&this._entity.model.off(this._onEntityModelDestroyed)),this._renderer.removeMarker(this),super.destroy()}}class X{constructor(e,t={}){this._color=t.color||"black",this._highlightClass="viewer-ruler-wire-highlighted",this._wire=document.createElement("div"),this._wire.className+=this._wire.className?" viewer-ruler-wire":"viewer-ruler-wire",this._wireClickable=document.createElement("div"),this._wireClickable.className+=this._wireClickable.className?" viewer-ruler-wire-clickable":"viewer-ruler-wire-clickable",this._thickness=t.thickness||1,this._thicknessClickable=t.thicknessClickable||6;var s=this._wire,n=s.style;n.border="solid "+this._thickness+"px "+this._color,n.position="absolute",n["z-index"]=void 0===t.zIndex?"2000001":t.zIndex,n.width="0px",n.height="0px",n.visibility="visible",n.top="0px",n.left="0px",n["-webkit-transform-origin"]="0 0",n["-moz-transform-origin"]="0 0",n["-ms-transform-origin"]="0 0",n["-o-transform-origin"]="0 0",n["transform-origin"]="0 0",n["-webkit-transform"]="rotate(0deg)",n["-moz-transform"]="rotate(0deg)",n["-ms-transform"]="rotate(0deg)",n["-o-transform"]="rotate(0deg)",n.transform="rotate(0deg)",n.opacity=1,n["pointer-events"]="none",t.onContextMenu,e.appendChild(s);var i=this._wireClickable,a=i.style;a.border="solid "+this._thicknessClickable+"px "+this._color,a.position="absolute",a["z-index"]=void 0===t.zIndex?"2000002":t.zIndex+1,a.width="0px",a.height="0px",a.visibility="visible",a.top="0px",a.left="0px",a["-webkit-transform-origin"]="0 0",a["-moz-transform-origin"]="0 0",a["-ms-transform-origin"]="0 0",a["-o-transform-origin"]="0 0",a["transform-origin"]="0 0",a["-webkit-transform"]="rotate(0deg)",a["-moz-transform"]="rotate(0deg)",a["-ms-transform"]="rotate(0deg)",a["-o-transform"]="rotate(0deg)",a.transform="rotate(0deg)",a.opacity=0,a["pointer-events"]="none",t.onContextMenu,e.appendChild(i),t.onMouseOver&&i.addEventListener("mouseover",(e=>{t.onMouseOver(e,this)})),t.onMouseLeave&&i.addEventListener("mouseleave",(e=>{t.onMouseLeave(e,this)})),t.onMouseWheel&&i.addEventListener("wheel",(e=>{t.onMouseWheel(e,this)})),t.onContextMenu&&i.addEventListener("contextmenu",(e=>{t.onContextMenu(e,this),e.preventDefault()})),this._x1=0,this._y1=0,this._x2=0,this._y2=0,this._update()}get _visible(){return"visible"===this._wire.style.visibility}_update(){var e=Math.abs(Math.sqrt((this._x1-this._x2)*(this._x1-this._x2)+(this._y1-this._y2)*(this._y1-this._y2))),t=180*Math.atan2(this._y2-this._y1,this._x2-this._x1)/Math.PI,s=this._wire.style;s.width=Math.round(e)+"px",s.left=Math.round(this._x1)+"px",s.top=Math.round(this._y1)+"px",s["-webkit-transform"]="rotate("+t+"deg)",s["-moz-transform"]="rotate("+t+"deg)",s["-ms-transform"]="rotate("+t+"deg)",s["-o-transform"]="rotate("+t+"deg)",s.transform="rotate("+t+"deg)";var n=this._wireClickable.style;n.width=Math.round(e)+"px",n.left=Math.round(this._x1)+"px",n.top=Math.round(this._y1)+"px",n["-webkit-transform"]="rotate("+t+"deg)",n["-moz-transform"]="rotate("+t+"deg)",n["-ms-transform"]="rotate("+t+"deg)",n["-o-transform"]="rotate("+t+"deg)",n.transform="rotate("+t+"deg)"}setStartAndEnd(e,t,s,n){this._x1=e,this._y1=t,this._x2=s,this._y2=n,this._update()}setColor(e){this._color=e||"black",this._wire.style.border="solid "+this._thickness+"px "+this._color}setOpacity(e){this._wire.style.opacity=e}setVisible(e){e=!!e,this._visible!==e&&(this._wire.style.visibility=e?"visible":"hidden")}setClickable(e){this._wireClickable.style["pointer-events"]=e?"all":"none"}setHighlighted(e){this._highlighted!==e&&(this._highlighted=!!e,this._highlighted?this._wire.classList.add(this._highlightClass):this._wire.classList.remove(this._highlightClass))}destroy(e){this._wire.parentElement&&this._wire.parentElement.removeChild(this._wire),this._wireClickable.parentElement&&this._wireClickable.parentElement.removeChild(this._wireClickable)}}class q{constructor(e,t={}){this._highlightClass="viewer-ruler-dot-highlighted",this._x=0,this._y=0,this._visible=!0,this._dot=document.createElement("div"),this._dot.className+=this._dot.className?" viewer-ruler-dot":"viewer-ruler-dot",this._dotClickable=document.createElement("div"),this._dotClickable.className+=this._dotClickable.className?" viewer-ruler-dot-clickable":"viewer-ruler-dot-clickable";var s=this._dot,n=s.style;n["border-radius"]="25px",n.border="solid 2px white",n.background="lightgreen",n.position="absolute",n["z-index"]=void 0===t.zIndex?"40000005":t.zIndex,n.width="8px",n.height="8px",n.visibility=!1!==t.visible?"visible":"hidden",n.top="0px",n.left="0px",n["box-shadow"]="0 2px 5px 0 #182A3D;",n.opacity=1,n["pointer-events"]="none",t.onContextMenu,e.appendChild(s);var i=this._dotClickable,a=i.style;a["border-radius"]="35px",a.border="solid 10px white",a.position="absolute",a["z-index"]=void 0===t.zIndex?"40000007":t.zIndex+1,a.width="8px",a.height="8px",a.visibility="visible",a.top="0px",a.left="0px",a.opacity=0,a["pointer-events"]="none",t.onContextMenu,e.appendChild(i),t.onMouseOver&&i.addEventListener("mouseover",(e=>{t.onMouseOver(e,this)})),t.onMouseLeave&&i.addEventListener("mouseleave",(e=>{t.onMouseLeave(e,this)})),t.onMouseWheel&&i.addEventListener("wheel",(e=>{t.onMouseWheel(e,this)})),t.onContextMenu&&i.addEventListener("contextmenu",(e=>{t.onContextMenu(e,this),e.preventDefault()})),this.setPos(t.x||0,t.y||0),this.setFillColor(t.fillColor),this.setBorderColor(t.borderColor)}setPos(e,t){this._x=e,this._y=t;var s=this._dot.style;s.left=Math.round(e)-4+"px",s.top=Math.round(t)-4+"px";var n=this._dotClickable.style;n.left=Math.round(e)-9+"px",n.top=Math.round(t)-9+"px"}setFillColor(e){this._dot.style.background=e||"lightgreen"}setBorderColor(e){this._dot.style.border="solid 2px"+(e||"black")}setOpacity(e){this._dot.style.opacity=e}setVisible(e){this._visible!==e&&(this._visible=!!e,this._dot.style.visibility=this._visible?"visible":"hidden")}setClickable(e){this._dotClickable.style["pointer-events"]=e?"all":"none"}setHighlighted(e){this._highlighted!==e&&(this._highlighted=!!e,this._highlighted?this._dot.classList.add(this._highlightClass):this._dot.classList.remove(this._highlightClass))}destroy(){this.setVisible(!1),this._dot.parentElement&&this._dot.parentElement.removeChild(this._dot),this._dotClickable.parentElement&&this._dotClickable.parentElement.removeChild(this._dotClickable)}}class J{constructor(e,t={}){this._highlightClass="viewer-ruler-label-highlighted",this._prefix=t.prefix||"",this._x=0,this._y=0,this._visible=!0,this._culled=!1,this._label=document.createElement("div"),this._label.className+=this._label.className?" viewer-ruler-label":"viewer-ruler-label";var s=this._label,n=s.style;n["border-radius"]="5px",n.color="white",n.padding="4px",n.border="solid 1px",n.background="lightgreen",n.position="absolute",n["z-index"]=void 0===t.zIndex?"5000005":t.zIndex,n.width="auto",n.height="auto",n.visibility="visible",n.top="0px",n.left="0px",n["pointer-events"]="all",n.opacity=1,t.onContextMenu,s.innerText="",e.appendChild(s),this.setPos(t.x||0,t.y||0),this.setFillColor(t.fillColor),this.setBorderColor(t.fillColor),this.setText(t.text),t.onMouseOver&&s.addEventListener("mouseover",(e=>{t.onMouseOver(e,this),e.preventDefault()})),t.onMouseLeave&&s.addEventListener("mouseleave",(e=>{t.onMouseLeave(e,this),e.preventDefault()})),t.onMouseWheel&&s.addEventListener("wheel",(e=>{t.onMouseWheel(e,this)})),t.onContextMenu&&s.addEventListener("contextmenu",(e=>{t.onContextMenu(e,this),e.preventDefault()}))}setPos(e,t){this._x=e,this._y=t;var s=this._label.style;s.left=Math.round(e)-20+"px",s.top=Math.round(t)-12+"px"}setPosOnWire(e,t,s,n){var i=e+.5*(s-e),a=t+.5*(n-t),r=this._label.style;r.left=Math.round(i)-20+"px",r.top=Math.round(a)-12+"px"}setPosBetweenWires(e,t,s,n,i,a){var r=(e+s+i)/3,l=(t+n+a)/3,o=this._label.style;o.left=Math.round(r)-20+"px",o.top=Math.round(l)-12+"px"}setText(e){this._label.innerHTML=this._prefix+(e||"")}setFillColor(e){this._fillColor=e||"lightgreen",this._label.style.background=this._fillColor}setBorderColor(e){this._borderColor=e||"black",this._label.style.border="solid 1px "+this._borderColor}setOpacity(e){this._label.style.opacity=e}setVisible(e){this._visible!==e&&(this._visible=!!e,this._label.style.visibility=this._visible&&!this._culled?"visible":"hidden")}setCulled(e){this._culled!==e&&(this._culled=!!e,this._label.style.visibility=this._visible&&!this._culled?"visible":"hidden")}setHighlighted(e){this._highlighted!==e&&(this._highlighted=!!e,this._highlighted?this._label.classList.add(this._highlightClass):this._label.classList.remove(this._highlightClass))}setClickable(e){this._label.style["pointer-events"]=e?"all":"none"}destroy(){this._label.parentElement&&this._label.parentElement.removeChild(this._label)}}var Z=h.vec3(),$=h.vec3();class ee extends C{constructor(e,t={}){if(super(e.viewer.scene,t),this.plugin=e,this._container=t.container,!this._container)throw"config missing: container";this._color=t.color||e.defaultColor;var s=this.plugin.viewer.scene;this._originMarker=new Y(s,t.origin),this._cornerMarker=new Y(s,t.corner),this._targetMarker=new Y(s,t.target),this._originWorld=h.vec3(),this._cornerWorld=h.vec3(),this._targetWorld=h.vec3(),this._wp=new Float64Array(12),this._vp=new Float64Array(12),this._pp=new Float64Array(12),this._cp=new Int16Array(6);const n=t.onMouseOver?e=>{t.onMouseOver(e,this)}:null,i=t.onMouseLeave?e=>{t.onMouseLeave(e,this)}:null,a=t.onContextMenu?e=>{t.onContextMenu(e,this)}:null,r=e=>{this.plugin.viewer.scene.canvas.canvas.dispatchEvent(new WheelEvent("wheel",e))};this._originDot=new q(this._container,{fillColor:this._color,zIndex:void 0!==e.zIndex?e.zIndex+2:void 0,onMouseOver:n,onMouseLeave:i,onMouseWheel:r,onContextMenu:a}),this._cornerDot=new q(this._container,{fillColor:this._color,zIndex:void 0!==e.zIndex?e.zIndex+2:void 0,onMouseOver:n,onMouseLeave:i,onMouseWheel:r,onContextMenu:a}),this._targetDot=new q(this._container,{fillColor:this._color,zIndex:void 0!==e.zIndex?e.zIndex+2:void 0,onMouseOver:n,onMouseLeave:i,onMouseWheel:r,onContextMenu:a}),this._originWire=new X(this._container,{color:this._color||"blue",thickness:1,zIndex:e.zIndex,onMouseOver:n,onMouseLeave:i,onMouseWheel:r,onContextMenu:a}),this._targetWire=new X(this._container,{color:this._color||"red",thickness:1,zIndex:void 0!==e.zIndex?e.zIndex+1:void 0,onMouseOver:n,onMouseLeave:i,onMouseWheel:r,onContextMenu:a}),this._angleLabel=new J(this._container,{fillColor:this._color||"#00BBFF",prefix:"",text:"",zIndex:e.zIndex+2,onMouseOver:n,onMouseLeave:i,onMouseWheel:r,onContextMenu:a}),this._wpDirty=!1,this._vpDirty=!1,this._cpDirty=!1,this._visible=!1,this._originVisible=!1,this._cornerVisible=!1,this._targetVisible=!1,this._originWireVisible=!1,this._targetWireVisible=!1,this._angleVisible=!1,this._labelsVisible=!1,this._clickable=!1,this._originMarker.on("worldPos",(e=>{this._originWorld.set(e||[0,0,0]),this._wpDirty=!0,this._needUpdate(0)})),this._cornerMarker.on("worldPos",(e=>{this._cornerWorld.set(e||[0,0,0]),this._wpDirty=!0,this._needUpdate(0)})),this._targetMarker.on("worldPos",(e=>{this._targetWorld.set(e||[0,0,0]),this._wpDirty=!0,this._needUpdate(0)})),this._onViewMatrix=s.camera.on("viewMatrix",(()=>{this._vpDirty=!0,this._needUpdate(0)})),this._onProjMatrix=s.camera.on("projMatrix",(()=>{this._cpDirty=!0,this._needUpdate()})),this._onCanvasBoundary=s.canvas.on("boundary",(()=>{this._cpDirty=!0,this._needUpdate(0)})),this.approximate=t.approximate,this.visible=t.visible,this.originVisible=t.originVisible,this.cornerVisible=t.cornerVisible,this.targetVisible=t.targetVisible,this.originWireVisible=t.originWireVisible,this.targetWireVisible=t.targetWireVisible,this.angleVisible=t.angleVisible,this.labelsVisible=t.labelsVisible}_update(){if(!this._visible)return;const e=this.plugin.viewer.scene;if(this._wpDirty&&(this._wp[0]=this._originWorld[0],this._wp[1]=this._originWorld[1],this._wp[2]=this._originWorld[2],this._wp[3]=1,this._wp[4]=this._cornerWorld[0],this._wp[5]=this._cornerWorld[1],this._wp[6]=this._cornerWorld[2],this._wp[7]=1,this._wp[8]=this._targetWorld[0],this._wp[9]=this._targetWorld[1],this._wp[10]=this._targetWorld[2],this._wp[11]=1,this._wpDirty=!1,this._vpDirty=!0),this._vpDirty&&(h.transformPositions4(e.camera.viewMatrix,this._wp,this._vp),this._vp[3]=1,this._vp[7]=1,this._vp[11]=1,this._vpDirty=!1,this._cpDirty=!0),this._cpDirty){const A=-.3,d=this._originMarker.viewPos[2],f=this._cornerMarker.viewPos[2],I=this._targetMarker.viewPos[2];if(d>A||f>A||I>A)return this._originDot.setVisible(!1),this._cornerDot.setVisible(!1),this._targetDot.setVisible(!1),this._originWire.setVisible(!1),this._targetWire.setVisible(!1),void this._angleLabel.setCulled(!0);h.transformPositions4(e.camera.project.matrix,this._vp,this._pp);var t=this._pp,s=this._cp,n=e.canvas.canvas.getBoundingClientRect();const y=this._container.getBoundingClientRect();for(var i=n.top-y.top,a=n.left-y.left,r=e.canvas.boundary,l=r[2],o=r[3],c=0,u=0,p=t.length;u<p;u+=4)s[c]=a+Math.floor((1+t[u+0]/t[u+3])*l/2),s[c+1]=i+Math.floor((1-t[u+1]/t[u+3])*o/2),c+=2;if(this._originDot.setPos(s[0],s[1]),this._cornerDot.setPos(s[2],s[3]),this._targetDot.setPos(s[4],s[5]),this._originWire.setStartAndEnd(s[0],s[1],s[2],s[3]),this._targetWire.setStartAndEnd(s[2],s[3],s[4],s[5]),this._angleLabel.setPosBetweenWires(s[0],s[1],s[2],s[3],s[4],s[5]),h.subVec3(this._originWorld,this._cornerWorld,Z),h.subVec3(this._targetWorld,this._cornerWorld,$),!(0===Z[0]&&0===Z[1]&&0===Z[2]||0===$[0]&&0===$[1]&&0===$[2])){const e=this._approximate?" ~ ":" = ";h.normalizeVec3(Z),h.normalizeVec3($);const t=Math.abs(h.angleVec3(Z,$));this._angle=t/h.DEGTORAD,this._angleLabel.setText(e+this._angle.toFixed(2)+"°")}else this._angleLabel.setText("");this._originDot.setVisible(this._visible&&this._originVisible),this._cornerDot.setVisible(this._visible&&this._cornerVisible),this._targetDot.setVisible(this._visible&&this._targetVisible),this._originWire.setVisible(this._visible&&this._originWireVisible),this._targetWire.setVisible(this._visible&&this._targetWireVisible),this._angleLabel.setCulled(!(this._visible&&this._angleVisible&&this.labelsVisible)),this._cpDirty=!1}}set approximate(e){e=!1!==e,this._approximate!==e&&(this._approximate=e,this._cpDirty=!0,this._needUpdate(0))}get approximate(){return this._approximate}get origin(){return this._originMarker}get corner(){return this._cornerMarker}get target(){return this._targetMarker}get angle(){return this._update(),this._angle}get color(){return this._color}set color(e){this._originDot.setFillColor(e),this._cornerDot.setFillColor(e),this._targetDot.setFillColor(e),this._originWire.setColor(e||"blue"),this._targetWire.setColor(e||"red"),this._angleLabel.setFillColor(e||"#00BBFF"),this._color=e}set visible(e){e=!1!==e,this._visible=e,this._originDot.setVisible(this._visible&&this._originVisible),this._cornerDot.setVisible(this._visible&&this._cornerVisible),this._targetDot.setVisible(this._visible&&this._targetVisible),this._originWire.setVisible(this._visible&&this._originWireVisible),this._targetWire.setVisible(this._visible&&this._targetWireVisible),this._angleLabel.setVisible(this._visible&&this._angleVisible),this._cpDirty=!0,this._needUpdate()}get visible(){return this._visible}set originVisible(e){e=!1!==e,this._originVisible=e,this._originDot.setVisible(this._visible&&this._originVisible),this._cpDirty=!0,this._needUpdate()}get originVisible(){return this._originVisible}set cornerVisible(e){e=!1!==e,this._cornerVisible=e,this._cornerDot.setVisible(this._visible&&this._cornerVisible),this._cpDirty=!0,this._needUpdate()}get cornerVisible(){return this._cornerVisible}set targetVisible(e){e=!1!==e,this._targetVisible=e,this._targetDot.setVisible(this._visible&&this._targetVisible),this._cpDirty=!0,this._needUpdate()}get targetVisible(){return this._targetVisible}set originWireVisible(e){e=!1!==e,this._originWireVisible=e,this._originWire.setVisible(this._visible&&this._originWireVisible),this._cpDirty=!0,this._needUpdate()}get originWireVisible(){return this._originWireVisible}set targetWireVisible(e){e=!1!==e,this._targetWireVisible=e,this._targetWire.setVisible(this._visible&&this._targetWireVisible),this._cpDirty=!0,this._needUpdate()}get targetWireVisible(){return this._targetWireVisible}set angleVisible(e){e=!1!==e,this._angleVisible=e,this._angleLabel.setVisible(this._visible&&this._angleVisible),this._cpDirty=!0,this._needUpdate()}get angleVisible(){return this._angleVisible}set labelsVisible(e){e=void 0!==e?Boolean(e):this.plugin.defaultLabelsVisible,this._labelsVisible=e;var t=this._visible&&this._labelsVisible;this._angleLabel.setVisible(t),this._cpDirty=!0,this._needUpdate()}get labelsVisible(){return this._labelsVisible}setHighlighted(e){this._originDot.setHighlighted(e),this._cornerDot.setHighlighted(e),this._targetDot.setHighlighted(e),this._originWire.setHighlighted(e),this._targetWire.setHighlighted(e),this._angleLabel.setHighlighted(e)}set clickable(e){e=!!e,this._clickable=e,this._originDot.setClickable(this._clickable),this._cornerDot.setClickable(this._clickable),this._targetDot.setClickable(this._clickable),this._originWire.setClickable(this._clickable),this._targetWire.setClickable(this._clickable),this._angleLabel.setClickable(this._clickable)}get clickable(){return this._clickable}destroy(){const e=this.plugin.viewer.scene;this._onViewMatrix&&e.camera.off(this._onViewMatrix),this._onProjMatrix&&e.camera.off(this._onProjMatrix),this._onCanvasBoundary&&e.canvas.off(this._onCanvasBoundary),this._originDot.destroy(),this._cornerDot.destroy(),this._targetDot.destroy(),this._originWire.destroy(),this._targetWire.destroy(),this._angleLabel.destroy(),super.destroy()}}class te extends C{constructor(e){super(e.viewer.scene),this.plugin=e,this._active=!1,this._state=0,this._currentAngleMeasurement=null;const t=document.createElement("div"),s=this.scene.canvas.canvas;s.parentNode.insertBefore(t,s),t.style.background="black",t.style.border="2px solid blue",t.style.borderRadius="10px",t.style.width="5px",t.style.height="5px",t.style.margin="-200px -200px",t.style.zIndex="100",t.style.position="absolute",t.style.pointerEvents="none",this.markerDiv=t,this._onMouseHoverSurface=null,this._onHoverNothing=null,this._onMouseHoverOff=null,this._onPickedNothing=null,this._onPickedSurface=null,this._onInputMouseDown=null,this._onInputMouseUp=null,this._onCanvasTouchStart=null,this._onCanvasTouchEnd=null}get active(){return this._active}activate(){if(this._active)return;const e=this.plugin,t=this.scene,s=e.viewer.cameraControl,n=t.canvas.canvas,i=t.input;let a=!1,r=null,l=h.vec3();const o=h.vec2();let c,u;const p=h.vec2(),A=h.vec2();h.vec3(),this._onMouseHoverSurface=s.on("hoverSnapOrSurface",(e=>{switch(a=!0,r=e.entity,l.set(e.worldPos),o.set(e.canvasPos),this._state){case 0:this.markerDiv.style.marginLeft=e.canvasPos[0]-5+"px",this.markerDiv.style.marginTop=e.canvasPos[1]-5+"px",this.markerDiv.style.background="pink",this.markerDiv.style.border="2px solid red";break;case 1:this._currentAngleMeasurement&&(this._currentAngleMeasurement.originWireVisible=!0,this._currentAngleMeasurement.targetWireVisible=!1,this._currentAngleMeasurement.cornerVisible=!0,this._currentAngleMeasurement.angleVisible=!1,this._currentAngleMeasurement.corner.worldPos=e.worldPos),this.markerDiv.style.marginLeft="-10000px",this.markerDiv.style.marginTop="-10000px",n.style.cursor="pointer";break;case 2:this._currentAngleMeasurement&&(this._currentAngleMeasurement.targetWireVisible=!0,this._currentAngleMeasurement.targetVisible=!0,this._currentAngleMeasurement.angleVisible=!0,this._currentAngleMeasurement.target.worldPos=e.worldPos),this.markerDiv.style.marginLeft="-10000px",this.markerDiv.style.marginTop="-10000px",n.style.cursor="pointer"}})),this._onInputMouseDown=i.on("mousedown",(e=>{c=e[0],u=e[1]})),this._onInputMouseUp=i.on("mouseup",(e=>{if(!(e[0]>c+5||e[0]<c-5||e[1]>u+5||e[1]<u-5))switch(this._state){case 0:a&&(this._currentAngleMeasurement=this.plugin.createMeasurement({id:h.createUUID(),origin:{entity:r,worldPos:l},corner:{entity:r,worldPos:l},target:{entity:r,worldPos:l},approximate:!0}),this._currentAngleMeasurement.originVisible=!0,this._currentAngleMeasurement.originWireVisible=!0,this._currentAngleMeasurement.cornerVisible=!1,this._currentAngleMeasurement.targetWireVisible=!1,this._currentAngleMeasurement.targetVisible=!1,this._currentAngleMeasurement.angleVisible=!1,this._state=1,this.fire("measurementStart",this._currentAngleMeasurement));break;case 1:a?(this._currentAngleMeasurement.targetWireVisible=!1,this._currentAngleMeasurement.targetVisible=!0,this._currentAngleMeasurement.angleVisible=!0,this._state=2):this._currentAngleMeasurement&&(this._currentAngleMeasurement.destroy(),this._currentAngleMeasurement=null,this._state=0,this.fire("measurementCancel",this._currentAngleMeasurement));break;case 2:a?(this._currentAngleMeasurement.targetVisible=!0,this._currentAngleMeasurement.angleVisible=!0,this.fire("measurementEnd",this._currentAngleMeasurement),this._currentAngleMeasurement=null,this._state=0):this._currentAngleMeasurement&&(this._currentAngleMeasurement.destroy(),this._currentAngleMeasurement=null,this._state=0,this.fire("measurementCancel",this._currentAngleMeasurement))}})),this._onMouseHoverOff=s.on("hoverSnapOrSurfaceOff",(e=>{if(a=!1,this.markerDiv.style.marginLeft="-100px",this.markerDiv.style.marginTop="-100px",this._currentAngleMeasurement){switch(this._state){case 0:this._currentAngleMeasurement.originVisible=!1;break;case 1:this._currentAngleMeasurement.cornerVisible=!1,this._currentAngleMeasurement.originWireVisible=!1,this._currentAngleMeasurement.targetVisible=!1,this._currentAngleMeasurement.targetWireVisible=!1,this._currentAngleMeasurement.angleVisible=!1;break;case 2:this._currentAngleMeasurement.targetVisible=!1,this._currentAngleMeasurement.targetWireVisible=!1,this._currentAngleMeasurement.angleVisible=!1}n.style.cursor="default"}})),n.addEventListener("touchstart",this._onCanvasTouchStart=e=>{const t=e.touches,s=e.changedTouches;1===t.length&&1===s.length&&se(t[0],p)},{passive:!0}),n.addEventListener("touchend",this._onCanvasTouchEnd=e=>{const s=e.touches,n=e.changedTouches;if(0===s.length&&1===n.length){if(se(n[0],A),A[0]>p[0]+5||A[0]<p[0]-5||A[1]>p[1]+5||A[1]<p[1]-5)return;const e=t.pick({canvasPos:A,pickSurface:!0});if(e&&e.worldPos)switch(this._state){case 0:this._currentAngleMeasurement=this.plugin.createMeasurement({id:h.createUUID(),origin:{entity:e.entity,worldPos:e.worldPos},corner:{entity:e.entity,worldPos:e.worldPos},target:{entity:e.entity,worldPos:e.worldPos},approximate:!0}),this._currentAngleMeasurement.originVisible=!0,this._currentAngleMeasurement.originWireVisible=!0,this._currentAngleMeasurement.cornerVisible=!1,this._currentAngleMeasurement.targetWireVisible=!1,this._currentAngleMeasurement.targetVisible=!1,this._currentAngleMeasurement.angleVisible=!1,this._currentAngleMeasurement.clickable=!1,this._state=1,this.fire("measurementStart",this._currentAngleMeasurement);break;case 1:this._currentAngleMeasurement.corner.worldPos=e.worldPos,this._currentAngleMeasurement.targetWireVisible=!1,this._currentAngleMeasurement.targetVisible=!0,this._currentAngleMeasurement.angleVisible=!0,this._state=2;break;case 2:this._currentAngleMeasurement.target.worldPos=e.worldPos,this._currentAngleMeasurement.targetVisible=!0,this._currentAngleMeasurement.angleVisible=!0,this._currentAngleMeasurement.clickable=!0,this.fire("measurementEnd",this._currentAngleMeasurement),this._currentAngleMeasurement=null,this._state=0}else this._currentAngleMeasurement&&(this._currentAngleMeasurement.destroy(),this._currentAngleMeasurement=null,this._state=0,this.fire("measurementCancel",this._currentAngleMeasurement))}},{passive:!0}),this._active=!0}deactivate(){if(!this._active)return;this.reset();const e=this.plugin.viewer.scene.input,t=this.plugin.viewer.cameraControl,s=this.plugin.viewer.scene.canvas.canvas;e.off(this._onInputMouseDown),e.off(this._onInputMouseUp),t.off(this._onMouseHoverSurface),t.off(this._onPickedSurface),t.off(this._onHoverNothing),t.off(this._onPickedNothing),s.removeEventListener("touchstart",this._onCanvasTouchStart),s.removeEventListener("touchend",this._onCanvasTouchEnd),this._currentAngleMeasurement=null,this._active=!1}reset(){this._active&&(this._currentAngleMeasurement&&(this._currentAngleMeasurement.destroy(),this._currentAngleMeasurement=null),this._state=0)}destroy(){this.deactivate(),super.destroy()}}const se=function(e,t){if(e){let s=e.target,n=0,i=0;for(;s.offsetParent;)n+=s.offsetLeft,i+=s.offsetTop,s=s.offsetParent;t[0]=e.pageX-n,t[1]=e.pageY-i}else e=window.event,t[0]=e.x,t[1]=e.y;return t};class ne extends Y{constructor(e,t){if(super(e,t),this.plugin=t.plugin,this._container=t.container,!this._container)throw"config missing: container";if(!t.markerElement&&!t.markerHTML)throw"config missing: need either markerElement or markerHTML";if(!t.labelElement&&!t.labelHTML)throw"config missing: need either labelElement or labelHTML";this._htmlDirty=!1,t.markerElement?(this._marker=t.markerElement,this._marker.addEventListener("click",this._onMouseClickedExternalMarker=()=>{this.plugin.fire("markerClicked",this)}),this._marker.addEventListener("mouseenter",this._onMouseEnterExternalMarker=()=>{this.plugin.fire("markerMouseEnter",this)}),this._marker.addEventListener("mouseleave",this._onMouseLeaveExternalMarker=()=>{this.plugin.fire("markerMouseLeave",this)}),this._markerExternal=!0):(this._markerHTML=t.markerHTML,this._htmlDirty=!0,this._markerExternal=!1),t.labelElement?(this._label=t.labelElement,this._labelExternal=!0):(this._labelHTML=t.labelHTML,this._htmlDirty=!0,this._labelExternal=!1),this._markerShown=!!t.markerShown,this._labelShown=!!t.labelShown,this._values=t.values||{},this._layoutDirty=!0,this._visibilityDirty=!0,this._buildHTML(),this._onTick=this.scene.on("tick",(()=>{this._htmlDirty&&(this._buildHTML(),this._htmlDirty=!1,this._layoutDirty=!0,this._visibilityDirty=!0),(this._layoutDirty||this._visibilityDirty)&&(this._markerShown||this._labelShown)&&(this._updatePosition(),this._layoutDirty=!1),this._visibilityDirty&&(this._marker.style.visibility=this.visible&&this._markerShown?"visible":"hidden",this._label.style.visibility=this.visible&&this._markerShown&&this._labelShown?"visible":"hidden",this._visibilityDirty=!1)})),this.on("canvasPos",(()=>{this._layoutDirty=!0})),this.on("visible",(()=>{this._visibilityDirty=!0})),this.setMarkerShown(!1!==t.markerShown),this.setLabelShown(t.labelShown),this.eye=t.eye?t.eye.slice():null,this.look=t.look?t.look.slice():null,this.up=t.up?t.up.slice():null,this.projection=t.projection}_buildHTML(){if(!this._markerExternal){this._marker&&(this._container.removeChild(this._marker),this._marker=null);let e=this._markerHTML||"<p></p>";y.isArray(e)&&(e=e.join("")),e=this._renderTemplate(e);const t=document.createRange().createContextualFragment(e);this._marker=t.firstChild,this._container.appendChild(this._marker),this._marker.style.visibility=this._markerShown?"visible":"hidden",this._marker.addEventListener("click",(()=>{this.plugin.fire("markerClicked",this)})),this._marker.addEventListener("mouseenter",(()=>{this.plugin.fire("markerMouseEnter",this)})),this._marker.addEventListener("mouseleave",(()=>{this.plugin.fire("markerMouseLeave",this)})),this._marker.addEventListener("wheel",(e=>{this.plugin.viewer.scene.canvas.canvas.dispatchEvent(new WheelEvent("wheel",e))}))}if(!this._labelExternal){this._label&&(this._container.removeChild(this._label),this._label=null);let e=this._labelHTML||"<p></p>";y.isArray(e)&&(e=e.join("")),e=this._renderTemplate(e);const t=document.createRange().createContextualFragment(e);this._label=t.firstChild,this._container.appendChild(this._label),this._label.style.visibility=this._markerShown&&this._labelShown?"visible":"hidden",this._label.addEventListener("wheel",(e=>{this.plugin.viewer.scene.canvas.canvas.dispatchEvent(new WheelEvent("wheel",e))}))}}_updatePosition(){const e=this.scene.canvas.boundary,t=e[0],s=e[1],n=this.canvasPos;this._marker.style.left=Math.floor(t+n[0])-12+"px",this._marker.style.top=Math.floor(s+n[1])-12+"px",this._marker.style["z-index"]=90005+Math.floor(this._viewPos[2])+1;this._label.style.left=20+Math.floor(t+n[0]+20)+"px",this._label.style.top=Math.floor(s+n[1]+-17)+"px",this._label.style["z-index"]=90005+Math.floor(this._viewPos[2])+1}_renderTemplate(e){for(var t in this._values)if(this._values.hasOwnProperty(t)){const s=this._values[t];e=e.replace(new RegExp("{{"+t+"}}","g"),s)}return e}setMarkerShown(e){e=!!e,this._markerShown!==e&&(this._markerShown=e,this._visibilityDirty=!0)}getMarkerShown(){return this._markerShown}setLabelShown(e){e=!!e,this._labelShown!==e&&(this._labelShown=e,this._visibilityDirty=!0)}getLabelShown(){return this._labelShown}setField(e,t){this._values[e]=t||"",this._htmlDirty=!0}getField(e){return this._values[e]}setValues(e){for(var t in e)if(e.hasOwnProperty(t)){const s=e[t];this.setField(t,s)}}getValues(){return this._values}destroy(){this._marker&&(this._markerExternal?(this._marker.removeEventListener("click",this._onMouseClickedExternalMarker),this._marker.removeEventListener("mouseenter",this._onMouseEnterExternalMarker),this._marker.removeEventListener("mouseleave",this._onMouseLeaveExternalMarker),this._marker=null):this._marker.parentNode.removeChild(this._marker)),this._label&&(this._labelExternal||this._label.parentNode.removeChild(this._label),this._label=null),this.scene.off(this._onTick),super.destroy()}}const ie=h.vec3(),ae=h.vec3(),re=h.vec3();class le extends C{get type(){return"Spinner"}constructor(e,t={}){super(e,t),this._canvas=t.canvas,this._element=null,this._isCustom=!1,t.elementId&&(this._element=document.getElementById(t.elementId),this._element?this._adjustPosition():this.error("Can't find given Spinner HTML element: '"+t.elementId+"' - will automatically create default element")),this._element||this._createDefaultSpinner(),this.processes=0}_createDefaultSpinner(){this._injectDefaultCSS();const e=document.createElement("div"),t=e.style;t["z-index"]="9000",t.position="absolute",e.innerHTML='<div class="sk-fading-circle"> <div class="sk-circle1 sk-circle"></div> <div class="sk-circle2 sk-circle"></div> <div class="sk-circle3 sk-circle"></div> <div class="sk-circle4 sk-circle"></div> <div class="sk-circle5 sk-circle"></div> <div class="sk-circle6 sk-circle"></div> <div class="sk-circle7 sk-circle"></div> <div class="sk-circle8 sk-circle"></div> <div class="sk-circle9 sk-circle"></div> <div class="sk-circle10 sk-circle"></div> <div class="sk-circle11 sk-circle"></div> <div class="sk-circle12 sk-circle"></div> </div>',this._canvas.parentElement.appendChild(e),this._element=e,this._isCustom=!1,this._adjustPosition()}_injectDefaultCSS(){const e="xeokit-spinner-css";if(document.getElementById(e))return;const t=document.createElement("style");t.innerHTML=".sk-fading-circle { background: transparent; margin: 20px auto; width: 50px; height:50px; position: relative; } .sk-fading-circle .sk-circle { width: 120%; height: 120%; position: absolute; left: 0; top: 0; } .sk-fading-circle .sk-circle:before { content: ''; display: block; margin: 0 auto; width: 15%; height: 15%; background-color: #ff8800; border-radius: 100%; -webkit-animation: sk-circleFadeDelay 1.2s infinite ease-in-out both; animation: sk-circleFadeDelay 1.2s infinite ease-in-out both; } .sk-fading-circle .sk-circle2 { -webkit-transform: rotate(30deg); -ms-transform: rotate(30deg); transform: rotate(30deg); } .sk-fading-circle .sk-circle3 { -webkit-transform: rotate(60deg); -ms-transform: rotate(60deg); transform: rotate(60deg); } .sk-fading-circle .sk-circle4 { -webkit-transform: rotate(90deg); -ms-transform: rotate(90deg); transform: rotate(90deg); } .sk-fading-circle .sk-circle5 { -webkit-transform: rotate(120deg); -ms-transform: rotate(120deg); transform: rotate(120deg); } .sk-fading-circle .sk-circle6 { -webkit-transform: rotate(150deg); -ms-transform: rotate(150deg); transform: rotate(150deg); } .sk-fading-circle .sk-circle7 { -webkit-transform: rotate(180deg); -ms-transform: rotate(180deg); transform: rotate(180deg); } .sk-fading-circle .sk-circle8 { -webkit-transform: rotate(210deg); -ms-transform: rotate(210deg); transform: rotate(210deg); } .sk-fading-circle .sk-circle9 { -webkit-transform: rotate(240deg); -ms-transform: rotate(240deg); transform: rotate(240deg); } .sk-fading-circle .sk-circle10 { -webkit-transform: rotate(270deg); -ms-transform: rotate(270deg); transform: rotate(270deg); } .sk-fading-circle .sk-circle11 { -webkit-transform: rotate(300deg); -ms-transform: rotate(300deg); transform: rotate(300deg); } .sk-fading-circle .sk-circle12 { -webkit-transform: rotate(330deg); -ms-transform: rotate(330deg); transform: rotate(330deg); } .sk-fading-circle .sk-circle2:before { -webkit-animation-delay: -1.1s; animation-delay: -1.1s; } .sk-fading-circle .sk-circle3:before { -webkit-animation-delay: -1s; animation-delay: -1s; } .sk-fading-circle .sk-circle4:before { -webkit-animation-delay: -0.9s; animation-delay: -0.9s; } .sk-fading-circle .sk-circle5:before { -webkit-animation-delay: -0.8s; animation-delay: -0.8s; } .sk-fading-circle .sk-circle6:before { -webkit-animation-delay: -0.7s; animation-delay: -0.7s; } .sk-fading-circle .sk-circle7:before { -webkit-animation-delay: -0.6s; animation-delay: -0.6s; } .sk-fading-circle .sk-circle8:before { -webkit-animation-delay: -0.5s; animation-delay: -0.5s; } .sk-fading-circle .sk-circle9:before { -webkit-animation-delay: -0.4s; animation-delay: -0.4s; } .sk-fading-circle .sk-circle10:before { -webkit-animation-delay: -0.3s; animation-delay: -0.3s; } .sk-fading-circle .sk-circle11:before { -webkit-animation-delay: -0.2s; animation-delay: -0.2s; } .sk-fading-circle .sk-circle12:before { -webkit-animation-delay: -0.1s; animation-delay: -0.1s; } @-webkit-keyframes sk-circleFadeDelay { 0%, 39%, 100% { opacity: 0; } 40% { opacity: 1; } } @keyframes sk-circleFadeDelay { 0%, 39%, 100% { opacity: 0; } 40% { opacity: 1; } }",t.id=e,document.body.appendChild(t)}_adjustPosition(){if(this._isCustom)return;const e=this._canvas,t=this._element,s=t.style;s.left=e.offsetLeft+.5*e.clientWidth-.5*t.clientWidth+"px",s.top=e.offsetTop+.5*e.clientHeight-.5*t.clientHeight+"px"}set processes(e){if(e=e||0,this._processes===e)return;if(e<0)return;const t=this._processes;this._processes=e;const s=this._element;s&&(s.style.visibility=this._processes>0?"visible":"hidden"),this.fire("processes",this._processes),0===this._processes&&this._processes!==t&&this.fire("zeroProcesses",this._processes)}get processes(){return this._processes}_destroy(){this._element&&!this._isCustom&&(this._element.parentNode.removeChild(this._element),this._element=null);const e=document.getElementById("xeokit-spinner-css");e&&e.parentNode.removeChild(e)}}const oe={WEBGL:!1,SUPPORTED_EXTENSIONS:{}},ce=document.createElement("canvas");if(ce){const e=ce.getContext("webgl",{antialias:!0})||ce.getContext("experimental-webgl",{antialias:!0});oe.WEBGL=!!e,oe.WEBGL&&(oe.ANTIALIAS=e.getContextAttributes().antialias,e.getShaderPrecisionFormat?e.getShaderPrecisionFormat(e.FRAGMENT_SHADER,e.HIGH_FLOAT).precision>0?oe.FS_MAX_FLOAT_PRECISION="highp":e.getShaderPrecisionFormat(e.FRAGMENT_SHADER,e.MEDIUM_FLOAT).precision>0?oe.FS_MAX_FLOAT_PRECISION="mediump":oe.FS_MAX_FLOAT_PRECISION="lowp":oe.FS_MAX_FLOAT_PRECISION="mediump",oe.DEPTH_BUFFER_BITS=e.getParameter(e.DEPTH_BITS),oe.MAX_TEXTURE_SIZE=e.getParameter(e.MAX_TEXTURE_SIZE),oe.MAX_CUBE_MAP_SIZE=e.getParameter(e.MAX_CUBE_MAP_TEXTURE_SIZE),oe.MAX_RENDERBUFFER_SIZE=e.getParameter(e.MAX_RENDERBUFFER_SIZE),oe.MAX_TEXTURE_UNITS=e.getParameter(e.MAX_COMBINED_TEXTURE_IMAGE_UNITS),oe.MAX_TEXTURE_IMAGE_UNITS=e.getParameter(e.MAX_TEXTURE_IMAGE_UNITS),oe.MAX_VERTEX_ATTRIBS=e.getParameter(e.MAX_VERTEX_ATTRIBS),oe.MAX_VERTEX_UNIFORM_VECTORS=e.getParameter(e.MAX_VERTEX_UNIFORM_VECTORS),oe.MAX_FRAGMENT_UNIFORM_VECTORS=e.getParameter(e.MAX_FRAGMENT_UNIFORM_VECTORS),oe.MAX_VARYING_VECTORS=e.getParameter(e.MAX_VARYING_VECTORS),e.getSupportedExtensions().forEach((function(e){oe.SUPPORTED_EXTENSIONS[e]=!0})))}const ue=["webgl2","experimental-webgl","webkit-3d","moz-webgl","moz-glweb20"];class he extends C{constructor(e,t={}){super(e,t),this._backgroundColor=h.vec3([t.backgroundColor?t.backgroundColor[0]:1,t.backgroundColor?t.backgroundColor[1]:1,t.backgroundColor?t.backgroundColor[2]:1]),this._backgroundColorFromAmbientLight=!!t.backgroundColorFromAmbientLight,this.canvas=t.canvas,this.gl=null,this.webgl2=!1,this.transparent=!!t.transparent,this.contextAttr=t.contextAttr||{},this.contextAttr.alpha=this.transparent,this.contextAttr.preserveDrawingBuffer=!!this.contextAttr.preserveDrawingBuffer,this.contextAttr.stencil=!1,this.contextAttr.premultipliedAlpha=!!this.contextAttr.premultipliedAlpha,this.contextAttr.antialias=!1!==this.contextAttr.antialias,this.resolutionScale=t.resolutionScale,this.canvas.width=Math.round(this.canvas.clientWidth*this._resolutionScale),this.canvas.height=Math.round(this.canvas.clientHeight*this._resolutionScale),this.boundary=[this.canvas.offsetLeft,this.canvas.offsetTop,this.canvas.clientWidth,this.canvas.clientHeight],this._initWebGL(t);const s=this;this.canvas.addEventListener("webglcontextlost",this._webglcontextlostListener=function(e){console.time("webglcontextrestored"),s.scene._webglContextLost(),s.fire("webglcontextlost"),e.preventDefault()},!1),this.canvas.addEventListener("webglcontextrestored",this._webglcontextrestoredListener=function(e){s._initWebGL(),s.gl&&(s.scene._webglContextRestored(s.gl),s.fire("webglcontextrestored",s.gl),e.preventDefault()),console.timeEnd("webglcontextrestored")},!1);let n=!0;new ResizeObserver((e=>{for(const t of e)t.contentBoxSize&&(n=!0)})).observe(this.canvas),this._tick=this.scene.on("tick",(()=>{n&&(n=!1,s.canvas.width=Math.round(s.canvas.clientWidth*s._resolutionScale),s.canvas.height=Math.round(s.canvas.clientHeight*s._resolutionScale),s.boundary[0]=s.canvas.offsetLeft,s.boundary[1]=s.canvas.offsetTop,s.boundary[2]=s.canvas.clientWidth,s.boundary[3]=s.canvas.clientHeight,s.fire("boundary",s.boundary))})),this._spinner=new le(this.scene,{canvas:this.canvas,elementId:t.spinnerElementId})}get type(){return"Canvas"}get backgroundColorFromAmbientLight(){return this._backgroundColorFromAmbientLight}set backgroundColorFromAmbientLight(e){this._backgroundColorFromAmbientLight=!1!==e,this.glRedraw()}get backgroundColor(){return this._backgroundColor}set backgroundColor(e){e?(this._backgroundColor[0]=e[0],this._backgroundColor[1]=e[1],this._backgroundColor[2]=e[2]):(this._backgroundColor[0]=1,this._backgroundColor[1]=1,this._backgroundColor[2]=1),this.glRedraw()}get resolutionScale(){return this._resolutionScale}set resolutionScale(e){if((e=e||1)===this._resolutionScale)return;this._resolutionScale=e;const t=this.canvas;t.width=Math.round(t.clientWidth*this._resolutionScale),t.height=Math.round(t.clientHeight*this._resolutionScale),this.glRedraw()}get spinner(){return this._spinner}_createCanvas(){const e="xeokit-canvas-"+h.createUUID(),t=document.getElementsByTagName("body")[0],s=document.createElement("div"),n=s.style;n.height="100%",n.width="100%",n.padding="0",n.margin="0",n.background="rgba(0,0,0,0);",n.float="left",n.left="0",n.top="0",n.position="absolute",n.opacity="1.0",n["z-index"]="-10000",s.innerHTML+='<canvas id="'+e+'" style="width: 100%; height: 100%; float: left; margin: 0; padding: 0;"></canvas>',t.appendChild(s),this.canvas=document.getElementById(e)}_getElementXY(e){let t=0,s=0;for(;e;)t+=e.offsetLeft-e.scrollLeft,s+=e.offsetTop-e.scrollTop,e=e.offsetParent;return{x:t,y:s}}_initWebGL(){if(!this.gl)for(let e=0;!this.gl&&e<ue.length;e++)try{this.gl=this.canvas.getContext(ue[e],this.contextAttr)}catch(e){}this.gl||(this.error("Failed to get a WebGL context"),this.fire("webglContextFailed",!0,!0));{const e=this.gl;let t="__",s=e.activeTexture;e.activeTexture=function(e){t!==e&&(t=e,s.call(this,e))};let n={},i=e.bindTexture;e.bindTexture=function(e,s){n[t]!==s&&(n[t]=s,i.call(this,e,s))}}this.gl&&this.webgl2&&(this.gl.hint(this.gl.FRAGMENT_SHADER_DERIVATIVE_HINT,this.gl.FASTEST),this.gl,WebGL2RenderingContext)}getSnapshot(e){throw"Canvas#getSnapshot() has been replaced by Viewer#getSnapshot() - use that method instead."}readPixels(e,t,s,n){return this.scene._renderer.readPixels(e,t,s,n)}loseWebGLContext(){this.canvas.loseContext&&this.canvas.loseContext()}destroy(){this.scene.off(this._tick),this._spinner._destroy(),this.canvas.removeEventListener("webglcontextlost",this._webglcontextlostListener),this.canvas.removeEventListener("webglcontextrestored",this._webglcontextrestoredListener),this.gl=null,super.destroy()}}class pe{constructor(e){this._scene=e,this._matPool=[],this._matPoolNextFreeIndex=0,this._rtcViewMats={},this._rtcPickViewMats={},this.reset()}reset(){this._matPoolNextFreeIndex=0,this._rtcViewMats={},this._rtcPickViewMats={},this.gl=this._scene.canvas.gl,this.lastProgramId=null,this.pbrEnabled=!1,this.colorTextureEnabled=!1,this.withSAO=!1,this.backfaces=!1,this.frontface=!0,this.textureUnit=0,this.drawElements=0,this.drawArrays=0,this.useProgram=0,this.bindTexture=0,this.bindArray=0,this.pass=0,this.shadowViewMatrix=null,this.shadowProjMatrix=null,this.pickViewMatrix=null,this.pickProjMatrix=null,this.pickZNear=.01,this.pickZFar=5e3,this.pickInvisible=!1,this.pickElementsCount=null,this.pickElementsOffset=null,this.lineWidth=1,this.snapPickLayerParams={},this.snapPickLayerNumber=0,this.snapPickCoordinateScale=h.vec3(),this.snapPickOrigin=h.vec3()}getRTCViewMatrix(e,t){let s=this._rtcViewMats[e];return s||(s=this._getNewMat(),B(this._scene.camera.viewMatrix,t,s),this._rtcViewMats[e]=s),s}getRTCPickViewMatrix(e,t){let s=this._rtcPickViewMats[e];if(!s){s=this._getNewMat();const n=this.pickViewMatrix||this._scene.camera.viewMatrix;B(n,t,s),this._rtcPickViewMats[e]=s}return s}_getNewMat(){let e=this._matPool[this._matPoolNextFreeIndex];return e||(e=h.mat4(),this._matPool[this._matPoolNextFreeIndex]=e),this._matPoolNextFreeIndex++,e}}class Ae{constructor(){this.entity=null,this.primitive=null,this.primIndex=-1,this.pickSurfacePrecision=!1,this.touchInput=!1,this._canvasPos=new Int16Array([0,0]),this._origin=new Float64Array([0,0,0]),this._direction=new Float64Array([0,0,0]),this._indices=new Int32Array(3),this._localPos=new Float64Array([0,0,0]),this._worldPos=new Float64Array([0,0,0]),this._viewPos=new Float64Array([0,0,0]),this._bary=new Float64Array([0,0,0]),this._worldNormal=new Float64Array([0,0,0]),this._uv=new Float64Array([0,0]),this.reset()}get canvasPos(){return this._gotCanvasPos?this._canvasPos:null}set canvasPos(e){e?(this._canvasPos[0]=e[0],this._canvasPos[1]=e[1],this._gotCanvasPos=!0):this._gotCanvasPos=!1}get origin(){return this._gotOrigin?this._origin:null}set origin(e){e?(this._origin[0]=e[0],this._origin[1]=e[1],this._origin[2]=e[2],this._gotOrigin=!0):this._gotOrigin=!1}get direction(){return this._gotDirection?this._direction:null}set direction(e){e?(this._direction[0]=e[0],this._direction[1]=e[1],this._direction[2]=e[2],this._gotDirection=!0):this._gotDirection=!1}get indices(){return this.entity&&this._gotIndices?this._indices:null}set indices(e){e?(this._indices[0]=e[0],this._indices[1]=e[1],this._indices[2]=e[2],this._gotIndices=!0):this._gotIndices=!1}get localPos(){return this.entity&&this._gotLocalPos?this._localPos:null}set localPos(e){e?(this._localPos[0]=e[0],this._localPos[1]=e[1],this._localPos[2]=e[2],this._gotLocalPos=!0):this._gotLocalPos=!1}get worldPos(){return this._gotWorldPos?this._worldPos:null}set worldPos(e){e?(this._worldPos[0]=e[0],this._worldPos[1]=e[1],this._worldPos[2]=e[2],this._gotWorldPos=!0):this._gotWorldPos=!1}get viewPos(){return this.entity&&this._gotViewPos?this._viewPos:null}set viewPos(e){e?(this._viewPos[0]=e[0],this._viewPos[1]=e[1],this._viewPos[2]=e[2],this._gotViewPos=!0):this._gotViewPos=!1}get bary(){return this.entity&&this._gotBary?this._bary:null}set bary(e){e?(this._bary[0]=e[0],this._bary[1]=e[1],this._bary[2]=e[2],this._gotBary=!0):this._gotBary=!1}get worldNormal(){return this.entity&&this._gotWorldNormal?this._worldNormal:null}set worldNormal(e){e?(this._worldNormal[0]=e[0],this._worldNormal[1]=e[1],this._worldNormal[2]=e[2],this._gotWorldNormal=!0):this._gotWorldNormal=!1}get uv(){return this.entity&&this._gotUV?this._uv:null}set uv(e){e?(this._uv[0]=e[0],this._uv[1]=e[1],this._gotUV=!0):this._gotUV=!1}reset(){this.entity=null,this.primIndex=-1,this.primitive=null,this.pickSurfacePrecision=!1,this._gotCanvasPos=!1,this._gotOrigin=!1,this._gotDirection=!1,this._gotIndices=!1,this._gotLocalPos=!1,this._gotWorldPos=!1,this._gotViewPos=!1,this._gotBary=!1,this._gotWorldNormal=!1,this._gotUV=!1,this.touchInput=!1}}class de{constructor(e,t,s){if(this.allocated=!1,this.compiled=!1,this.handle=e.createShader(t),this.handle){if(this.allocated=!0,e.shaderSource(this.handle,s),e.compileShader(this.handle),this.compiled=e.getShaderParameter(this.handle,e.COMPILE_STATUS),!this.compiled&&!e.isContextLost()){const t=s.split("\n"),n=[];for(let e=0;e<t.length;e++)n.push(e+1+": "+t[e]+"\n");this.errors=[],this.errors.push(""),this.errors.push(e.getShaderInfoLog(this.handle)),this.errors=this.errors.concat(n.join(""))}}else this.errors=["Failed to allocate"]}destroy(){}}class fe{constructor(e,t){this.bindTexture=function(s,n){return!!s.bind(n)&&(e.uniform1i(t,n),!0)}}}class Ie{constructor(e,t){this._gl=e,this.location=t}bindArrayBuffer(e){e&&(e.bind(),this._gl.enableVertexAttribArray(this.location),this._gl.vertexAttribPointer(this.location,e.itemSize,e.itemType,e.normalized,e.stride,e.offset))}}const ye=new e({});function me(e){const t=[];let s,n;for(let i=0,a=e.length;i<a;i++)s=e[i],n=s.indexOf("/"),n>0&&"/"===s.charAt(n+1)&&(s=s.substring(0,n)),t.push(s);return t.join("\n")}function ve(e){console.error(e.join("\n"))}class we{constructor(e,t){this.id=ye.addItem({}),this.source=t,this.init(e)}init(e){if(this.gl=e,this.allocated=!1,this.compiled=!1,this.linked=!1,this.validated=!1,this.errors=null,this.uniforms={},this.samplers={},this.attributes={},this._vertexShader=new de(e,e.VERTEX_SHADER,me(this.source.vertex)),this._fragmentShader=new de(e,e.FRAGMENT_SHADER,me(this.source.fragment)),!this._vertexShader.allocated)return this.errors=["Vertex shader failed to allocate"].concat(this._vertexShader.errors),void ve(this.errors);if(!this._fragmentShader.allocated)return this.errors=["Fragment shader failed to allocate"].concat(this._fragmentShader.errors),void ve(this.errors);if(this.allocated=!0,!this._vertexShader.compiled)return this.errors=["Vertex shader failed to compile"].concat(this._vertexShader.errors),void ve(this.errors);if(!this._fragmentShader.compiled)return this.errors=["Fragment shader failed to compile"].concat(this._fragmentShader.errors),void ve(this.errors);let t,s,n,i,a;if(this.compiled=!0,this.handle=e.createProgram(),!this.handle)return void(this.errors=["Failed to allocate program"]);if(e.attachShader(this.handle,this._vertexShader.handle),e.attachShader(this.handle,this._fragmentShader.handle),e.linkProgram(this.handle),this.linked=e.getProgramParameter(this.handle,e.LINK_STATUS),this.validated=!0,!this.linked||!this.validated)return this.errors=[],this.errors.push(""),this.errors.push(e.getProgramInfoLog(this.handle)),this.errors.push("\nVertex shader:\n"),this.errors=this.errors.concat(this.source.vertex),this.errors.push("\nFragment shader:\n"),this.errors=this.errors.concat(this.source.fragment),void ve(this.errors);const r=e.getProgramParameter(this.handle,e.ACTIVE_UNIFORMS);for(s=0;s<r;++s)n=e.getActiveUniform(this.handle,s),n&&(i=n.name,"\0"===i[i.length-1]&&(i=i.substr(0,i.length-1)),a=e.getUniformLocation(this.handle,i),n.type===e.SAMPLER_2D||n.type===e.SAMPLER_CUBE||35682===n.type||e instanceof WebGL2RenderingContext&&(n.type===e.UNSIGNED_INT_SAMPLER_2D||n.type===e.INT_SAMPLER_2D)?this.samplers[i]=new fe(e,a):this.uniforms[i]=a);const l=e.getProgramParameter(this.handle,e.ACTIVE_ATTRIBUTES);for(s=0;s<l;s++)t=e.getActiveAttrib(this.handle,s),t&&(a=e.getAttribLocation(this.handle,t.name),this.attributes[t.name]=new Ie(e,a));this.allocated=!0}bind(){this.allocated&&this.gl.useProgram(this.handle)}getLocation(e){if(this.allocated)return this.uniforms[e]}getAttribute(e){if(this.allocated)return this.attributes[e]}bindTexture(e,t,s){if(!this.allocated)return!1;const n=this.samplers[e];return!!n&&n.bindTexture(t,s)}destroy(){this.allocated&&(ye.removeItem(this.id),this.gl.deleteProgram(this.handle),this.gl.deleteShader(this._vertexShader.handle),this.gl.deleteShader(this._fragmentShader.handle),this.handle=null,this.attributes=null,this.uniforms=null,this.samplers=null,this.allocated=!1)}}class ge{constructor(e,t,s,n,i,a,r,l,o){switch(this._gl=e,this.type=t,this.allocated=!1,s.constructor){case Uint8Array:this.itemType=e.UNSIGNED_BYTE,this.itemByteSize=1;break;case Int8Array:this.itemType=e.BYTE,this.itemByteSize=1;break;case Uint16Array:this.itemType=e.UNSIGNED_SHORT,this.itemByteSize=2;break;case Int16Array:this.itemType=e.SHORT,this.itemByteSize=2;break;case Uint32Array:this.itemType=e.UNSIGNED_INT,this.itemByteSize=4;break;case Int32Array:this.itemType=e.INT,this.itemByteSize=4;break;default:this.itemType=e.FLOAT,this.itemByteSize=4}this.usage=a,this.length=0,this.dataLength=n,this.numItems=0,this.itemSize=i,this.normalized=!!r,this.stride=l||0,this.offset=o||0,this._allocate(s)}_allocate(e){if(this.allocated=!1,this._handle=this._gl.createBuffer(),!this._handle)throw"Failed to allocate WebGL ArrayBuffer";this._handle&&(this._gl.bindBuffer(this.type,this._handle),this._gl.bufferData(this.type,e.length>this.dataLength?e.slice(0,this.dataLength):e,this.usage),this._gl.bindBuffer(this.type,null),this.length=e.length,this.numItems=this.length/this.itemSize,this.allocated=!0)}setData(e,t){this.allocated&&(e.length+(t||0)>this.length?(this.destroy(),this._allocate(e)):(this._gl.bindBuffer(this.type,this._handle),t||0===t?this._gl.bufferSubData(this.type,t*this.itemByteSize,e):this._gl.bufferData(this.type,e,this.usage),this._gl.bindBuffer(this.type,null)))}bind(){this.allocated&&this._gl.bindBuffer(this.type,this._handle)}unbind(){this.allocated&&this._gl.bindBuffer(this.type,null)}destroy(){this.allocated&&(this._gl.deleteBuffer(this._handle),this._handle=null,this.allocated=!1)}}class Te{constructor(e,t){this.scene=e,this.aabb=h.AABB3(),this.origin=h.vec3(t),this.originHash=this.origin.join(),this.numMarkers=0,this.markers={},this.markerList=[],this.markerIndices={},this.positions=[],this.indices=[],this.positionsBuf=null,this.lenPositionsBuf=0,this.indicesBuf=null,this.sectionPlanesActive=[],this.culledBySectionPlanes=!1,this.occlusionTestList=[],this.lenOcclusionTestList=0,this.pixels=[],this.aabbDirty=!1,this.markerListDirty=!1,this.positionsDirty=!0,this.occlusionTestListDirty=!1}addMarker(e){this.markers[e.id]=e,this.markerListDirty=!0,this.numMarkers++}markerWorldPosUpdated(e){if(!this.markers[e.id])return;const t=this.markerIndices[e.id];this.positions[3*t+0]=e.worldPos[0],this.positions[3*t+1]=e.worldPos[1],this.positions[3*t+2]=e.worldPos[2],this.positionsDirty=!0}removeMarker(e){delete this.markers[e.id],this.markerListDirty=!0,this.numMarkers--}update(){this.markerListDirty&&(this._buildMarkerList(),this.markerListDirty=!1,this.positionsDirty=!0,this.occlusionTestListDirty=!0),this.positionsDirty&&(this._buildPositions(),this.positionsDirty=!1,this.aabbDirty=!0,this.vbosDirty=!0),this.aabbDirty&&(this._buildAABB(),this.aabbDirty=!1),this.vbosDirty&&(this._buildVBOs(),this.vbosDirty=!1),this.occlusionTestListDirty&&this._buildOcclusionTestList(),this._updateActiveSectionPlanes()}_buildMarkerList(){for(var e in this.numMarkers=0,this.markers)this.markers.hasOwnProperty(e)&&(this.markerList[this.numMarkers]=this.markers[e],this.markerIndices[e]=this.numMarkers,this.numMarkers++);this.markerList.length=this.numMarkers}_buildPositions(){let e=0;for(let t=0;t<this.numMarkers;t++)if(this.markerList[t]){const s=this.markerList[t].worldPos;this.positions[e++]=s[0],this.positions[e++]=s[1],this.positions[e++]=s[2],this.indices[t]=t}this.positions.length=3*this.numMarkers,this.indices.length=this.numMarkers}_buildAABB(){const e=this.aabb;h.collapseAABB3(e),h.expandAABB3Points3(e,this.positions);const t=this.origin;e[0]+=t[0],e[1]+=t[1],e[2]+=t[2],e[3]+=t[0],e[4]+=t[1],e[5]+=t[2]}_buildVBOs(){if(this.positionsBuf){if(this.lenPositionsBuf===this.positions.length)return void this.positionsBuf.setData(this.positions);this.positionsBuf.destroy(),this.positionsBuf=null,this.indicesBuf.destroy(),this.indicesBuf=null}const e=this.scene.canvas.gl,t=3*this.numMarkers,s=this.numMarkers;this.positionsBuf=new ge(e,e.ARRAY_BUFFER,new Float32Array(this.positions),t,3,e.STATIC_DRAW),this.indicesBuf=new ge(e,e.ELEMENT_ARRAY_BUFFER,new Uint16Array(this.indices),s,1,e.STATIC_DRAW),this.lenPositionsBuf=this.positions.length}_buildOcclusionTestList(){const e=this.scene.canvas,t=this.scene.camera.perspective.near,s=e.boundary,n=s[2],i=s[3];let a=0;this.lenOcclusionTestList=0;for(let e=0;e<this.numMarkers;e++){const s=this.markerList[e];if(s.viewPos[2]>-t){s._setVisible(!1);continue}const r=s.canvasPos,l=r[0],o=r[1];l+10<0||o+10<0||l-10>n||o-10>i?s._setVisible(!1):!s.entity||s.entity.visible?s.occludable?(this.occlusionTestList[this.lenOcclusionTestList++]=s,this.pixels[a++]=l,this.pixels[a++]=o):s._setVisible(!0):s._setVisible(!1)}}_updateActiveSectionPlanes(){const e=this.scene._sectionPlanesState.sectionPlanes,t=e.length;if(t>0)for(let s=0;s<t;s++){const t=e[s];if(t.active){const e=h.planeAABB3Intersect(t.dir,t.dist,this.aabb);if(-1===e)return void(this.culledBySectionPlanes=!0);const n=0===e;this.sectionPlanesActive[s]=n}else this.sectionPlanesActive[s]=!1}this.culledBySectionPlanes=!1}destroy(){this.markers={},this.markerList.length=0,this.positionsBuf&&this.positionsBuf.destroy(),this.indicesBuf&&this.indicesBuf.destroy()}}const Ee=h.vec3([1,0,0]),be=h.vec3();class De{constructor(e,t){this._scene=e,this._renderBufferManager=t,this._occlusionLayers={},this._occlusionLayersList=[],this._occlusionLayersListDirty=!1,this._shaderSource=null,this._program=null,this._shaderSourceHash=null,this._shaderSourceDirty=!0,this._programDirty=!1,this._markersToOcclusionLayersMap={},this._onCameraViewMatrix=e.camera.on("viewMatrix",(()=>{this._occlusionTestListDirty=!0})),this._onCameraProjMatrix=e.camera.on("projMatrix",(()=>{this._occlusionTestListDirty=!0})),this._onCanvasBoundary=e.canvas.on("boundary",(()=>{this._occlusionTestListDirty=!0}))}addMarker(e){const t=e.origin.join();let s=this._occlusionLayers[t];s||(s=new Te(this._scene,e.origin),this._occlusionLayers[s.originHash]=s,this._occlusionLayersListDirty=!0),s.addMarker(e),this._markersToOcclusionLayersMap[e.id]=s,this._occlusionTestListDirty=!0}markerWorldPosUpdated(e){const t=this._markersToOcclusionLayersMap[e.id];if(!t)return void e.error("Marker has not been added to OcclusionTester");const s=e.origin.join();if(s!==t.originHash){1===t.numMarkers?(t.destroy(),delete this._occlusionLayers[t.originHash],this._occlusionLayersListDirty=!0):t.removeMarker(e);let n=this._occlusionLayers[s];n||(n=new Te(this._scene,e.origin),this._occlusionLayers[s]=t,this._occlusionLayersListDirty=!0),n.addMarker(e),this._markersToOcclusionLayersMap[e.id]=n}else t.markerWorldPosUpdated(e)}removeMarker(e){const t=e.origin.join();let s=this._occlusionLayers[t];s&&(1===s.numMarkers?(s.destroy(),delete this._occlusionLayers[s.originHash],this._occlusionLayersListDirty=!0):s.removeMarker(e),delete this._markersToOcclusionLayersMap[e.id])}get needOcclusionTest(){return this._occlusionTestListDirty}bindRenderBuf(){const e=[this._scene.canvas.canvas.id,this._scene._sectionPlanesState.getHash()].join(";");if(e!==this._shaderSourceHash&&(this._shaderSourceHash=e,this._shaderSourceDirty=!0),this._shaderSourceDirty&&(this._buildShaderSource(),this._shaderSourceDirty=!1,this._programDirty=!0),this._programDirty&&(this._buildProgram(),this._programDirty=!1,this._occlusionTestListDirty=!0),this._occlusionLayersListDirty&&(this._buildOcclusionLayersList(),this._occlusionLayersListDirty=!1),this._occlusionTestListDirty){for(let e=0,t=this._occlusionLayersList.length;e<t;e++){this._occlusionLayersList[e].occlusionTestListDirty=!0}this._occlusionTestListDirty=!1}this._readPixelBuf=this._renderBufferManager.getRenderBuffer("occlusionReadPix"),this._readPixelBuf.bind(),this._readPixelBuf.clear()}_buildOcclusionLayersList(){let e=0;for(let t in this._occlusionLayers)this._occlusionLayers.hasOwnProperty(t)&&(this._occlusionLayersList[e++]=this._occlusionLayers[t]);this._occlusionLayersList.length=e}_buildShaderSource(){this._shaderSource={vertex:this._buildVertexShaderSource(),fragment:this._buildFragmentShaderSource()}}_buildVertexShaderSource(){const e=this._scene,t=e._sectionPlanesState.sectionPlanes.length>0,s=[];return s.push("#version 300 es"),s.push("// OcclusionTester vertex shader"),s.push("in vec3 position;"),s.push("uniform mat4 modelMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;")),t&&s.push("out vec4 vWorldPosition;"),s.push("void main(void) {"),s.push("vec4 worldPosition = vec4(position, 1.0); "),s.push(" vec4 viewPosition = viewMatrix * worldPosition;"),t&&s.push(" vWorldPosition = worldPosition;"),s.push(" vec4 clipPos = projMatrix * viewPosition;"),s.push(" gl_PointSize = 20.0;"),e.logarithmicDepthBufferEnabled?s.push("vFragDepth = 1.0 + clipPos.w;"):s.push("clipPos.z += -0.001;"),s.push(" gl_Position = clipPos;"),s.push("}"),s}_buildFragmentShaderSource(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// OcclusionTester fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),s){n.push("in vec4 vWorldPosition;");for(let e=0;e<t.sectionPlanes.length;e++)n.push("uniform bool sectionPlaneActive"+e+";"),n.push("uniform vec3 sectionPlanePos"+e+";"),n.push("uniform vec3 sectionPlaneDir"+e+";")}if(n.push("out vec4 outColor;"),n.push("void main(void) {"),s){n.push(" float dist = 0.0;");for(var i=0;i<t.sectionPlanes.length;i++)n.push("if (sectionPlaneActive"+i+") {"),n.push(" dist += clamp(dot(-sectionPlaneDir"+i+".xyz, vWorldPosition.xyz - sectionPlanePos"+i+".xyz), 0.0, 1000.0);"),n.push("}");n.push(" if (dist > 0.0) { discard; }")}return e.logarithmicDepthBufferEnabled&&n.push("gl_FragDepth = log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" outColor = vec4(1.0, 0.0, 0.0, 1.0); "),n.push("}"),n}_buildProgram(){this._program&&this._program.destroy();const e=this._scene,t=e.canvas.gl,s=e._sectionPlanesState;if(this._program=new we(t,this._shaderSource),this._program.errors)return void(this.errors=this._program.errors);const n=this._program;this._uViewMatrix=n.getLocation("viewMatrix"),this._uProjMatrix=n.getLocation("projMatrix"),this._uSectionPlanes=[];for(let e=0,t=s.sectionPlanes.length;e<t;e++)this._uSectionPlanes.push({active:n.getLocation("sectionPlaneActive"+e),pos:n.getLocation("sectionPlanePos"+e),dir:n.getLocation("sectionPlaneDir"+e)});this._aPosition=n.getAttribute("position"),e.logarithmicDepthBufferEnabled&&(this._uLogDepthBufFC=n.getLocation("logDepthBufFC"))}drawMarkers(){const e=this._scene,t=e.canvas.gl,s=this._program,n=e._sectionPlanesState,i=e.camera,a=e.camera.project;if(s.bind(),t.uniformMatrix4fv(this._uProjMatrix,!1,i._project._state.matrix),e.logarithmicDepthBufferEnabled){const e=2/(Math.log(a.far+1)/Math.LN2);t.uniform1f(this._uLogDepthBufFC,e)}for(let e=0,s=this._occlusionLayersList.length;e<s;e++){const s=this._occlusionLayersList[e];if(s.update(),s.culledBySectionPlanes)continue;const a=s.origin;t.uniformMatrix4fv(this._uViewMatrix,!1,B(i.viewMatrix,a));const r=n.sectionPlanes.length;if(r>0){const e=n.sectionPlanes;for(let n=0;n<r;n++){const i=this._uSectionPlanes[n];if(i){const r=s.sectionPlanesActive[n];if(t.uniform1i(i.active,r?1:0),r){const s=e[n];t.uniform3fv(i.pos,N(s.dist,s.dir,a,be)),t.uniform3fv(i.dir,s.dir)}}}}this._aPosition.bindArrayBuffer(s.positionsBuf);const l=s.indicesBuf;l.bind(),t.drawElements(t.POINTS,l.numItems,l.itemType,0)}}doOcclusionTest(){{const e=this._scene.canvas.resolutionScale,t=255*Ee[0],s=255*Ee[1],n=255*Ee[2];for(let i=0,a=this._occlusionLayersList.length;i<a;i++){const a=this._occlusionLayersList[i];for(let i=0;i<a.lenOcclusionTestList;i++){const r=a.occlusionTestList[i],l=2*i,o=this._readPixelBuf.read(Math.round(a.pixels[l]*e),Math.round(a.pixels[l+1]*e)),c=o[0]===t&&o[1]===s&&o[2]===n;r._setVisible(c)}}}}unbindRenderBuf(){this._readPixelBuf.unbind()}destroy(){if(!this.destroyed){for(let e=0,t=this._occlusionLayersList.length;e<t;e++){this._occlusionLayersList[e].destroy()}this._program&&this._program.destroy(),this._scene.camera.off(this._onCameraViewMatrix),this._scene.camera.off(this._onCameraProjMatrix),this._scene.canvas.off(this._onCanvasBoundary),this.destroyed=!0}}}const Pe=h.vec2();class Re{constructor(e){this._scene=e,this._numSamples=null,this._program=null,this._programError=!1,this._aPosition=null,this._aUV=null,this._uDepthTexture="uDepthTexture",this._uCameraNear=null,this._uCameraFar=null,this._uCameraProjectionMatrix=null,this._uCameraInverseProjectionMatrix=null,this._uScale=null,this._uIntensity=null,this._uBias=null,this._uKernelRadius=null,this._uMinResolution=null,this._uRandomSeed=null,this._uvBuf=null,this._positionsBuf=null,this._indicesBuf=null}render(e){if(this._build(),this._programError)return;this._getInverseProjectMat||(this._getInverseProjectMat=(()=>{let e=!0;this._scene.camera.on("projMatrix",(function(){e=!0}));const t=h.mat4();return()=>(e&&h.inverseMat4(n.camera.projMatrix,t),t)})());const t=this._scene.canvas.gl,s=this._program,n=this._scene,i=n.sao,a=t.drawingBufferWidth,r=t.drawingBufferHeight,l=n.camera.project._state,o=l.near,c=l.far,u=l.matrix,p=this._getInverseProjectMat(),A=Math.random(),d="perspective"===n.camera.projection;Pe[0]=a,Pe[1]=r,t.viewport(0,0,a,r),t.clearColor(0,0,0,1),t.disable(t.DEPTH_TEST),t.disable(t.BLEND),t.frontFace(t.CCW),t.clear(t.COLOR_BUFFER_BIT),s.bind(),t.uniform1f(this._uCameraNear,o),t.uniform1f(this._uCameraFar,c),t.uniformMatrix4fv(this._uCameraProjectionMatrix,!1,u),t.uniformMatrix4fv(this._uCameraInverseProjectionMatrix,!1,p),t.uniform1i(this._uPerspective,d),t.uniform1f(this._uScale,i.scale*(c/5)),t.uniform1f(this._uIntensity,i.intensity),t.uniform1f(this._uBias,i.bias),t.uniform1f(this._uKernelRadius,i.kernelRadius),t.uniform1f(this._uMinResolution,i.minResolution),t.uniform2fv(this._uViewport,Pe),t.uniform1f(this._uRandomSeed,A);const f=e.getDepthTexture();s.bindTexture(this._uDepthTexture,f,0),this._aUV.bindArrayBuffer(this._uvBuf),this._aPosition.bindArrayBuffer(this._positionsBuf),this._indicesBuf.bind(),t.drawElements(t.TRIANGLES,this._indicesBuf.numItems,this._indicesBuf.itemType,0)}_build(){let e=!1;const t=this._scene.sao;if(t.numSamples!==this._numSamples&&(this._numSamples=Math.floor(t.numSamples),e=!0),!e)return;const s=this._scene.canvas.gl;if(this._program&&(this._program.destroy(),this._program=null),this._program=new we(s,{vertex:["#version 300 es\n precision highp float;\n precision highp int;\n \n in vec3 aPosition;\n in vec2 aUV; \n \n out vec2 vUV;\n \n void main () {\n gl_Position = vec4(aPosition, 1.0);\n vUV = aUV;\n }"],fragment:[`#version 300 es \n precision highp float;\n precision highp int; \n \n #define NORMAL_TEXTURE 0\n #define PI 3.14159265359\n #define PI2 6.28318530718\n #define EPSILON 1e-6\n #define NUM_SAMPLES ${this._numSamples}\n #define NUM_RINGS 4 \n \n in vec2 vUV;\n \n uniform sampler2D uDepthTexture;\n \n uniform float uCameraNear;\n uniform float uCameraFar;\n uniform mat4 uProjectMatrix;\n uniform mat4 uInverseProjectMatrix;\n \n uniform bool uPerspective;\n\n uniform float uScale;\n uniform float uIntensity;\n uniform float uBias;\n uniform float uKernelRadius;\n uniform float uMinResolution;\n uniform vec2 uViewport;\n uniform float uRandomSeed;\n\n float pow2( const in float x ) { return x*x; }\n \n highp float rand( const in vec2 uv ) {\n const highp float a = 12.9898, b = 78.233, c = 43758.5453;\n highp float dt = dot( uv.xy, vec2( a,b ) ), sn = mod( dt, PI );\n return fract(sin(sn) * c);\n }\n\n vec3 packNormalToRGB( const in vec3 normal ) {\n return normalize( normal ) * 0.5 + 0.5;\n }\n\n vec3 unpackRGBToNormal( const in vec3 rgb ) {\n return 2.0 * rgb.xyz - 1.0;\n }\n\n const float packUpscale = 256. / 255.;\n const float unpackDownScale = 255. / 256.; \n\n const vec3 packFactors = vec3( 256. * 256. * 256., 256. * 256., 256. );\n const vec4 unPackFactors = unpackDownScale / vec4( packFactors, 1. ); \n\n const float shiftRights = 1. / 256.;\n\n vec4 packFloatToRGBA( const in float v ) {\n vec4 r = vec4( fract( v * packFactors ), v );\n r.yzw -= r.xyz * shiftRights; \n return r * packUpscale;\n }\n\n float unpackRGBAToFloat( const in vec4 v ) { \n return dot( floor( v * 255.0 + 0.5 ) / 255.0, unPackFactors );\n }\n \n float perspectiveDepthToViewZ( const in float invClipZ, const in float near, const in float far ) {\n return ( near * far ) / ( ( far - near ) * invClipZ - far );\n }\n\n float orthographicDepthToViewZ( const in float linearClipZ, const in float near, const in float far ) {\n return linearClipZ * ( near - far ) - near;\n }\n \n float getDepth( const in vec2 screenPosition ) {\n return vec4(texture(uDepthTexture, screenPosition)).r;\n }\n\n float getViewZ( const in float depth ) {\n if (uPerspective) {\n return perspectiveDepthToViewZ( depth, uCameraNear, uCameraFar );\n } else {\n return orthographicDepthToViewZ( depth, uCameraNear, uCameraFar );\n }\n }\n\n vec3 getViewPos( const in vec2 screenPos, const in float depth, const in float viewZ ) {\n \tfloat clipW = uProjectMatrix[2][3] * viewZ + uProjectMatrix[3][3];\n \tvec4 clipPosition = vec4( ( vec3( screenPos, depth ) - 0.5 ) * 2.0, 1.0 );\n \tclipPosition *= clipW; \n \treturn ( uInverseProjectMatrix * clipPosition ).xyz;\n }\n\n vec3 getViewNormal( const in vec3 viewPosition, const in vec2 screenPos ) { \n return normalize( cross( dFdx( viewPosition ), dFdy( viewPosition ) ) );\n }\n\n float scaleDividedByCameraFar;\n float minResolutionMultipliedByCameraFar;\n\n float getOcclusion( const in vec3 centerViewPosition, const in vec3 centerViewNormal, const in vec3 sampleViewPosition ) {\n \tvec3 viewDelta = sampleViewPosition - centerViewPosition;\n \tfloat viewDistance = length( viewDelta );\n \tfloat scaledScreenDistance = scaleDividedByCameraFar * viewDistance;\n \treturn max(0.0, (dot(centerViewNormal, viewDelta) - minResolutionMultipliedByCameraFar) / scaledScreenDistance - uBias) / (1.0 + pow2( scaledScreenDistance ) );\n }\n\n const float ANGLE_STEP = PI2 * float( NUM_RINGS ) / float( NUM_SAMPLES );\n const float INV_NUM_SAMPLES = 1.0 / float( NUM_SAMPLES );\n\n float getAmbientOcclusion( const in vec3 centerViewPosition ) {\n \n \tscaleDividedByCameraFar = uScale / uCameraFar;\n \tminResolutionMultipliedByCameraFar = uMinResolution * uCameraFar;\n \tvec3 centerViewNormal = getViewNormal( centerViewPosition, vUV );\n\n \tfloat angle = rand( vUV + uRandomSeed ) * PI2;\n \tvec2 radius = vec2( uKernelRadius * INV_NUM_SAMPLES ) / uViewport;\n \tvec2 radiusStep = radius;\n\n \tfloat occlusionSum = 0.0;\n \tfloat weightSum = 0.0;\n\n \tfor( int i = 0; i < NUM_SAMPLES; i ++ ) {\n \t\tvec2 sampleUv = vUV + vec2( cos( angle ), sin( angle ) ) * radius;\n \t\tradius += radiusStep;\n \t\tangle += ANGLE_STEP;\n\n \t\tfloat sampleDepth = getDepth( sampleUv );\n \t\tif( sampleDepth >= ( 1.0 - EPSILON ) ) {\n \t\t\tcontinue;\n \t\t}\n\n \t\tfloat sampleViewZ = getViewZ( sampleDepth );\n \t\tvec3 sampleViewPosition = getViewPos( sampleUv, sampleDepth, sampleViewZ );\n \t\tocclusionSum += getOcclusion( centerViewPosition, centerViewNormal, sampleViewPosition );\n \t\tweightSum += 1.0;\n \t}\n\n \tif( weightSum == 0.0 ) discard;\n\n \treturn occlusionSum * ( uIntensity / weightSum );\n }\n\n out vec4 outColor;\n \n void main() {\n \n \tfloat centerDepth = getDepth( vUV );\n \t\n \tif( centerDepth >= ( 1.0 - EPSILON ) ) {\n \t\tdiscard;\n \t}\n\n \tfloat centerViewZ = getViewZ( centerDepth );\n \tvec3 viewPosition = getViewPos( vUV, centerDepth, centerViewZ );\n\n \tfloat ambientOcclusion = getAmbientOcclusion( viewPosition );\n \n \toutColor = packFloatToRGBA( 1.0- ambientOcclusion );\n }`]}),this._program.errors)return console.error(this._program.errors.join("\n")),void(this._programError=!0);const n=new Float32Array([1,1,0,1,0,0,1,0]),i=new Float32Array([1,1,0,-1,1,0,-1,-1,0,1,-1,0]),a=new Uint32Array([0,1,2,0,2,3]);this._positionsBuf=new ge(s,s.ARRAY_BUFFER,i,i.length,3,s.STATIC_DRAW),this._uvBuf=new ge(s,s.ARRAY_BUFFER,n,n.length,2,s.STATIC_DRAW),this._indicesBuf=new ge(s,s.ELEMENT_ARRAY_BUFFER,a,a.length,1,s.STATIC_DRAW),this._program.bind(),this._uCameraNear=this._program.getLocation("uCameraNear"),this._uCameraFar=this._program.getLocation("uCameraFar"),this._uCameraProjectionMatrix=this._program.getLocation("uProjectMatrix"),this._uCameraInverseProjectionMatrix=this._program.getLocation("uInverseProjectMatrix"),this._uPerspective=this._program.getLocation("uPerspective"),this._uScale=this._program.getLocation("uScale"),this._uIntensity=this._program.getLocation("uIntensity"),this._uBias=this._program.getLocation("uBias"),this._uKernelRadius=this._program.getLocation("uKernelRadius"),this._uMinResolution=this._program.getLocation("uMinResolution"),this._uViewport=this._program.getLocation("uViewport"),this._uRandomSeed=this._program.getLocation("uRandomSeed"),this._aPosition=this._program.getAttribute("aPosition"),this._aUV=this._program.getAttribute("aUV"),this._dirty=!1}destroy(){this._program&&(this._program.destroy(),this._program=null)}}const Ce=new Float32Array(xe(17,[0,1])),_e=new Float32Array(xe(17,[1,0])),Be=new Float32Array(function(e,t){const s=[];for(let n=0;n<=e;n++)s.push(Ne(n,t));return s}(17,4)),Oe=new Float32Array(2);class Se{constructor(e){this._scene=e,this._program=null,this._programError=!1,this._aPosition=null,this._aUV=null,this._uDepthTexture="uDepthTexture",this._uOcclusionTexture="uOcclusionTexture",this._uViewport=null,this._uCameraNear=null,this._uCameraFar=null,this._uCameraProjectionMatrix=null,this._uCameraInverseProjectionMatrix=null,this._uvBuf=null,this._positionsBuf=null,this._indicesBuf=null,this.init()}init(){const e=this._scene.canvas.gl;if(this._program=new we(e,{vertex:["#version 300 es\n precision highp float;\n precision highp int;\n \n in vec3 aPosition;\n in vec2 aUV;\n uniform vec2 uViewport;\n out vec2 vUV;\n out vec2 vInvSize;\n void main () {\n vUV = aUV;\n vInvSize = 1.0 / uViewport;\n gl_Position = vec4(aPosition, 1.0);\n }"],fragment:["#version 300 es\n precision highp float;\n precision highp int;\n \n #define PI 3.14159265359\n #define PI2 6.28318530718\n #define EPSILON 1e-6\n\n #define KERNEL_RADIUS 16\n\n in vec2 vUV;\n in vec2 vInvSize;\n \n uniform sampler2D uDepthTexture;\n uniform sampler2D uOcclusionTexture; \n \n uniform float uCameraNear;\n uniform float uCameraFar; \n uniform float uDepthCutoff;\n\n uniform vec2 uSampleOffsets[ KERNEL_RADIUS + 1 ];\n uniform float uSampleWeights[ KERNEL_RADIUS + 1 ];\n\n const float unpackDownscale = 255. / 256.; \n\n const vec3 packFactors = vec3( 256. * 256. * 256., 256. * 256., 256. );\n const vec4 unpackFactors = unpackDownscale / vec4( packFactors, 1. ); \n\n const float packUpscale = 256. / 255.;\n \n const float shiftRights = 1. / 256.;\n \n float unpackRGBAToFloat( const in vec4 v ) {\n return dot( floor( v * 255.0 + 0.5 ) / 255.0, unpackFactors );\n } \n\n vec4 packFloatToRGBA( const in float v ) {\n vec4 r = vec4( fract( v * packFactors ), v );\n r.yzw -= r.xyz * shiftRights; \n return r * packUpscale;\n }\n\n float viewZToOrthographicDepth( const in float viewZ) {\n return ( viewZ + uCameraNear ) / ( uCameraNear - uCameraFar );\n }\n \n float orthographicDepthToViewZ( const in float linearClipZ) {\n return linearClipZ * ( uCameraNear - uCameraFar ) - uCameraNear;\n }\n\n float viewZToPerspectiveDepth( const in float viewZ) {\n return (( uCameraNear + viewZ ) * uCameraFar ) / (( uCameraFar - uCameraNear ) * viewZ );\n }\n \n float perspectiveDepthToViewZ( const in float invClipZ) {\n return ( uCameraNear * uCameraFar ) / ( ( uCameraFar - uCameraNear ) * invClipZ - uCameraFar );\n }\n\n float getDepth( const in vec2 screenPosition ) {\n return vec4(texture(uDepthTexture, screenPosition)).r;\n }\n\n float getViewZ( const in float depth ) {\n return perspectiveDepthToViewZ( depth );\n }\n\n out vec4 outColor;\n \n void main() {\n \n float depth = getDepth( vUV );\n if( depth >= ( 1.0 - EPSILON ) ) {\n discard;\n }\n\n float centerViewZ = -getViewZ( depth );\n bool rBreak = false;\n bool lBreak = false;\n\n float weightSum = uSampleWeights[0];\n float occlusionSum = unpackRGBAToFloat(texture( uOcclusionTexture, vUV )) * weightSum;\n\n for( int i = 1; i <= KERNEL_RADIUS; i ++ ) {\n\n float sampleWeight = uSampleWeights[i];\n vec2 sampleUVOffset = uSampleOffsets[i] * vInvSize;\n\n vec2 sampleUV = vUV + sampleUVOffset;\n float viewZ = -getViewZ( getDepth( sampleUV ) );\n\n if( abs( viewZ - centerViewZ ) > uDepthCutoff ) {\n rBreak = true;\n }\n\n if( ! rBreak ) {\n occlusionSum += unpackRGBAToFloat(texture( uOcclusionTexture, sampleUV )) * sampleWeight;\n weightSum += sampleWeight;\n }\n\n sampleUV = vUV - sampleUVOffset;\n viewZ = -getViewZ( getDepth( sampleUV ) );\n\n if( abs( viewZ - centerViewZ ) > uDepthCutoff ) {\n lBreak = true;\n }\n\n if( ! lBreak ) {\n occlusionSum += unpackRGBAToFloat(texture( uOcclusionTexture, sampleUV )) * sampleWeight;\n weightSum += sampleWeight;\n }\n }\n\n outColor = packFloatToRGBA(occlusionSum / weightSum);\n }"]}),this._program.errors)return console.error(this._program.errors.join("\n")),void(this._programError=!0);const t=new Float32Array([1,1,0,1,0,0,1,0]),s=new Float32Array([1,1,0,-1,1,0,-1,-1,0,1,-1,0]),n=new Uint32Array([0,1,2,0,2,3]);this._positionsBuf=new ge(e,e.ARRAY_BUFFER,s,s.length,3,e.STATIC_DRAW),this._uvBuf=new ge(e,e.ARRAY_BUFFER,t,t.length,2,e.STATIC_DRAW),this._indicesBuf=new ge(e,e.ELEMENT_ARRAY_BUFFER,n,n.length,1,e.STATIC_DRAW),this._program.bind(),this._uViewport=this._program.getLocation("uViewport"),this._uCameraNear=this._program.getLocation("uCameraNear"),this._uCameraFar=this._program.getLocation("uCameraFar"),this._uDepthCutoff=this._program.getLocation("uDepthCutoff"),this._uSampleOffsets=e.getUniformLocation(this._program.handle,"uSampleOffsets"),this._uSampleWeights=e.getUniformLocation(this._program.handle,"uSampleWeights"),this._aPosition=this._program.getAttribute("aPosition"),this._aUV=this._program.getAttribute("aUV")}render(e,t,s){if(this._programError)return;this._getInverseProjectMat||(this._getInverseProjectMat=(()=>{let e=!0;this._scene.camera.on("projMatrix",(function(){e=!0}));const t=h.mat4();return()=>(e&&h.inverseMat4(a.camera.projMatrix,t),t)})());const n=this._scene.canvas.gl,i=this._program,a=this._scene,r=n.drawingBufferWidth,l=n.drawingBufferHeight,o=a.camera.project._state,c=o.near,u=o.far;n.viewport(0,0,r,l),n.clearColor(0,0,0,1),n.enable(n.DEPTH_TEST),n.disable(n.BLEND),n.frontFace(n.CCW),n.clear(n.COLOR_BUFFER_BIT|n.DEPTH_BUFFER_BIT),i.bind(),Oe[0]=r,Oe[1]=l,n.uniform2fv(this._uViewport,Oe),n.uniform1f(this._uCameraNear,c),n.uniform1f(this._uCameraFar,u),n.uniform1f(this._uDepthCutoff,.01),0===s?n.uniform2fv(this._uSampleOffsets,_e):n.uniform2fv(this._uSampleOffsets,Ce),n.uniform1fv(this._uSampleWeights,Be);const p=e.getDepthTexture(),A=t.getTexture();i.bindTexture(this._uDepthTexture,p,0),i.bindTexture(this._uOcclusionTexture,A,1),this._aUV.bindArrayBuffer(this._uvBuf),this._aPosition.bindArrayBuffer(this._positionsBuf),this._indicesBuf.bind(),n.drawElements(n.TRIANGLES,this._indicesBuf.numItems,this._indicesBuf.itemType,0)}destroy(){this._program.destroy()}}function Ne(e,t){return Math.exp(-e*e/(t*t*2))/(Math.sqrt(2*Math.PI)*t)}function xe(e,t){const s=[];for(let n=0;n<=e;n++)s.push(t[0]*n),s.push(t[1]*n);return s}class Le{constructor(e,t,s){s=s||{},this.gl=t,this.allocated=!1,this.canvas=e,this.buffer=null,this.bound=!1,this.size=s.size,this._hasDepthTexture=!!s.depthTexture}setSize(e){this.size=e}webglContextRestored(e){this.gl=e,this.buffer=null,this.allocated=!1,this.bound=!1}bind(e=null){if(this._touch(e),this.bound)return;const t=this.gl;t.bindFramebuffer(t.FRAMEBUFFER,this.buffer.framebuf),this.bound=!0}_touch(e=null){let t,s;const n=this.gl;if(this.size?(t=this.size[0],s=this.size[1]):(t=n.drawingBufferWidth,s=n.drawingBufferHeight),this.buffer){if(this.buffer.width===t&&this.buffer.height===s)return;n.deleteTexture(this.buffer.texture),n.deleteFramebuffer(this.buffer.framebuf),n.deleteRenderbuffer(this.buffer.renderbuf)}const i=n.createTexture();let a;n.bindTexture(n.TEXTURE_2D,i),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_MIN_FILTER,n.NEAREST),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_MAG_FILTER,n.NEAREST),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_WRAP_S,n.CLAMP_TO_EDGE),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_WRAP_T,n.CLAMP_TO_EDGE),e?n.texStorage2D(n.TEXTURE_2D,1,e,t,s):n.texImage2D(n.TEXTURE_2D,0,n.RGBA,t,s,0,n.RGBA,n.UNSIGNED_BYTE,null),this._hasDepthTexture&&(a=n.createTexture(),n.bindTexture(n.TEXTURE_2D,a),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_MAG_FILTER,n.NEAREST),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_MIN_FILTER,n.NEAREST),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_WRAP_S,n.CLAMP_TO_EDGE),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_WRAP_T,n.CLAMP_TO_EDGE),n.texImage2D(n.TEXTURE_2D,0,n.DEPTH_COMPONENT32F,t,s,0,n.DEPTH_COMPONENT,n.FLOAT,null));const r=n.createRenderbuffer();n.bindRenderbuffer(n.RENDERBUFFER,r),n.renderbufferStorage(n.RENDERBUFFER,n.DEPTH_COMPONENT32F,t,s);const l=n.createFramebuffer();if(n.bindFramebuffer(n.FRAMEBUFFER,l),n.framebufferTexture2D(n.FRAMEBUFFER,n.COLOR_ATTACHMENT0,n.TEXTURE_2D,i,0),this._hasDepthTexture?n.framebufferTexture2D(n.FRAMEBUFFER,n.DEPTH_ATTACHMENT,n.TEXTURE_2D,a,0):n.framebufferRenderbuffer(n.FRAMEBUFFER,n.DEPTH_ATTACHMENT,n.RENDERBUFFER,r),n.bindTexture(n.TEXTURE_2D,null),n.bindRenderbuffer(n.RENDERBUFFER,null),n.bindFramebuffer(n.FRAMEBUFFER,null),n.bindFramebuffer(n.FRAMEBUFFER,l),!n.isFramebuffer(l))throw"Invalid framebuffer";n.bindFramebuffer(n.FRAMEBUFFER,null);const o=n.checkFramebufferStatus(n.FRAMEBUFFER);switch(o){case n.FRAMEBUFFER_COMPLETE:break;case n.FRAMEBUFFER_INCOMPLETE_ATTACHMENT:throw"Incomplete framebuffer: FRAMEBUFFER_INCOMPLETE_ATTACHMENT";case n.FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:throw"Incomplete framebuffer: FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT";case n.FRAMEBUFFER_INCOMPLETE_DIMENSIONS:throw"Incomplete framebuffer: FRAMEBUFFER_INCOMPLETE_DIMENSIONS";case n.FRAMEBUFFER_UNSUPPORTED:throw"Incomplete framebuffer: FRAMEBUFFER_UNSUPPORTED";default:throw"Incomplete framebuffer: "+o}this.buffer={framebuf:l,renderbuf:r,texture:i,depthTexture:a,width:t,height:s},this.bound=!1}clear(){if(!this.bound)throw"Render buffer not bound";const e=this.gl;e.clear(e.COLOR_BUFFER_BIT|e.DEPTH_BUFFER_BIT)}read(e,t,s=null,n=null,i=Uint8Array,a=4){const r=e,l=(this.buffer.height||this.gl.drawingBufferHeight)-t,o=new i(a),c=this.gl;return c.readPixels(r,l,1,1,s||c.RGBA,n||c.UNSIGNED_BYTE,o,0),o}readArray(e=null,t=null,s=Uint8Array,n=4){const i=new s(this.buffer.width*this.buffer.height*n),a=this.gl;return a.readPixels(0,0,this.buffer.width,this.buffer.height,e||a.RGBA,t||a.UNSIGNED_BYTE,i,0),i}readImageAsCanvas(){const e=this.gl,t=this._getImageDataCache(),s=t.pixelData,n=t.canvas,i=t.imageData,a=t.context;e.readPixels(0,0,this.buffer.width,this.buffer.height,e.RGBA,e.UNSIGNED_BYTE,s);const r=this.buffer.width,l=this.buffer.height,o=l/2|0,c=4*r,u=new Uint8Array(4*r);for(let e=0;e<o;++e){const t=e*c,n=(l-e-1)*c;u.set(s.subarray(t,t+c)),s.copyWithin(t,n,n+c),s.set(u,n)}return i.data.set(s),a.putImageData(i,0,0),n}readImage(e){const t=this.gl,s=this._getImageDataCache(),n=s.pixelData,i=s.canvas,a=s.imageData,r=s.context,{width:l,height:o}=this.buffer;t.readPixels(0,0,l,o,t.RGBA,t.UNSIGNED_BYTE,n),a.data.set(n),r.putImageData(a,0,0),r.save(),r.globalCompositeOperation="copy",r.scale(1,-1),r.drawImage(i,0,-o,l,o),r.restore();let c=e.format||"png";return"jpeg"!==c&&"png"!==c&&"bmp"!==c&&(console.error("Unsupported image format: '"+c+"' - supported types are 'jpeg', 'bmp' and 'png' - defaulting to 'png'"),c="png"),i.toDataURL(`image/${c}`)}_getImageDataCache(e=Uint8Array,t=4){const s=this.buffer.width,n=this.buffer.height;let i=this._imageDataCache;if(i&&(i.width===s&&i.height===n||(this._imageDataCache=null,i=null)),!i){const a=document.createElement("canvas"),r=a.getContext("2d");a.width=s,a.height=n,i={pixelData:new e(s*n*t),canvas:a,context:r,imageData:r.createImageData(s,n),width:s,height:n},this._imageDataCache=i}return i.context.resetTransform(),i}unbind(){const e=this.gl;e.bindFramebuffer(e.FRAMEBUFFER,null),this.bound=!1}getTexture(){const e=this;return this._texture||(this._texture={renderBuffer:this,bind:function(t){return!(!e.buffer||!e.buffer.texture)&&(e.gl.activeTexture(e.gl["TEXTURE"+t]),e.gl.bindTexture(e.gl.TEXTURE_2D,e.buffer.texture),!0)},unbind:function(t){e.buffer&&e.buffer.texture&&(e.gl.activeTexture(e.gl["TEXTURE"+t]),e.gl.bindTexture(e.gl.TEXTURE_2D,null))}})}hasDepthTexture(){return this._hasDepthTexture}getDepthTexture(){if(!this._hasDepthTexture)return null;const e=this;return this._depthTexture||(this._dethTexture={renderBuffer:this,bind:function(t){return!(!e.buffer||!e.buffer.depthTexture)&&(e.gl.activeTexture(e.gl["TEXTURE"+t]),e.gl.bindTexture(e.gl.TEXTURE_2D,e.buffer.depthTexture),!0)},unbind:function(t){e.buffer&&e.buffer.depthTexture&&(e.gl.activeTexture(e.gl["TEXTURE"+t]),e.gl.bindTexture(e.gl.TEXTURE_2D,null))}})}destroy(){if(this.allocated){const e=this.gl;e.deleteTexture(this.buffer.texture),e.deleteTexture(this.buffer.depthTexture),e.deleteFramebuffer(this.buffer.framebuf),e.deleteRenderbuffer(this.buffer.renderbuf),this.allocated=!1,this.buffer=null,this.bound=!1}this._imageDataCache=null,this._texture=null,this._depthTexture=null}}class Me{constructor(e){this.scene=e,this._renderBuffersBasic={},this._renderBuffersScaled={}}getRenderBuffer(e,t){const s=1===this.scene.canvas.resolutionScale?this._renderBuffersBasic:this._renderBuffersScaled;let n=s[e];return n||(n=new Le(this.scene.canvas.canvas,this.scene.canvas.gl,t),s[e]=n),n}destroy(){for(let e in this._renderBuffersBasic)this._renderBuffersBasic[e].destroy();for(let e in this._renderBuffersScaled)this._renderBuffersScaled[e].destroy()}}function Fe(e,t){if(void 0===e._cachedExtensions&&(e._cachedExtensions={}),void 0!==e._cachedExtensions[t])return e._cachedExtensions[t];let s;switch(t){case"WEBGL_depth_texture":s=e.getExtension("WEBGL_depth_texture")||e.getExtension("MOZ_WEBGL_depth_texture")||e.getExtension("WEBKIT_WEBGL_depth_texture");break;case"EXT_texture_filter_anisotropic":s=e.getExtension("EXT_texture_filter_anisotropic")||e.getExtension("MOZ_EXT_texture_filter_anisotropic")||e.getExtension("WEBKIT_EXT_texture_filter_anisotropic");break;case"WEBGL_compressed_texture_s3tc":s=e.getExtension("WEBGL_compressed_texture_s3tc")||e.getExtension("MOZ_WEBGL_compressed_texture_s3tc")||e.getExtension("WEBKIT_WEBGL_compressed_texture_s3tc");break;case"WEBGL_compressed_texture_pvrtc":s=e.getExtension("WEBGL_compressed_texture_pvrtc")||e.getExtension("WEBKIT_WEBGL_compressed_texture_pvrtc");break;default:s=e.getExtension(t)}return e._cachedExtensions[t]=s,s}const He=function(t,s){s=s||{};const n=new pe(t),i=t.canvas.canvas,a=t.canvas.gl,r=!!s.transparent,l=s.alphaDepthMask,o=new e({});let c={},u={},p=!0,d=!0,f=!0,I=!0,y=!0,m=!0,v=!0,w=!0;const g=new Me(t);let T=!1;const E=new Re(t),b=new Se(t);function D(){p&&(!function(){for(let e in c)if(c.hasOwnProperty(e)){const t=c[e],s=t.drawableMap,n=t.drawableListPreCull;let i=0;for(let e in s)s.hasOwnProperty(e)&&(n[i++]=s[e]);n.length=i}}(),p=!1,d=!0),d&&(!function(){for(let e in c)if(c.hasOwnProperty(e)){const t=c[e];t.isStateSortable&&t.drawableListPreCull.sort(t.stateSortCompare)}}(),d=!1,f=!0),f&&function(){for(let e in c)if(c.hasOwnProperty(e)){const t=c[e],s=t.drawableListPreCull,n=t.drawableList;let i=0;for(let e=0,t=s.length;e<t;e++){const t=s[e];t.rebuildRenderFlags(),t.renderFlags.culled||(n[i++]=t)}n.length=i}}()}function P(e){if(!e.castsShadow)return;const t=e.getShadowRenderBuf();if(t){t.bind(),n.reset(),n.backfaces=!0,n.frontface=!0,n.shadowViewMatrix=e.getShadowViewMatrix(),n.shadowProjMatrix=e.getShadowProjMatrix(),a.viewport(0,0,a.drawingBufferWidth,a.drawingBufferHeight),a.clearColor(0,0,0,1),a.enable(a.DEPTH_TEST),a.disable(a.BLEND),a.clear(a.COLOR_BUFFER_BIT|a.DEPTH_BUFFER_BIT);for(let e in c)if(c.hasOwnProperty(e)){const t=c[e].drawableList;for(let e=0,s=t.length;e<s;e++){const s=t[e];!1!==s.visible&&s.castsShadow&&s.drawShadow&&(s.renderFlags.colorOpaque&&s.drawShadow(n))}}t.unbind()}}this.scene=t,this._occlusionTester=null,this.capabilities={astcSupported:!!Fe(a,"WEBGL_compressed_texture_astc"),etc1Supported:!0,etc2Supported:!!Fe(a,"WEBGL_compressed_texture_etc"),dxtSupported:!!Fe(a,"WEBGL_compressed_texture_s3tc"),bptcSupported:!!Fe(a,"EXT_texture_compression_bptc"),pvrtcSupported:!(!Fe(a,"WEBGL_compressed_texture_pvrtc")&&!Fe(a,"WEBKIT_WEBGL_compressed_texture_pvrtc"))},this.setTransparentEnabled=function(e){I=e,f=!0},this.setEdgesEnabled=function(e){y=e,f=!0},this.setSAOEnabled=function(e){m=e,f=!0},this.setPBREnabled=function(e){v=e,f=!0},this.setColorTextureEnabled=function(e){w=e,f=!0},this.needStateSort=function(){d=!0},this.shadowsDirty=function(){},this.imageDirty=function(){f=!0},this.webglContextLost=function(){},this.webglContextRestored=function(e){E.init(),b.init(),f=!0},this.addDrawable=function(e,t){const s=t.type;if(!s)return void console.error("Renderer#addDrawable() : drawable with ID "+e+" has no 'type' - ignoring");let n=c[s];n||(n={type:t.type,count:0,isStateSortable:t.isStateSortable,stateSortCompare:t.stateSortCompare,drawableMap:{},drawableListPreCull:[],drawableList:[]},c[s]=n),n.count++,n.drawableMap[e]=t,u[e]=t,p=!0},this.removeDrawable=function(e){const t=u[e];if(!t)return void console.error("Renderer#removeDrawable() : drawable not found with ID "+e+" - ignoring");const s=t.type,n=c[s];--n.count<=0?delete c[s]:delete n.drawableMap[e],delete u[e],p=!0},this.getPickID=function(e){return o.addItem(e)},this.putPickID=function(e){o.removeItem(e)},this.clear=function(e){if(a.viewport(0,0,a.drawingBufferWidth,a.drawingBufferHeight),r)a.clearColor(1,1,1,1);else{const e=t.canvas.backgroundColorFromAmbientLight?this.lights.getAmbientColorAndIntensity():t.canvas.backgroundColor;a.clearColor(e[0],e[1],e[2],1)}a.clear(a.COLOR_BUFFER_BIT|a.DEPTH_BUFFER_BIT)},this.needsRender=function(){return f||p||d},this.render=function(e){(e=e||{}).force&&(f=!0),D(),f&&(!function(e){const s=t.sao;m&&s.possible&&function(e){const s=t.sao,i=g.getRenderBuffer("saoDepth",{depthTexture:!0});i.bind(),i.clear(),function(e){n.reset(),n.pass=e.pass,a.viewport(0,0,a.drawingBufferWidth,a.drawingBufferHeight),a.clearColor(0,0,0,0),a.enable(a.DEPTH_TEST),a.frontFace(a.CCW),a.enable(a.CULL_FACE),a.depthMask(!0),!1!==e.clear&&a.clear(a.COLOR_BUFFER_BIT|a.DEPTH_BUFFER_BIT);for(let e in c)if(c.hasOwnProperty(e)){const t=c[e].drawableList;for(let e=0,s=t.length;e<s;e++){const s=t[e];!0!==s.culled&&!1!==s.visible&&s.drawDepth&&s.saoEnabled&&(s.renderFlags.colorOpaque&&s.drawDepth(n))}}}(e),i.unbind();const r=g.getRenderBuffer("saoOcclusion");if(r.bind(),r.clear(),E.render(i),r.unbind(),s.blur){const e=g.getRenderBuffer("saoOcclusion2");e.bind(),e.clear(),b.render(i,r,0),e.unbind(),r.bind(),r.clear(),b.render(i,e,1),r.unbind()}}(e);(function(){let e=t._lightsState.lights;for(let t=0,s=e.length;t<s;t++){const s=e[t];s.castsShadow&&P(s)}})(),function(e){const s=[],i=[],o=[],u=[],h=[],p=[],d=[],f=[],T=[],E=[],b=[],D=[],P=[],R=[],C=[],_=[],B=t._lightsState.getAmbientColorAndIntensity();if(n.reset(),n.pass=e.pass,n.withSAO=!1,n.pbrEnabled=v&&!!t.pbrEnabled,n.colorTextureEnabled=w&&!!t.colorTextureEnabled,a.viewport(0,0,a.drawingBufferWidth,a.drawingBufferHeight),r)a.clearColor(0,0,0,0);else{const e=t.canvas.backgroundColorFromAmbientLight?B:t.canvas.backgroundColor;a.clearColor(e[0],e[1],e[2],1)}a.enable(a.DEPTH_TEST),a.frontFace(a.CCW),a.enable(a.CULL_FACE),a.depthMask(!0),a.lineWidth(1),n.lineWidth=1;const O=t.sao.possible;if(m&&O){const e=g.getRenderBuffer("saoOcclusion");n.occlusionTexture=e?e.getTexture():null}else n.occlusionTexture=null;let S,N,x;const L=Date.now();!1!==e.clear&&a.clear(a.COLOR_BUFFER_BIT|a.DEPTH_BUFFER_BIT);let M=0,F=0,H=0,U=0,G=0,V=0,j=0,k=0,Q=0,W=0,z=0,K=0,Y=0,X=0,q=0,J=0;for(let e in c)if(c.hasOwnProperty(e)){const t=c[e].drawableList;for(S=0,N=t.length;S<N;S++){if(x=t[S],!0===x.culled||!1===x.visible)continue;const e=x.renderFlags;e.colorOpaque&&(m&&O&&x.saoEnabled?s[M++]=x:x.drawColorOpaque(n)),I&&e.colorTransparent&&(o[H++]=x),e.xrayedSilhouetteTransparent&&(d[j++]=x),e.xrayedSilhouetteOpaque&&(h[G++]=x),e.highlightedSilhouetteTransparent&&(b[z++]=x),e.highlightedSilhouetteOpaque&&(T[Q++]=x),e.selectedSilhouetteTransparent&&(C[q++]=x),e.selectedSilhouetteOpaque&&(P[Y++]=x),y&&(e.edgesOpaque&&(i[F++]=x),e.edgesTransparent&&(u[U++]=x)),e.selectedEdgesTransparent&&(_[J++]=x),e.selectedEdgesOpaque&&(R[X++]=x),e.xrayedEdgesTransparent&&(f[k++]=x),e.xrayedEdgesOpaque&&(p[V++]=x),e.highlightedEdgesTransparent&&(D[K++]=x),e.highlightedEdgesOpaque&&(E[W++]=x)}}if(M>0)for(n.withSAO=!0,S=0;S<M;S++)s[S].drawColorOpaque(n);if(F>0)for(S=0;S<F;S++)i[S].drawEdgesColorOpaque(n);if(G>0)for(S=0;S<G;S++)h[S].drawSilhouetteXRayed(n);if(V>0)for(S=0;S<V;S++)p[S].drawEdgesXRayed(n);if(j>0||k>0||H>0||U>0){if(a.enable(a.CULL_FACE),a.enable(a.BLEND),r?(a.blendEquation(a.FUNC_ADD),a.blendFuncSeparate(a.SRC_ALPHA,a.ONE_MINUS_SRC_ALPHA,a.ONE,a.ONE_MINUS_SRC_ALPHA)):(a.blendEquation(a.FUNC_ADD),a.blendFunc(a.SRC_ALPHA,a.ONE_MINUS_SRC_ALPHA)),n.backfaces=!1,l||a.depthMask(!1),(H>0||U>0)&&a.blendFunc(a.SRC_ALPHA,a.ONE_MINUS_SRC_ALPHA),U>0)for(S=0;S<U;S++)x=u[S],x.drawEdgesColorTransparent(n);if(H>0)for(S=0;S<H;S++)x=o[S],x.drawColorTransparent(n);if(k>0)for(S=0;S<k;S++)f[S].drawEdgesXRayed(n);if(j>0)for(S=0;S<j;S++)d[S].drawSilhouetteXRayed(n);a.disable(a.BLEND),l||a.depthMask(!0)}if(Q>0||W>0){if(n.lastProgramId=null,t.highlightMaterial.glowThrough&&a.clear(a.DEPTH_BUFFER_BIT),W>0)for(S=0;S<W;S++)E[S].drawEdgesHighlighted(n);if(Q>0)for(S=0;S<Q;S++)T[S].drawSilhouetteHighlighted(n)}if(z>0||K>0||Q>0){if(n.lastProgramId=null,t.selectedMaterial.glowThrough&&a.clear(a.DEPTH_BUFFER_BIT),a.enable(a.BLEND),r?(a.blendEquation(a.FUNC_ADD),a.blendFuncSeparate(a.SRC_ALPHA,a.ONE_MINUS_SRC_ALPHA,a.ONE,a.ONE_MINUS_SRC_ALPHA)):a.blendFunc(a.SRC_ALPHA,a.ONE_MINUS_SRC_ALPHA),a.enable(a.CULL_FACE),K>0)for(S=0;S<K;S++)D[S].drawEdgesHighlighted(n);if(z>0)for(S=0;S<z;S++)b[S].drawSilhouetteHighlighted(n);a.disable(a.BLEND)}if(Y>0||X>0){if(n.lastProgramId=null,t.selectedMaterial.glowThrough&&a.clear(a.DEPTH_BUFFER_BIT),X>0)for(S=0;S<X;S++)R[S].drawEdgesSelected(n);if(Y>0)for(S=0;S<Y;S++)P[S].drawSilhouetteSelected(n)}if(q>0||J>0){if(n.lastProgramId=null,t.selectedMaterial.glowThrough&&a.clear(a.DEPTH_BUFFER_BIT),a.enable(a.CULL_FACE),a.enable(a.BLEND),r?(a.blendEquation(a.FUNC_ADD),a.blendFuncSeparate(a.SRC_ALPHA,a.ONE_MINUS_SRC_ALPHA,a.ONE,a.ONE_MINUS_SRC_ALPHA)):a.blendFunc(a.SRC_ALPHA,a.ONE_MINUS_SRC_ALPHA),J>0)for(S=0;S<J;S++)_[S].drawEdgesSelected(n);if(q>0)for(S=0;S<q;S++)C[S].drawSilhouetteSelected(n);a.disable(a.BLEND)}const Z=Date.now(),$=A.frame;$.renderTime=(Z-L)/1e3,$.drawElements=n.drawElements,$.drawArrays=n.drawArrays,$.useProgram=n.useProgram,$.bindTexture=n.bindTexture,$.bindArray=n.bindArray;const ee=oe.MAX_TEXTURE_IMAGE_UNITS;for(let e=0;e<ee;e++)a.activeTexture(a.TEXTURE0+e);a.bindTexture(a.TEXTURE_CUBE_MAP,null),a.bindTexture(a.TEXTURE_2D,null);const te=oe.MAX_VERTEX_ATTRIBS;for(let e=0;e<te;e++)a.disableVertexAttribArray(e)}(e)}(e),A.frame.frameCount++,f=!1)},this.pick=function(){const e=h.vec3(),s=h.mat4(),r=h.mat4(),l=h.vec3(),u=h.vec3([0,1,0]),p=new Ae,A=h.vec2(),d=h.vec3(),f=h.vec3(),I=h.vec3();return h.vec3(),h.vec3(),function(y,m=p){let v;m.reset(),D();let w=null,T=null;if(m.pickSurface=y.pickSurface,y.canvasPos)d[0]=y.canvasPos[0],d[1]=y.canvasPos[1],w=t.camera.viewMatrix,T=t.camera.projMatrix,m.canvasPos=y.canvasPos;else{const n=h.frustumMat4(-1,1,-1,1,.01,t.camera.project.far,s);y.matrix?(w=y.matrix,T=n):(f.set(y.origin||[0,0,0]),I.set(y.direction||[0,0,1]),v=h.addVec3(f,I,e),l[0]=Math.random(),l[1]=Math.random(),l[2]=Math.random(),h.normalizeVec3(l),h.cross3Vec3(I,l,u),w=h.lookAtMat4v(f,v,u,r),T=n,m.origin=f,m.direction=I),d[0]=.5*i.clientWidth,d[1]=.5*i.clientHeight}for(let e in c)if(c.hasOwnProperty(e)){const t=c[e].drawableList;for(let e=0,s=t.length;e<s;e++){const s=t[e];s.setPickMatrices&&s.setPickMatrices(w,T)}}const E=g.getRenderBuffer("pick");E.bind();const b=function(e,s,i,r,l,u){n.reset(),n.backfaces=!0,n.frontface=!0,n.pickOrigin=u.origin,n.pickViewMatrix=i,n.pickProjMatrix=r,n.pickInvisible=!!l.pickInvisible,a.viewport(0,0,a.drawingBufferWidth,a.drawingBufferHeight),a.clearColor(0,0,0,0),a.enable(a.DEPTH_TEST),a.disable(a.CULL_FACE),a.disable(a.BLEND),a.clear(a.COLOR_BUFFER_BIT|a.DEPTH_BUFFER_BIT);const h=l.includeEntityIds,p=l.excludeEntityIds;for(let e in c)if(c.hasOwnProperty(e)){const t=c[e].drawableList;for(let e=0,s=t.length;e<s;e++){const s=t[e];!s.drawPickMesh||!0!==l.pickInvisible&&!1===s.visible||!1===s.pickable||(h&&!h[s.id]||p&&p[s.id]||s.drawPickMesh(n))}}const A=t.canvas.resolutionScale,d=e.read(Math.round(s[0]*A),Math.round(s[1]*A));let f=d[0]+256*d[1]+256*d[2]*256+256*d[3]*256*256;if(f<0)return;return o.items[f]}(E,d,w,T,y,m);if(!b)return E.unbind(),null;const P=b.delegatePickedEntity?b.delegatePickedEntity():b;return P?(y.pickSurface&&(b.canPickTriangle&&b.canPickTriangle()?(!function(e,s,i,r,l,o){if(!s.drawPickTriangles)return;n.reset(),n.backfaces=!0,n.frontface=!0,n.pickOrigin=o.origin,n.pickViewMatrix=r,n.pickProjMatrix=l,a.viewport(0,0,a.drawingBufferWidth,a.drawingBufferHeight),a.clearColor(0,0,0,0),a.enable(a.DEPTH_TEST),a.disable(a.CULL_FACE),a.disable(a.BLEND),a.clear(a.COLOR_BUFFER_BIT|a.DEPTH_BUFFER_BIT),s.drawPickTriangles(n);const c=t.canvas.resolutionScale,u=e.read(Math.round(i[0]*c),Math.round(i[1]*c));let h=u[0]+256*u[1]+256*u[2]*256+256*u[3]*256*256;h*=3,o.primIndex=h}(E,b,d,w,T,m),b.pickTriangleSurface(w,T,m),m.pickSurfacePrecision=!1):b.canPickWorldPos&&b.canPickWorldPos()&&(A[0]=t.camera.project.near,A[1]=t.camera.project.far,R(E,b,d,w,T,A,m),!1!==y.pickSurfaceNormal&&function(e,s,i,r,l,o){n.reset(),n.backfaces=!0,n.frontface=!0,n.pickOrigin=o.origin,n.pickViewMatrix=r,n.pickProjMatrix=l,a.viewport(0,0,a.drawingBufferWidth,a.drawingBufferHeight),a.clearColor(0,0,0,0),a.enable(a.DEPTH_TEST),a.disable(a.CULL_FACE),a.disable(a.BLEND),a.clear(a.COLOR_BUFFER_BIT|a.DEPTH_BUFFER_BIT),s.drawPickNormals(n);const c=t.canvas.resolutionScale,u=e.read(Math.round(i[0]*c),Math.round(i[1]*c)),p=[u[0]/256-.5,u[1]/256-.5,u[2]/256-.5];h.normalizeVec3(p),o.worldNormal=p}(E,b,d,w,T,m),m.pickSurfacePrecision=!1)),E.unbind(),m.entity=P,m):(E.unbind(),null)}}();const R=function(){const e=h.vec4(),s=h.vec4(),r=h.vec4(),l=h.vec4(),o=h.vec4(),c=h.mat4(),u=h.mat4(),p=h.mat4();return function(A,d,f,I,y,m,v){n.reset(),n.backfaces=!0,n.frontface=!0,n.pickOrigin=v.origin,n.pickViewMatrix=I,n.pickProjMatrix=y,n.pickZNear=m[0],n.pickZFar=m[1],n.pickElementsCount=d.pickElementsCount,n.pickElementsOffset=d.pickElementsOffset,a.viewport(0,0,a.drawingBufferWidth,a.drawingBufferHeight),a.clearColor(0,0,0,0),a.enable(a.DEPTH_TEST),a.disable(a.CULL_FACE),a.disable(a.BLEND),a.clear(a.COLOR_BUFFER_BIT|a.DEPTH_BUFFER_BIT),d.drawPickDepths(n);const w=t.canvas.resolutionScale,g=function(e){const t=[e[0]/256,e[1]/256,e[2]/256,e[3]/256],s=[1/16777216,1/65536,1/256,1];return h.dotVec4(t,s)}(A.read(Math.round(f[0]*w),Math.round(f[1]*w))),T=(f[0]-i.clientWidth/2)/(i.clientWidth/2),E=-(f[1]-i.clientHeight/2)/(i.clientHeight/2),b=d.origin;let D;if(b){const e=B(I,b,c);D=h.mulMat4(y,e,u)}else D=h.mulMat4(y,I,u);const P=h.inverseMat4(D,p);e[0]=T,e[1]=E,e[2]=-1,e[3]=1;let R=h.transformVec4(P,e);R=h.mulVec4Scalar(R,1/R[3]),s[0]=T,s[1]=E,s[2]=1,s[3]=1;let C=h.transformVec4(P,s);C=h.mulVec4Scalar(C,1/C[3]);const _=h.subVec3(C,R,r),O=h.addVec3(R,h.mulVec4Scalar(_,g,l),o);b&&h.addVec3(O,b),v.worldPos=O}}();function C(e,t){return 1-e/t*2}this.snapPick=function(e,s=50,i="vertex"){n.reset(),n.backfaces=!0,n.frontface=!0,n.pickZNear=t.camera.project.near,n.pickZFar=t.camera.project.far;let r=g.getRenderBuffer("uniquePickColors-aabs",{depthTexture:!0,size:[2*s+1,2*s+1]});var l,o;n.snapVectorA=[(l=e[0],o=a.drawingBufferWidth,l/o*2-1),C(e[1],a.drawingBufferHeight)],n.snapInvVectorAB=[a.drawingBufferWidth/(2*s),a.drawingBufferHeight/(2*s)],n.snapMode=i,r.bind(a.RGBA32I),a.viewport(0,0,r.size[0],r.size[1]),a.enable(a.DEPTH_TEST),a.frontFace(a.CCW),a.disable(a.CULL_FACE),a.depthMask(!0),a.disable(a.BLEND),a.depthFunc(a.LESS),a.clear(a.DEPTH_BUFFER_BIT),a.clearBufferiv(a.COLOR,0,new Int32Array([0,0,0,0]));const u=t.camera.viewMatrix,h=t.camera.projMatrix;for(let e in c)if(c.hasOwnProperty(e)){const t=c[e].drawableList;for(let e=0,s=t.length;e<s;e++){const s=t[e];s.setPickMatrices&&s.setPickMatrices(u,h)}}const p=function(e){e.snapPickLayerParams={},e.snapPickLayerNumber=0;for(let t in c){const s=c[t].drawableList;for(let t=0,n=s.length;t<n;t++){const n=s[t];n.drawSnapInitDepthBuf&&!n.culled&&n.visible&&n.pickable&&n.drawSnapInitDepthBuf(e)}}return e.snapPickLayerParams}(n),A=function(e){e.snapPickLayerParams={},e.snapPickLayerNumber=0;for(let t in c){const s=c[t].drawableList;for(let t=0,n=s.length;t<n;t++){const n=s[t];n.drawSnapDepths&&!n.culled&&n.visible&&n.pickable&&n.drawSnapDepths(e)}}return e.snapPickLayerParams}(n),d=r.readArray(a.RGBA_INTEGER,a.INT,Int32Array,4);r.unbind();let f=null;const I=4*s+s*r.size[0]*4,y=d.slice(I,I+4);if(0!==y[3]){const e=p[Math.abs(y[3])],t=e.origin,s=e.coordinateScale;f=[y[0]*s[0]+t[0],y[1]*s[1]+t[1],y[2]*s[2]+t[2]]}let m=[];for(let e=0;e<d.length;e+=4)if(d[e+3]>0){const t=Math.floor(e/4),s=r.size[0];r.size[1];const n=t%s-Math.floor(s/2),i=Math.floor(t/s)-Math.floor(s/2),a=Math.sqrt(Math.pow(n,2)+Math.pow(i,2));m.push({x:n,y:i,dist:a,result:[d[e+0],d[e+1],d[e+2],d[e+3]]})}let v=null;if(m.length>0){m.sort(((e,t)=>e.dist-t.dist)),m=m[0].result;const e=A[m[3]],t=e.origin,s=e.coordinateScale;v=[m[0]*s[0]+t[0],m[1]*s[1]+t[1],m[2]*s[2]+t[2]]}if(null===f&&null==v)return null;let w=null;return null!==v&&(w=t.camera.projectWorldPos(v)),{worldPos:f,snappedWorldPos:v,snappedCanvasPos:w}},this.addMarker=function(e){this._occlusionTester=this._occlusionTester||new De(t,g),this._occlusionTester.addMarker(e),t.occlusionTestCountdown=0},this.markerWorldPosUpdated=function(e){this._occlusionTester.markerWorldPosUpdated(e)},this.removeMarker=function(e){this._occlusionTester.removeMarker(e)},this.doOcclusionTest=function(){if(this._occlusionTester&&this._occlusionTester.needOcclusionTest){D(),this._occlusionTester.bindRenderBuf(),n.reset(),n.backfaces=!0,n.frontface=!0,a.viewport(0,0,a.drawingBufferWidth,a.drawingBufferHeight),a.clearColor(0,0,0,0),a.enable(a.DEPTH_TEST),a.disable(a.CULL_FACE),a.disable(a.BLEND),a.clear(a.COLOR_BUFFER_BIT|a.DEPTH_BUFFER_BIT);for(let e in c)if(c.hasOwnProperty(e)){const t=c[e].drawableList;for(let e=0,s=t.length;e<s;e++){const s=t[e];s.drawOcclusion&&!0!==s.culled&&!1!==s.visible&&!1!==s.pickable&&s.drawOcclusion(n)}}this._occlusionTester.drawMarkers(n),this._occlusionTester.doOcclusionTest(),this._occlusionTester.unbindRenderBuf()}},this.readPixels=function(e,t,s,n){const i=g.getRenderBuffer("snapshot");let a,r,l,o;for(i.bind(),i.clear(),this.render({force:!0,opaqueOnly:n}),r=0;r<s;r++)l=2*r,o=4*r,a=i.read(e[l],e[l+1]),t[o]=a[0],t[o+1]=a[1],t[o+2]=a[2],t[o+3]=a[3];i.unbind(),f=!0},this.beginSnapshot=function(e={}){const t=g.getRenderBuffer("snapshot");e.width&&e.height&&t.setSize([e.width,e.height]),t.bind(),t.clear(),T=!0},this.renderSnapshot=function(){if(!T)return;g.getRenderBuffer("snapshot").clear(),this.render({force:!0,opaqueOnly:!1}),f=!0},this.readSnapshot=function(e){return g.getRenderBuffer("snapshot").readImage(e)},this.readSnapshotAsCanvas=function(){return g.getRenderBuffer("snapshot").readImageAsCanvas()},this.endSnapshot=function(){if(!T)return;g.getRenderBuffer("snapshot").unbind(),T=!1},this.destroy=function(){c={},u={},g.destroy(),E.destroy(),b.destroy(),this._occlusionTester&&this._occlusionTester.destroy()}};class Ue extends C{constructor(e,t={}){super(e,t),this.KEY_BACKSPACE=8,this.KEY_TAB=9,this.KEY_ENTER=13,this.KEY_SHIFT=16,this.KEY_CTRL=17,this.KEY_ALT=18,this.KEY_PAUSE_BREAK=19,this.KEY_CAPS_LOCK=20,this.KEY_ESCAPE=27,this.KEY_PAGE_UP=33,this.KEY_PAGE_DOWN=34,this.KEY_END=35,this.KEY_HOME=36,this.KEY_LEFT_ARROW=37,this.KEY_UP_ARROW=38,this.KEY_RIGHT_ARROW=39,this.KEY_DOWN_ARROW=40,this.KEY_INSERT=45,this.KEY_DELETE=46,this.KEY_NUM_0=48,this.KEY_NUM_1=49,this.KEY_NUM_2=50,this.KEY_NUM_3=51,this.KEY_NUM_4=52,this.KEY_NUM_5=53,this.KEY_NUM_6=54,this.KEY_NUM_7=55,this.KEY_NUM_8=56,this.KEY_NUM_9=57,this.KEY_A=65,this.KEY_B=66,this.KEY_C=67,this.KEY_D=68,this.KEY_E=69,this.KEY_F=70,this.KEY_G=71,this.KEY_H=72,this.KEY_I=73,this.KEY_J=74,this.KEY_K=75,this.KEY_L=76,this.KEY_M=77,this.KEY_N=78,this.KEY_O=79,this.KEY_P=80,this.KEY_Q=81,this.KEY_R=82,this.KEY_S=83,this.KEY_T=84,this.KEY_U=85,this.KEY_V=86,this.KEY_W=87,this.KEY_X=88,this.KEY_Y=89,this.KEY_Z=90,this.KEY_LEFT_WINDOW=91,this.KEY_RIGHT_WINDOW=92,this.KEY_SELECT_KEY=93,this.KEY_NUMPAD_0=96,this.KEY_NUMPAD_1=97,this.KEY_NUMPAD_2=98,this.KEY_NUMPAD_3=99,this.KEY_NUMPAD_4=100,this.KEY_NUMPAD_5=101,this.KEY_NUMPAD_6=102,this.KEY_NUMPAD_7=103,this.KEY_NUMPAD_8=104,this.KEY_NUMPAD_9=105,this.KEY_MULTIPLY=106,this.KEY_ADD=107,this.KEY_SUBTRACT=109,this.KEY_DECIMAL_POINT=110,this.KEY_DIVIDE=111,this.KEY_F1=112,this.KEY_F2=113,this.KEY_F3=114,this.KEY_F4=115,this.KEY_F5=116,this.KEY_F6=117,this.KEY_F7=118,this.KEY_F8=119,this.KEY_F9=120,this.KEY_F10=121,this.KEY_F11=122,this.KEY_F12=123,this.KEY_NUM_LOCK=144,this.KEY_SCROLL_LOCK=145,this.KEY_SEMI_COLON=186,this.KEY_EQUAL_SIGN=187,this.KEY_COMMA=188,this.KEY_DASH=189,this.KEY_PERIOD=190,this.KEY_FORWARD_SLASH=191,this.KEY_GRAVE_ACCENT=192,this.KEY_OPEN_BRACKET=219,this.KEY_BACK_SLASH=220,this.KEY_CLOSE_BRACKET=221,this.KEY_SINGLE_QUOTE=222,this.KEY_SPACE=32,this.element=t.element,this.altDown=!1,this.ctrlDown=!1,this.mouseDownLeft=!1,this.mouseDownMiddle=!1,this.mouseDownRight=!1,this.keyDown=[],this.enabled=!0,this.keyboardEnabled=!0,this.mouseover=!1,this.mouseCanvasPos=h.vec2(),this._keyboardEventsElement=t.keyboardEventsElement||document,this._bindEvents()}_bindEvents(){if(!this._eventsBound){this._keyboardEventsElement.addEventListener("keydown",this._keyDownListener=e=>{this.enabled&&this.keyboardEnabled&&"INPUT"!==e.target.tagName&&"TEXTAREA"!==e.target.tagName&&(e.keyCode===this.KEY_CTRL?this.ctrlDown=!0:e.keyCode===this.KEY_ALT?this.altDown=!0:e.keyCode===this.KEY_SHIFT&&(this.shiftDown=!0),this.keyDown[e.keyCode]=!0,this.fire("keydown",e.keyCode,!0))},!1),this._keyboardEventsElement.addEventListener("keyup",this._keyUpListener=e=>{this.enabled&&this.keyboardEnabled&&"INPUT"!==e.target.tagName&&"TEXTAREA"!==e.target.tagName&&(e.keyCode===this.KEY_CTRL?this.ctrlDown=!1:e.keyCode===this.KEY_ALT?this.altDown=!1:e.keyCode===this.KEY_SHIFT&&(this.shiftDown=!1),this.keyDown[e.keyCode]=!1,this.fire("keyup",e.keyCode,!0))}),this.element.addEventListener("mouseenter",this._mouseEnterListener=e=>{this.enabled&&(this.mouseover=!0,this._getMouseCanvasPos(e),this.fire("mouseenter",this.mouseCanvasPos,!0))}),this.element.addEventListener("mouseleave",this._mouseLeaveListener=e=>{this.enabled&&(this.mouseover=!1,this._getMouseCanvasPos(e),this.fire("mouseleave",this.mouseCanvasPos,!0))}),this.element.addEventListener("mousedown",this._mouseDownListener=e=>{if(this.enabled){switch(e.which){case 1:this.mouseDownLeft=!0;break;case 2:this.mouseDownMiddle=!0;break;case 3:this.mouseDownRight=!0}this._getMouseCanvasPos(e),this.element.focus(),this.fire("mousedown",this.mouseCanvasPos,!0),this.mouseover&&e.preventDefault()}}),document.addEventListener("mouseup",this._mouseUpListener=e=>{if(this.enabled){switch(e.which){case 1:this.mouseDownLeft=!1;break;case 2:this.mouseDownMiddle=!1;break;case 3:this.mouseDownRight=!1}this.fire("mouseup",this.mouseCanvasPos,!0)}},!0),document.addEventListener("click",this._clickListener=e=>{if(this.enabled){switch(e.which){case 1:case 3:this.mouseDownLeft=!1,this.mouseDownRight=!1;break;case 2:this.mouseDownMiddle=!1}this._getMouseCanvasPos(e),this.fire("click",this.mouseCanvasPos,!0),this.mouseover&&e.preventDefault()}}),document.addEventListener("dblclick",this._dblClickListener=e=>{if(this.enabled){switch(e.which){case 1:case 3:this.mouseDownLeft=!1,this.mouseDownRight=!1;break;case 2:this.mouseDownMiddle=!1}this._getMouseCanvasPos(e),this.fire("dblclick",this.mouseCanvasPos,!0),this.mouseover&&e.preventDefault()}}),this.element.addEventListener("mousemove",this._mouseMoveListener=e=>{this.enabled&&(this._getMouseCanvasPos(e),this.fire("mousemove",this.mouseCanvasPos,!0),this.mouseover&&e.preventDefault())}),this.element.addEventListener("wheel",this._mouseWheelListener=(e,t)=>{if(!this.enabled)return;const s=Math.max(-1,Math.min(1,40*-e.deltaY));this.fire("mousewheel",s,!0)},{passive:!0});{let e,t;const s=2;this.on("mousedown",(s=>{e=s[0],t=s[1]})),this.on("mouseup",(n=>{e>=n[0]-s&&e<=n[0]+s&&t>=n[1]-s&&t<=n[1]+s&&this.fire("mouseclicked",n,!0)}))}this._eventsBound=!0}}_unbindEvents(){this._eventsBound&&(this._keyboardEventsElement.removeEventListener("keydown",this._keyDownListener),this._keyboardEventsElement.removeEventListener("keyup",this._keyUpListener),this.element.removeEventListener("mouseenter",this._mouseEnterListener),this.element.removeEventListener("mouseleave",this._mouseLeaveListener),this.element.removeEventListener("mousedown",this._mouseDownListener),document.removeEventListener("mouseup",this._mouseDownListener),document.removeEventListener("click",this._clickListener),document.removeEventListener("dblclick",this._dblClickListener),this.element.removeEventListener("mousemove",this._mouseMoveListener),this.element.removeEventListener("wheel",this._mouseWheelListener),window.OrientationChangeEvent&&window.removeEventListener("orientationchange",this._orientationchangedListener),window.DeviceMotionEvent&&window.removeEventListener("devicemotion",this._deviceMotionListener),window.DeviceOrientationEvent&&window.removeEventListener("deviceorientation",this._deviceOrientListener),this._eventsBound=!1)}_getMouseCanvasPos(e){if(e){let t=e.target,s=0,n=0;for(;t.offsetParent;)s+=t.offsetLeft,n+=t.offsetTop,t=t.offsetParent;this.mouseCanvasPos[0]=e.pageX-s,this.mouseCanvasPos[1]=e.pageY-n}else e=window.event,this.mouseCanvasPos[0]=e.x,this.mouseCanvasPos[1]=e.y}setEnabled(e){this.enabled!==e&&this.fire("enabled",this.enabled=e)}getEnabled(){return this.enabled}setKeyboardEnabled(e){this.keyboardEnabled=e}getKeyboardEnabled(){return this.keyboardEnabled}destroy(){super.destroy(),this._unbindEvents()}}const Ge=new e({});class Ve{constructor(e){this.id=Ge.addItem({});for(const t in e)e.hasOwnProperty(t)&&(this[t]=e[t])}destroy(){Ge.removeItem(this.id)}}class je extends C{get type(){return"Viewport"}constructor(e,t={}){super(e,t),this._state=new Ve({boundary:[0,0,100,100]}),this.boundary=t.boundary,this.autoBoundary=t.autoBoundary}set boundary(e){if(!this._autoBoundary){if(!e){const t=this.scene.canvas.boundary;e=[0,0,t[2],t[3]]}this._state.boundary=e,this.glRedraw(),this.fire("boundary",this._state.boundary)}}get boundary(){return this._state.boundary}set autoBoundary(e){(e=!!e)!==this._autoBoundary&&(this._autoBoundary=e,this._autoBoundary?this._onCanvasSize=this.scene.canvas.on("boundary",(function(e){const t=e[2],s=e[3];this._state.boundary=[0,0,t,s],this.glRedraw(),this.fire("boundary",this._state.boundary)}),this):this._onCanvasSize&&(this.scene.canvas.off(this._onCanvasSize),this._onCanvasSize=null),this.fire("autoBoundary",this._autoBoundary))}get autoBoundary(){return this._autoBoundary}_getState(){return this._state}destroy(){super.destroy(),this._state.destroy()}}class ke extends C{get type(){return"Perspective"}constructor(e,t={}){super(e,t),this.camera=e,this._state=new Ve({matrix:h.mat4(),inverseMatrix:h.mat4(),transposedMatrix:h.mat4(),near:.1,far:2e3}),this._inverseMatrixDirty=!0,this._transposedMatrixDirty=!0,this._fov=60,this._canvasResized=this.scene.canvas.on("boundary",this._needUpdate,this),this.fov=t.fov,this.fovAxis=t.fovAxis,this.near=t.near,this.far=t.far}_update(){const e=this.scene.viewport.boundary,t=e[2]/e[3],s=this._fovAxis;let n=this._fov;("x"===s||"min"===s&&t<1||"max"===s&&t>1)&&(n/=t),n=Math.min(n,120),h.perspectiveMat4(n*(Math.PI/180),t,this._state.near,this._state.far,this._state.matrix),this._inverseMatrixDirty=!0,this._transposedMatrixDirty=!0,this.glRedraw(),this.camera._updateScheduled=!0,this.fire("matrix",this._state.matrix)}set fov(e){(e=null!=e?e:60)!==this._fov&&(this._fov=e,this._needUpdate(0),this.fire("fov",this._fov))}get fov(){return this._fov}set fovAxis(e){e=e||"min",this._fovAxis!==e&&("x"!==e&&"y"!==e&&"min"!==e&&(this.error("Unsupported value for 'fovAxis': "+e+" - defaulting to 'min'"),e="min"),this._fovAxis=e,this._needUpdate(0),this.fire("fovAxis",this._fovAxis))}get fovAxis(){return this._fovAxis}set near(e){const t=null!=e?e:.1;this._state.near!==t&&(this._state.near=t,this._needUpdate(0),this.fire("near",this._state.near))}get near(){return this._state.near}set far(e){const t=null!=e?e:2e3;this._state.far!==t&&(this._state.far=t,this._needUpdate(0),this.fire("far",this._state.far))}get far(){return this._state.far}get matrix(){return this._updateScheduled&&this._doUpdate(),this._state.matrix}get inverseMatrix(){return this._updateScheduled&&this._doUpdate(),this._inverseMatrixDirty&&(h.inverseMat4(this._state.matrix,this._state.inverseMatrix),this._inverseMatrixDirty=!1),this._state.inverseMatrix}get transposedMatrix(){return this._updateScheduled&&this._doUpdate(),this._transposedMatrixDirty&&(h.transposeMat4(this._state.matrix,this._state.transposedMatrix),this._transposedMatrixDirty=!1),this._state.transposedMatrix}unproject(e,t,s,n,i){const a=this.scene.canvas.canvas,r=a.offsetWidth/2,l=a.offsetHeight/2;return s[0]=(e[0]-r)/r,s[1]=(e[1]-l)/l,s[2]=t,s[3]=1,h.mulMat4v4(this.inverseMatrix,s,n),h.mulVec3Scalar(n,1/n[3]),n[3]=1,n[1]*=-1,h.mulMat4v4(this.camera.inverseViewMatrix,n,i),i}destroy(){super.destroy(),this._state.destroy(),this.scene.canvas.off(this._canvasResized)}}class Qe extends C{get type(){return"Ortho"}constructor(e,t={}){super(e,t),this.camera=e,this._state=new Ve({matrix:h.mat4(),inverseMatrix:h.mat4(),transposedMatrix:h.mat4(),near:.1,far:2e3}),this._inverseMatrixDirty=!0,this._transposedMatrixDirty=!0,this.scale=t.scale,this.near=t.near,this.far=t.far,this._onCanvasBoundary=this.scene.canvas.on("boundary",this._needUpdate,this)}_update(){const e=this.scene,t=.5*this._scale,s=e.viewport.boundary,n=s[2],i=s[3],a=n/i;let r,l,o,c;n>i?(r=-t,l=t,o=t/a,c=-t/a):(r=-t*a,l=t*a,o=t,c=-t),h.orthoMat4c(r,l,c,o,this._state.near,this._state.far,this._state.matrix),this._inverseMatrixDirty=!0,this._transposedMatrixDirty=!0,this.glRedraw(),this.fire("matrix",this._state.matrix)}set scale(e){null==e&&(e=1),e<=0&&(e=.01),this._scale=e,this._needUpdate(0),this.fire("scale",this._scale)}get scale(){return this._scale}set near(e){const t=null!=e?e:.1;this._state.near!==t&&(this._state.near=t,this._needUpdate(0),this.fire("near",this._state.near))}get near(){return this._state.near}set far(e){const t=null!=e?e:2e3;this._state.far!==t&&(this._state.far=t,this._needUpdate(0),this.fire("far",this._state.far))}get far(){return this._state.far}get matrix(){return this._updateScheduled&&this._doUpdate(),this._state.matrix}get inverseMatrix(){return this._updateScheduled&&this._doUpdate(),this._inverseMatrixDirty&&(h.inverseMat4(this._state.matrix,this._state.inverseMatrix),this._inverseMatrixDirty=!1),this._state.inverseMatrix}get transposedMatrix(){return this._updateScheduled&&this._doUpdate(),this._transposedMatrixDirty&&(h.transposeMat4(this._state.matrix,this._state.transposedMatrix),this._transposedMatrixDirty=!1),this._state.transposedMatrix}unproject(e,t,s,n,i){const a=this.scene.canvas.canvas,r=a.offsetWidth/2,l=a.offsetHeight/2;return s[0]=(e[0]-r)/r,s[1]=(e[1]-l)/l,s[2]=t,s[3]=1,h.mulMat4v4(this.inverseMatrix,s,n),h.mulVec3Scalar(n,1/n[3]),n[3]=1,n[1]*=-1,h.mulMat4v4(this.camera.inverseViewMatrix,n,i),i}destroy(){super.destroy(),this._state.destroy(),this.scene.canvas.off(this._onCanvasBoundary)}}class We extends C{get type(){return"Frustum"}constructor(e,t={}){super(e,t),this.camera=e,this._state=new Ve({matrix:h.mat4(),inverseMatrix:h.mat4(),transposedMatrix:h.mat4(),near:.1,far:1e4}),this._left=-1,this._right=1,this._bottom=-1,this._top=1,this._inverseMatrixDirty=!0,this._transposedMatrixDirty=!0,this.left=t.left,this.right=t.right,this.bottom=t.bottom,this.top=t.top,this.near=t.near,this.far=t.far}_update(){h.frustumMat4(this._left,this._right,this._bottom,this._top,this._state.near,this._state.far,this._state.matrix),this._inverseMatrixDirty=!0,this._transposedMatrixDirty=!0,this.glRedraw(),this.fire("matrix",this._state.matrix)}set left(e){this._left=null!=e?e:-1,this._needUpdate(0),this.fire("left",this._left)}get left(){return this._left}set right(e){this._right=null!=e?e:1,this._needUpdate(0),this.fire("right",this._right)}get right(){return this._right}set top(e){this._top=null!=e?e:1,this._needUpdate(0),this.fire("top",this._top)}get top(){return this._top}set bottom(e){this._bottom=null!=e?e:-1,this._needUpdate(0),this.fire("bottom",this._bottom)}get bottom(){return this._bottom}set near(e){this._state.near=null!=e?e:.1,this._needUpdate(0),this.fire("near",this._state.near)}get near(){return this._state.near}set far(e){this._state.far=null!=e?e:1e4,this._needUpdate(0),this.fire("far",this._state.far)}get far(){return this._state.far}get matrix(){return this._updateScheduled&&this._doUpdate(),this._state.matrix}get inverseMatrix(){return this._updateScheduled&&this._doUpdate(),this._inverseMatrixDirty&&(h.inverseMat4(this._state.matrix,this._state.inverseMatrix),this._inverseMatrixDirty=!1),this._state.inverseMatrix}get transposedMatrix(){return this._updateScheduled&&this._doUpdate(),this._transposedMatrixDirty&&(h.transposeMat4(this._state.matrix,this._state.transposedMatrix),this._transposedMatrixDirty=!1),this._state.transposedMatrix}unproject(e,t,s,n,i){const a=this.scene.canvas.canvas,r=a.offsetWidth/2,l=a.offsetHeight/2;return s[0]=(e[0]-r)/r,s[1]=(e[1]-l)/l,s[2]=t,s[3]=1,h.mulMat4v4(this.inverseMatrix,s,n),h.mulVec3Scalar(n,1/n[3]),n[3]=1,n[1]*=-1,h.mulMat4v4(this.camera.inverseViewMatrix,n,i),i}destroy(){super.destroy(),this._state.destroy(),super.destroy()}}class ze extends C{get type(){return"CustomProjection"}constructor(e,t={}){super(e,t),this.camera=e,this._state=new Ve({matrix:h.mat4(),inverseMatrix:h.mat4(),transposedMatrix:h.mat4()}),this._inverseMatrixDirty=!0,this._transposedMatrixDirty=!1,this.matrix=t.matrix}set matrix(e){this._state.matrix.set(e||[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]),this._inverseMatrixDirty=!0,this._transposedMatrixDirty=!0,this.glRedraw(),this.fire("matrix",this._state.matrix)}get matrix(){return this._state.matrix}get inverseMatrix(){return this._updateScheduled&&this._doUpdate(),this._inverseMatrixDirty&&(h.inverseMat4(this._state.matrix,this._state.inverseMatrix),this._inverseMatrixDirty=!1),this._state.inverseMatrix}get transposedMatrix(){return this._updateScheduled&&this._doUpdate(),this._transposedMatrixDirty&&(h.transposeMat4(this._state.matrix,this._state.transposedMatrix),this._transposedMatrixDirty=!1),this._state.transposedMatrix}unproject(e,t,s,n,i){const a=this.scene.canvas.canvas,r=a.offsetWidth/2,l=a.offsetHeight/2;return s[0]=(e[0]-r)/r,s[1]=(e[1]-l)/l,s[2]=t,s[3]=1,h.mulMat4v4(this.inverseMatrix,s,n),h.mulVec3Scalar(n,1/n[3]),n[3]=1,n[1]*=-1,h.mulMat4v4(this.camera.inverseViewMatrix,n,i),i}destroy(){super.destroy(),this._state.destroy()}}const Ke=h.vec3(),Ye=h.vec3(),Xe=h.vec3(),qe=h.vec3(),Je=h.vec3(),Ze=h.vec3(),$e=h.vec4(),et=h.vec4(),tt=h.vec4(),st=h.mat4(),nt=h.mat4(),it=h.vec3(),at=h.vec3(),rt=h.vec3(),lt=h.vec3();class ot extends C{get type(){return"Camera"}constructor(e,t={}){super(e,t),this._state=new Ve({deviceMatrix:h.mat4(),hasDeviceMatrix:!1,matrix:h.mat4(),normalMatrix:h.mat4(),inverseMatrix:h.mat4()}),this._perspective=new ke(this),this._ortho=new Qe(this),this._frustum=new We(this),this._customProjection=new ze(this),this._project=this._perspective,this._eye=h.vec3([0,0,10]),this._look=h.vec3([0,0,0]),this._up=h.vec3([0,1,0]),this._worldUp=h.vec3([0,1,0]),this._worldRight=h.vec3([1,0,0]),this._worldForward=h.vec3([0,0,-1]),this.deviceMatrix=t.deviceMatrix,this.eye=t.eye,this.look=t.look,this.up=t.up,this.worldAxis=t.worldAxis,this.gimbalLock=t.gimbalLock,this.constrainPitch=t.constrainPitch,this.projection=t.projection,this._perspective.on("matrix",(()=>{"perspective"===this._projectionType&&this.fire("projMatrix",this._perspective.matrix)})),this._ortho.on("matrix",(()=>{"ortho"===this._projectionType&&this.fire("projMatrix",this._ortho.matrix)})),this._frustum.on("matrix",(()=>{"frustum"===this._projectionType&&this.fire("projMatrix",this._frustum.matrix)})),this._customProjection.on("matrix",(()=>{"customProjection"===this._projectionType&&this.fire("projMatrix",this._customProjection.matrix)}))}_update(){const e=this._state;let t;"ortho"===this.projection?(h.subVec3(this._eye,this._look,it),h.normalizeVec3(it,at),h.mulVec3Scalar(at,1e3,rt),h.addVec3(this._look,rt,lt),t=lt):t=this._eye,e.hasDeviceMatrix?(h.lookAtMat4v(t,this._look,this._up,nt),h.mulMat4(e.deviceMatrix,nt,e.matrix)):h.lookAtMat4v(t,this._look,this._up,e.matrix),h.inverseMat4(this._state.matrix,this._state.inverseMatrix),h.transposeMat4(this._state.inverseMatrix,this._state.normalMatrix),this.glRedraw(),this.fire("matrix",this._state.matrix),this.fire("viewMatrix",this._state.matrix)}orbitYaw(e){let t=h.subVec3(this._eye,this._look,Ke);h.rotationMat4v(.0174532925*e,this._gimbalLock?this._worldUp:this._up,st),t=h.transformPoint3(st,t,Ye),this.eye=h.addVec3(this._look,t,Xe),this.up=h.transformPoint3(st,this._up,qe)}orbitPitch(e){if(this._constrainPitch&&(e=h.dotVec3(this._up,this._worldUp)/h.DEGTORAD)<1)return;let t=h.subVec3(this._eye,this._look,Ke);const s=h.cross3Vec3(h.normalizeVec3(t,Ye),h.normalizeVec3(this._up,Xe));h.rotationMat4v(.0174532925*e,s,st),t=h.transformPoint3(st,t,qe),this.up=h.transformPoint3(st,this._up,Je),this.eye=h.addVec3(t,this._look,Ze)}yaw(e){let t=h.subVec3(this._look,this._eye,Ke);h.rotationMat4v(.0174532925*e,this._gimbalLock?this._worldUp:this._up,st),t=h.transformPoint3(st,t,Ye),this.look=h.addVec3(t,this._eye,Xe),this._gimbalLock&&(this.up=h.transformPoint3(st,this._up,qe))}pitch(e){if(this._constrainPitch&&(e=h.dotVec3(this._up,this._worldUp)/h.DEGTORAD)<1)return;let t=h.subVec3(this._look,this._eye,Ke);const s=h.cross3Vec3(h.normalizeVec3(t,Ye),h.normalizeVec3(this._up,Xe));h.rotationMat4v(.0174532925*e,s,st),this.up=h.transformPoint3(st,this._up,Ze),t=h.transformPoint3(st,t,qe),this.look=h.addVec3(t,this._eye,Je)}pan(e){const t=h.subVec3(this._eye,this._look,Ke),s=[0,0,0];let n;if(0!==e[0]){const i=h.cross3Vec3(h.normalizeVec3(t,[]),h.normalizeVec3(this._up,Ye));n=h.mulVec3Scalar(i,e[0]),s[0]+=n[0],s[1]+=n[1],s[2]+=n[2]}0!==e[1]&&(n=h.mulVec3Scalar(h.normalizeVec3(this._up,Xe),e[1]),s[0]+=n[0],s[1]+=n[1],s[2]+=n[2]),0!==e[2]&&(n=h.mulVec3Scalar(h.normalizeVec3(t,qe),e[2]),s[0]+=n[0],s[1]+=n[1],s[2]+=n[2]),this.eye=h.addVec3(this._eye,s,Je),this.look=h.addVec3(this._look,s,Ze)}zoom(e){const t=h.subVec3(this._eye,this._look,Ke),s=Math.abs(h.lenVec3(t,Ye)),n=Math.abs(s+e);if(n<.5)return;const i=h.normalizeVec3(t,Xe);this.eye=h.addVec3(this._look,h.mulVec3Scalar(i,n),qe)}set eye(e){this._eye.set(e||[0,0,10]),this._needUpdate(0),this.fire("eye",this._eye)}get eye(){return this._eye}set look(e){this._look.set(e||[0,0,0]),this._needUpdate(0),this.fire("look",this._look)}get look(){return this._look}set up(e){this._up.set(e||[0,1,0]),this._needUpdate(0),this.fire("up",this._up)}get up(){return this._up}set deviceMatrix(e){this._state.deviceMatrix.set(e||[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]),this._state.hasDeviceMatrix=!!e,this._needUpdate(0),this.fire("deviceMatrix",this._state.deviceMatrix)}get deviceMatrix(){return this._state.deviceMatrix}set worldAxis(e){e=e||[1,0,0,0,1,0,0,0,1],this._worldAxis?this._worldAxis.set(e):this._worldAxis=h.vec3(e),this._worldRight[0]=this._worldAxis[0],this._worldRight[1]=this._worldAxis[1],this._worldRight[2]=this._worldAxis[2],this._worldUp[0]=this._worldAxis[3],this._worldUp[1]=this._worldAxis[4],this._worldUp[2]=this._worldAxis[5],this._worldForward[0]=this._worldAxis[6],this._worldForward[1]=this._worldAxis[7],this._worldForward[2]=this._worldAxis[8],this.fire("worldAxis",this._worldAxis)}get worldAxis(){return this._worldAxis}get worldUp(){return this._worldUp}get xUp(){return this._worldUp[0]>this._worldUp[1]&&this._worldUp[0]>this._worldUp[2]}get yUp(){return this._worldUp[1]>this._worldUp[0]&&this._worldUp[1]>this._worldUp[2]}get zUp(){return this._worldUp[2]>this._worldUp[0]&&this._worldUp[2]>this._worldUp[1]}get worldRight(){return this._worldRight}get worldForward(){return this._worldForward}set gimbalLock(e){this._gimbalLock=!1!==e,this.fire("gimbalLock",this._gimbalLock)}get gimbalLock(){return this._gimbalLock}set constrainPitch(e){this._constrainPitch=!!e,this.fire("constrainPitch",this._constrainPitch)}get eyeLookDist(){return h.lenVec3(h.subVec3(this._look,this._eye,Ke))}get matrix(){return this._updateScheduled&&this._doUpdate(),this._state.matrix}get viewMatrix(){return this._updateScheduled&&this._doUpdate(),this._state.matrix}get normalMatrix(){return this._updateScheduled&&this._doUpdate(),this._state.normalMatrix}get viewNormalMatrix(){return this._updateScheduled&&this._doUpdate(),this._state.normalMatrix}get inverseViewMatrix(){return this._updateScheduled&&this._doUpdate(),this._state.inverseMatrix}get projMatrix(){return this[this.projection].matrix}get perspective(){return this._perspective}get ortho(){return this._ortho}get frustum(){return this._frustum}get customProjection(){return this._customProjection}set projection(e){e=e||"perspective",this._projectionType!==e&&("perspective"===e?this._project=this._perspective:"ortho"===e?this._project=this._ortho:"frustum"===e?this._project=this._frustum:"customProjection"===e?this._project=this._customProjection:(this.error("Unsupported value for 'projection': "+e+" defaulting to 'perspective'"),this._project=this._perspective,e="perspective"),this._project._update(),this._projectionType=e,this.glRedraw(),this._update(),this.fire("dirty"),this.fire("projection",this._projectionType),this.fire("projMatrix",this._project.matrix))}get projection(){return this._projectionType}get project(){return this._project}projectWorldPos(e){const t=$e,s=et,n=tt;t[0]=e[0],t[1]=e[1],t[2]=e[2],t[3]=1,h.mulMat4v4(this.viewMatrix,t,s),h.mulMat4v4(this.projMatrix,s,n),h.mulVec3Scalar(n,1/n[3]),n[3]=1,n[1]*=-1;const i=this.scene.canvas.canvas,a=i.offsetWidth/2,r=i.offsetHeight/2;return[n[0]*a+a,n[1]*r+r]}destroy(){super.destroy(),this._state.destroy()}}class ct extends C{get type(){return"Light"}get isLight(){return!0}constructor(e,t={}){super(e,t)}}class ut extends ct{get type(){return"DirLight"}constructor(e,t={}){super(e,t),this._shadowRenderBuf=null,this._shadowViewMatrix=null,this._shadowProjMatrix=null,this._shadowViewMatrixDirty=!0,this._shadowProjMatrixDirty=!0;const s=this.scene.camera,n=this.scene.canvas;this._onCameraViewMatrix=s.on("viewMatrix",(()=>{this._shadowViewMatrixDirty=!0})),this._onCameraProjMatrix=s.on("projMatrix",(()=>{this._shadowProjMatrixDirty=!0})),this._onCanvasBoundary=n.on("boundary",(()=>{this._shadowProjMatrixDirty=!0})),this._state=new Ve({type:"dir",dir:h.vec3([1,1,1]),color:h.vec3([.7,.7,.8]),intensity:1,space:t.space||"view",castsShadow:!1,getShadowViewMatrix:()=>{if(this._shadowViewMatrixDirty){this._shadowViewMatrix||(this._shadowViewMatrix=h.identityMat4());const e=this.scene.camera,t=this._state.dir,s=e.look,n=[s[0]-t[0],s[1]-t[1],s[2]-t[2]],i=[0,1,0];h.lookAtMat4v(n,s,i,this._shadowViewMatrix),this._shadowViewMatrixDirty=!1}return this._shadowViewMatrix},getShadowProjMatrix:()=>(this._shadowProjMatrixDirty&&(this._shadowProjMatrix||(this._shadowProjMatrix=h.identityMat4()),h.orthoMat4c(-40,40,-40,40,-40,80,this._shadowProjMatrix),this._shadowProjMatrixDirty=!1),this._shadowProjMatrix),getShadowRenderBuf:()=>(this._shadowRenderBuf||(this._shadowRenderBuf=new Le(this.scene.canvas.canvas,this.scene.canvas.gl,{size:[1024,1024]})),this._shadowRenderBuf)}),this.dir=t.dir,this.color=t.color,this.intensity=t.intensity,this.castsShadow=t.castsShadow,this.scene._lightCreated(this)}set dir(e){this._state.dir.set(e||[1,1,1]),this._shadowViewMatrixDirty=!0,this.glRedraw()}get dir(){return this._state.dir}set color(e){this._state.color.set(e||[.7,.7,.8]),this.glRedraw()}get color(){return this._state.color}set intensity(e){e=void 0!==e?e:1,this._state.intensity=e,this.glRedraw()}get intensity(){return this._state.intensity}set castsShadow(e){e=!!e,this._state.castsShadow!==e&&(this._state.castsShadow=e,this._shadowViewMatrixDirty=!0,this.glRedraw())}get castsShadow(){return this._state.castsShadow}destroy(){const e=this.scene.camera,t=this.scene.canvas;e.off(this._onCameraViewMatrix),e.off(this._onCameraProjMatrix),t.off(this._onCanvasBoundary),super.destroy(),this._state.destroy(),this._shadowRenderBuf&&this._shadowRenderBuf.destroy(),this.scene._lightDestroyed(this),this.glRedraw()}}class ht extends ct{get type(){return"AmbientLight"}constructor(e,t={}){super(e,t),this._state={type:"ambient",color:h.vec3([.7,.7,.7]),intensity:1},this.color=t.color,this.intensity=t.intensity,this.scene._lightCreated(this)}set color(e){this._state.color.set(e||[.7,.7,.8]),this.glRedraw()}get color(){return this._state.color}set intensity(e){this._state.intensity=void 0!==e?e:1,this.glRedraw()}get intensity(){return this._state.intensity}destroy(){super.destroy(),this.scene._lightDestroyed(this)}}class pt extends C{get type(){return"Geometry"}get isGeometry(){return!0}constructor(e,t={}){super(e,t),A.memory.meshes++}destroy(){super.destroy(),A.memory.meshes--}}var At=function(){const e=[],t=[],s=[],n=[],i=[];let a=0;const r=new Uint16Array(3),l=new Uint16Array(3),o=new Uint16Array(3),c=h.vec3(),u=h.vec3(),p=h.vec3(),A=h.vec3(),d=h.vec3(),f=h.vec3(),I=h.vec3();return function(y,m,v,w){!function(i,a){const r={};let l,o,c,u;const h=Math.pow(10,4);let p,A,d=0;for(p=0,A=i.length;p<A;p+=3)l=i[p],o=i[p+1],c=i[p+2],u=Math.round(l*h)+"_"+Math.round(o*h)+"_"+Math.round(c*h),void 0===r[u]&&(r[u]=d/3,e[d++]=l,e[d++]=o,e[d++]=c),t[p/3]=r[u];for(p=0,A=a.length;p<A;p++)n[p]=t[a[p]],s[n[p]]=a[p]}(y,m),function(t,s){a=0;for(let y=0,m=t;y<m;y+=3){const t=3*n[y],m=3*n[y+1],v=3*n[y+2];s?(r[0]=e[t],r[1]=e[t+1],r[2]=e[t+2],l[0]=e[m],l[1]=e[m+1],l[2]=e[m+2],o[0]=e[v],o[1]=e[v+1],o[2]=e[v+2],h.decompressPosition(r,s,c),h.decompressPosition(l,s,u),h.decompressPosition(o,s,p)):(c[0]=e[t],c[1]=e[t+1],c[2]=e[t+2],u[0]=e[m],u[1]=e[m+1],u[2]=e[m+2],p[0]=e[v],p[1]=e[v+1],p[2]=e[v+2]),h.subVec3(p,u,A),h.subVec3(c,u,d),h.cross3Vec3(A,d,f),h.normalizeVec3(f,I);const w=i[a]||(i[a]={normal:h.vec3()});w.normal[0]=I[0],w.normal[1]=I[1],w.normal[2]=I[2],a++}}(m.length,v);const g=[],T=Math.cos(h.DEGTORAD*w),E={};let b,D,P,R,C,_,B,O,S,N,x,L=!1;for(let e=0,t=m.length;e<t;e+=3){const t=e/3;for(let s=0;s<3;s++)b=n[e+s],D=n[e+(s+1)%3],P=Math.min(b,D),R=Math.max(b,D),C=P+","+R,void 0===E[C]?E[C]={index1:P,index2:R,face1:t,face2:void 0}:E[C].face2=t}for(C in E)_=E[C],void 0!==_.face2&&(B=i[_.face1].normal,O=i[_.face2].normal,S=h.dotVec3(B,O),S>T)||(N=s[_.index1],x=s[_.index2],(!L&&N>65535||x>65535)&&(L=!0),g.push(N),g.push(x));return L?new Uint32Array(g):new Uint16Array(g)}}();const dt=function(){const e=h.mat4(),t=h.mat4();return function(s,n){n=n||h.mat4();const i=s[0],a=s[1],r=s[2],l=s[3]-i,o=s[4]-a,c=s[5]-r,u=65535;return h.identityMat4(e),h.translationMat4v(s,e),h.identityMat4(t),h.scalingMat4v([l/u,o/u,c/u],t),h.mulMat4(e,t,n),n}}();var ft=function(){const e=h.mat4(),t=h.mat4();return function(s,n,i){const a=new Uint16Array(s.length),r=new Float32Array([i[0]!==n[0]?65535/(i[0]-n[0]):0,i[1]!==n[1]?65535/(i[1]-n[1]):0,i[2]!==n[2]?65535/(i[2]-n[2]):0]);let l;for(l=0;l<s.length;l+=3)a[l+0]=Math.max(0,Math.min(65535,Math.floor((s[l+0]-n[0])*r[0]))),a[l+1]=Math.max(0,Math.min(65535,Math.floor((s[l+1]-n[1])*r[1]))),a[l+2]=Math.max(0,Math.min(65535,Math.floor((s[l+2]-n[2])*r[2])));h.identityMat4(e),h.translationMat4v(n,e),h.identityMat4(t),h.scalingMat4v([(i[0]-n[0])/65535,(i[1]-n[1])/65535,(i[2]-n[2])/65535],t);return{quantized:a,decodeMatrix:h.mulMat4(e,t,h.identityMat4())}}}();var It=function(){const e=h.mat3(),t=h.mat3();return function(s,n,i){const a=new Uint16Array(s.length),r=new Float32Array([65535/(i[0]-n[0]),65535/(i[1]-n[1])]);let l;for(l=0;l<s.length;l+=2)a[l+0]=Math.max(0,Math.min(65535,Math.floor((s[l+0]-n[0])*r[0]))),a[l+1]=Math.max(0,Math.min(65535,Math.floor((s[l+1]-n[1])*r[1])));h.identityMat3(e),h.translationMat3v(n,e),h.identityMat3(t),h.scalingMat3v([(i[0]-n[0])/65535,(i[1]-n[1])/65535],t);return{quantized:a,decodeMatrix:h.mulMat3(e,t,h.identityMat3())}}}();function yt(e,t,s,n){let i=e[t]/(Math.abs(e[t])+Math.abs(e[t+1])+Math.abs(e[t+2])),a=e[t+1]/(Math.abs(e[t])+Math.abs(e[t+1])+Math.abs(e[t+2]));if(e[t+2]<0){let e=(1-Math.abs(a))*(i>=0?1:-1),t=(1-Math.abs(i))*(a>=0?1:-1);i=e,a=t}return new Int8Array([Math[s](127.5*i+(i<0?-1:0)),Math[n](127.5*a+(a<0?-1:0))])}function mt(e){let t=e[0],s=e[1];t/=t<0?127:128,s/=s<0?127:128;const n=1-Math.abs(t)-Math.abs(s);n<0&&(t=(1-Math.abs(s))*(t>=0?1:-1),s=(1-Math.abs(t))*(s>=0?1:-1));const i=Math.sqrt(t*t+s*s+n*n);return[t/i,s/i,n/i]}function vt(e,t,s){return e[t]*s[0]+e[t+1]*s[1]+e[t+2]*s[2]}const wt={getPositionsBounds:function(e){const t=new Float32Array(3),s=new Float32Array(3);let n,i;for(n=0;n<3;n++)t[n]=Number.MAX_VALUE,s[n]=-Number.MAX_VALUE;for(n=0;n<e.length;n+=3)for(i=0;i<3;i++)t[i]=Math.min(t[i],e[n+i]),s[i]=Math.max(s[i],e[n+i]);return{min:t,max:s}},createPositionsDecodeMatrix:dt,compressPositions:ft,compressPosition:function(e,t,s){const n=new Float32Array([t[3]!==t[0]?65535/(t[3]-t[0]):0,t[4]!==t[1]?65535/(t[4]-t[1]):0,t[5]!==t[2]?65535/(t[5]-t[2]):0]);s[0]=Math.max(0,Math.min(65535,Math.floor((e[0]-t[0])*n[0]))),s[1]=Math.max(0,Math.min(65535,Math.floor((e[1]-t[1])*n[1]))),s[2]=Math.max(0,Math.min(65535,Math.floor((e[2]-t[2])*n[2])))},decompressPositions:function(e,t,s=new Float32Array(e.length)){for(let n=0,i=e.length;n<i;n+=3)s[n+0]=e[n+0]*t[0]+t[12],s[n+1]=e[n+1]*t[5]+t[13],s[n+2]=e[n+2]*t[10]+t[14];return s},decompressPosition:function(e,t,s){return s[0]=e[0]*t[0]+t[12],s[1]=e[1]*t[5]+t[13],s[2]=e[2]*t[10]+t[14],s},decompressAABB:function(e,t,s=e){return s[0]=e[0]*t[0]+t[12],s[1]=e[1]*t[5]+t[13],s[2]=e[2]*t[10]+t[14],s[3]=e[3]*t[0]+t[12],s[4]=e[4]*t[5]+t[13],s[5]=e[5]*t[10]+t[14],s},getUVBounds:function(e){const t=new Float32Array(2),s=new Float32Array(2);let n,i;for(n=0;n<2;n++)t[n]=Number.MAX_VALUE,s[n]=-Number.MAX_VALUE;for(n=0;n<e.length;n+=2)for(i=0;i<2;i++)t[i]=Math.min(t[i],e[n+i]),s[i]=Math.max(s[i],e[n+i]);return{min:t,max:s}},compressUVs:It,decompressUVs:function(e,t,s=new Float32Array(e.length)){for(let n=0,i=e.length;n<i;n+=3)s[n+0]=e[n+0]*t[0]+t[6],s[n+1]=e[n+1]*t[4]+t[7];return s},decompressUV:function(e,t,s){s[0]=e[0]*t[0]+t[6],s[1]=e[1]*t[4]+t[7]},compressNormals:function(e){const t=new Int8Array(e.length);let s,n,i,a,r;for(let l=0;l<e.length;l+=3)i=s=yt(e,l,"floor","floor"),n=mt(s),a=r=vt(e,l,n),s=yt(e,l,"ceil","floor"),n=mt(s),a=vt(e,l,n),a>r&&(i=s,r=a),s=yt(e,l,"floor","ceil"),n=mt(s),a=vt(e,l,n),a>r&&(i=s,r=a),s=yt(e,l,"ceil","ceil"),n=mt(s),a=vt(e,l,n),a>r&&(i=s,r=a),t[l]=i[0],t[l+1]=i[1];return t},decompressNormals:function(e,t){for(let s=0,n=0,i=e.length;s<i;s+=2){let i=e[s+0],a=e[s+1];i=(2*i+1)/255,a=(2*a+1)/255;const r=1-Math.abs(i)-Math.abs(a);r<0&&(i=(1-Math.abs(a))*(i>=0?1:-1),a=(1-Math.abs(i))*(a>=0?1:-1));const l=Math.sqrt(i*i+a*a+r*r);t[n+0]=i/l,t[n+1]=a/l,t[n+2]=r/l,n+=3}return t},decompressNormal:function(e,t){let s=e[0],n=e[1];s=(2*s+1)/255,n=(2*n+1)/255;const i=1-Math.abs(s)-Math.abs(n);i<0&&(s=(1-Math.abs(n))*(s>=0?1:-1),n=(1-Math.abs(s))*(n>=0?1:-1));const a=Math.sqrt(s*s+n*n+i*i);return t[0]=s/a,t[1]=n/a,t[2]=i/a,t}},gt=A.memory,Tt=h.AABB3();class Et extends pt{get type(){return"ReadableGeometry"}get isReadableGeometry(){return!0}constructor(e,t={}){super(e,t),this._state=new Ve({compressGeometry:!!t.compressGeometry,primitive:null,primitiveName:null,positions:null,normals:null,colors:null,uv:null,indices:null,positionsDecodeMatrix:null,uvDecodeMatrix:null,positionsBuf:null,normalsBuf:null,colorsbuf:null,uvBuf:null,indicesBuf:null,hash:""}),this._numTriangles=0,this._edgeThreshold=t.edgeThreshold||10,this._edgeIndicesBuf=null,this._pickTrianglePositionsBuf=null,this._pickTriangleColorsBuf=null,this._aabbDirty=!0,this._boundingSphere=!0,this._aabb=null,this._aabbDirty=!0,this._obb=null,this._obbDirty=!0;const s=this._state,n=this.scene.canvas.gl;switch(t.primitive=t.primitive||"triangles",t.primitive){case"points":s.primitive=n.POINTS,s.primitiveName=t.primitive;break;case"lines":s.primitive=n.LINES,s.primitiveName=t.primitive;break;case"line-loop":s.primitive=n.LINE_LOOP,s.primitiveName=t.primitive;break;case"line-strip":s.primitive=n.LINE_STRIP,s.primitiveName=t.primitive;break;case"triangles":s.primitive=n.TRIANGLES,s.primitiveName=t.primitive;break;case"triangle-strip":s.primitive=n.TRIANGLE_STRIP,s.primitiveName=t.primitive;break;case"triangle-fan":s.primitive=n.TRIANGLE_FAN,s.primitiveName=t.primitive;break;default:this.error("Unsupported value for 'primitive': '"+t.primitive+"' - supported values are 'points', 'lines', 'line-loop', 'line-strip', 'triangles', 'triangle-strip' and 'triangle-fan'. Defaulting to 'triangles'."),s.primitive=n.TRIANGLES,s.primitiveName=t.primitive}if(t.positions)if(this._state.compressGeometry){const e=wt.getPositionsBounds(t.positions),n=wt.compressPositions(t.positions,e.min,e.max);s.positions=n.quantized,s.positionsDecodeMatrix=n.decodeMatrix}else s.positions=t.positions.constructor===Float32Array?t.positions:new Float32Array(t.positions);if(t.colors&&(s.colors=t.colors.constructor===Float32Array?t.colors:new Float32Array(t.colors)),t.uv)if(this._state.compressGeometry){const e=wt.getUVBounds(t.uv),n=wt.compressUVs(t.uv,e.min,e.max);s.uv=n.quantized,s.uvDecodeMatrix=n.decodeMatrix}else s.uv=t.uv.constructor===Float32Array?t.uv:new Float32Array(t.uv);t.normals&&(this._state.compressGeometry?s.normals=wt.compressNormals(t.normals):s.normals=t.normals.constructor===Float32Array?t.normals:new Float32Array(t.normals)),t.indices&&(s.indices=t.indices.constructor===Uint32Array||t.indices.constructor===Uint16Array?t.indices:new Uint32Array(t.indices),"triangles"===this._state.primitiveName&&(this._numTriangles=t.indices.length/3)),this._buildHash(),gt.meshes++,this._buildVBOs()}_buildVBOs(){const e=this._state,t=this.scene.canvas.gl;if(e.indices&&(e.indicesBuf=new ge(t,t.ELEMENT_ARRAY_BUFFER,e.indices,e.indices.length,1,t.STATIC_DRAW),gt.indices+=e.indicesBuf.numItems),e.positions&&(e.positionsBuf=new ge(t,t.ARRAY_BUFFER,e.positions,e.positions.length,3,t.STATIC_DRAW),gt.positions+=e.positionsBuf.numItems),e.normals){let s=e.compressGeometry;e.normalsBuf=new ge(t,t.ARRAY_BUFFER,e.normals,e.normals.length,3,t.STATIC_DRAW,s),gt.normals+=e.normalsBuf.numItems}e.colors&&(e.colorsBuf=new ge(t,t.ARRAY_BUFFER,e.colors,e.colors.length,4,t.STATIC_DRAW),gt.colors+=e.colorsBuf.numItems),e.uv&&(e.uvBuf=new ge(t,t.ARRAY_BUFFER,e.uv,e.uv.length,2,t.STATIC_DRAW),gt.uvs+=e.uvBuf.numItems)}_buildHash(){const e=this._state,t=["/g"];t.push("/"+e.primitive+";"),e.positions&&t.push("p"),e.colors&&t.push("c"),(e.normals||e.autoVertexNormals)&&t.push("n"),e.uv&&t.push("u"),e.compressGeometry&&t.push("cp"),t.push(";"),e.hash=t.join("")}_getEdgeIndices(){return this._edgeIndicesBuf||this._buildEdgeIndices(),this._edgeIndicesBuf}_getPickTrianglePositions(){return this._pickTrianglePositionsBuf||this._buildPickTriangleVBOs(),this._pickTrianglePositionsBuf}_getPickTriangleColors(){return this._pickTriangleColorsBuf||this._buildPickTriangleVBOs(),this._pickTriangleColorsBuf}_buildEdgeIndices(){const e=this._state;if(!e.positions||!e.indices)return;const t=this.scene.canvas.gl,s=At(e.positions,e.indices,e.positionsDecodeMatrix,this._edgeThreshold);this._edgeIndicesBuf=new ge(t,t.ELEMENT_ARRAY_BUFFER,s,s.length,1,t.STATIC_DRAW),gt.indices+=this._edgeIndicesBuf.numItems}_buildPickTriangleVBOs(){const e=this._state;if(!e.positions||!e.indices)return;const t=this.scene.canvas.gl,s=h.buildPickTriangles(e.positions,e.indices,e.compressGeometry),n=s.positions,i=s.colors;this._pickTrianglePositionsBuf=new ge(t,t.ARRAY_BUFFER,n,n.length,3,t.STATIC_DRAW),this._pickTriangleColorsBuf=new ge(t,t.ARRAY_BUFFER,i,i.length,4,t.STATIC_DRAW,!0),gt.positions+=this._pickTrianglePositionsBuf.numItems,gt.colors+=this._pickTriangleColorsBuf.numItems}_buildPickVertexVBOs(){}_webglContextLost(){this._sceneVertexBufs&&this._sceneVertexBufs.webglContextLost()}_webglContextRestored(){this._sceneVertexBufs&&this._sceneVertexBufs.webglContextRestored(),this._buildVBOs(),this._edgeIndicesBuf=null,this._pickVertexPositionsBuf=null,this._pickTrianglePositionsBuf=null,this._pickTriangleColorsBuf=null,this._pickVertexPositionsBuf=null,this._pickVertexColorsBuf=null}get primitive(){return this._state.primitiveName}get compressGeometry(){return this._state.compressGeometry}get positions(){return this._state.positions?this._state.compressGeometry?(this._decompressedPositions||(this._decompressedPositions=new Float32Array(this._state.positions.length),wt.decompressPositions(this._state.positions,this._state.positionsDecodeMatrix,this._decompressedPositions)),this._decompressedPositions):this._state.positions:null}set positions(e){const t=this._state,s=t.positions;if(s)if(s.length===e.length){if(this._state.compressGeometry){const s=wt.getPositionsBounds(e),n=wt.compressPositions(e,s.min,s.max);e=n.quantized,t.positionsDecodeMatrix=n.decodeMatrix}s.set(e),t.positionsBuf&&t.positionsBuf.setData(s),this._setAABBDirty(),this.glRedraw()}else this.error("can't update geometry positions - new positions are wrong length");else this.error("can't update geometry positions - geometry has no positions")}get normals(){if(this._state.normals){if(!this._state.compressGeometry)return this._state.normals;if(!this._decompressedNormals){const e=this._state.normals.length,t=e+e/2;this._decompressedNormals=new Float32Array(t),wt.decompressNormals(this._state.normals,this._decompressedNormals)}return this._decompressedNormals}}set normals(e){if(this._state.compressGeometry)return void this.error("can't update geometry normals - quantized geometry is immutable");const t=this._state,s=t.normals;s?s.length===e.length?(s.set(e),t.normalsBuf&&t.normalsBuf.setData(s),this.glRedraw()):this.error("can't update geometry normals - new normals are wrong length"):this.error("can't update geometry normals - geometry has no normals")}get uv(){return this._state.uv?this._state.compressGeometry?(this._decompressedUV||(this._decompressedUV=new Float32Array(this._state.uv.length),wt.decompressUVs(this._state.uv,this._state.uvDecodeMatrix,this._decompressedUV)),this._decompressedUV):this._state.uv:null}set uv(e){if(this._state.compressGeometry)return void this.error("can't update geometry UVs - quantized geometry is immutable");const t=this._state,s=t.uv;s?s.length===e.length?(s.set(e),t.uvBuf&&t.uvBuf.setData(s),this.glRedraw()):this.error("can't update geometry UVs - new UVs are wrong length"):this.error("can't update geometry UVs - geometry has no UVs")}get colors(){return this._state.colors}set colors(e){if(this._state.compressGeometry)return void this.error("can't update geometry colors - quantized geometry is immutable");const t=this._state,s=t.colors;s?s.length===e.length?(s.set(e),t.colorsBuf&&t.colorsBuf.setData(s),this.glRedraw()):this.error("can't update geometry colors - new colors are wrong length"):this.error("can't update geometry colors - geometry has no colors")}get indices(){return this._state.indices}get aabb(){return this._aabbDirty&&(this._aabb||(this._aabb=h.AABB3()),h.positions3ToAABB3(this._state.positions,this._aabb,this._state.positionsDecodeMatrix),this._aabbDirty=!1),this._aabb}get obb(){return this._obbDirty&&(this._obb||(this._obb=h.OBB3()),h.positions3ToAABB3(this._state.positions,Tt,this._state.positionsDecodeMatrix),h.AABB3ToOBB3(Tt,this._obb),this._obbDirty=!1),this._obb}get numTriangles(){return this._numTriangles}_setAABBDirty(){this._aabbDirty||(this._aabbDirty=!0,this._aabbDirty=!0,this._obbDirty=!0)}_getState(){return this._state}destroy(){super.destroy();const e=this._state;e.indicesBuf&&e.indicesBuf.destroy(),e.positionsBuf&&e.positionsBuf.destroy(),e.normalsBuf&&e.normalsBuf.destroy(),e.uvBuf&&e.uvBuf.destroy(),e.colorsBuf&&e.colorsBuf.destroy(),this._edgeIndicesBuf&&this._edgeIndicesBuf.destroy(),this._pickTrianglePositionsBuf&&this._pickTrianglePositionsBuf.destroy(),this._pickTriangleColorsBuf&&this._pickTriangleColorsBuf.destroy(),this._pickVertexPositionsBuf&&this._pickVertexPositionsBuf.destroy(),this._pickVertexColorsBuf&&this._pickVertexColorsBuf.destroy(),e.destroy(),gt.meshes--}}function bt(e={}){let t=e.xSize||1;t<0&&(console.error("negative xSize not allowed - will invert"),t*=-1);let s=e.ySize||1;s<0&&(console.error("negative ySize not allowed - will invert"),s*=-1);let n=e.zSize||1;n<0&&(console.error("negative zSize not allowed - will invert"),n*=-1);const i=e.center,a=i?i[0]:0,r=i?i[1]:0,l=i?i[2]:0,o=-t+a,c=-s+r,u=-n+l,h=t+a,p=s+r,A=n+l;return y.apply(e,{positions:[h,p,A,o,p,A,o,c,A,h,c,A,h,p,A,h,c,A,h,c,u,h,p,u,h,p,A,h,p,u,o,p,u,o,p,A,o,p,A,o,p,u,o,c,u,o,c,A,o,c,u,h,c,u,h,c,A,o,c,A,h,c,u,o,c,u,o,p,u,h,p,u],normals:[0,0,1,0,0,1,0,0,1,0,0,1,1,0,0,1,0,0,1,0,0,1,0,0,0,1,0,0,1,0,0,1,0,0,1,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,0,-1,0,0,-1,0,0,-1,0,0,-1],uv:[1,0,0,0,0,1,1,1,0,0,0,1,1,1,1,0,1,1,1,0,0,0,0,1,1,0,0,0,0,1,1,1,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0],indices:[0,1,2,0,2,3,4,5,6,4,6,7,8,9,10,8,10,11,12,13,14,12,14,15,16,17,18,16,18,19,20,21,22,20,22,23]})}class Dt extends C{get type(){return"Material"}constructor(e,t={}){super(e,t),A.memory.materials++}destroy(){super.destroy(),A.memory.materials--}}const Pt={opaque:0,mask:1,blend:2},Rt=["opaque","mask","blend"];class Ct extends Dt{get type(){return"PhongMaterial"}constructor(e,t={}){super(e,t),this._state=new Ve({type:"PhongMaterial",ambient:h.vec3([1,1,1]),diffuse:h.vec3([1,1,1]),specular:h.vec3([1,1,1]),emissive:h.vec3([0,0,0]),alpha:null,shininess:null,reflectivity:null,alphaMode:null,alphaCutoff:null,lineWidth:null,pointSize:null,backfaces:null,frontface:null,hash:null}),this.ambient=t.ambient,this.diffuse=t.diffuse,this.specular=t.specular,this.emissive=t.emissive,this.alpha=t.alpha,this.shininess=t.shininess,this.reflectivity=t.reflectivity,this.lineWidth=t.lineWidth,this.pointSize=t.pointSize,t.ambientMap&&(this._ambientMap=this._checkComponent("Texture",t.ambientMap)),t.diffuseMap&&(this._diffuseMap=this._checkComponent("Texture",t.diffuseMap)),t.specularMap&&(this._specularMap=this._checkComponent("Texture",t.specularMap)),t.emissiveMap&&(this._emissiveMap=this._checkComponent("Texture",t.emissiveMap)),t.alphaMap&&(this._alphaMap=this._checkComponent("Texture",t.alphaMap)),t.reflectivityMap&&(this._reflectivityMap=this._checkComponent("Texture",t.reflectivityMap)),t.normalMap&&(this._normalMap=this._checkComponent("Texture",t.normalMap)),t.occlusionMap&&(this._occlusionMap=this._checkComponent("Texture",t.occlusionMap)),t.diffuseFresnel&&(this._diffuseFresnel=this._checkComponent("Fresnel",t.diffuseFresnel)),t.specularFresnel&&(this._specularFresnel=this._checkComponent("Fresnel",t.specularFresnel)),t.emissiveFresnel&&(this._emissiveFresnel=this._checkComponent("Fresnel",t.emissiveFresnel)),t.alphaFresnel&&(this._alphaFresnel=this._checkComponent("Fresnel",t.alphaFresnel)),t.reflectivityFresnel&&(this._reflectivityFresnel=this._checkComponent("Fresnel",t.reflectivityFresnel)),this.alphaMode=t.alphaMode,this.alphaCutoff=t.alphaCutoff,this.backfaces=t.backfaces,this.frontface=t.frontface,this._makeHash()}_makeHash(){const e=this._state,t=["/p"];this._normalMap&&(t.push("/nm"),this._normalMap.hasMatrix&&t.push("/mat")),this._ambientMap&&(t.push("/am"),this._ambientMap.hasMatrix&&t.push("/mat"),t.push("/"+this._ambientMap.encoding)),this._diffuseMap&&(t.push("/dm"),this._diffuseMap.hasMatrix&&t.push("/mat"),t.push("/"+this._diffuseMap.encoding)),this._specularMap&&(t.push("/sm"),this._specularMap.hasMatrix&&t.push("/mat")),this._emissiveMap&&(t.push("/em"),this._emissiveMap.hasMatrix&&t.push("/mat"),t.push("/"+this._emissiveMap.encoding)),this._alphaMap&&(t.push("/opm"),this._alphaMap.hasMatrix&&t.push("/mat")),this._reflectivityMap&&(t.push("/rm"),this._reflectivityMap.hasMatrix&&t.push("/mat")),this._occlusionMap&&(t.push("/ocm"),this._occlusionMap.hasMatrix&&t.push("/mat")),this._diffuseFresnel&&t.push("/df"),this._specularFresnel&&t.push("/sf"),this._emissiveFresnel&&t.push("/ef"),this._alphaFresnel&&t.push("/of"),this._reflectivityFresnel&&t.push("/rf"),t.push(";"),e.hash=t.join("")}set ambient(e){let t=this._state.ambient;if(t){if(e&&t[0]===e[0]&&t[1]===e[1]&&t[2]===e[2])return}else t=this._state.ambient=new Float32Array(3);e?(t[0]=e[0],t[1]=e[1],t[2]=e[2]):(t[0]=.2,t[1]=.2,t[2]=.2),this.glRedraw()}get ambient(){return this._state.ambient}set diffuse(e){let t=this._state.diffuse;if(t){if(e&&t[0]===e[0]&&t[1]===e[1]&&t[2]===e[2])return}else t=this._state.diffuse=new Float32Array(3);e?(t[0]=e[0],t[1]=e[1],t[2]=e[2]):(t[0]=1,t[1]=1,t[2]=1),this.glRedraw()}get diffuse(){return this._state.diffuse}set specular(e){let t=this._state.specular;if(t){if(e&&t[0]===e[0]&&t[1]===e[1]&&t[2]===e[2])return}else t=this._state.specular=new Float32Array(3);e?(t[0]=e[0],t[1]=e[1],t[2]=e[2]):(t[0]=1,t[1]=1,t[2]=1),this.glRedraw()}get specular(){return this._state.specular}set emissive(e){let t=this._state.emissive;if(t){if(e&&t[0]===e[0]&&t[1]===e[1]&&t[2]===e[2])return}else t=this._state.emissive=new Float32Array(3);e?(t[0]=e[0],t[1]=e[1],t[2]=e[2]):(t[0]=0,t[1]=0,t[2]=0),this.glRedraw()}get emissive(){return this._state.emissive}set alpha(e){e=null!=e?e:1,this._state.alpha!==e&&(this._state.alpha=e,this.glRedraw())}get alpha(){return this._state.alpha}set shininess(e){this._state.shininess=void 0!==e?e:80,this.glRedraw()}get shininess(){return this._state.shininess}set lineWidth(e){this._state.lineWidth=e||1,this.glRedraw()}get lineWidth(){return this._state.lineWidth}set pointSize(e){this._state.pointSize=e||1,this.glRedraw()}get pointSize(){return this._state.pointSize}set reflectivity(e){this._state.reflectivity=void 0!==e?e:1,this.glRedraw()}get reflectivity(){return this._state.reflectivity}get normalMap(){return this._normalMap}get ambientMap(){return this._ambientMap}get diffuseMap(){return this._diffuseMap}get specularMap(){return this._specularMap}get emissiveMap(){return this._emissiveMap}get alphaMap(){return this._alphaMap}get reflectivityMap(){return this._reflectivityMap}get occlusionMap(){return this._occlusionMap}get diffuseFresnel(){return this._diffuseFresnel}get specularFresnel(){return this._specularFresnel}get emissiveFresnel(){return this._emissiveFresnel}get alphaFresnel(){return this._alphaFresnel}get reflectivityFresnel(){return this._reflectivityFresnel}set alphaMode(e){let t=Pt[e=e||"opaque"];void 0===t&&(this.error("Unsupported value for 'alphaMode': "+e+" - defaulting to 'opaque'"),t="opaque"),this._state.alphaMode!==t&&(this._state.alphaMode=t,this.glRedraw())}get alphaMode(){return Rt[this._state.alphaMode]}set alphaCutoff(e){null==e&&(e=.5),this._state.alphaCutoff!==e&&(this._state.alphaCutoff=e)}get alphaCutoff(){return this._state.alphaCutoff}set backfaces(e){e=!!e,this._state.backfaces!==e&&(this._state.backfaces=e,this.glRedraw())}get backfaces(){return this._state.backfaces}set frontface(e){e="cw"!==e,this._state.frontface!==e&&(this._state.frontface=e,this.glRedraw())}get frontface(){return this._state.frontface?"ccw":"cw"}destroy(){super.destroy(),this._state.destroy()}}const _t={default:{fill:!0,fillColor:[.4,.4,.4],fillAlpha:.2,edges:!0,edgeColor:[.2,.2,.2],edgeAlpha:.5,edgeWidth:1},defaultWhiteBG:{fill:!0,fillColor:[1,1,1],fillAlpha:.6,edgeColor:[.2,.2,.2],edgeAlpha:1,edgeWidth:1},defaultLightBG:{fill:!0,fillColor:[.4,.4,.4],fillAlpha:.2,edges:!0,edgeColor:[.2,.2,.2],edgeAlpha:.5,edgeWidth:1},defaultDarkBG:{fill:!0,fillColor:[.4,.4,.4],fillAlpha:.2,edges:!0,edgeColor:[.5,.5,.5],edgeAlpha:.5,edgeWidth:1},phosphorous:{fill:!0,fillColor:[0,0,0],fillAlpha:.4,edges:!0,edgeColor:[.9,.9,.9],edgeAlpha:.5,edgeWidth:2},sunset:{fill:!0,fillColor:[.9,.9,.6],fillAlpha:.2,edges:!0,edgeColor:[.9,.9,.9],edgeAlpha:.5,edgeWidth:1},vectorscope:{fill:!0,fillColor:[0,0,0],fillAlpha:.7,edges:!0,edgeColor:[.2,1,.2],edgeAlpha:1,edgeWidth:2},battlezone:{fill:!0,fillColor:[0,0,0],fillAlpha:1,edges:!0,edgeColor:[.2,1,.2],edgeAlpha:1,edgeWidth:3},sepia:{fill:!0,fillColor:[.970588207244873,.7965892553329468,.6660899519920349],fillAlpha:.4,edges:!0,edgeColor:[.529411792755127,.4577854573726654,.4100345969200134],edgeAlpha:1,edgeWidth:1},yellowHighlight:{fill:!0,fillColor:[1,1,0],fillAlpha:.5,edges:!0,edgeColor:[.529411792755127,.4577854573726654,.4100345969200134],edgeAlpha:1,edgeWidth:1},greenSelected:{fill:!0,fillColor:[0,1,0],fillAlpha:.5,edges:!0,edgeColor:[.4577854573726654,.529411792755127,.4100345969200134],edgeAlpha:1,edgeWidth:1},gamegrid:{fill:!0,fillColor:[.2,.2,.7],fillAlpha:.9,edges:!0,edgeColor:[.4,.4,1.6],edgeAlpha:.8,edgeWidth:3}};class Bt extends Dt{get type(){return"EmphasisMaterial"}get presets(){return _t}constructor(e,t={}){super(e,t),this._state=new Ve({type:"EmphasisMaterial",fill:null,fillColor:null,fillAlpha:null,edges:null,edgeColor:null,edgeAlpha:null,edgeWidth:null,backfaces:!0,glowThrough:!0}),this._preset="default",t.preset?(this.preset=t.preset,void 0!==t.fill&&(this.fill=t.fill),t.fillColor&&(this.fillColor=t.fillColor),void 0!==t.fillAlpha&&(this.fillAlpha=t.fillAlpha),void 0!==t.edges&&(this.edges=t.edges),t.edgeColor&&(this.edgeColor=t.edgeColor),void 0!==t.edgeAlpha&&(this.edgeAlpha=t.edgeAlpha),void 0!==t.edgeWidth&&(this.edgeWidth=t.edgeWidth),void 0!==t.backfaces&&(this.backfaces=t.backfaces),void 0!==t.glowThrough&&(this.glowThrough=t.glowThrough)):(this.fill=t.fill,this.fillColor=t.fillColor,this.fillAlpha=t.fillAlpha,this.edges=t.edges,this.edgeColor=t.edgeColor,this.edgeAlpha=t.edgeAlpha,this.edgeWidth=t.edgeWidth,this.backfaces=t.backfaces,this.glowThrough=t.glowThrough)}set fill(e){e=!1!==e,this._state.fill!==e&&(this._state.fill=e,this.glRedraw())}get fill(){return this._state.fill}set fillColor(e){let t=this._state.fillColor;if(t){if(e&&t[0]===e[0]&&t[1]===e[1]&&t[2]===e[2])return}else t=this._state.fillColor=new Float32Array(3);e?(t[0]=e[0],t[1]=e[1],t[2]=e[2]):(t[0]=.4,t[1]=.4,t[2]=.4),this.glRedraw()}get fillColor(){return this._state.fillColor}set fillAlpha(e){e=null!=e?e:.2,this._state.fillAlpha!==e&&(this._state.fillAlpha=e,this.glRedraw())}get fillAlpha(){return this._state.fillAlpha}set edges(e){e=!1!==e,this._state.edges!==e&&(this._state.edges=e,this.glRedraw())}get edges(){return this._state.edges}set edgeColor(e){let t=this._state.edgeColor;if(t){if(e&&t[0]===e[0]&&t[1]===e[1]&&t[2]===e[2])return}else t=this._state.edgeColor=new Float32Array(3);e?(t[0]=e[0],t[1]=e[1],t[2]=e[2]):(t[0]=.2,t[1]=.2,t[2]=.2),this.glRedraw()}get edgeColor(){return this._state.edgeColor}set edgeAlpha(e){e=null!=e?e:.5,this._state.edgeAlpha!==e&&(this._state.edgeAlpha=e,this.glRedraw())}get edgeAlpha(){return this._state.edgeAlpha}set edgeWidth(e){this._state.edgeWidth=e||1,this.glRedraw()}get edgeWidth(){return this._state.edgeWidth}set backfaces(e){e=!!e,this._state.backfaces!==e&&(this._state.backfaces=e,this.glRedraw())}get backfaces(){return this._state.backfaces}set glowThrough(e){e=!1!==e,this._state.glowThrough!==e&&(this._state.glowThrough=e,this.glRedraw())}get glowThrough(){return this._state.glowThrough}set preset(e){if(e=e||"default",this._preset===e)return;const t=_t[e];t?(this.fill=t.fill,this.fillColor=t.fillColor,this.fillAlpha=t.fillAlpha,this.edges=t.edges,this.edgeColor=t.edgeColor,this.edgeAlpha=t.edgeAlpha,this.edgeWidth=t.edgeWidth,this.glowThrough=t.glowThrough,this._preset=e):this.error("unsupported preset: '"+e+"' - supported values are "+Object.keys(_t).join(", "))}get preset(){return this._preset}destroy(){super.destroy(),this._state.destroy()}}const Ot={default:{edgeColor:[0,0,0],edgeAlpha:1,edgeWidth:1},defaultWhiteBG:{edgeColor:[.2,.2,.2],edgeAlpha:1,edgeWidth:1},defaultLightBG:{edgeColor:[.2,.2,.2],edgeAlpha:1,edgeWidth:1},defaultDarkBG:{edgeColor:[.5,.5,.5],edgeAlpha:1,edgeWidth:1}};class St extends Dt{get type(){return"EdgeMaterial"}get presets(){return Ot}constructor(e,t={}){super(e,t),this._state=new Ve({type:"EdgeMaterial",edges:null,edgeColor:null,edgeAlpha:null,edgeWidth:null}),this._preset="default",t.preset?(this.preset=t.preset,t.edgeColor&&(this.edgeColor=t.edgeColor),void 0!==t.edgeAlpha&&(this.edgeAlpha=t.edgeAlpha),void 0!==t.edgeWidth&&(this.edgeWidth=t.edgeWidth)):(this.edgeColor=t.edgeColor,this.edgeAlpha=t.edgeAlpha,this.edgeWidth=t.edgeWidth),this.edges=!1!==t.edges}set edges(e){e=!1!==e,this._state.edges!==e&&(this._state.edges=e,this.glRedraw())}get edges(){return this._state.edges}set edgeColor(e){let t=this._state.edgeColor;if(t){if(e&&t[0]===e[0]&&t[1]===e[1]&&t[2]===e[2])return}else t=this._state.edgeColor=new Float32Array(3);e?(t[0]=e[0],t[1]=e[1],t[2]=e[2]):(t[0]=.2,t[1]=.2,t[2]=.2),this.glRedraw()}get edgeColor(){return this._state.edgeColor}set edgeAlpha(e){e=null!=e?e:1,this._state.edgeAlpha!==e&&(this._state.edgeAlpha=e,this.glRedraw())}get edgeAlpha(){return this._state.edgeAlpha}set edgeWidth(e){this._state.edgeWidth=e||1,this.glRedraw()}get edgeWidth(){return this._state.edgeWidth}set preset(e){if(e=e||"default",this._preset===e)return;const t=Ot[e];t?(this.edgeColor=t.edgeColor,this.edgeAlpha=t.edgeAlpha,this.edgeWidth=t.edgeWidth,this._preset=e):this.error("unsupported preset: '"+e+"' - supported values are "+Object.keys(Ot).join(", "))}get preset(){return this._preset}destroy(){super.destroy(),this._state.destroy()}}const Nt={meters:{abbrev:"m"},metres:{abbrev:"m"},centimeters:{abbrev:"cm"},centimetres:{abbrev:"cm"},millimeters:{abbrev:"mm"},millimetres:{abbrev:"mm"},yards:{abbrev:"yd"},feet:{abbrev:"ft"},inches:{abbrev:"in"}};class xt extends C{constructor(e,t={}){super(e,t),this._units="meters",this._scale=1,this._origin=h.vec3([0,0,0]),this.units=t.units,this.scale=t.scale,this.origin=t.origin}get unitsInfo(){return Nt}set units(e){e||(e="meters");Nt[e]||(this.error("Unsupported value for 'units': "+e+" defaulting to 'meters'"),e="meters"),this._units=e,this.fire("units",this._units)}get units(){return this._units}set scale(e){(e=e||1)<=0?this.error("scale value should be larger than zero"):(this._scale=e,this.fire("scale",this._scale))}get scale(){return this._scale}set origin(e){if(!e)return this._origin[0]=0,this._origin[1]=0,void(this._origin[2]=0);this._origin[0]=e[0],this._origin[1]=e[1],this._origin[2]=e[2],this.fire("origin",this._origin)}get origin(){return this._origin}worldToRealPos(e,t=h.vec3(3)){t[0]=this._origin[0]+this._scale*e[0],t[1]=this._origin[1]+this._scale*e[1],t[2]=this._origin[2]+this._scale*e[2]}realToWorldPos(e,t=h.vec3(3)){return t[0]=(e[0]-this._origin[0])/this._scale,t[1]=(e[1]-this._origin[1])/this._scale,t[2]=(e[2]-this._origin[2])/this._scale,t}}class Lt extends C{constructor(e,t={}){super(e,t),this._supported=oe.SUPPORTED_EXTENSIONS.OES_standard_derivatives,this.enabled=t.enabled,this.kernelRadius=t.kernelRadius,this.intensity=t.intensity,this.bias=t.bias,this.scale=t.scale,this.minResolution=t.minResolution,this.numSamples=t.numSamples,this.blur=t.blur,this.blendCutoff=t.blendCutoff,this.blendFactor=t.blendFactor}get supported(){return this._supported}set enabled(e){e=!!e,this._enabled!==e&&(this._enabled=e,this.glRedraw())}get enabled(){return this._enabled}get possible(){if(!this._supported)return!1;if(!this._enabled)return!1;const e=this.scene.camera.projection;return"customProjection"!==e&&"frustum"!==e}get active(){return this._active}set kernelRadius(e){null==e&&(e=100),this._kernelRadius!==e&&(this._kernelRadius=e,this.glRedraw())}get kernelRadius(){return this._kernelRadius}set intensity(e){null==e&&(e=.15),this._intensity!==e&&(this._intensity=e,this.glRedraw())}get intensity(){return this._intensity}set bias(e){null==e&&(e=.5),this._bias!==e&&(this._bias=e,this.glRedraw())}get bias(){return this._bias}set scale(e){null==e&&(e=1),this._scale!==e&&(this._scale=e,this.glRedraw())}get scale(){return this._scale}set minResolution(e){null==e&&(e=0),this._minResolution!==e&&(this._minResolution=e,this.glRedraw())}get minResolution(){return this._minResolution}set numSamples(e){null==e&&(e=10),this._numSamples!==e&&(this._numSamples=e,this.glRedraw())}get numSamples(){return this._numSamples}set blur(e){e=!1!==e,this._blur!==e&&(this._blur=e,this.glRedraw())}get blur(){return this._blur}set blendCutoff(e){null==e&&(e=.3),this._blendCutoff!==e&&(this._blendCutoff=e,this.glRedraw())}get blendCutoff(){return this._blendCutoff}set blendFactor(e){null==e&&(e=1),this._blendFactor!==e&&(this._blendFactor=e,this.glRedraw())}get blendFactor(){return this._blendFactor}destroy(){super.destroy()}}const Mt={default:{pointSize:4,roundPoints:!0,perspectivePoints:!0},square:{pointSize:4,roundPoints:!1,perspectivePoints:!0},round:{pointSize:4,roundPoints:!0,perspectivePoints:!0}};class Ft extends Dt{get type(){return"PointsMaterial"}get presets(){return Mt}constructor(e,t={}){super(e,t),this._state=new Ve({type:"PointsMaterial",pointSize:null,roundPoints:null,perspectivePoints:null,minPerspectivePointSize:null,maxPerspectivePointSize:null,filterIntensity:null,minIntensity:null,maxIntensity:null}),t.preset?(this.preset=t.preset,void 0!==t.pointSize&&(this.pointSize=t.pointSize),void 0!==t.roundPoints&&(this.roundPoints=t.roundPoints),void 0!==t.perspectivePoints&&(this.perspectivePoints=t.perspectivePoints),void 0!==t.minPerspectivePointSize&&(this.minPerspectivePointSize=t.minPerspectivePointSize),void 0!==t.maxPerspectivePointSize&&(this.maxPerspectivePointSize=t.minPerspectivePointSize)):(this._preset="default",this.pointSize=t.pointSize,this.roundPoints=t.roundPoints,this.perspectivePoints=t.perspectivePoints,this.minPerspectivePointSize=t.minPerspectivePointSize,this.maxPerspectivePointSize=t.maxPerspectivePointSize),this.filterIntensity=t.filterIntensity,this.minIntensity=t.minIntensity,this.maxIntensity=t.maxIntensity}set pointSize(e){this._state.pointSize=e||2,this.glRedraw()}get pointSize(){return this._state.pointSize}set roundPoints(e){e=!1!==e,this._state.roundPoints!==e&&(this._state.roundPoints=e,this.scene._needRecompile=!0,this.glRedraw())}get roundPoints(){return this._state.roundPoints}set perspectivePoints(e){e=!1!==e,this._state.perspectivePoints!==e&&(this._state.perspectivePoints=e,this.scene._needRecompile=!0,this.glRedraw())}get perspectivePoints(){return this._state.perspectivePoints}set minPerspectivePointSize(e){this._state.minPerspectivePointSize=e||1,this.scene._needRecompile=!0,this.glRedraw()}get minPerspectivePointSize(){return this._state.minPerspectivePointSize}set maxPerspectivePointSize(e){this._state.maxPerspectivePointSize=e||6,this.scene._needRecompile=!0,this.glRedraw()}get maxPerspectivePointSize(){return this._state.maxPerspectivePointSize}set filterIntensity(e){e=!1!==e,this._state.filterIntensity!==e&&(this._state.filterIntensity=e,this.scene._needRecompile=!0,this.glRedraw())}get filterIntensity(){return this._state.filterIntensity}set minIntensity(e){this._state.minIntensity=null!=e?e:0,this.glRedraw()}get minIntensity(){return this._state.minIntensity}set maxIntensity(e){this._state.maxIntensity=null!=e?e:1,this.glRedraw()}get maxIntensity(){return this._state.maxIntensity}set preset(e){if(e=e||"default",this._preset===e)return;const t=Mt[e];t?(this.pointSize=t.pointSize,this.roundPoints=t.roundPoints,this.perspectivePoints=t.perspectivePoints,this.minPerspectivePointSize=t.minPerspectivePointSize,this.maxPerspectivePointSize=t.maxPerspectivePointSize,this._preset=e):this.error("unsupported preset: '"+e+"' - supported values are "+Object.keys(Mt).join(", "))}get preset(){return this._preset}get hash(){return[this.pointSize,this.roundPoints,this.perspectivePoints,this.minPerspectivePointSize,this.maxPerspectivePointSize,this.filterIntensity].join(";")}destroy(){super.destroy(),this._state.destroy()}}const Ht={default:{lineWidth:1},thick:{lineWidth:2},thicker:{lineWidth:4}};class Ut extends Dt{get type(){return"LinesMaterial"}get presets(){return Ht}constructor(e,t={}){super(e,t),this._state=new Ve({type:"LinesMaterial",lineWidth:null}),t.preset?(this.preset=t.preset,void 0!==t.lineWidth&&(this.lineWidth=t.lineWidth)):(this._preset="default",this.lineWidth=t.lineWidth)}set lineWidth(e){this._state.lineWidth=e||1,this.glRedraw()}get lineWidth(){return this._state.lineWidth}set preset(e){if(e=e||"default",this._preset===e)return;const t=Ht[e];t?(this.lineWidth=t.lineWidth,this._preset=e):this.error("unsupported preset: '"+e+"' - supported values are "+Object.keys(Ht).join(", "))}get preset(){return this._preset}get hash(){return[""+this.lineWidth].join(";")}destroy(){super.destroy(),this._state.destroy()}}const Gt=h.vec3(),Vt=h.vec3(),jt=h.mat4();class kt{constructor(){this.normal=h.vec3(),this.offset=0,this.testVertex=h.vec3()}set(e,t,s,n){const i=1/Math.sqrt(e*e+t*t+s*s);this.normal[0]=e*i,this.normal[1]=t*i,this.normal[2]=s*i,this.offset=n*i,this.testVertex[0]=this.normal[0]>=0?1:0,this.testVertex[1]=this.normal[1]>=0?1:0,this.testVertex[2]=this.normal[2]>=0?1:0}}class Qt{constructor(){this.planes=[new kt,new kt,new kt,new kt,new kt,new kt]}}function Wt(e,t,s){const n=h.mulMat4(s,t,jt),i=n[0],a=n[1],r=n[2],l=n[3],o=n[4],c=n[5],u=n[6],p=n[7],A=n[8],d=n[9],f=n[10],I=n[11],y=n[12],m=n[13],v=n[14],w=n[15];e.planes[0].set(l-i,p-o,I-A,w-y),e.planes[1].set(l+i,p+o,I+A,w+y),e.planes[2].set(l-a,p-c,I-d,w-m),e.planes[3].set(l+a,p+c,I+d,w+m),e.planes[4].set(l-r,p-u,I-f,w-v),e.planes[5].set(l+r,p+u,I+f,w+v)}function zt(e,t){let s=Qt.INSIDE;const n=Gt,i=Vt;n[0]=t[0],n[1]=t[1],n[2]=t[2],i[0]=t[3],i[1]=t[4],i[2]=t[5];const a=[n,i];for(let t=0;t<6;++t){const n=e.planes[t];if(n.normal[0]*a[n.testVertex[0]][0]+n.normal[1]*a[n.testVertex[1]][1]+n.normal[2]*a[n.testVertex[2]][2]+n.offset<0)return Qt.OUTSIDE;n.normal[0]*a[1-n.testVertex[0]][0]+n.normal[1]*a[1-n.testVertex[1]][1]+n.normal[2]*a[1-n.testVertex[2]][2]+n.offset<0&&(s=Qt.INTERSECT)}return s}Qt.INSIDE=0,Qt.INTERSECT=1,Qt.OUTSIDE=2;class Kt{constructor(e,t){this.primLODLevels=e,this.entitiesInLOD={},this.primCountInLOD={},this.targetFps=t,this.lodLevelIndex=0,this.consecutiveFramesWithTargetFps=0,this.consecutiveFramesWithoutTargetFps=0}initializeLodState(e){const t=Object.values(e.objects);if(0===t.length)return;const s={},n={};for(let e=0,i=t.length;e<i;e++){const i=t[e];i.numPrimitives,h.getAABB3Diag(i.aabb);let a,r=0;for(r=0,a=this.primLODLevels.length;r<a&&!(i.numPrimitives>=this.primLODLevels[r]);r++);const l=this.primLODLevels[r]||0;l in s||(s[l]=[]),s[l].push(i),l in n||(n[l]=0),n[l]+=i.numPrimitives}this.entitiesInLOD=s,this.primCountInLOD=n}}class Yt{constructor(e,t,s,n){this.id=t.id,this.scene=e,this.sceneModel=t,this.lodState=new Kt(s,n),this.lodState.initializeLodState(t)}_increaseLODLevelIndex(){const e=this.lodState;if(e.lodLevelIndex===e.primLODLevels.length)return!1;const t=e.entitiesInLOD[e.primLODLevels[e.lodLevelIndex]]||[];for(let e=0,s=t.length;e<s;e++)t[e].culledLOD=!0;return e.lodLevelIndex++,!0}_decreaseLODLevelIndex(){const e=this.lodState;if(0===e.lodLevelIndex)return!1;const t=e.entitiesInLOD[e.primLODLevels[e.lodLevelIndex-1]]||[];for(let e=0,s=t.length;e<s;e++)t[e].culledLOD=!1;return e.lodLevelIndex--,!0}applyLodCulling(e){let t=this.lodState,s=!1;return e<t.targetFps?++t.consecutiveFramesWithoutTargetFps>0&&(t.consecutiveFramesWithoutTargetFps=0,s=this._increaseLODLevelIndex()):e>t.targetFps+4&&++t.consecutiveFramesWithTargetFps>1&&(t.consecutiveFramesWithTargetFps=0,s=this._decreaseLODLevelIndex()),s}resetLodCulling(){let e=!1,t=!1;do{e|=t=this._decreaseLODLevelIndex()}while(t);return e}}class Xt extends C{constructor(e,t={}){super(e,t),this._scene=e,this._lodLevels=[2e3,600,150,80,20],this._lodManagers={},this._lodManagerList=[],this.enabled=t.enabled,this.targetFPS=t.targetFPS,this._init()}_init(){const e=new Array(4);let t=0,s=-1,n=Date.now(),i=0,a=0,r=a;this._scene.on("rendering",(()=>{if(-1!==s)for(let e=0,t=this._lodManagerList.length;e<t;e++)this._lodManagerList[e].applyLodCulling(s)})),this._scene.on("rendered",(()=>{n=Date.now(),window.requestAnimationFrame((()=>{t++;const a=Date.now();i=a-n,n=a,e[t%4]=i;let r=0;if(t>4){for(let t=0;t<4;t++)r+=e[t];s=4/r*1e3}}))})),this._scene.camera.on("matrix",(()=>{r=a})),this._scene.on("tick",(()=>{if(a-r>3)for(let e=0,t=this._lodManagerList.length;e<t;e++)this._lodManagerList[e].resetLodCulling();a++}))}set enabled(e){e=!!e,this._enabled!==e&&(this._enabled=e,this.glRedraw())}get enabled(){return this._enabled}set targetFPS(e){null==e&&(e=30),this._targetFPS!==e&&(this._targetFPS=e,this.glRedraw())}get targetFPS(){return this._targetFPS}getLODManager(e){const t=new Yt(this.scene,e,this._lodLevels,this._targetFPS);return this._lodManagers[t.id]=t,this._lodManagerList=Object.values(this._lodManagers),t}putLODManager(e){delete this._lodManagers[e.id],this._lodManagerList=Object.values(this._lodManagers)}destroy(){super.destroy()}}var qt={},Jt={};!function(e){if("object"==typeof Jt&&void 0!==qt)qt.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).RBush3D=e()}}((function(){return function e(t,s,n){function i(r,l){if(!s[r]){if(!t[r]){var o="function"==typeof require&&require;if(!l&&o)return o(r,!0);if(a)return a(r,!0);var c=new Error("Cannot find module '"+r+"'");throw c.code="MODULE_NOT_FOUND",c}var u=s[r]={exports:{}};t[r][0].call(u.exports,(function(e){return i(t[r][1][e]||e)}),u,u.exports,e,t,s,n)}return s[r].exports}for(var a="function"==typeof require&&require,r=0;r<n.length;r++)i(n[r]);return i}({1:[function(e,t,s){Object.defineProperty(s,"__esModule",{value:!0});var n=e("quickselect"),i=[],a=function(e){return i.push(e)},r=function(e){e&&(a(e),u(e)||e.children.forEach(r))},l=function(e){var t=i.pop();return t?(t.children=e,t.height=1,t.leaf=!0,t.minX=1/0,t.minY=1/0,t.minZ=1/0,t.maxX=-1/0,t.maxY=-1/0,t.maxZ=-1/0):t={children:e,height:1,leaf:!0,minX:1/0,minY:1/0,minZ:1/0,maxX:-1/0,maxY:-1/0,maxZ:-1/0},t},o=[],c=function(e,t){var s=o.pop();return s?(s.dist=e,s.node=t):s={dist:e,node:t},s},u=function(e){return e.leaf},h=function(e,t){return e.leaf},p=function(e,t,s){if(!s)return t.indexOf(e);for(var n=0;n<t.length;n++)if(s(e,t[n]))return n;return-1},A=function(e){d(e,0,e.children.length,e)},d=function(e,t,s,n){var i=n;i?(i.minX=1/0,i.minY=1/0,i.minZ=1/0,i.maxX=-1/0,i.maxY=-1/0,i.maxZ=-1/0):i=l([]);for(var a=t,r=void 0;a<s;a++)r=e.children[a],f(i,r);return i},f=function(e,t){return e.minX=Math.min(e.minX,t.minX),e.minY=Math.min(e.minY,t.minY),e.minZ=Math.min(e.minZ,t.minZ),e.maxX=Math.max(e.maxX,t.maxX),e.maxY=Math.max(e.maxY,t.maxY),e.maxZ=Math.max(e.maxZ,t.maxZ),e},I=function(e){return(e.maxX-e.minX)*(e.maxY-e.minY)*(e.maxZ-e.minZ)},y=function(e){return e.maxX-e.minX+(e.maxY-e.minY)+(e.maxZ-e.minZ)},m=function(e,t){var s=Math.min(e.minX,t.minX),n=Math.min(e.minY,t.minY),i=Math.min(e.minZ,t.minZ);return(Math.max(e.maxX,t.maxX)-s)*(Math.max(e.maxY,t.maxY)-n)*(Math.max(e.maxZ,t.maxZ)-i)},v=function(e,t){var s=Math.max(e.minX,t.minX),n=Math.max(e.minY,t.minY),i=Math.max(e.minZ,t.minZ),a=Math.min(e.maxX,t.maxX),r=Math.min(e.maxY,t.maxY),l=Math.min(e.maxZ,t.maxZ);return Math.max(0,a-s)*Math.max(0,r-n)*Math.max(0,l-i)},w=function(e,t){return e.minX<=t.minX&&e.minY<=t.minY&&e.minZ<=t.minZ&&t.maxX<=e.maxX&&t.maxY<=e.maxY&&t.maxZ<=e.maxZ};s.intersects=function(e,t){return t.minX<=e.maxX&&t.minY<=e.maxY&&t.minZ<=e.maxZ&&t.maxX>=e.minX&&t.maxY>=e.minY&&t.maxZ>=e.minZ},s.boxRayIntersects=function(e,t,s,n,i,a,r){var l=(e.minX-t)*i,o=(e.maxX-t)*i,c=(e.minY-s)*a,u=(e.maxY-s)*a,h=(e.minZ-n)*r,p=(e.maxZ-n)*r,A=Math.min(h,p),d=Math.max(h,p),f=Math.min(c,u),I=Math.max(c,u),y=Math.min(l,o),m=Math.max(l,o),v=Math.max(0,y,f,A);return Math.min(m,I,d)>=v?v:1/0};var g=function(e,t,s,i,a){for(var r,l=[t,s];l.length;)(s=l.pop())-(t=l.pop())<=i||(r=t+Math.ceil((s-t)/i/2)*i,n(e,r,t,s,a),l.push(t,r,r,s))},T=function(e,t){return e.minX-t.minX},E=function(e,t){return e.minY-t.minY},b=function(e,t){return e.minZ-t.minZ},D=function(){function e(e){void 0===e&&(e=16),this.maxEntries=Math.max(e,8),this.minEntries=Math.max(4,Math.ceil(.4*this.maxEntries)),this.clear()}return e.alloc=function(){return this.pool.pop()||new this},e.free=function(e){e.clear(),this.pool.push(e)},e.prototype.searchCustom=function(e,t){var s=this.data,n=[];if(!e(s,h(s)))return n;for(var i=[];s;){for(var a=0,r=s.children.length;a<r;a++){var l=s.children[a];e(l,h(s))&&(h(s)?n.push(l):t(l)?this._all(l,n):i.push(l))}s=i.pop()}return n},e.prototype.analyzeTriangles=function(e){void 0===e&&(e=this.data);var t=0;if(u(e))for(var s=0,n=e.children.length;s<n;s++)t+=e.children[s].numPrimitives;else for(s=0,n=e.children.length;s<n;s++)t+=this.analyzeTriangles(e.children[s]);return e.totalTriangles=t},e.prototype.groupNodesByNumTrianglesThreshold=function(e,t){void 0===t&&(this.analyzeTriangles(),t=this.data);for(var s=[],n=0,i=t.children.length;n<i;n++){var a=t.children[n];s.push({item:a,triangles:a.totalTriangles||a.numPrimitives})}s.sort((function(e,t){return-(e.item.triangles-t.item.triangles)}));var r=[],l=[],o=0;for(n=0,i=s.length;n<i;n++){var c=s[n];if(o+c.triangles<e)l.push(c),o+=c.triangles;else if(l.length&&(r.push(l),l=[],o=0),o+c.triangles<e)n--;else if(h(t,c.item))r.push([c]);else{for(var u=this.groupNodesByNumTrianglesThreshold(e,c.item),p=[],A=0,d=0,f=u.length;d<f-1;d++)r.push(u[n]);if(u.length>1){for(d=0,f=(u=u[u.length-1]).length;d<f;d++)(A+=u[d].triangles)<e?p.push(u[d]):(0==A&&p.push(u[d]),r.push(p),A=0,p=[]);p.forEach((function(e){l.push(e),o+=e.triangles}))}}}return l.length&&r.push(l),r},e.prototype.search=function(e){var t=this.data,n=[];if(!s.intersects(e,t))return n;for(var i=[];t;){for(var a=0,r=t.children.length;a<r;a++){var l=t.children[a];s.intersects(e,l)&&(h(t)?n.push(l):w(e,l)?this._all(l,n):i.push(l))}t=i.pop()}return n},e.prototype.collides=function(e){var t=this.data;if(!s.intersects(e,t))return!1;for(var n=[];t;){for(var i=0,a=t.children.length;i<a;i++){var r=t.children[i];if(s.intersects(e,r)){if(h(t)||w(e,r))return!0;n.push(r)}}t=n.pop()}return!1},e.prototype.raycastInv=function(e,t,n,i,a,r,l){void 0===l&&(l=1/0);var u=this.data;if(i===1/0&&a===1/0&&r===1/0)return c(1/0,void 0);if(s.boxRayIntersects(u,e,t,n,i,a,r)===1/0)return c(1/0,void 0);for(var p,A=[c(0,u)],d=function(e,t){var s=A[e];A[e]=A[t],A[t]=s},f=function(){var e=A[0],t=A.length-1;A[0]=A[t],A.length=t;for(var s=0;;){var n=s<<1|1;if(n>=t)break;var i=n+1;if(i<t&&A[i].dist<A[n].dist&&(n=i),A[s].dist<A[n].dist)break;d(s,n),s=n}return function(e){o.push(e)}(e),e.node},I=function(e,t){var s=A.length;for(A.push(c(e,t));s>0;){var n=s-1>>1;if(A[n].dist<=A[s].dist)break;d(s,n),s=n}},y=l;A.length&&A[0].dist<y;)for(var m=0,v=(u=f()).children.length;m<v;m++){var w=u.children[m],g=s.boxRayIntersects(w,e,t,n,i,a,r);if(h(u)){if(g<y){if(0===g)return c(g,w);y=g,p=w}}else I(g,w)}return c(y<l?y:1/0,p)},e.prototype.raycast=function(e,t,s,n,i,a,r){return void 0===r&&(r=1/0),this.raycastInv(e,t,s,1/n,1/i,1/a,r)},e.prototype.all=function(){return this._all(this.data,[])},e.prototype.load=function(e){if(!e||!e.length)return this;if(e.length<this.minEntries){for(var t=0,s=e.length;t<s;t++)this.insert(e[t]);return this}var n=this.build(e.slice(),0,e.length-1,0);if(this.data.children.length)if(this.data.height===n.height)this.splitRoot(this.data,n);else{if(this.data.height<n.height){var i=this.data;this.data=n,n=i}this._insert(n,this.data.height-n.height-1,!0)}else this.data=n;return this},e.prototype.insert=function(e){return e&&this._insert(e,this.data.height-1),this},e.prototype.clear=function(){return this.data&&r(this.data),this.data=l([]),this},e.prototype.remove=function(e,t){if(!e)return this;for(var s,n,i=this.data,a=0,r=!1,l=[],o=[];i||l.length;){if(i||(i=l.pop(),a=o.pop(),n=l[l.length-1],r=!0),u(i)&&-1!==(s=p(e,i.children,t)))return i.children.splice(s,1),l.push(i),this.condense(l),this;r||u(i)||!w(i,e)?n?(a++,i=n.children[a],r=!1):i=void 0:(l.push(i),o.push(a),a=0,n=i,i=i.children[0])}return this},e.prototype.toJSON=function(){return this.data},e.prototype.fromJSON=function(e){return r(this.data),this.data=e,this},e.prototype.build=function(e,t,s,n){var i,a=s-t+1,r=this.maxEntries;if(a<=r)return i=l(e.slice(t,s+1)),A(i),i;n||(n=Math.ceil(Math.log(a)/Math.log(r)),r=Math.ceil(a/Math.pow(r,n-1))),(i=l([])).leaf=!1,i.height=n;var o=Math.ceil(a/r),c=o*Math.ceil(Math.pow(r,2/3)),u=o*Math.ceil(Math.pow(r,1/3));g(e,t,s,u,T);for(var h=t;h<=s;h+=u){var p=Math.min(h+u-1,s);g(e,h,p,c,E);for(var d=h;d<=p;d+=c){var f=Math.min(d+c-1,p);g(e,d,f,o,b);for(var I=d;I<=f;I+=o){var y=Math.min(I+o-1,f);i.children.push(this.build(e,I,y,n-1))}}}return A(i),i},e.prototype._all=function(e,t){for(var s=[];e;)u(e)?t.push.apply(t,e.children):s.push.apply(s,e.children),e=s.pop();return t},e.prototype.chooseSubtree=function(e,t,s,n){for(var i,a,r;n.push(t),!u(t)&&n.length-1!==s;){i=a=1/0;for(var l=0,o=t.children.length;l<o;l++){var c=t.children[l],h=I(c),p=m(e,c)-h;p<a?(a=p,i=h<i?h:i,r=c):p===a&&h<i&&(i=h,r=c)}t=r||t.children[0]}return t},e.prototype.split=function(e,t){var s=e[t],n=s.children.length,i=this.minEntries;this.chooseSplitAxis(s,i,n);var a=this.chooseSplitIndex(s,i,n),r=l(s.children.splice(a,s.children.length-a));r.height=s.height,r.leaf=s.leaf,A(s),A(r),t?e[t-1].children.push(r):this.splitRoot(s,r)},e.prototype.splitRoot=function(e,t){this.data=l([e,t]),this.data.height=e.height+1,this.data.leaf=!1,A(this.data)},e.prototype.chooseSplitIndex=function(e,t,s){for(var n,i=1/0,a=1/0,r=t;r<=s-t;r++){var l=d(e,0,r),o=d(e,r,s),c=v(l,o),u=I(l)+I(o);c<i?(i=c,n=r,a=u<a?u:a):c===i&&u<a&&(a=u,n=r)}return n},e.prototype.chooseSplitAxis=function(e,t,s){var n=this.allDistMargin(e,t,s,T),i=this.allDistMargin(e,t,s,E),a=this.allDistMargin(e,t,s,b);n<i&&n<a?e.children.sort(T):i<n&&i<a&&e.children.sort(E)},e.prototype.allDistMargin=function(e,t,s,n){e.children.sort(n);for(var i=d(e,0,t),a=d(e,s-t,s),r=y(i)+y(a),l=t;l<s-t;l++){var o=e.children[l];f(i,o),r+=y(i)}for(l=s-t-1;l>=t;l--){o=e.children[l];f(a,o),r+=y(a)}return r},e.prototype.adjustParentBBoxes=function(e,t,s){for(var n=s;n>=0;n--)f(t[n],e)},e.prototype.condense=function(e){for(var t=e.length-1,s=void 0;t>=0;t--)0===e[t].children.length?t>0?((s=e[t-1].children).splice(s.indexOf(e[t]),1),a(e[t])):this.clear():A(e[t])},e.prototype._insert=function(e,t,s){var n=[],i=this.chooseSubtree(e,this.data,t,n);for(i.children.push(e),f(i,e);t>=0&&n[t].children.length>this.maxEntries;)this.split(n,t),t--;this.adjustParentBBoxes(e,n,t)},e.pool=[],e}();s.RBush3D=D},{quickselect:2}],2:[function(e,t,s){!function(e,n){"object"==typeof s&&void 0!==t?t.exports=n():e.quickselect=n()}(this,(function(){function e(s,n,i,a,r){for(;a>i;){if(a-i>600){var l=a-i+1,o=n-i+1,c=Math.log(l),u=.5*Math.exp(2*c/3),h=.5*Math.sqrt(c*u*(l-u)/l)*(o-l/2<0?-1:1);e(s,n,Math.max(i,Math.floor(n-o*u/l+h)),Math.min(a,Math.floor(n+(l-o)*u/l+h)),r)}var p=s[n],A=i,d=a;for(t(s,i,n),r(s[a],p)>0&&t(s,i,a);A<d;){for(t(s,A,d),A++,d--;r(s[A],p)<0;)A++;for(;r(s[d],p)>0;)d--}0===r(s[i],p)?t(s,i,d):t(s,++d,a),d<=n&&(i=d+1),n<=d&&(a=d-1)}}function t(e,t,s){var n=e[t];e[t]=e[s],e[s]=n}function s(e,t){return e<t?-1:e>t?1:0}return function(t,n,i,a,r){e(t,n,i||0,a||t.length-1,r||s)}}))},{}]},{},[1])(1)}));var Zt=qt.exports.RBush3D,$t=function(e){function t(e){var t=0;return e.meshes.forEach((function(e){t+=e.numPrimitives})),t}function s(e,t){var s={pos:{x:0,y:0},left:0,right:e,top:0,bottom:t,dir:0};function n(){(0==s.dir&&s.pos.x+1>=s.right||1==s.dir&&s.pos.y+1>=s.bottom||2==s.dir&&s.pos.x-1<=s.left-1||3==s.dir&&s.pos.y-1<=s.top-1)&&(s.dir=(s.dir+1)%4,0==s.dir&&s.left++,1==s.dir&&s.top++,2==s.dir&&s.right--,3==s.dir&&s.bottom--),0==s.dir&&s.pos.x++,1==s.dir&&s.pos.y++,2==s.dir&&s.pos.x--,3==s.dir&&s.pos.y--}for(var i=[],a=e*t;i.length<a;n())i.push(s.pos.x+"_"+s.pos.y);return i}var n,i,a,r,l=0,o=(i=function(t,s){for(var n=s||function(e){return!0},i=(e.aabbTree.data.maxX-e.aabbTree.data.minX)/10,a=(e.aabbTree.data.maxZ-e.aabbTree.data.minZ)/10,r=[],l=e.aabbTree.data.minX,o=0;o<10;o++,l+=i)for(var c=e.aabbTree.data.minZ,u=0;u<10;u++,c+=a)r.push({minX:l,maxX:l+i,minY:-1e8,maxY:1e8,minZ:c,maxZ:c+a,indexX:o,indexZ:u});var h={cellsByEntity:{},entitiesByCell:{},cellsX:10,cellsZ:10};return(r||[]).forEach((function(t){e.aabbTree.search(t).filter((function(e){return n(e.entity)})).forEach((function(e){var s=e.entity.id;h.cellsByEntity[s]=h.cellsByEntity[s]||{cells:[],entity:e.entity},h.cellsByEntity[s].cells.push(t);var n=t.indexX+"_"+t.indexZ;h.entitiesByCell[n]=h.entitiesByCell[n]||{entities:[],cell:t},h.entitiesByCell[n].entities.push(e.entity)}))})),h}((n={cellSideInMeters:.05,entityFilterFunc:function(e){return!0},maxPolygonsPerCluster:9e4,minPercentOfClustredPolygons:.9}).cellSideInMeters,n.entityFilterFunc),a=function(e,s){var n={},i=0;Object.keys(s).forEach((function(e){var a=s[e].cells.length,r=t(s[e].entity);n[a]=(n[a]||0)+r,i+=r}));var a=Object.keys(n);a.sort((function(e,t){return e-t}));for(var r=0,l=0,o=0;o<a.length&&l<e;o++)r=a[o],l+=n[r]/i;return{maxCellsPerEntity:r,polygonStats:{percentClustered:l,numberClustered:Math.round(l*i),numberUnclustered:i-Math.round(l*i)}}}(n.minPercentOfClustredPolygons,i.cellsByEntity),r=function(e,n,i,a,r,l){var o={},c={accumEntities:[]},u=[];s(e,n).forEach((function(e){var s=function(e,t,s,n){return(s[e]||{entities:[]}).entities.filter((function(e){return n[e.id].cells.length<=t}))}(e,i,a,r).filter((function(e){return!(e.id in o)}));s.forEach((function(e){o[e.id]=!0})),s.sort((function(e,s){return t(e)-t(s)}));var n=c.accumEntities.concat(s),h=[],p=l,A=0;do{for(;A<n.length;A++){var d=n[A],f=t(d);f>p&&(u.push(h),h=[],p=l),h.push(d),p-=f}}while(A<n.length);c.accumEntities=h})),c.accumEntities.length&&u.push(c.accumEntities);var h=[];Object.keys(r).forEach((function(e){e in o||h.push(r[e].entity)}));var p=l,A=[];h.forEach((function(e){var s=t(e);s>p&&(u.push(A),A=[],p=l),A.push(e),p-=s})),A.length&&u.push(A);var d={};return u.forEach((function(e,t){e.forEach((function(e){d[e.id]=t}))})),{clusters:u,entityIdToClusterIdMapping:d}}(i.cellsX,i.cellsZ,a.maxCellsPerEntity,i.entitiesByCell,i.cellsByEntity,n.maxPolygonsPerCluster),{clusters:{total:r.clusters.length},clusteringResult:r});l+=o.clusters.total,console.log("Total clusters: "+l);var c=[];return o.clusteringResult.clusters.forEach((function(e){e.forEach((function(e){c.push(e.id)}))})),o.orderedEntityIds=c,o};
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});class e{constructor(e,t){this.items=e||[],this._lastUniqueId=(t||0)+1}addItem(){let e;if(2===arguments.length){const t=arguments[0];if(e=arguments[1],this.items[t])throw"ID clash: '"+t+"'";return this.items[t]=e,t}for(e=arguments[0]||{};;){const t=this._lastUniqueId++;if(!this.items[t])return this.items[t]=e,t}}removeItem(e){const t=this.items[e];return delete this.items[e],t}}const t=new e;class s{constructor(e){this.id=e,this.parentItem=null,this.groups=[],this.menuElement=null,this.shown=!1,this.mouseOver=0}}class n{constructor(){this.items=[]}}class i{constructor(e,t,s,n,i){this.id=e,this.getTitle=t,this.doAction=s,this.getEnabled=n,this.getShown=i,this.itemElement=null,this.subMenu=null,this.enabled=!0}}class a{constructor(e,t,s){this.id=s&&s.id?s.id:e,this.viewer=t,this._subIdMap=null,this._subIdEvents=null,this._eventSubs=null,this._eventSubsNum=null,this._events=null,this._eventCallDepth=0,t.addPlugin(this)}fire(e,t,s){this._events||(this._events={}),this._eventSubs||(this._eventSubs={},this._eventSubsNum={}),!0!==s&&(this._events[e]=t||!0);const n=this._eventSubs[e];let i;if(n)for(const s in n)n.hasOwnProperty(s)&&(i=n[s],this._eventCallDepth++,this._eventCallDepth<300?i.callback.call(i.scope,t):this.error("fire: potential stack overflow from recursive event '"+e+"' - dropping this event"),this._eventCallDepth--)}on(t,s,n){this._events||(this._events={}),this._subIdMap||(this._subIdMap=new e),this._subIdEvents||(this._subIdEvents={}),this._eventSubs||(this._eventSubs={}),this._eventSubsNum||(this._eventSubsNum={});let i=this._eventSubs[t];i?this._eventSubsNum[t]++:(i={},this._eventSubs[t]=i,this._eventSubsNum[t]=1);const a=this._subIdMap.addItem();i[a]={callback:s,scope:n||this},this._subIdEvents[a]=t;const r=this._events[t];return void 0!==r&&s.call(n||this,r),a}off(e){if(null==e)return;if(!this._subIdEvents)return;const t=this._subIdEvents[e];if(t){delete this._subIdEvents[e];const s=this._eventSubs[t];s&&(delete s[e],this._eventSubsNum[t]--),this._subIdMap.removeItem(e)}}once(e,t,s){const n=this,i=this.on(e,(function(e){n.off(i),t.call(s||this,e)}),s)}hasSubs(e){return this._eventSubsNum&&this._eventSubsNum[e]>0}log(e){console.log(`[xeokit plugin ${this.id}]: ${e}`)}warn(e){console.warn(`[xeokit plugin ${this.id}]: ${e}`)}error(e){console.error(`[xeokit plugin ${this.id}]: ${e}`)}send(e,t){}destroy(){this.viewer.removePlugin(this)}}let r=!0,l=r?Float64Array:Float32Array;const o=new l(16),c=new l(16),u=new l(4),h={setDoublePrecisionEnabled(e){r=e,l=r?Float64Array:Float32Array},getDoublePrecisionEnabled:()=>r,MIN_DOUBLE:-Number.MAX_SAFE_INTEGER,MAX_DOUBLE:Number.MAX_SAFE_INTEGER,MAX_INT:1e7,DEGTORAD:.0174532925,RADTODEG:57.295779513,unglobalizeObjectId(e,t){const s=t.indexOf("#");return s===e.length&&t.startsWith(e)?t.substring(s+1):t},globalizeObjectId:(e,t)=>e+"#"+t,safeInv(e){const t=1/e;return isNaN(t)||!isFinite(t)?1:t},vec2:e=>new l(e||2),vec3:e=>new l(e||3),vec4:e=>new l(e||4),mat3:e=>new l(e||9),mat3ToMat4:(e,t=new l(16))=>(t[0]=e[0],t[1]=e[1],t[2]=e[2],t[3]=0,t[4]=e[3],t[5]=e[4],t[6]=e[5],t[7]=0,t[8]=e[6],t[9]=e[7],t[10]=e[8],t[11]=0,t[12]=0,t[13]=0,t[14]=0,t[15]=1,t),mat4:e=>new l(e||16),mat4ToMat3(e,t){},doublesToFloats(e,t,s){const n=new l(2);for(let i=0,a=e.length;i<a;i++)h.splitDouble(e[i],n),t[i]=n[0],s[i]=n[1]},splitDouble(e,t){const s=l.from([e])[0],n=e-s;t[0]=s,t[1]=n},createUUID:(()=>{const e=[];for(let t=0;t<256;t++)e[t]=(t<16?"0":"")+t.toString(16);return()=>{const t=4294967295*Math.random()|0,s=4294967295*Math.random()|0,n=4294967295*Math.random()|0,i=4294967295*Math.random()|0;return`${e[255&t]+e[t>>8&255]+e[t>>16&255]+e[t>>24&255]}-${e[255&s]}${e[s>>8&255]}-${e[s>>16&15|64]}${e[s>>24&255]}-${e[63&n|128]}${e[n>>8&255]}-${e[n>>16&255]}${e[n>>24&255]}${e[255&i]}${e[i>>8&255]}${e[i>>16&255]}${e[i>>24&255]}`}})(),clamp:(e,t,s)=>Math.max(t,Math.min(s,e)),fmod(e,t){if(e<t)return console.error("math.fmod : Attempting to find modulus within negative range - would be infinite loop - ignoring"),e;for(;t<=e;)e-=t;return e},compareVec3:(e,t)=>e[0]===t[0]&&e[1]===t[1]&&e[2]===t[2],negateVec3:(e,t)=>(t||(t=e),t[0]=-e[0],t[1]=-e[1],t[2]=-e[2],t),negateVec4:(e,t)=>(t||(t=e),t[0]=-e[0],t[1]=-e[1],t[2]=-e[2],t[3]=-e[3],t),addVec4:(e,t,s)=>(s||(s=e),s[0]=e[0]+t[0],s[1]=e[1]+t[1],s[2]=e[2]+t[2],s[3]=e[3]+t[3],s),addVec4Scalar:(e,t,s)=>(s||(s=e),s[0]=e[0]+t,s[1]=e[1]+t,s[2]=e[2]+t,s[3]=e[3]+t,s),addVec3:(e,t,s)=>(s||(s=e),s[0]=e[0]+t[0],s[1]=e[1]+t[1],s[2]=e[2]+t[2],s),addVec3Scalar:(e,t,s)=>(s||(s=e),s[0]=e[0]+t,s[1]=e[1]+t,s[2]=e[2]+t,s),subVec4:(e,t,s)=>(s||(s=e),s[0]=e[0]-t[0],s[1]=e[1]-t[1],s[2]=e[2]-t[2],s[3]=e[3]-t[3],s),subVec3:(e,t,s)=>(s||(s=e),s[0]=e[0]-t[0],s[1]=e[1]-t[1],s[2]=e[2]-t[2],s),subVec2:(e,t,s)=>(s||(s=e),s[0]=e[0]-t[0],s[1]=e[1]-t[1],s),geometricMeanVec2(...e){const t=new l(e[0]);for(let s=1;s<e.length;s++)t[0]+=e[s][0],t[1]+=e[s][1];return t[0]/=e.length,t[1]/=e.length,t},subVec4Scalar:(e,t,s)=>(s||(s=e),s[0]=e[0]-t,s[1]=e[1]-t,s[2]=e[2]-t,s[3]=e[3]-t,s),subScalarVec4:(e,t,s)=>(s||(s=e),s[0]=t-e[0],s[1]=t-e[1],s[2]=t-e[2],s[3]=t-e[3],s),mulVec4:(e,t,s)=>(s||(s=e),s[0]=e[0]*t[0],s[1]=e[1]*t[1],s[2]=e[2]*t[2],s[3]=e[3]*t[3],s),mulVec4Scalar:(e,t,s)=>(s||(s=e),s[0]=e[0]*t,s[1]=e[1]*t,s[2]=e[2]*t,s[3]=e[3]*t,s),mulVec3Scalar:(e,t,s)=>(s||(s=e),s[0]=e[0]*t,s[1]=e[1]*t,s[2]=e[2]*t,s),mulVec2Scalar:(e,t,s)=>(s||(s=e),s[0]=e[0]*t,s[1]=e[1]*t,s),divVec3:(e,t,s)=>(s||(s=e),s[0]=e[0]/t[0],s[1]=e[1]/t[1],s[2]=e[2]/t[2],s),divVec4:(e,t,s)=>(s||(s=e),s[0]=e[0]/t[0],s[1]=e[1]/t[1],s[2]=e[2]/t[2],s[3]=e[3]/t[3],s),divScalarVec3:(e,t,s)=>(s||(s=t),s[0]=e/t[0],s[1]=e/t[1],s[2]=e/t[2],s),divVec3Scalar:(e,t,s)=>(s||(s=e),s[0]=e[0]/t,s[1]=e[1]/t,s[2]=e[2]/t,s),divVec4Scalar:(e,t,s)=>(s||(s=e),s[0]=e[0]/t,s[1]=e[1]/t,s[2]=e[2]/t,s[3]=e[3]/t,s),divScalarVec4:(e,t,s)=>(s||(s=t),s[0]=e/t[0],s[1]=e/t[1],s[2]=e/t[2],s[3]=e/t[3],s),dotVec4:(e,t)=>e[0]*t[0]+e[1]*t[1]+e[2]*t[2]+e[3]*t[3],cross3Vec4(e,t){const s=e[0],n=e[1],i=e[2],a=t[0],r=t[1],l=t[2];return[n*l-i*r,i*a-s*l,s*r-n*a,0]},cross3Vec3(e,t,s){s||(s=e);const n=e[0],i=e[1],a=e[2],r=t[0],l=t[1],o=t[2];return s[0]=i*o-a*l,s[1]=a*r-n*o,s[2]=n*l-i*r,s},sqLenVec4:e=>h.dotVec4(e,e),lenVec4:e=>Math.sqrt(h.sqLenVec4(e)),dotVec3:(e,t)=>e[0]*t[0]+e[1]*t[1]+e[2]*t[2],dotVec2:(e,t)=>e[0]*t[0]+e[1]*t[1],sqLenVec3:e=>h.dotVec3(e,e),sqLenVec2:e=>h.dotVec2(e,e),lenVec3:e=>Math.sqrt(h.sqLenVec3(e)),distVec3:(()=>{const e=new l(3);return(t,s)=>h.lenVec3(h.subVec3(t,s,e))})(),lenVec2:e=>Math.sqrt(h.sqLenVec2(e)),distVec2:(()=>{const e=new l(2);return(t,s)=>h.lenVec2(h.subVec2(t,s,e))})(),rcpVec3:(e,t)=>h.divScalarVec3(1,e,t),normalizeVec4(e,t){const s=1/h.lenVec4(e);return h.mulVec4Scalar(e,s,t)},normalizeVec3(e,t){const s=1/h.lenVec3(e);return h.mulVec3Scalar(e,s,t)},normalizeVec2(e,t){const s=1/h.lenVec2(e);return h.mulVec2Scalar(e,s,t)},angleVec3(e,t){let s=h.dotVec3(e,t)/Math.sqrt(h.sqLenVec3(e)*h.sqLenVec3(t));return s=s<-1?-1:s>1?1:s,Math.acos(s)},vec3FromMat4Scale:(()=>{const e=new l(3);return(t,s)=>(e[0]=t[0],e[1]=t[1],e[2]=t[2],s[0]=h.lenVec3(e),e[0]=t[4],e[1]=t[5],e[2]=t[6],s[1]=h.lenVec3(e),e[0]=t[8],e[1]=t[9],e[2]=t[10],s[2]=h.lenVec3(e),s)})(),vecToArray:(()=>{function e(e){return Math.round(1e5*e)/1e5}return t=>{for(let s=0,n=(t=Array.prototype.slice.call(t)).length;s<n;s++)t[s]=e(t[s]);return t}})(),xyzArrayToObject:e=>({x:e[0],y:e[1],z:e[2]}),xyzObjectToArray:(e,t)=>((t=t||h.vec3())[0]=e.x,t[1]=e.y,t[2]=e.z,t),dupMat4:e=>e.slice(0,16),mat4To3:e=>[e[0],e[1],e[2],e[4],e[5],e[6],e[8],e[9],e[10]],m4s:e=>[e,e,e,e,e,e,e,e,e,e,e,e,e,e,e,e],setMat4ToZeroes:()=>h.m4s(0),setMat4ToOnes:()=>h.m4s(1),diagonalMat4v:e=>new l([e[0],0,0,0,0,e[1],0,0,0,0,e[2],0,0,0,0,e[3]]),diagonalMat4c:(e,t,s,n)=>h.diagonalMat4v([e,t,s,n]),diagonalMat4s:e=>h.diagonalMat4c(e,e,e,e),identityMat4:(e=new l(16))=>(e[0]=1,e[1]=0,e[2]=0,e[3]=0,e[4]=0,e[5]=1,e[6]=0,e[7]=0,e[8]=0,e[9]=0,e[10]=1,e[11]=0,e[12]=0,e[13]=0,e[14]=0,e[15]=1,e),identityMat3:(e=new l(9))=>(e[0]=1,e[1]=0,e[2]=0,e[3]=0,e[4]=1,e[5]=0,e[6]=0,e[7]=0,e[8]=1,e),isIdentityMat4:e=>1===e[0]&&0===e[1]&&0===e[2]&&0===e[3]&&0===e[4]&&1===e[5]&&0===e[6]&&0===e[7]&&0===e[8]&&0===e[9]&&1===e[10]&&0===e[11]&&0===e[12]&&0===e[13]&&0===e[14]&&1===e[15],negateMat4:(e,t)=>(t||(t=e),t[0]=-e[0],t[1]=-e[1],t[2]=-e[2],t[3]=-e[3],t[4]=-e[4],t[5]=-e[5],t[6]=-e[6],t[7]=-e[7],t[8]=-e[8],t[9]=-e[9],t[10]=-e[10],t[11]=-e[11],t[12]=-e[12],t[13]=-e[13],t[14]=-e[14],t[15]=-e[15],t),addMat4:(e,t,s)=>(s||(s=e),s[0]=e[0]+t[0],s[1]=e[1]+t[1],s[2]=e[2]+t[2],s[3]=e[3]+t[3],s[4]=e[4]+t[4],s[5]=e[5]+t[5],s[6]=e[6]+t[6],s[7]=e[7]+t[7],s[8]=e[8]+t[8],s[9]=e[9]+t[9],s[10]=e[10]+t[10],s[11]=e[11]+t[11],s[12]=e[12]+t[12],s[13]=e[13]+t[13],s[14]=e[14]+t[14],s[15]=e[15]+t[15],s),addMat4Scalar:(e,t,s)=>(s||(s=e),s[0]=e[0]+t,s[1]=e[1]+t,s[2]=e[2]+t,s[3]=e[3]+t,s[4]=e[4]+t,s[5]=e[5]+t,s[6]=e[6]+t,s[7]=e[7]+t,s[8]=e[8]+t,s[9]=e[9]+t,s[10]=e[10]+t,s[11]=e[11]+t,s[12]=e[12]+t,s[13]=e[13]+t,s[14]=e[14]+t,s[15]=e[15]+t,s),addScalarMat4:(e,t,s)=>h.addMat4Scalar(t,e,s),subMat4:(e,t,s)=>(s||(s=e),s[0]=e[0]-t[0],s[1]=e[1]-t[1],s[2]=e[2]-t[2],s[3]=e[3]-t[3],s[4]=e[4]-t[4],s[5]=e[5]-t[5],s[6]=e[6]-t[6],s[7]=e[7]-t[7],s[8]=e[8]-t[8],s[9]=e[9]-t[9],s[10]=e[10]-t[10],s[11]=e[11]-t[11],s[12]=e[12]-t[12],s[13]=e[13]-t[13],s[14]=e[14]-t[14],s[15]=e[15]-t[15],s),subMat4Scalar:(e,t,s)=>(s||(s=e),s[0]=e[0]-t,s[1]=e[1]-t,s[2]=e[2]-t,s[3]=e[3]-t,s[4]=e[4]-t,s[5]=e[5]-t,s[6]=e[6]-t,s[7]=e[7]-t,s[8]=e[8]-t,s[9]=e[9]-t,s[10]=e[10]-t,s[11]=e[11]-t,s[12]=e[12]-t,s[13]=e[13]-t,s[14]=e[14]-t,s[15]=e[15]-t,s),subScalarMat4:(e,t,s)=>(s||(s=t),s[0]=e-t[0],s[1]=e-t[1],s[2]=e-t[2],s[3]=e-t[3],s[4]=e-t[4],s[5]=e-t[5],s[6]=e-t[6],s[7]=e-t[7],s[8]=e-t[8],s[9]=e-t[9],s[10]=e-t[10],s[11]=e-t[11],s[12]=e-t[12],s[13]=e-t[13],s[14]=e-t[14],s[15]=e-t[15],s),mulMat4(e,t,s){s||(s=e);const n=e[0],i=e[1],a=e[2],r=e[3],l=e[4],o=e[5],c=e[6],u=e[7],h=e[8],p=e[9],A=e[10],d=e[11],f=e[12],I=e[13],y=e[14],m=e[15],v=t[0],w=t[1],g=t[2],T=t[3],E=t[4],b=t[5],D=t[6],P=t[7],R=t[8],C=t[9],_=t[10],B=t[11],O=t[12],S=t[13],N=t[14],x=t[15];return s[0]=v*n+w*l+g*h+T*f,s[1]=v*i+w*o+g*p+T*I,s[2]=v*a+w*c+g*A+T*y,s[3]=v*r+w*u+g*d+T*m,s[4]=E*n+b*l+D*h+P*f,s[5]=E*i+b*o+D*p+P*I,s[6]=E*a+b*c+D*A+P*y,s[7]=E*r+b*u+D*d+P*m,s[8]=R*n+C*l+_*h+B*f,s[9]=R*i+C*o+_*p+B*I,s[10]=R*a+C*c+_*A+B*y,s[11]=R*r+C*u+_*d+B*m,s[12]=O*n+S*l+N*h+x*f,s[13]=O*i+S*o+N*p+x*I,s[14]=O*a+S*c+N*A+x*y,s[15]=O*r+S*u+N*d+x*m,s},mulMat3(e,t,s){s||(s=new l(9));const n=e[0],i=e[3],a=e[6],r=e[1],o=e[4],c=e[7],u=e[2],h=e[5],p=e[8],A=t[0],d=t[3],f=t[6],I=t[1],y=t[4],m=t[7],v=t[2],w=t[5],g=t[8];return s[0]=n*A+i*I+a*v,s[3]=n*d+i*y+a*w,s[6]=n*f+i*m+a*g,s[1]=r*A+o*I+c*v,s[4]=r*d+o*y+c*w,s[7]=r*f+o*m+c*g,s[2]=u*A+h*I+p*v,s[5]=u*d+h*y+p*w,s[8]=u*f+h*m+p*g,s},mulMat4Scalar:(e,t,s)=>(s||(s=e),s[0]=e[0]*t,s[1]=e[1]*t,s[2]=e[2]*t,s[3]=e[3]*t,s[4]=e[4]*t,s[5]=e[5]*t,s[6]=e[6]*t,s[7]=e[7]*t,s[8]=e[8]*t,s[9]=e[9]*t,s[10]=e[10]*t,s[11]=e[11]*t,s[12]=e[12]*t,s[13]=e[13]*t,s[14]=e[14]*t,s[15]=e[15]*t,s),mulMat4v4(e,t,s=h.vec4()){const n=t[0],i=t[1],a=t[2],r=t[3];return s[0]=e[0]*n+e[4]*i+e[8]*a+e[12]*r,s[1]=e[1]*n+e[5]*i+e[9]*a+e[13]*r,s[2]=e[2]*n+e[6]*i+e[10]*a+e[14]*r,s[3]=e[3]*n+e[7]*i+e[11]*a+e[15]*r,s},transposeMat4(e,t){const s=e[4],n=e[14],i=e[8],a=e[13],r=e[12],l=e[9];if(!t||e===t){const t=e[1],o=e[2],c=e[3],u=e[6],h=e[7],p=e[11];return e[1]=s,e[2]=i,e[3]=r,e[4]=t,e[6]=l,e[7]=a,e[8]=o,e[9]=u,e[11]=n,e[12]=c,e[13]=h,e[14]=p,e}return t[0]=e[0],t[1]=s,t[2]=i,t[3]=r,t[4]=e[1],t[5]=e[5],t[6]=l,t[7]=a,t[8]=e[2],t[9]=e[6],t[10]=e[10],t[11]=n,t[12]=e[3],t[13]=e[7],t[14]=e[11],t[15]=e[15],t},transposeMat3(e,t){if(t===e){const s=e[1],n=e[2],i=e[5];t[1]=e[3],t[2]=e[6],t[3]=s,t[5]=e[7],t[6]=n,t[7]=i}else t[0]=e[0],t[1]=e[3],t[2]=e[6],t[3]=e[1],t[4]=e[4],t[5]=e[7],t[6]=e[2],t[7]=e[5],t[8]=e[8];return t},determinantMat4(e){const t=e[0],s=e[1],n=e[2],i=e[3],a=e[4],r=e[5],l=e[6],o=e[7],c=e[8],u=e[9],h=e[10],p=e[11],A=e[12],d=e[13],f=e[14],I=e[15];return A*u*l*i-c*d*l*i-A*r*h*i+a*d*h*i+c*r*f*i-a*u*f*i-A*u*n*o+c*d*n*o+A*s*h*o-t*d*h*o-c*s*f*o+t*u*f*o+A*r*n*p-a*d*n*p-A*s*l*p+t*d*l*p+a*s*f*p-t*r*f*p-c*r*n*I+a*u*n*I+c*s*l*I-t*u*l*I-a*s*h*I+t*r*h*I},inverseMat4(e,t){t||(t=e);const s=e[0],n=e[1],i=e[2],a=e[3],r=e[4],l=e[5],o=e[6],c=e[7],u=e[8],h=e[9],p=e[10],A=e[11],d=e[12],f=e[13],I=e[14],y=e[15],m=s*l-n*r,v=s*o-i*r,w=s*c-a*r,g=n*o-i*l,T=n*c-a*l,E=i*c-a*o,b=u*f-h*d,D=u*I-p*d,P=u*y-A*d,R=h*I-p*f,C=h*y-A*f,_=p*y-A*I,B=1/(m*_-v*C+w*R+g*P-T*D+E*b);return t[0]=(l*_-o*C+c*R)*B,t[1]=(-n*_+i*C-a*R)*B,t[2]=(f*E-I*T+y*g)*B,t[3]=(-h*E+p*T-A*g)*B,t[4]=(-r*_+o*P-c*D)*B,t[5]=(s*_-i*P+a*D)*B,t[6]=(-d*E+I*w-y*v)*B,t[7]=(u*E-p*w+A*v)*B,t[8]=(r*C-l*P+c*b)*B,t[9]=(-s*C+n*P-a*b)*B,t[10]=(d*T-f*w+y*m)*B,t[11]=(-u*T+h*w-A*m)*B,t[12]=(-r*R+l*D-o*b)*B,t[13]=(s*R-n*D+i*b)*B,t[14]=(-d*g+f*v-I*m)*B,t[15]=(u*g-h*v+p*m)*B,t},traceMat4:e=>e[0]+e[5]+e[10]+e[15],translationMat4v(e,t){const s=t||h.identityMat4();return s[12]=e[0],s[13]=e[1],s[14]=e[2],s},translationMat3v(e,t){const s=t||h.identityMat3();return s[6]=e[0],s[7]=e[1],s},translationMat4c:(()=>{const e=new l(3);return(t,s,n,i)=>(e[0]=t,e[1]=s,e[2]=n,h.translationMat4v(e,i))})(),translationMat4s:(e,t)=>h.translationMat4c(e,e,e,t),translateMat4v:(e,t)=>h.translateMat4c(e[0],e[1],e[2],t),translateMat4c(e,t,s,n){const i=n[3];n[0]+=i*e,n[1]+=i*t,n[2]+=i*s;const a=n[7];n[4]+=a*e,n[5]+=a*t,n[6]+=a*s;const r=n[11];n[8]+=r*e,n[9]+=r*t,n[10]+=r*s;const l=n[15];return n[12]+=l*e,n[13]+=l*t,n[14]+=l*s,n},setMat4Translation:(e,t,s)=>(s[0]=e[0],s[1]=e[1],s[2]=e[2],s[3]=e[3],s[4]=e[4],s[5]=e[5],s[6]=e[6],s[7]=e[7],s[8]=e[8],s[9]=e[9],s[10]=e[10],s[11]=e[11],s[12]=t[0],s[13]=t[1],s[14]=t[2],s[15]=e[15],s),rotationMat4v(e,t,s){const n=h.normalizeVec4([t[0],t[1],t[2],0],[]),i=Math.sin(e),a=Math.cos(e),r=1-a,l=n[0],o=n[1],c=n[2];let u,p,A,d,f,I;return u=l*o,p=o*c,A=c*l,d=l*i,f=o*i,I=c*i,(s=s||h.mat4())[0]=r*l*l+a,s[1]=r*u+I,s[2]=r*A-f,s[3]=0,s[4]=r*u-I,s[5]=r*o*o+a,s[6]=r*p+d,s[7]=0,s[8]=r*A+f,s[9]=r*p-d,s[10]=r*c*c+a,s[11]=0,s[12]=0,s[13]=0,s[14]=0,s[15]=1,s},rotationMat4c:(e,t,s,n,i)=>h.rotationMat4v(e,[t,s,n],i),scalingMat4v:(e,t=h.identityMat4())=>(t[0]=e[0],t[5]=e[1],t[10]=e[2],t),scalingMat3v:(e,t=h.identityMat3())=>(t[0]=e[0],t[4]=e[1],t),scalingMat4c:(()=>{const e=new l(3);return(t,s,n,i)=>(e[0]=t,e[1]=s,e[2]=n,h.scalingMat4v(e,i))})(),scaleMat4c:(e,t,s,n)=>(n[0]*=e,n[4]*=t,n[8]*=s,n[1]*=e,n[5]*=t,n[9]*=s,n[2]*=e,n[6]*=t,n[10]*=s,n[3]*=e,n[7]*=t,n[11]*=s,n),scaleMat4v(e,t){const s=e[0],n=e[1],i=e[2];return t[0]*=s,t[4]*=n,t[8]*=i,t[1]*=s,t[5]*=n,t[9]*=i,t[2]*=s,t[6]*=n,t[10]*=i,t[3]*=s,t[7]*=n,t[11]*=i,t},scalingMat4s:e=>h.scalingMat4c(e,e,e),rotationTranslationMat4(e,t,s=h.mat4()){const n=e[0],i=e[1],a=e[2],r=e[3],l=n+n,o=i+i,c=a+a,u=n*l,p=n*o,A=n*c,d=i*o,f=i*c,I=a*c,y=r*l,m=r*o,v=r*c;return s[0]=1-(d+I),s[1]=p+v,s[2]=A-m,s[3]=0,s[4]=p-v,s[5]=1-(u+I),s[6]=f+y,s[7]=0,s[8]=A+m,s[9]=f-y,s[10]=1-(u+d),s[11]=0,s[12]=t[0],s[13]=t[1],s[14]=t[2],s[15]=1,s},mat4ToEuler(e,t,s=h.vec4()){const n=h.clamp,i=e[0],a=e[4],r=e[8],l=e[1],o=e[5],c=e[9],u=e[2],p=e[6],A=e[10];return"XYZ"===t?(s[1]=Math.asin(n(r,-1,1)),Math.abs(r)<.99999?(s[0]=Math.atan2(-c,A),s[2]=Math.atan2(-a,i)):(s[0]=Math.atan2(p,o),s[2]=0)):"YXZ"===t?(s[0]=Math.asin(-n(c,-1,1)),Math.abs(c)<.99999?(s[1]=Math.atan2(r,A),s[2]=Math.atan2(l,o)):(s[1]=Math.atan2(-u,i),s[2]=0)):"ZXY"===t?(s[0]=Math.asin(n(p,-1,1)),Math.abs(p)<.99999?(s[1]=Math.atan2(-u,A),s[2]=Math.atan2(-a,o)):(s[1]=0,s[2]=Math.atan2(l,i))):"ZYX"===t?(s[1]=Math.asin(-n(u,-1,1)),Math.abs(u)<.99999?(s[0]=Math.atan2(p,A),s[2]=Math.atan2(l,i)):(s[0]=0,s[2]=Math.atan2(-a,o))):"YZX"===t?(s[2]=Math.asin(n(l,-1,1)),Math.abs(l)<.99999?(s[0]=Math.atan2(-c,o),s[1]=Math.atan2(-u,i)):(s[0]=0,s[1]=Math.atan2(r,A))):"XZY"===t&&(s[2]=Math.asin(-n(a,-1,1)),Math.abs(a)<.99999?(s[0]=Math.atan2(p,o),s[1]=Math.atan2(r,i)):(s[0]=Math.atan2(-c,A),s[1]=0)),s},composeMat4:(e,t,s,n=h.mat4())=>(h.quaternionToRotationMat4(t,n),h.scaleMat4v(s,n),h.translateMat4v(e,n),n),decomposeMat4:(()=>{const e=new l(3),t=new l(16);return function(s,n,i,a){e[0]=s[0],e[1]=s[1],e[2]=s[2];let r=h.lenVec3(e);e[0]=s[4],e[1]=s[5],e[2]=s[6];const l=h.lenVec3(e);e[8]=s[8],e[9]=s[9],e[10]=s[10];const o=h.lenVec3(e);h.determinantMat4(s)<0&&(r=-r),n[0]=s[12],n[1]=s[13],n[2]=s[14],t.set(s);const c=1/r,u=1/l,p=1/o;return t[0]*=c,t[1]*=c,t[2]*=c,t[4]*=u,t[5]*=u,t[6]*=u,t[8]*=p,t[9]*=p,t[10]*=p,h.mat4ToQuaternion(t,i),a[0]=r,a[1]=l,a[2]=o,this}})(),getColMat4(e,t){const s=4*t;return[e[s],e[s+1],e[s+2],e[s+3]]},setRowMat4(e,t,s){e[t]=s[0],e[t+4]=s[1],e[t+8]=s[2],e[t+12]=s[3]},lookAtMat4v(e,t,s,n){n||(n=h.mat4());const i=e[0],a=e[1],r=e[2],l=s[0],o=s[1],c=s[2],u=t[0],p=t[1],A=t[2];if(i===u&&a===p&&r===A)return h.identityMat4();let d,f,I,y,m,v,w,g,T,E;return d=i-u,f=a-p,I=r-A,E=1/Math.sqrt(d*d+f*f+I*I),d*=E,f*=E,I*=E,y=o*I-c*f,m=c*d-l*I,v=l*f-o*d,E=Math.sqrt(y*y+m*m+v*v),E?(E=1/E,y*=E,m*=E,v*=E):(y=0,m=0,v=0),w=f*v-I*m,g=I*y-d*v,T=d*m-f*y,E=Math.sqrt(w*w+g*g+T*T),E?(E=1/E,w*=E,g*=E,T*=E):(w=0,g=0,T=0),n[0]=y,n[1]=w,n[2]=d,n[3]=0,n[4]=m,n[5]=g,n[6]=f,n[7]=0,n[8]=v,n[9]=T,n[10]=I,n[11]=0,n[12]=-(y*i+m*a+v*r),n[13]=-(w*i+g*a+T*r),n[14]=-(d*i+f*a+I*r),n[15]=1,n},lookAtMat4c:(e,t,s,n,i,a,r,l,o)=>h.lookAtMat4v([e,t,s],[n,i,a],[r,l,o],[]),orthoMat4c(e,t,s,n,i,a,r){r||(r=h.mat4());const l=t-e,o=n-s,c=a-i;return r[0]=2/l,r[1]=0,r[2]=0,r[3]=0,r[4]=0,r[5]=2/o,r[6]=0,r[7]=0,r[8]=0,r[9]=0,r[10]=-2/c,r[11]=0,r[12]=-(e+t)/l,r[13]=-(n+s)/o,r[14]=-(a+i)/c,r[15]=1,r},frustumMat4v(e,t,s){s||(s=h.mat4());const n=[e[0],e[1],e[2],0],i=[t[0],t[1],t[2],0];h.addVec4(i,n,o),h.subVec4(i,n,c);const a=2*n[2],r=c[0],l=c[1],u=c[2];return s[0]=a/r,s[1]=0,s[2]=0,s[3]=0,s[4]=0,s[5]=a/l,s[6]=0,s[7]=0,s[8]=o[0]/r,s[9]=o[1]/l,s[10]=-o[2]/u,s[11]=-1,s[12]=0,s[13]=0,s[14]=-a*i[2]/u,s[15]=0,s},frustumMat4(e,t,s,n,i,a,r){r||(r=h.mat4());const l=t-e,o=n-s,c=a-i;return r[0]=2*i/l,r[1]=0,r[2]=0,r[3]=0,r[4]=0,r[5]=2*i/o,r[6]=0,r[7]=0,r[8]=(t+e)/l,r[9]=(n+s)/o,r[10]=-(a+i)/c,r[11]=-1,r[12]=0,r[13]=0,r[14]=-a*i*2/c,r[15]=0,r},perspectiveMat4(e,t,s,n,i){const a=[],r=[];return a[2]=s,r[2]=n,r[1]=a[2]*Math.tan(e/2),a[1]=-r[1],r[0]=r[1]*t,a[0]=-r[0],h.frustumMat4v(a,r,i)},compareMat4:(e,t)=>e[0]===t[0]&&e[1]===t[1]&&e[2]===t[2]&&e[3]===t[3]&&e[4]===t[4]&&e[5]===t[5]&&e[6]===t[6]&&e[7]===t[7]&&e[8]===t[8]&&e[9]===t[9]&&e[10]===t[10]&&e[11]===t[11]&&e[12]===t[12]&&e[13]===t[13]&&e[14]===t[14]&&e[15]===t[15],transformPoint3(e,t,s=h.vec3()){const n=t[0],i=t[1],a=t[2];return s[0]=e[0]*n+e[4]*i+e[8]*a+e[12],s[1]=e[1]*n+e[5]*i+e[9]*a+e[13],s[2]=e[2]*n+e[6]*i+e[10]*a+e[14],s},transformPoint4:(e,t,s=h.vec4())=>(s[0]=e[0]*t[0]+e[4]*t[1]+e[8]*t[2]+e[12]*t[3],s[1]=e[1]*t[0]+e[5]*t[1]+e[9]*t[2]+e[13]*t[3],s[2]=e[2]*t[0]+e[6]*t[1]+e[10]*t[2]+e[14]*t[3],s[3]=e[3]*t[0]+e[7]*t[1]+e[11]*t[2]+e[15]*t[3],s),transformPoints3(e,t,s){const n=s||[],i=t.length;let a,r,l,o;const c=e[0],u=e[1],h=e[2],p=e[3],A=e[4],d=e[5],f=e[6],I=e[7],y=e[8],m=e[9],v=e[10],w=e[11],g=e[12],T=e[13],E=e[14],b=e[15];let D;for(let e=0;e<i;++e)o=t[e],a=o[0],r=o[1],l=o[2],D=n[e]||(n[e]=[0,0,0]),D[0]=c*a+A*r+y*l+g,D[1]=u*a+d*r+m*l+T,D[2]=h*a+f*r+v*l+E,D[3]=p*a+I*r+w*l+b;return n.length=i,n},transformPositions3(e,t,s=t){let n;const i=t.length;let a,r,l;const o=e[0],c=e[1],u=e[2];e[3];const h=e[4],p=e[5],A=e[6];e[7];const d=e[8],f=e[9],I=e[10];e[11];const y=e[12],m=e[13],v=e[14];for(e[15],n=0;n<i;n+=3)a=t[n+0],r=t[n+1],l=t[n+2],s[n+0]=o*a+h*r+d*l+y,s[n+1]=c*a+p*r+f*l+m,s[n+2]=u*a+A*r+I*l+v;return s},transformPositions4(e,t,s=t){let n;const i=t.length;let a,r,l;const o=e[0],c=e[1],u=e[2],h=e[3],p=e[4],A=e[5],d=e[6],f=e[7],I=e[8],y=e[9],m=e[10],v=e[11],w=e[12],g=e[13],T=e[14],E=e[15];for(n=0;n<i;n+=4)a=t[n+0],r=t[n+1],l=t[n+2],s[n+0]=o*a+p*r+I*l+w,s[n+1]=c*a+A*r+y*l+g,s[n+2]=u*a+d*r+m*l+T,s[n+3]=h*a+f*r+v*l+E;return s},transformVec3(e,t,s){const n=t[0],i=t[1],a=t[2];return(s=s||this.vec3())[0]=e[0]*n+e[4]*i+e[8]*a,s[1]=e[1]*n+e[5]*i+e[9]*a,s[2]=e[2]*n+e[6]*i+e[10]*a,s},transformVec4(e,t,s){const n=t[0],i=t[1],a=t[2],r=t[3];return(s=s||h.vec4())[0]=e[0]*n+e[4]*i+e[8]*a+e[12]*r,s[1]=e[1]*n+e[5]*i+e[9]*a+e[13]*r,s[2]=e[2]*n+e[6]*i+e[10]*a+e[14]*r,s[3]=e[3]*n+e[7]*i+e[11]*a+e[15]*r,s},rotateVec2(e,t,s,n=e){const i=Math.cos(s),a=Math.sin(s),r=e[0]-t[0],l=e[1]-t[1];return n[0]=r*i-l*a+t[0],n[1]=r*a+l*i+t[1],e},rotateVec3X(e,t,s,n){const i=[],a=[];return i[0]=e[0]-t[0],i[1]=e[1]-t[1],i[2]=e[2]-t[2],a[0]=i[0],a[1]=i[1]*Math.cos(s)-i[2]*Math.sin(s),a[2]=i[1]*Math.sin(s)+i[2]*Math.cos(s),n[0]=a[0]+t[0],n[1]=a[1]+t[1],n[2]=a[2]+t[2],n},rotateVec3Y(e,t,s,n){const i=[],a=[];return i[0]=e[0]-t[0],i[1]=e[1]-t[1],i[2]=e[2]-t[2],a[0]=i[2]*Math.sin(s)+i[0]*Math.cos(s),a[1]=i[1],a[2]=i[2]*Math.cos(s)-i[0]*Math.sin(s),n[0]=a[0]+t[0],n[1]=a[1]+t[1],n[2]=a[2]+t[2],n},rotateVec3Z(e,t,s,n){const i=[],a=[];return i[0]=e[0]-t[0],i[1]=e[1]-t[1],i[2]=e[2]-t[2],a[0]=i[0]*Math.cos(s)-i[1]*Math.sin(s),a[1]=i[0]*Math.sin(s)+i[1]*Math.cos(s),a[2]=i[2],n[0]=a[0]+t[0],n[1]=a[1]+t[1],n[2]=a[2]+t[2],n},projectVec4(e,t){const s=1/e[3];return(t=t||h.vec2())[0]=e[0]*s,t[1]=e[1]*s,t},unprojectVec3:(()=>{const e=new l(16),t=new l(16),s=new l(16);return function(n,i,a,r){return this.transformVec3(this.mulMat4(this.inverseMat4(i,e),this.inverseMat4(a,t),s),n,r)}})(),lerpVec3(e,t,s,n,i,a){const r=a||h.vec3(),l=(e-t)/(s-t);return r[0]=n[0]+l*(i[0]-n[0]),r[1]=n[1]+l*(i[1]-n[1]),r[2]=n[2]+l*(i[2]-n[2]),r},lerpMat4(e,t,s,n,i,a){const r=a||h.mat4(),l=(e-t)/(s-t);return r[0]=n[0]+l*(i[0]-n[0]),r[1]=n[1]+l*(i[1]-n[1]),r[2]=n[2]+l*(i[2]-n[2]),r[3]=n[3]+l*(i[3]-n[3]),r[4]=n[4]+l*(i[4]-n[4]),r[5]=n[5]+l*(i[5]-n[5]),r[6]=n[6]+l*(i[6]-n[6]),r[7]=n[7]+l*(i[7]-n[7]),r[8]=n[8]+l*(i[8]-n[8]),r[9]=n[9]+l*(i[9]-n[9]),r[10]=n[10]+l*(i[10]-n[10]),r[11]=n[11]+l*(i[11]-n[11]),r[12]=n[12]+l*(i[12]-n[12]),r[13]=n[13]+l*(i[13]-n[13]),r[14]=n[14]+l*(i[14]-n[14]),r[15]=n[15]+l*(i[15]-n[15]),r},flatten(e){const t=[];let s,n,i,a,r;for(s=0,n=e.length;s<n;s++)for(r=e[s],i=0,a=r.length;i<a;i++)t.push(r[i]);return t},identityQuaternion:(e=h.vec4())=>(e[0]=0,e[1]=0,e[2]=0,e[3]=1,e),eulerToQuaternion(e,t,s=h.vec4()){const n=e[0]*h.DEGTORAD/2,i=e[1]*h.DEGTORAD/2,a=e[2]*h.DEGTORAD/2,r=Math.cos(n),l=Math.cos(i),o=Math.cos(a),c=Math.sin(n),u=Math.sin(i),p=Math.sin(a);return"XYZ"===t?(s[0]=c*l*o+r*u*p,s[1]=r*u*o-c*l*p,s[2]=r*l*p+c*u*o,s[3]=r*l*o-c*u*p):"YXZ"===t?(s[0]=c*l*o+r*u*p,s[1]=r*u*o-c*l*p,s[2]=r*l*p-c*u*o,s[3]=r*l*o+c*u*p):"ZXY"===t?(s[0]=c*l*o-r*u*p,s[1]=r*u*o+c*l*p,s[2]=r*l*p+c*u*o,s[3]=r*l*o-c*u*p):"ZYX"===t?(s[0]=c*l*o-r*u*p,s[1]=r*u*o+c*l*p,s[2]=r*l*p-c*u*o,s[3]=r*l*o+c*u*p):"YZX"===t?(s[0]=c*l*o+r*u*p,s[1]=r*u*o+c*l*p,s[2]=r*l*p-c*u*o,s[3]=r*l*o-c*u*p):"XZY"===t&&(s[0]=c*l*o-r*u*p,s[1]=r*u*o-c*l*p,s[2]=r*l*p+c*u*o,s[3]=r*l*o+c*u*p),s},mat4ToQuaternion(e,t=h.vec4()){const s=e[0],n=e[4],i=e[8],a=e[1],r=e[5],l=e[9],o=e[2],c=e[6],u=e[10];let p;const A=s+r+u;return A>0?(p=.5/Math.sqrt(A+1),t[3]=.25/p,t[0]=(c-l)*p,t[1]=(i-o)*p,t[2]=(a-n)*p):s>r&&s>u?(p=2*Math.sqrt(1+s-r-u),t[3]=(c-l)/p,t[0]=.25*p,t[1]=(n+a)/p,t[2]=(i+o)/p):r>u?(p=2*Math.sqrt(1+r-s-u),t[3]=(i-o)/p,t[0]=(n+a)/p,t[1]=.25*p,t[2]=(l+c)/p):(p=2*Math.sqrt(1+u-s-r),t[3]=(a-n)/p,t[0]=(i+o)/p,t[1]=(l+c)/p,t[2]=.25*p),t},vec3PairToQuaternion(e,t,s=h.vec4()){const n=Math.sqrt(h.dotVec3(e,e)*h.dotVec3(t,t));let i=n+h.dotVec3(e,t);return i<1e-8*n?(i=0,Math.abs(e[0])>Math.abs(e[2])?(s[0]=-e[1],s[1]=e[0],s[2]=0):(s[0]=0,s[1]=-e[2],s[2]=e[1])):h.cross3Vec3(e,t,s),s[3]=i,h.normalizeQuaternion(s)},angleAxisToQuaternion(e,t=h.vec4()){const s=e[3]/2,n=Math.sin(s);return t[0]=n*e[0],t[1]=n*e[1],t[2]=n*e[2],t[3]=Math.cos(s),t},quaternionToEuler:(()=>{const e=new l(16);return(t,s,n)=>(n=n||h.vec3(),h.quaternionToRotationMat4(t,e),h.mat4ToEuler(e,s,n),n)})(),mulQuaternions(e,t,s=h.vec4()){const n=e[0],i=e[1],a=e[2],r=e[3],l=t[0],o=t[1],c=t[2],u=t[3];return s[0]=r*l+n*u+i*c-a*o,s[1]=r*o+i*u+a*l-n*c,s[2]=r*c+a*u+n*o-i*l,s[3]=r*u-n*l-i*o-a*c,s},vec3ApplyQuaternion(e,t,s=h.vec3()){const n=t[0],i=t[1],a=t[2],r=e[0],l=e[1],o=e[2],c=e[3],u=c*n+l*a-o*i,p=c*i+o*n-r*a,A=c*a+r*i-l*n,d=-r*n-l*i-o*a;return s[0]=u*c+d*-r+p*-o-A*-l,s[1]=p*c+d*-l+A*-r-u*-o,s[2]=A*c+d*-o+u*-l-p*-r,s},quaternionToMat4(e,t){t=h.identityMat4(t);const s=e[0],n=e[1],i=e[2],a=e[3],r=2*s,l=2*n,o=2*i,c=r*a,u=l*a,p=o*a,A=r*s,d=l*s,f=o*s,I=l*n,y=o*n,m=o*i;return t[0]=1-(I+m),t[1]=d+p,t[2]=f-u,t[4]=d-p,t[5]=1-(A+m),t[6]=y+c,t[8]=f+u,t[9]=y-c,t[10]=1-(A+I),t},quaternionToRotationMat4(e,t){const s=e[0],n=e[1],i=e[2],a=e[3],r=s+s,l=n+n,o=i+i,c=s*r,u=s*l,h=s*o,p=n*l,A=n*o,d=i*o,f=a*r,I=a*l,y=a*o;return t[0]=1-(p+d),t[4]=u-y,t[8]=h+I,t[1]=u+y,t[5]=1-(c+d),t[9]=A-f,t[2]=h-I,t[6]=A+f,t[10]=1-(c+p),t[3]=0,t[7]=0,t[11]=0,t[12]=0,t[13]=0,t[14]=0,t[15]=1,t},normalizeQuaternion(e,t=e){const s=h.lenVec4([e[0],e[1],e[2],e[3]]);return t[0]=e[0]/s,t[1]=e[1]/s,t[2]=e[2]/s,t[3]=e[3]/s,t},conjugateQuaternion:(e,t=e)=>(t[0]=-e[0],t[1]=-e[1],t[2]=-e[2],t[3]=e[3],t),inverseQuaternion:(e,t)=>h.normalizeQuaternion(h.conjugateQuaternion(e,t)),quaternionToAngleAxis(e,t=h.vec4()){const s=(e=h.normalizeQuaternion(e,u))[3],n=2*Math.acos(s),i=Math.sqrt(1-s*s);return i<.001?(t[0]=e[0],t[1]=e[1],t[2]=e[2]):(t[0]=e[0]/i,t[1]=e[1]/i,t[2]=e[2]/i),t[3]=n,t},AABB3:e=>new l(e||6),AABB2:e=>new l(e||4),OBB3:e=>new l(e||32),OBB2:e=>new l(e||16),Sphere3:(e,t,s,n)=>new l([e,t,s,n]),transformOBB3(e,t,s=t){let n;const i=t.length;let a,r,l;const o=e[0],c=e[1],u=e[2],h=e[3],p=e[4],A=e[5],d=e[6],f=e[7],I=e[8],y=e[9],m=e[10],v=e[11],w=e[12],g=e[13],T=e[14],E=e[15];for(n=0;n<i;n+=4)a=t[n+0],r=t[n+1],l=t[n+2],s[n+0]=o*a+p*r+I*l+w,s[n+1]=c*a+A*r+y*l+g,s[n+2]=u*a+d*r+m*l+T,s[n+3]=h*a+f*r+v*l+E;return s},containsAABB3:function(e,t){return e[0]<=t[0]&&t[3]<=e[3]&&e[1]<=t[1]&&t[4]<=e[4]&&e[2]<=t[2]&&t[5]<=e[5]},getAABB3Diag:(()=>{const e=new l(3),t=new l(3),s=new l(3);return n=>(e[0]=n[0],e[1]=n[1],e[2]=n[2],t[0]=n[3],t[1]=n[4],t[2]=n[5],h.subVec3(t,e,s),Math.abs(h.lenVec3(s)))})(),getAABB3DiagPoint:(()=>{const e=new l(3),t=new l(3),s=new l(3);return(n,i)=>{e[0]=n[0],e[1]=n[1],e[2]=n[2],t[0]=n[3],t[1]=n[4],t[2]=n[5];const a=h.subVec3(t,e,s),r=i[0]-n[0],l=n[3]-i[0],o=i[1]-n[1],c=n[4]-i[1],u=i[2]-n[2],p=n[5]-i[2];return a[0]+=r>l?r:l,a[1]+=o>c?o:c,a[2]+=u>p?u:p,Math.abs(h.lenVec3(a))}})(),getAABB3Area:e=>(e[3]-e[0])*(e[4]-e[1])*(e[5]-e[2]),getAABB3Center(e,t){const s=t||h.vec3();return s[0]=(e[0]+e[3])/2,s[1]=(e[1]+e[4])/2,s[2]=(e[2]+e[5])/2,s},getAABB2Center(e,t){const s=t||h.vec2();return s[0]=(e[2]+e[0])/2,s[1]=(e[3]+e[1])/2,s},collapseAABB3:(e=h.AABB3())=>(e[0]=h.MAX_DOUBLE,e[1]=h.MAX_DOUBLE,e[2]=h.MAX_DOUBLE,e[3]=h.MIN_DOUBLE,e[4]=h.MIN_DOUBLE,e[5]=h.MIN_DOUBLE,e),AABB3ToOBB3:(e,t=h.OBB3())=>(t[0]=e[0],t[1]=e[1],t[2]=e[2],t[3]=1,t[4]=e[3],t[5]=e[1],t[6]=e[2],t[7]=1,t[8]=e[3],t[9]=e[4],t[10]=e[2],t[11]=1,t[12]=e[0],t[13]=e[4],t[14]=e[2],t[15]=1,t[16]=e[0],t[17]=e[1],t[18]=e[5],t[19]=1,t[20]=e[3],t[21]=e[1],t[22]=e[5],t[23]=1,t[24]=e[3],t[25]=e[4],t[26]=e[5],t[27]=1,t[28]=e[0],t[29]=e[4],t[30]=e[5],t[31]=1,t),positions3ToAABB3:(()=>{const e=new l(3);return(t,s,n)=>{s=s||h.AABB3();let i,a,r,l=h.MAX_DOUBLE,o=h.MAX_DOUBLE,c=h.MAX_DOUBLE,u=h.MIN_DOUBLE,p=h.MIN_DOUBLE,A=h.MIN_DOUBLE;for(let s=0,d=t.length;s<d;s+=3)n?(e[0]=t[s+0],e[1]=t[s+1],e[2]=t[s+2],h.decompressPosition(e,n,e),i=e[0],a=e[1],r=e[2]):(i=t[s+0],a=t[s+1],r=t[s+2]),i<l&&(l=i),a<o&&(o=a),r<c&&(c=r),i>u&&(u=i),a>p&&(p=a),r>A&&(A=r);return s[0]=l,s[1]=o,s[2]=c,s[3]=u,s[4]=p,s[5]=A,s}})(),OBB3ToAABB3(e,t=h.AABB3()){let s,n,i,a=h.MAX_DOUBLE,r=h.MAX_DOUBLE,l=h.MAX_DOUBLE,o=h.MIN_DOUBLE,c=h.MIN_DOUBLE,u=h.MIN_DOUBLE;for(let t=0,h=e.length;t<h;t+=4)s=e[t+0],n=e[t+1],i=e[t+2],s<a&&(a=s),n<r&&(r=n),i<l&&(l=i),s>o&&(o=s),n>c&&(c=n),i>u&&(u=i);return t[0]=a,t[1]=r,t[2]=l,t[3]=o,t[4]=c,t[5]=u,t},points3ToAABB3(e,t=h.AABB3()){let s,n,i,a=h.MAX_DOUBLE,r=h.MAX_DOUBLE,l=h.MAX_DOUBLE,o=h.MIN_DOUBLE,c=h.MIN_DOUBLE,u=h.MIN_DOUBLE;for(let t=0,h=e.length;t<h;t++)s=e[t][0],n=e[t][1],i=e[t][2],s<a&&(a=s),n<r&&(r=n),i<l&&(l=i),s>o&&(o=s),n>c&&(c=n),i>u&&(u=i);return t[0]=a,t[1]=r,t[2]=l,t[3]=o,t[4]=c,t[5]=u,t},points3ToSphere3:(()=>{const e=new l(3);return(t,s)=>{s=s||h.vec4();let n,i=0,a=0,r=0;const l=t.length;for(n=0;n<l;n++)i+=t[n][0],a+=t[n][1],r+=t[n][2];s[0]=i/l,s[1]=a/l,s[2]=r/l;let o,c=0;for(n=0;n<l;n++)o=Math.abs(h.lenVec3(h.subVec3(t[n],s,e))),o>c&&(c=o);return s[3]=c,s}})(),positions3ToSphere3:(()=>{const e=new l(3),t=new l(3);return(s,n)=>{n=n||h.vec4();let i,a=0,r=0,l=0;const o=s.length;let c=0;for(i=0;i<o;i+=3)a+=s[i],r+=s[i+1],l+=s[i+2];const u=o/3;let p;for(n[0]=a/u,n[1]=r/u,n[2]=l/u,i=0;i<o;i+=3)e[0]=s[i],e[1]=s[i+1],e[2]=s[i+2],p=Math.abs(h.lenVec3(h.subVec3(e,n,t))),p>c&&(c=p);return n[3]=c,n}})(),OBB3ToSphere3:(()=>{const e=new l(3),t=new l(3);return(s,n)=>{n=n||h.vec4();let i,a=0,r=0,l=0;const o=s.length,c=o/4;for(i=0;i<o;i+=4)a+=s[i+0],r+=s[i+1],l+=s[i+2];n[0]=a/c,n[1]=r/c,n[2]=l/c;let u,p=0;for(i=0;i<o;i+=4)e[0]=s[i+0],e[1]=s[i+1],e[2]=s[i+2],u=Math.abs(h.lenVec3(h.subVec3(e,n,t))),u>p&&(p=u);return n[3]=p,n}})(),getSphere3Center:(e,t=h.vec3())=>(t[0]=e[0],t[1]=e[1],t[2]=e[2],t),getPositionsCenter(e,t=h.vec3()){let s=0,n=0,i=0;for(var a=0,r=e.length;a<r;a+=3)s+=e[a+0],n+=e[a+1],i+=e[a+2];const l=e.length/3;return t[0]=s/l,t[1]=n/l,t[2]=i/l,t},expandAABB3:(e,t)=>(e[0]>t[0]&&(e[0]=t[0]),e[1]>t[1]&&(e[1]=t[1]),e[2]>t[2]&&(e[2]=t[2]),e[3]<t[3]&&(e[3]=t[3]),e[4]<t[4]&&(e[4]=t[4]),e[5]<t[5]&&(e[5]=t[5]),e),expandAABB3Point3:(e,t)=>(e[0]>t[0]&&(e[0]=t[0]),e[1]>t[1]&&(e[1]=t[1]),e[2]>t[2]&&(e[2]=t[2]),e[3]<t[0]&&(e[3]=t[0]),e[4]<t[1]&&(e[4]=t[1]),e[5]<t[2]&&(e[5]=t[2]),e),expandAABB3Points3(e,t){for(var s,n,i,a=0,r=t.length;a<r;a+=3)s=t[a],n=t[a+1],i=t[a+2],e[0]>s&&(e[0]=s),e[1]>n&&(e[1]=n),e[2]>i&&(e[2]=i),e[3]<s&&(e[3]=s),e[4]<n&&(e[4]=n),e[5]<i&&(e[5]=i);return e},collapseAABB2:(e=h.AABB2())=>(e[0]=h.MAX_DOUBLE,e[1]=h.MAX_DOUBLE,e[2]=h.MIN_DOUBLE,e[3]=h.MIN_DOUBLE,e),point3AABB3Intersect:(e,t)=>e[0]>t[0]||e[3]<t[0]||e[1]>t[1]||e[4]<t[1]||e[2]>t[2]||e[5]<t[2],planeAABB3Intersect(e,t,s){let n,i;e[0]>0?(n=e[0]*s[0],i=e[0]*s[3]):(n=e[0]*s[3],i=e[0]*s[0]),e[1]>0?(n+=e[1]*s[1],i+=e[1]*s[4]):(n+=e[1]*s[4],i+=e[1]*s[1]),e[2]>0?(n+=e[2]*s[2],i+=e[2]*s[5]):(n+=e[2]*s[5],i+=e[2]*s[2]);if(n<=-t&&i<=-t)return-1;return n>=-t&&i>=-t?1:0},OBB3ToAABB2(e,t=h.AABB2()){let s,n,i,a,r=h.MAX_DOUBLE,l=h.MAX_DOUBLE,o=h.MIN_DOUBLE,c=h.MIN_DOUBLE;for(let t=0,u=e.length;t<u;t+=4)s=e[t+0],n=e[t+1],i=e[t+3]||1,a=1/i,s*=a,n*=a,s<r&&(r=s),n<l&&(l=n),s>o&&(o=s),n>c&&(c=n);return t[0]=r,t[1]=l,t[2]=o,t[3]=c,t},expandAABB2:(e,t)=>(e[0]>t[0]&&(e[0]=t[0]),e[1]>t[1]&&(e[1]=t[1]),e[2]<t[2]&&(e[2]=t[2]),e[3]<t[3]&&(e[3]=t[3]),e),expandAABB2Point2:(e,t)=>(e[0]>t[0]&&(e[0]=t[0]),e[1]>t[1]&&(e[1]=t[1]),e[2]<t[0]&&(e[2]=t[0]),e[3]<t[1]&&(e[3]=t[1]),e),AABB2ToCanvas(e,t,s,n=e){const i=.5*(e[0]+1),a=.5*(e[1]+1),r=.5*(e[2]+1),l=.5*(e[3]+1);return n[0]=Math.floor(i*t),n[1]=s-Math.floor(l*s),n[2]=Math.floor(r*t),n[3]=s-Math.floor(a*s),n},tangentQuadraticBezier:(e,t,s,n)=>2*(1-e)*(s-t)+2*e*(n-s),tangentQuadraticBezier3:(e,t,s,n,i)=>-3*t*(1-e)*(1-e)+3*s*(1-e)*(1-e)-6*e*s*(1-e)+6*e*n*(1-e)-3*e*e*n+3*e*e*i,tangentSpline:e=>6*e*e-6*e+(3*e*e-4*e+1)+(-6*e*e+6*e)+(3*e*e-2*e),catmullRomInterpolate(e,t,s,n,i){const a=.5*(s-e),r=.5*(n-t),l=i*i;return(2*t-2*s+a+r)*(i*l)+(-3*t+3*s-2*a-r)*l+a*i+t},b2p0(e,t){const s=1-e;return s*s*t},b2p1:(e,t)=>2*(1-e)*e*t,b2p2:(e,t)=>e*e*t,b2(e,t,s,n){return this.b2p0(e,t)+this.b2p1(e,s)+this.b2p2(e,n)},b3p0(e,t){const s=1-e;return s*s*s*t},b3p1(e,t){const s=1-e;return 3*s*s*e*t},b3p2:(e,t)=>3*(1-e)*e*e*t,b3p3:(e,t)=>e*e*e*t,b3(e,t,s,n,i){return this.b3p0(e,t)+this.b3p1(e,s)+this.b3p2(e,n)+this.b3p3(e,i)},triangleNormal(e,t,s,n=h.vec3()){const i=t[0]-e[0],a=t[1]-e[1],r=t[2]-e[2],l=s[0]-e[0],o=s[1]-e[1],c=s[2]-e[2],u=a*c-r*o,p=r*l-i*c,A=i*o-a*l,d=Math.sqrt(u*u+p*p+A*A);return 0===d?(n[0]=0,n[1]=0,n[2]=0):(n[0]=u/d,n[1]=p/d,n[2]=A/d),n},rayTriangleIntersect:(()=>{const e=new l(3),t=new l(3),s=new l(3),n=new l(3),i=new l(3);return(a,r,l,o,c,u)=>{u=u||h.vec3();const p=h.subVec3(o,l,e),A=h.subVec3(c,l,t),d=h.cross3Vec3(r,A,s),f=h.dotVec3(p,d);if(f<1e-6)return null;const I=h.subVec3(a,l,n),y=h.dotVec3(I,d);if(y<0||y>f)return null;const m=h.cross3Vec3(I,p,i),v=h.dotVec3(r,m);if(v<0||y+v>f)return null;const w=h.dotVec3(A,m)/f;return u[0]=a[0]+w*r[0],u[1]=a[1]+w*r[1],u[2]=a[2]+w*r[2],u}})(),rayPlaneIntersect:(()=>{const e=new l(3),t=new l(3),s=new l(3),n=new l(3);return(i,a,r,l,o,c)=>{c=c||h.vec3(),a=h.normalizeVec3(a,e);const u=h.subVec3(l,r,t),p=h.subVec3(o,r,s),A=h.cross3Vec3(u,p,n);h.normalizeVec3(A,A);const d=-h.dotVec3(r,A),f=-(h.dotVec3(i,A)+d)/h.dotVec3(a,A);return c[0]=i[0]+f*a[0],c[1]=i[1]+f*a[1],c[2]=i[2]+f*a[2],c}})(),cartesianToBarycentric:(()=>{const e=new l(3),t=new l(3),s=new l(3);return(n,i,a,r,l)=>{const o=h.subVec3(r,i,e),c=h.subVec3(a,i,t),u=h.subVec3(n,i,s),p=h.dotVec3(o,o),A=h.dotVec3(o,c),d=h.dotVec3(o,u),f=h.dotVec3(c,c),I=h.dotVec3(c,u),y=p*f-A*A;if(0===y)return null;const m=1/y,v=(f*d-A*I)*m,w=(p*I-A*d)*m;return l[0]=1-v-w,l[1]=w,l[2]=v,l}})(),barycentricInsideTriangle(e){const t=e[1],s=e[2];return s>=0&&t>=0&&s+t<1},barycentricToCartesian(e,t,s,n,i=h.vec3()){const a=e[0],r=e[1],l=e[2];return i[0]=t[0]*a+s[0]*r+n[0]*l,i[1]=t[1]*a+s[1]*r+n[1]*l,i[2]=t[2]*a+s[2]*r+n[2]*l,i},mergeVertices(e,t,s,n){const i={},a=[],r=[],l=t?[]:null,o=s?[]:null,c=[];let u,h,p,A;const d=1e4;let f,I,y=0;for(f=0,I=e.length;f<I;f+=3)u=e[f],h=e[f+1],p=e[f+2],A=`${Math.round(u*d)}_${Math.round(h*d)}_${Math.round(p*d)}`,void 0===i[A]&&(i[A]=r.length/3,r.push(u),r.push(h),r.push(p),t&&(l.push(t[f]),l.push(t[f+1]),l.push(t[f+2])),s&&(o.push(s[y]),o.push(s[y+1]))),a[f/3]=i[A],y+=2;for(f=0,I=n.length;f<I;f++)c[f]=a[n[f]];const m={positions:r,indices:c};return l&&(m.normals=l),o&&(m.uv=o),m},buildNormals:(()=>{const e=new l(3),t=new l(3),s=new l(3),n=new l(3),i=new l(3),a=new l(3);return(r,l,o)=>{let c,u;const p=new Array(r.length/3);let A,d,f,I,y,m,v;for(c=0,u=l.length;c<u;c+=3){A=l[c],d=l[c+1],f=l[c+2],e[0]=r[3*A],e[1]=r[3*A+1],e[2]=r[3*A+2],t[0]=r[3*d],t[1]=r[3*d+1],t[2]=r[3*d+2],s[0]=r[3*f],s[1]=r[3*f+1],s[2]=r[3*f+2],h.subVec3(t,e,n),h.subVec3(s,e,i);const o=h.vec3();h.normalizeVec3(h.cross3Vec3(n,i,a),o),p[A]||(p[A]=[]),p[d]||(p[d]=[]),p[f]||(p[f]=[]),p[A].push(o),p[d].push(o),p[f].push(o)}for(o=o&&o.length===r.length?o:new Float32Array(r.length),c=0,u=p.length;c<u;c++){I=p[c].length,y=0,m=0,v=0;for(let e=0;e<I;e++)y+=p[c][e][0],m+=p[c][e][1],v+=p[c][e][2];o[3*c]=y/I,o[3*c+1]=m/I,o[3*c+2]=v/I}return o}})(),buildTangents:(()=>{const e=new l(3),t=new l(3),s=new l(3),n=new l(3),i=new l(3),a=new l(3),r=new l(3);return(l,o,c)=>{const u=new Float32Array(l.length);for(let p=0;p<o.length;p+=3){let A=o[p];const d=l.subarray(3*A,3*A+3),f=c.subarray(2*A,2*A+2);A=o[p+1];const I=l.subarray(3*A,3*A+3),y=c.subarray(2*A,2*A+2);A=o[p+2];const m=l.subarray(3*A,3*A+3),v=c.subarray(2*A,2*A+2),w=h.subVec3(I,d,e),g=h.subVec3(m,d,t),T=h.subVec2(y,f,s),E=h.subVec2(v,f,n),b=1/(T[0]*E[1]-T[1]*E[0]),D=h.mulVec3Scalar(h.subVec3(h.mulVec3Scalar(w,E[1],i),h.mulVec3Scalar(g,T[1],a),r),b,a);let P;for(let e=0;e<3;e++)P=3*o[p+e],u[P]+=D[0],u[P+1]+=D[1],u[P+2]+=D[2]}return u}})(),buildPickTriangles(e,t,s){const n=t.length,i=s?new Uint16Array(9*n):new Float32Array(9*n),a=new Uint8Array(12*n);let r,l,o,c,u,h,p=0,A=0,d=0;for(let s=0;s<n;s+=3)h=p>>24&255,u=p>>16&255,c=p>>8&255,o=255&p,l=t[s],r=3*l,i[A++]=e[r],i[A++]=e[r+1],i[A++]=e[r+2],a[d++]=o,a[d++]=c,a[d++]=u,a[d++]=h,l=t[s+1],r=3*l,i[A++]=e[r],i[A++]=e[r+1],i[A++]=e[r+2],a[d++]=o,a[d++]=c,a[d++]=u,a[d++]=h,l=t[s+2],r=3*l,i[A++]=e[r],i[A++]=e[r+1],i[A++]=e[r+2],a[d++]=o,a[d++]=c,a[d++]=u,a[d++]=h,p++;return{positions:i,colors:a}},faceToVertexNormals(e,t,s={}){const n=s.smoothNormalsAngleThreshold||20,i={},a=[],r={};let l,o,c,u,p;const A=1e4;let d,f,I,y,m,v;for(f=0,y=e.length;f<y;f+=3){d=f/3,o=e[f],c=e[f+1],u=e[f+2],p=`${Math.round(o*A)}_${Math.round(c*A)}_${Math.round(u*A)}`,void 0===i[p]?i[p]=[d]:i[p].push(d);const s=h.normalizeVec3([t[f],t[f+1],t[f+2]]);a[d]=s,l=h.vec4([s[0],s[1],s[2],1]),r[d]=l}for(p in i)if(i.hasOwnProperty(p)){const e=i[p],t=e.length;for(f=0;f<t;f++){const s=e[f];for(l=r[s],I=0;I<t;I++){if(f===I)continue;const t=e[I];m=a[s],v=a[t];Math.abs(h.angleVec3(m,v)/h.DEGTORAD)<n&&(l[0]+=v[0],l[1]+=v[1],l[2]+=v[2],l[3]+=1)}}}for(f=0,y=t.length;f<y;f+=3)l=r[f/3],t[f+0]=l[0]/l[3],t[f+1]=l[1]/l[3],t[f+2]=l[2]/l[3]},transformRay:(()=>{const e=new l(4),t=new l(4);return(s,n,i,a,r)=>{e[0]=n[0],e[1]=n[1],e[2]=n[2],e[3]=1,h.transformVec4(s,e,t),a[0]=t[0],a[1]=t[1],a[2]=t[2],e[0]=i[0],e[1]=i[1],e[2]=i[2],h.transformVec3(s,e,t),h.normalizeVec3(t),r[0]=t[0],r[1]=t[1],r[2]=t[2]}})(),canvasPosToWorldRay:(()=>{const e=new l(16),t=new l(16),s=new l(4),n=new l(4),i=new l(4),a=new l(4);return(r,l,o,c,u,p)=>{const A=h.mulMat4(o,l,e),d=h.inverseMat4(A,t),f=r.width,I=r.height,y=(c[0]-f/2)/(f/2),m=-(c[1]-I/2)/(I/2);s[0]=y,s[1]=m,s[2]=-1,s[3]=1,h.transformVec4(d,s,n),h.mulVec4Scalar(n,1/n[3]),i[0]=y,i[1]=m,i[2]=1,i[3]=1,h.transformVec4(d,i,a),h.mulVec4Scalar(a,1/a[3]),u[0]=a[0],u[1]=a[1],u[2]=a[2],h.subVec3(a,n,p),h.normalizeVec3(p)}})(),canvasPosToLocalRay:(()=>{const e=new l(3),t=new l(3);return(s,n,i,a,r,l,o)=>{h.canvasPosToWorldRay(s,n,i,r,e,t),h.worldRayToLocalRay(a,e,t,l,o)}})(),worldRayToLocalRay:(()=>{const e=new l(16),t=new l(4),s=new l(4);return(n,i,a,r,l)=>{const o=h.inverseMat4(n,e);t[0]=i[0],t[1]=i[1],t[2]=i[2],t[3]=1,h.transformVec4(o,t,s),r[0]=s[0],r[1]=s[1],r[2]=s[2],h.transformVec3(o,a,l)}})(),buildKDTree:(()=>{const e=new Float32Array;function t(s,n,i,a){const r=new l(6),o={triangles:null,left:null,right:null,leaf:!1,splitDim:0,aabb:r};let c,u;for(r[0]=r[1]=r[2]=Number.POSITIVE_INFINITY,r[3]=r[4]=r[5]=Number.NEGATIVE_INFINITY,c=0,u=s.length;c<u;++c){var h=3*s[c];for(let e=0;e<3;++e){const t=3*n[h+e];i[t]<r[0]&&(r[0]=i[t]),i[t]>r[3]&&(r[3]=i[t]),i[t+1]<r[1]&&(r[1]=i[t+1]),i[t+1]>r[4]&&(r[4]=i[t+1]),i[t+2]<r[2]&&(r[2]=i[t+2]),i[t+2]>r[5]&&(r[5]=i[t+2])}}if(s.length<20||a>10)return o.triangles=s,o.leaf=!0,o;e[0]=r[3]-r[0],e[1]=r[4]-r[1],e[2]=r[5]-r[2];let p=0;e[1]>e[p]&&(p=1),e[2]>e[p]&&(p=2),o.splitDim=p;const A=(r[p]+r[p+3])/2,d=new Array(s.length);let f=0;const I=new Array(s.length);let y=0;for(c=0,u=s.length;c<u;++c){const e=n[h=3*s[c]],t=3*n[h+1],a=3*n[h+2];i[3*e+p]<=A||i[t+p]<=A||i[a+p]<=A?d[f++]=s[c]:I[y++]=s[c]}return d.length=f,I.length=y,o.left=t(d,n,i,a+1),o.right=t(I,n,i,a+1),o}return(e,s)=>{const n=e.length/3,i=new Array(n);for(let e=0;e<n;++e)i[e]=e;return t(i,e,s,0)}})(),decompressPosition(e,t,s){(s=s||e)[0]=e[0]*t[0]+t[12],s[1]=e[1]*t[5]+t[13],s[2]=e[2]*t[10]+t[14]},decompressPositions(e,t,s=new Float32Array(e.length)){for(let n=0,i=e.length;n<i;n+=3)s[n+0]=e[n+0]*t[0]+t[12],s[n+1]=e[n+1]*t[5]+t[13],s[n+2]=e[n+2]*t[10]+t[14];return s},decompressUV(e,t,s){s[0]=e[0]*t[0]+t[6],s[1]=e[1]*t[4]+t[7]},decompressUVs(e,t,s=new Float32Array(e.length)){for(let n=0,i=e.length;n<i;n+=3)s[n+0]=e[n+0]*t[0]+t[6],s[n+1]=e[n+1]*t[4]+t[7];return s},octDecodeVec2(e,t){let s=e[0],n=e[1];s=(2*s+1)/255,n=(2*n+1)/255;const i=1-Math.abs(s)-Math.abs(n);i<0&&(s=(1-Math.abs(n))*(s>=0?1:-1),n=(1-Math.abs(s))*(n>=0?1:-1));const a=Math.sqrt(s*s+n*n+i*i);return t[0]=s/a,t[1]=n/a,t[2]=i/a,t},octDecodeVec2s(e,t){for(let s=0,n=0,i=e.length;s<i;s+=2){let i=e[s+0],a=e[s+1];i=(2*i+1)/255,a=(2*a+1)/255;const r=1-Math.abs(i)-Math.abs(a);r<0&&(i=(1-Math.abs(a))*(i>=0?1:-1),a=(1-Math.abs(i))*(a>=0?1:-1));const l=Math.sqrt(i*i+a*a+r*r);t[n+0]=i/l,t[n+1]=a/l,t[n+2]=r/l,n+=3}return t}};h.buildEdgeIndices=function(){const e=[],t=[],s=[],n=[],i=[];let a=0;const r=new Uint16Array(3),l=new Uint16Array(3),o=new Uint16Array(3),c=h.vec3(),u=h.vec3(),p=h.vec3(),A=h.vec3(),d=h.vec3(),f=h.vec3(),I=h.vec3();return function(y,m,v,w){!function(i,a){const r={};let l,o,c,u;const h=Math.pow(10,4);let p,A,d=0;for(p=0,A=i.length;p<A;p+=3)l=i[p],o=i[p+1],c=i[p+2],u=Math.round(l*h)+"_"+Math.round(o*h)+"_"+Math.round(c*h),void 0===r[u]&&(r[u]=d/3,e[d++]=l,e[d++]=o,e[d++]=c),t[p/3]=r[u];for(p=0,A=a.length;p<A;p++)n[p]=t[a[p]],s[n[p]]=a[p]}(y,m),function(t,s){a=0;for(let y=0,m=t;y<m;y+=3){const t=3*n[y],m=3*n[y+1],v=3*n[y+2];s?(r[0]=e[t],r[1]=e[t+1],r[2]=e[t+2],l[0]=e[m],l[1]=e[m+1],l[2]=e[m+2],o[0]=e[v],o[1]=e[v+1],o[2]=e[v+2],h.decompressPosition(r,s,c),h.decompressPosition(l,s,u),h.decompressPosition(o,s,p)):(c[0]=e[t],c[1]=e[t+1],c[2]=e[t+2],u[0]=e[m],u[1]=e[m+1],u[2]=e[m+2],p[0]=e[v],p[1]=e[v+1],p[2]=e[v+2]),h.subVec3(p,u,A),h.subVec3(c,u,d),h.cross3Vec3(A,d,f),h.normalizeVec3(f,I);const w=i[a]||(i[a]={normal:h.vec3()});w.normal[0]=I[0],w.normal[1]=I[1],w.normal[2]=I[2],a++}}(m.length,v);const g=[],T=Math.cos(h.DEGTORAD*w),E={};let b,D,P,R,C,_,B,O,S,N,x,L=!1;for(let e=0,t=m.length;e<t;e+=3){const t=e/3;for(let s=0;s<3;s++)b=n[e+s],D=n[e+(s+1)%3],P=Math.min(b,D),R=Math.max(b,D),C=P+","+R,void 0===E[C]?E[C]={index1:P,index2:R,face1:t,face2:void 0}:E[C].face2=t}for(C in E)_=E[C],void 0!==_.face2&&(B=i[_.face1].normal,O=i[_.face2].normal,S=h.dotVec3(B,O),S>T)||(N=s[_.index1],x=s[_.index2],(!L&&N>65535||x>65535)&&(L=!0),g.push(N),g.push(x));return L?new Uint32Array(g):new Uint16Array(g)}}();class p{constructor(){this._head=[],this._headLength=0,this._tail=[],this._index=0,this._length=0}get length(){return this._length}shift(){if(this._index>=this._headLength){const e=this._head;if(e.length=0,this._head=this._tail,this._tail=e,this._index=0,this._headLength=this._head.length,!this._headLength)return}const e=this._head[this._index];return this._index<0?delete this._head[this._index++]:this._head[this._index++]=void 0,this._length--,e}push(e){return this._length++,this._tail.push(e),this}unshift(e){return this._head[--this._index]=e,this._length++,this}}const A={build:{version:"0.8"},client:{browser:navigator&&navigator.userAgent?navigator.userAgent:"n/a"},components:{scenes:0,models:0,meshes:0,objects:0},memory:{meshes:0,positions:0,colors:0,normals:0,uvs:0,indices:0,textures:0,transforms:0,materials:0,programs:0},frame:{frameCount:0,fps:0,useProgram:0,bindTexture:0,bindArray:0,drawElements:0,drawArrays:0,tasksRun:0,tasksScheduled:0}};var d=[["0",10],["A",26],["a",26],["_",1],["$",1]].map((function(e){for(var t=[],s=e[0].charCodeAt(0),n=s+e[1],i=s;i<n;++i)t.push(i);return String.fromCharCode.apply(null,t)})).join("");function f(e,t){return(t&&4!==t?[0,6]:[0,6,12,18]).map((function(t){return d.substr(parseInt(e/(1<<t))%64,1)})).reverse().join("")}const I=function(){for(var e={},t=window.location.search.substring(1).split("&"),s=0;s<t.length;s++){var n=t[s].split("=");if(void 0===e[n[0]])e[n[0]]=decodeURIComponent(n[1]);else if("string"==typeof e[n[0]]){var i=[e[n[0]],decodeURIComponent(n[1])];e[n[0]]=i}else e[n[0]].push(decodeURIComponent(n[1]))}return e}();const y={xmlToJson:function e(t,s){if(t.nodeType===t.TEXT_NODE){var n=t.nodeValue;if(null===n.match(/^\s+$/))return n}else if(t.nodeType===t.ELEMENT_NODE||t.nodeType===t.DOCUMENT_NODE){var i={type:t.nodeName,children:[]};if(t.nodeType===t.ELEMENT_NODE)for(var a=0;a<t.attributes.length;a++){var r=t.attributes[a];i[s[r.nodeName]||r.nodeName]=r.nodeValue}for(var l=0;l<t.childNodes.length;l++){(a=e(t.childNodes[l],s))&&i.children.push(a)}return i}},clone:function(e){return JSON.parse(JSON.stringify(e))},compressGuid:function(e){var t=[0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30].map((function(t){return parseInt(e.substr(t,2),16)}));return f(t[0],2)+[1,4,7,10,13].map((function(e){return f((t[e]<<16)+(t[e+1]<<8)+t[e+2])})).join("")},findNodeOfType:function(e,t){var s=[],n=function(e){e.type===t&&s.push(e),(e.children||[]).forEach((function(e){n(e)}))};return n(e),s},timeout:function(e){return new Promise((function(t,s){setTimeout(t,e)}))},httpRequest:function(e){return new Promise((function(t,s){var n=new XMLHttpRequest;n.open(e.method||"GET",e.url,!0),n.onload=function(i){console.log(e.url,n.readyState,n.status),4===n.readyState&&(200===n.status?t(n.responseXML):s(n.statusText))},n.send(null)}))},loadJSON:function(e,t,s){var n=e=>{};t=t||n,s=s||n;var i=new XMLHttpRequest;i.overrideMimeType("application/json"),i.open("GET",e,!0),i.addEventListener("load",(function(e){var n=e.target.response;if(200===this.status){var i;try{i=JSON.parse(n)}catch(e){s(`utils.loadJSON(): Failed to parse JSON response - ${e}`)}t(i)}else if(0===this.status){console.warn("loadFile: HTTP Status 0 received.");try{t(JSON.parse(n))}catch(e){s(`utils.loadJSON(): Failed to parse JSON response - ${e}`)}}else s(e)}),!1),i.addEventListener("error",(function(e){s(e)}),!1),i.send(null)},loadArraybuffer:function(e,t,s){var n=e=>{};t=t||n,s=s||n;const i=e.match(/^data:(.*?)(;base64)?,(.*)$/);if(i){const e=!!i[2];var a=i[3];a=window.decodeURIComponent(a),e&&(a=window.atob(a));try{const e=new ArrayBuffer(a.length),s=new Uint8Array(e);for(var r=0;r<a.length;r++)s[r]=a.charCodeAt(r);window.setTimeout((function(){t(e)}),0)}catch(e){window.setTimeout((function(){s(e)}),0)}}else{const n=new XMLHttpRequest;n.open("GET",e,!0),n.responseType="arraybuffer",n.onreadystatechange=function(){4===n.readyState&&(200===n.status?t(n.response):s("loadArrayBuffer error : "+n.response))},n.send(null)}},queryString:I,isArray:function(e){return e&&!e.propertyIsEnumerable("length")&&"object"==typeof e&&"number"==typeof e.length},isString:function(e){return"string"==typeof e||e instanceof String},isNumeric:function(e){return!isNaN(parseFloat(e))&&isFinite(e)},isID:function(e){return y.isString(e)||y.isNumeric(e)},isSameComponent:function(e,t){return!(!e||!t)&&(y.isNumeric(e)||y.isString(e)?`${e}`:e.id)===(y.isNumeric(t)||y.isString(t)?`${t}`:t.id)},isFunction:function(e){return"function"==typeof e},isObject:function(e){const t={}.constructor;return!!e&&e.constructor===t},copy:function(e){return y.apply(e,{})},apply:function(e,t){for(const s in e)e.hasOwnProperty(s)&&(t[s]=e[s]);return t},apply2:function(e,t){for(const s in e)e.hasOwnProperty(s)&&void 0!==e[s]&&null!==e[s]&&(t[s]=e[s]);return t},applyIf:function(e,t){for(const s in e)e.hasOwnProperty(s)&&(void 0!==t[s]&&null!==t[s]||(t[s]=e[s]));return t},isEmptyObject:function(e){for(const t in e)if(e.hasOwnProperty(t))return!1;return!0},inQuotes:function(e){return y.isNumeric(e)?`${e}`:`'${e}'`},concat:function(e,t){const s=new e.constructor(e.length+t.length);return s.set(e),s.set(t,e.length),s},flattenParentChildHierarchy:function(e){var t=[];return function e(s){s.id=s.uuid,delete s.oid,t.push(s);var n=s.children;if(n)for(var i=0,a=n.length;i<a;i++){n[i].parent=s.id,e(n[i])}s.children=[]}(e),t}},m={},v=new e,w=new p,g={sceneId:null,time:null,startTime:null,prevTime:null,deltaTime:null},T=[];let E,b=0,D=0;const P=new function(){this.version="1.0.0",this.scenes={},this._superTypes={},this._addScene=function(e){if(e.id){if(P.scenes[e.id])return void console.error(`[ERROR] Scene ${y.inQuotes(e.id)} already exists`)}else e.id=v.addItem({});P.scenes[e.id]=e;const t=e.ticksPerOcclusionTest,s=e.ticksPerRender;m[e.id]={ticksPerOcclusionTest:t,ticksPerRender:s,renderCountdown:s},A.components.scenes++,e.once("destroyed",(()=>{v.removeItem(e.id),delete P.scenes[e.id],delete m[e.id],A.components.scenes--}))},this.clear=function(){let e;for(const t in P.scenes)P.scenes.hasOwnProperty(t)&&(e=P.scenes[t],"default.scene"===t?e.clear():(e.destroy(),delete P.scenes[e.id]))},this.scheduleTask=function(e,t){w.push(e),w.push(t)},this.runTasks=function(e=-1){let t,s,n=(new Date).getTime(),i=0;for(;w.length>0&&(e<0||n<e);)t=w.shift(),s=w.shift(),s?t.call(s):t(),n=(new Date).getTime(),i++;return i},this.getNumTasks=function(){return w.length}},R=function(){let e=Date.now();if(E=e-b,b>0&&E>0){var t=1e3/E;D+=t,T.push(t),T.length>=30&&(D-=T.shift()),A.frame.fps=Math.round(D/T.length)}!function(e){const t=P.runTasks(e+10),s=P.getNumTasks();A.frame.tasksRun=t,A.frame.tasksScheduled=s,A.frame.tasksBudget=10}(e),function(e){for(var t in g.time=e,P.scenes)if(P.scenes.hasOwnProperty(t)){var s=P.scenes[t];g.sceneId=t,g.startTime=s.startTime,g.deltaTime=null!=g.prevTime?g.time-g.prevTime:0,s.fire("tick",g,!0)}g.prevTime=e}(e),function(){const e=P.scenes,t=!1;let s,n,i,a,r;for(r in e)e.hasOwnProperty(r)&&(s=e[r],n=m[r],n||(n=m[r]={}),i=s.ticksPerOcclusionTest,n.ticksPerOcclusionTest!==i&&(n.ticksPerOcclusionTest=i,n.renderCountdown=i),--s.occlusionTestCountdown<=0&&(s.doOcclusionTest(),s.occlusionTestCountdown=i),a=s.ticksPerRender,n.ticksPerRender!==a&&(n.ticksPerRender=a,n.renderCountdown=a),0==--n.renderCountdown&&(s.render(t),n.renderCountdown=a))}(),b=e,void 0!==window.requestPostAnimationFrame?window.requestPostAnimationFrame(R):requestAnimationFrame(R)};void 0!==window.requestPostAnimationFrame?window.requestPostAnimationFrame(R):requestAnimationFrame(R);class C{get type(){return"Component"}get isComponent(){return!0}constructor(e=null,t={}){if(this.scene=null,"Scene"===this.type)this.scene=this,this.viewer=t.viewer;else{if("Scene"===e.type)this.scene=e;else{if(!(e instanceof C))throw"Invalid param: owner must be a Component";this.scene=e.scene}this._owner=e}this._dontClear=!!t.dontClear,this._renderer=this.scene._renderer,this.meta=t.meta||{},this.id=t.id,this.destroyed=!1,this._attached={},this._attachments=null,this._subIdMap=null,this._subIdEvents=null,this._eventSubs=null,this._eventSubsNum=null,this._events=null,this._eventCallDepth=0,this._ownedComponents=null,this!==this.scene&&this.scene._addComponent(this),this._updateScheduled=!1,e&&e._own(this)}glRedraw(){this._renderer&&(this._renderer.imageDirty(),this.castsShadow&&this._renderer.shadowsDirty())}glResort(){this._renderer&&this._renderer.needStateSort()}get owner(){return this._owner}isType(e){return this.type===e}fire(e,t,s){this._events||(this._events={}),this._eventSubs||(this._eventSubs={},this._eventSubsNum={}),!0!==s&&(this._events[e]=t||!0);const n=this._eventSubs[e];let i;if(n)for(const s in n)n.hasOwnProperty(s)&&(i=n[s],this._eventCallDepth++,this._eventCallDepth<300?i.callback.call(i.scope,t):this.error("fire: potential stack overflow from recursive event '"+e+"' - dropping this event"),this._eventCallDepth--)}on(t,s,n){this._events||(this._events={}),this._subIdMap||(this._subIdMap=new e),this._subIdEvents||(this._subIdEvents={}),this._eventSubs||(this._eventSubs={}),this._eventSubsNum||(this._eventSubsNum={});let i=this._eventSubs[t];i?this._eventSubsNum[t]++:(i={},this._eventSubs[t]=i,this._eventSubsNum[t]=1);const a=this._subIdMap.addItem();i[a]={callback:s,scope:n||this},this._subIdEvents[a]=t;const r=this._events[t];return void 0!==r&&s.call(n||this,r),a}off(e){if(null==e)return;if(!this._subIdEvents)return;const t=this._subIdEvents[e];if(t){delete this._subIdEvents[e];const s=this._eventSubs[t];s&&(delete s[e],this._eventSubsNum[t]--),this._subIdMap.removeItem(e)}}once(e,t,s){const n=this,i=this.on(e,(function(e){n.off(i),t.call(s||this,e)}),s)}hasSubs(e){return this._eventSubsNum&&this._eventSubsNum[e]>0}log(e){e="[LOG]"+this._message(e),window.console.log(e),this.scene.fire("log",e)}_message(e){return" ["+this.type+" "+y.inQuotes(this.id)+"]: "+e}warn(e){e="[WARN]"+this._message(e),window.console.warn(e),this.scene.fire("warn",e)}error(e){e="[ERROR]"+this._message(e),window.console.error(e),this.scene.fire("error",e)}_attach(e){const t=e.name;if(!t)return void this.error("Component 'name' expected");let s=e.component;const n=e.sceneDefault,i=e.sceneSingleton,a=e.type,r=e.on,l=!1!==e.recompiles;if(s&&(y.isNumeric(s)||y.isString(s))){const e=s;if(s=this.scene.components[e],!s)return void this.error("Component not found: "+y.inQuotes(e))}if(!s)if(!0===i){const e=this.scene.types[a];for(const t in e)if(e.hasOwnProperty){s=e[t];break}if(!s)return this.error("Scene has no default component for '"+t+"'"),null}else if(!0===n&&(s=this.scene[t],!s))return this.error("Scene has no default component for '"+t+"'"),null;if(s){if(s.scene.id!==this.scene.id)return void this.error("Not in same scene: "+s.type+" "+y.inQuotes(s.id));if(a&&!s.isType(a))return void this.error("Expected a "+a+" type or subtype: "+s.type+" "+y.inQuotes(s.id))}this._attachments||(this._attachments={});const o=this._attached[t];let c,u,h;if(o){if(s&&o.id===s.id)return;const e=this._attachments[o.id];for(c=e.subs,u=0,h=c.length;u<h;u++)o.off(c[u]);delete this._attached[t],delete this._attachments[o.id];const n=e.params.onDetached;n&&(y.isFunction(n)?n(o):n.scope?n.callback.call(n.scope,o):n.callback(o)),e.managingLifecycle&&o.destroy()}if(s){const n={params:e,component:s,subs:[],managingLifecycle:false};n.subs.push(s.once("destroyed",(function(){n.params.component=null,this._attach(n.params)}),this)),l&&n.subs.push(s.on("dirty",(function(){this.fire("dirty",this)}),this)),this._attached[t]=s,this._attachments[s.id]=n;const i=e.onAttached;if(i&&(y.isFunction(i)?i(s):i.scope?i.callback.call(i.scope,s):i.callback(s)),r){let e,t,i,a;for(e in r)if(r.hasOwnProperty(e)){if(t=r[e],y.isFunction(t)?(i=t,a=null):(i=t.callback,a=t.scope),!i)continue;n.subs.push(s.on(e,i,a))}}}return l&&this.fire("dirty",this),this.fire(t,s),s}_checkComponent(e,t){if(!t.isComponent){if(!y.isID(t))return void this.error("Expected a Component or ID");{const e=t;if(!(t=this.scene.components[e]))return void this.error("Component not found: "+e)}}if(e===t.type){if(t.scene.id===this.scene.id)return t;this.error("Not in same scene: "+t.type)}else this.error("Expected a "+e+" Component")}_checkComponent2(e,t){if(!t.isComponent){if(!y.isID(t))return void this.error("Expected a Component or ID");{const e=t;if(!(t=this.scene.components[e]))return void this.error("Component not found: "+e)}}if(t.scene.id===this.scene.id){for(var s=0,n=e.length;s<n;s++)if(e[s]===t.type)return t;return this.error("Expected component types: "+e),null}this.error("Not in same scene: "+t.type)}_own(e){this._ownedComponents||(this._ownedComponents={}),this._ownedComponents[e.id]||(this._ownedComponents[e.id]=e),e.once("destroyed",(()=>{delete this._ownedComponents[e.id]}),this)}_needUpdate(e){this._updateScheduled||(this._updateScheduled=!0,0===e?this._doUpdate():P.scheduleTask(this._doUpdate,this))}_doUpdate(){this._updateScheduled&&(this._updateScheduled=!1,this._update&&this._update())}_update(){}clear(){if(this._ownedComponents)for(var e in this._ownedComponents)if(this._ownedComponents.hasOwnProperty(e)){this._ownedComponents[e].destroy(),delete this._ownedComponents[e]}}destroy(){if(this.destroyed)return;let e,t,s,n,i,a;if(this.fire("destroyed",this.destroyed=!0),this._attachments)for(e in this._attachments)if(this._attachments.hasOwnProperty(e)){for(t=this._attachments[e],s=t.component,n=t.subs,i=0,a=n.length;i<a;i++)s.off(n[i]);t.managingLifecycle&&s.destroy()}if(this._ownedComponents)for(e in this._ownedComponents)this._ownedComponents.hasOwnProperty(e)&&(s=this._ownedComponents[e],s.destroy(),delete this._ownedComponents[e]);this.scene._removeComponent(this),this._attached={},this._attachments=null,this._subIdMap=null,this._subIdEvents=null,this._eventSubs=null,this._events=null,this._eventCallDepth=0,this._ownedComponents=null,this._updateScheduled=!1}}const _=h.vec3(),B=function(){const e=new Float64Array(16),t=new Float64Array(4),s=new Float64Array(4);return function(n,i,a){return a=a||e,t[0]=i[0],t[1]=i[1],t[2]=i[2],t[3]=1,h.transformVec4(n,t,s),h.setMat4Translation(n,s,a),a.slice()}}();function O(e,t,s){const n=Float32Array.from([e[0]])[0],i=e[0]-n,a=Float32Array.from([e[1]])[0],r=e[1]-a,l=Float32Array.from([e[2]])[0],o=e[2]-l;t[0]=n,t[1]=a,t[2]=l,s[0]=i,s[1]=r,s[2]=o}function S(e,t,s,n=1e3){const i=h.getPositionsCenter(e,_),a=Math.round(i[0]/n)*n,r=Math.round(i[1]/n)*n,l=Math.round(i[2]/n)*n;s[0]=a,s[1]=r,s[2]=l;const o=0!==s[0]||0!==s[1]||0!==s[2];if(o)for(let s=0,n=e.length;s<n;s+=3)t[s+0]=e[s+0]-a,t[s+1]=e[s+1]-r,t[s+2]=e[s+2]-l;return o}function N(e,t,s,n){const i=h.dotVec3(t,s)+e,a=h.normalizeVec3(t,_);return h.mulVec3Scalar(a,-i,n),n}const x=1,L=4,M=8,F=16,H=32,U=256,G=512,V=1024,j=2048,k=new Float32Array([0,0,0]),Q=new Uint16Array([0,0,0]);class W{constructor(e,t,s,n,i,a,r){this._isObject=t,this.scene=e.scene,this.model=e,this.meshes=n,this._numPrimitives=0;for(let e=0,t=this.meshes.length;e<t;e++){const t=this.meshes[e];t.parent=this,this._numPrimitives+=t.numPrimitives}this.id=s,this.originalSystemId=h.unglobalizeObjectId(e.id,s),this._flags=i,this._aabb=a,this._offsetAABB=h.AABB3(a),this._offset=h.vec3(),this._colorizeUpdated=!1,this._opacityUpdated=!1,this._lodCullable=!!r,this._culled=!1,this._culledVFC=!1,this._culledLOD=!1,this._isObject&&e.scene._registerObject(this)}get isEntity(){return!0}get isModel(){return!1}get isObject(){return this._isObject}get aabb(){return this._offsetAABB}get numPrimitives(){return this._numPrimitives}get numTriangles(){return this._numPrimitives}get visible(){return this._getFlag(x)}set visible(e){if(!!(this._flags&x)!==e){this._flags=e?this._flags|x:this._flags&~x;for(let e=0,t=this.meshes.length;e<t;e++)this.meshes[e]._setVisible(this._flags);this._isObject&&this.model.scene._objectVisibilityUpdated(this),this.model.glRedraw()}}get highlighted(){return this._getFlag(G)}set highlighted(e){if(!!(this._flags&G)!==e){this._flags=e?this._flags|G:this._flags&~G;for(var t=0,s=this.meshes.length;t<s;t++)this.meshes[t]._setHighlighted(this._flags);this._isObject&&this.model.scene._objectHighlightedUpdated(this),this.model.glRedraw()}}get xrayed(){return this._getFlag(U)}set xrayed(e){if(!!(this._flags&U)!==e){this._flags=e?this._flags|U:this._flags&~U;for(let e=0,t=this.meshes.length;e<t;e++)this.meshes[e]._setXRayed(this._flags);this._isObject&&this.model.scene._objectXRayedUpdated(this),this.model.glRedraw()}}get selected(){return this._getFlag(V)}set selected(e){if(!!(this._flags&V)!==e){this._flags=e?this._flags|V:this._flags&~V;for(let e=0,t=this.meshes.length;e<t;e++)this.meshes[e]._setSelected(this._flags);this._isObject&&this.model.scene._objectSelectedUpdated(this),this.model.glRedraw()}}get edges(){return this._getFlag(j)}set edges(e){if(!!(this._flags&j)!==e){this._flags=e?this._flags|j:this._flags&~j;for(var t=0,s=this.meshes.length;t<s;t++)this.meshes[t]._setEdges(this._flags);this.model.glRedraw()}}get culledVFC(){return!!this._culledVFC}set culledVFC(e){this._culledVFC=e,this._setCulled()}get culledLOD(){return!!this._culledLOD}set culledLOD(e){this._culledLOD=e,this._setCulled()}get culled(){return!!this._culled}set culled(e){this._culled=e,this._setCulled()}_setCulled(){let e=!!this._culled||!(!this._culledLOD||!this._lodCullable)||!!this._culledVFC;if(!!(this._flags&L)!==e){this._flags=e?this._flags|L:this._flags&~L;for(var t=0,s=this.meshes.length;t<s;t++)this.meshes[t]._setCulled(this._flags);this.model.glRedraw()}}get clippable(){return this._getFlag(F)}set clippable(e){if(!!(this._flags&F)!==e){this._flags=e?this._flags|F:this._flags&~F;for(var t=0,s=this.meshes.length;t<s;t++)this.meshes[t]._setClippable(this._flags);this.model.glRedraw()}}get collidable(){return this._getFlag(H)}set collidable(e){if(!!(this._flags&H)!==e){this._flags=e?this._flags|H:this._flags&~H;for(var t=0,s=this.meshes.length;t<s;t++)this.meshes[t]._setCollidable(this._flags)}}get pickable(){return this._getFlag(M)}set pickable(e){if(!!(this._flags&M)!==e){this._flags=e?this._flags|M:this._flags&~M;for(var t=0,s=this.meshes.length;t<s;t++)this.meshes[t]._setPickable(this._flags)}}get colorize(){if(0===this.meshes.length)return null;const e=this.meshes[0]._colorize;return k[0]=e[0]/255,k[1]=e[1]/255,k[2]=e[2]/255,k}set colorize(e){if(e){Q[0]=Math.floor(255*e[0]),Q[1]=Math.floor(255*e[1]),Q[2]=Math.floor(255*e[2]);for(let e=0,t=this.meshes.length;e<t;e++)this.meshes[e]._setColorize(Q)}else for(let e=0,t=this.meshes.length;e<t;e++)this.meshes[e]._setColorize(null);if(this._isObject){const t=!!e;this.scene._objectColorizeUpdated(this,t),this._colorizeUpdated=t}this.model.glRedraw()}get opacity(){return this.meshes.length>0?this.meshes[0]._colorize[3]/255:1}set opacity(e){if(0===this.meshes.length)return;const t=null!=e,s=this.meshes[0]._colorize[3];let n=255;if(t){if(e<0?e=0:e>1&&(e=1),n=Math.floor(255*e),s===n)return}else if(n=255,s===n)return;for(let e=0,t=this.meshes.length;e<t;e++)this.meshes[e]._setOpacity(n,this._flags);this._isObject&&(this.scene._objectOpacityUpdated(this,t),this._opacityUpdated=t),this.model.glRedraw()}get offset(){return this._offset}set offset(e){e?(this._offset[0]=e[0],this._offset[1]=e[1],this._offset[2]=e[2]):(this._offset[0]=0,this._offset[1]=0,this._offset[2]=0);for(let e=0,t=this.meshes.length;e<t;e++)this.meshes[e]._setOffset(this._offset);this._offsetAABB[0]=this._aabb[0]+this._offset[0],this._offsetAABB[1]=this._aabb[1]+this._offset[1],this._offsetAABB[2]=this._aabb[2]+this._offset[2],this._offsetAABB[3]=this._aabb[3]+this._offset[0],this._offsetAABB[4]=this._aabb[4]+this._offset[1],this._offsetAABB[5]=this._aabb[5]+this._offset[2],this.scene._aabbDirty=!0,this.scene._objectOffsetUpdated(this,e),this.model._aabbDirty=!0,this.model.glRedraw()}get saoEnabled(){return this.model.saoEnabled}getEachVertex(e){for(let t=0,s=this.meshes.length;t<s;t++)this.meshes[t].getEachVertex(e)}_getFlag(e){return!!(this._flags&e)}_finalize(){const e=this.model.scene;this._isObject&&(this.visible&&e._objectVisibilityUpdated(this),this.highlighted&&e._objectHighlightedUpdated(this),this.xrayed&&e._objectXRayedUpdated(this),this.selected&&e._objectSelectedUpdated(this));for(let e=0,t=this.meshes.length;e<t;e++)this.meshes[e]._finalize(this._flags)}_finalize2(){for(let e=0,t=this.meshes.length;e<t;e++)this.meshes[e]._finalize2()}_destroy(){const e=this.model.scene;this._isObject&&(e._deregisterObject(this),this.visible&&e._objectVisibilityUpdated(this,!1),this.xrayed&&e._objectXRayedUpdated(this),this.selected&&e._objectSelectedUpdated(this),this.highlighted&&e._objectHighlightedUpdated(this),this._opacityUpdated&&this.scene._objectColorizeUpdated(this,!1),this._opacityUpdated&&this.scene._objectOpacityUpdated(this,!1),this.scene._objectOffsetUpdated(this,!1));for(let e=0,t=this.meshes.length;e<t;e++)this.meshes[e]._destroy();e._aabbDirty=!0}}const z=h.vec4(),K=h.vec4();class Y extends C{constructor(e,t){super(e,t),this._entity=null,this._visible=null,this._worldPos=h.vec3(),this._origin=h.vec3(),this._rtcPos=h.vec3(),this._viewPos=h.vec3(),this._canvasPos=h.vec2(),this._occludable=!1,this._onCameraViewMatrix=this.scene.camera.on("matrix",(()=>{this._viewPosDirty=!0,this._needUpdate()})),this._onCameraProjMatrix=this.scene.camera.on("projMatrix",(()=>{this._canvasPosDirty=!0,this._needUpdate()})),this._onEntityDestroyed=null,this._onEntityModelDestroyed=null,this._renderer.addMarker(this),this.entity=t.entity,this.worldPos=t.worldPos,this.occludable=t.occludable}_update(){if(this._viewPosDirty&&(h.transformPoint3(this.scene.camera.viewMatrix,this._worldPos,this._viewPos),this._viewPosDirty=!1,this._canvasPosDirty=!0,this.fire("viewPos",this._viewPos)),this._canvasPosDirty){z.set(this._viewPos),z[3]=1,h.transformPoint4(this.scene.camera.projMatrix,z,K);const e=this.scene.canvas.boundary;this._canvasPos[0]=Math.floor((1+K[0]/K[3])*e[2]/2),this._canvasPos[1]=Math.floor((1-K[1]/K[3])*e[3]/2),this._canvasPosDirty=!1,this.fire("canvasPos",this._canvasPos)}}_setVisible(e){this._visible,this._visible=e,this.fire("visible",this._visible)}set entity(e){if(this._entity){if(this._entity===e)return;null!==this._onEntityDestroyed&&(this._entity.off(this._onEntityDestroyed),this._onEntityDestroyed=null),null!==this._onEntityModelDestroyed&&(this._entity.model.off(this._onEntityModelDestroyed),this._onEntityModelDestroyed=null)}this._entity=e,this._entity&&(this._entity instanceof W?this._onEntityModelDestroyed=this._entity.model.on("destroyed",(()=>{this._entity=null,this._onEntityModelDestroyed=null})):this._onEntityDestroyed=this._entity.on("destroyed",(()=>{this._entity=null,this._onEntityDestroyed=null}))),this.fire("entity",this._entity,!0)}get entity(){return this._entity}set occludable(e){(e=!!e)!==this._occludable&&(this._occludable=e)}get occludable(){return this._occludable}set worldPos(e){this._worldPos.set(e||[0,0,0]),O(this._worldPos,this._origin,this._rtcPos),this._occludable&&this._renderer.markerWorldPosUpdated(this),this._viewPosDirty=!0,this.fire("worldPos",this._worldPos),this._needUpdate()}get worldPos(){return this._worldPos}get origin(){return this._origin}get rtcPos(){return this._rtcPos}get viewPos(){return this._update(),this._viewPos}get canvasPos(){return this._update(),this._canvasPos}get visible(){return!!this._visible}destroy(){this.fire("destroyed",!0),this.scene.camera.off(this._onCameraViewMatrix),this.scene.camera.off(this._onCameraProjMatrix),this._entity&&(null!==this._onEntityDestroyed&&this._entity.off(this._onEntityDestroyed),null!==this._onEntityModelDestroyed&&this._entity.model.off(this._onEntityModelDestroyed)),this._renderer.removeMarker(this),super.destroy()}}class X{constructor(e,t={}){this._color=t.color||"black",this._highlightClass="viewer-ruler-wire-highlighted",this._wire=document.createElement("div"),this._wire.className+=this._wire.className?" viewer-ruler-wire":"viewer-ruler-wire",this._wireClickable=document.createElement("div"),this._wireClickable.className+=this._wireClickable.className?" viewer-ruler-wire-clickable":"viewer-ruler-wire-clickable",this._thickness=t.thickness||1,this._thicknessClickable=t.thicknessClickable||6;var s=this._wire,n=s.style;n.border="solid "+this._thickness+"px "+this._color,n.position="absolute",n["z-index"]=void 0===t.zIndex?"2000001":t.zIndex,n.width="0px",n.height="0px",n.visibility="visible",n.top="0px",n.left="0px",n["-webkit-transform-origin"]="0 0",n["-moz-transform-origin"]="0 0",n["-ms-transform-origin"]="0 0",n["-o-transform-origin"]="0 0",n["transform-origin"]="0 0",n["-webkit-transform"]="rotate(0deg)",n["-moz-transform"]="rotate(0deg)",n["-ms-transform"]="rotate(0deg)",n["-o-transform"]="rotate(0deg)",n.transform="rotate(0deg)",n.opacity=1,n["pointer-events"]="none",t.onContextMenu,e.appendChild(s);var i=this._wireClickable,a=i.style;a.border="solid "+this._thicknessClickable+"px "+this._color,a.position="absolute",a["z-index"]=void 0===t.zIndex?"2000002":t.zIndex+1,a.width="0px",a.height="0px",a.visibility="visible",a.top="0px",a.left="0px",a["-webkit-transform-origin"]="0 0",a["-moz-transform-origin"]="0 0",a["-ms-transform-origin"]="0 0",a["-o-transform-origin"]="0 0",a["transform-origin"]="0 0",a["-webkit-transform"]="rotate(0deg)",a["-moz-transform"]="rotate(0deg)",a["-ms-transform"]="rotate(0deg)",a["-o-transform"]="rotate(0deg)",a.transform="rotate(0deg)",a.opacity=0,a["pointer-events"]="none",t.onContextMenu,e.appendChild(i),t.onMouseOver&&i.addEventListener("mouseover",(e=>{t.onMouseOver(e,this)})),t.onMouseLeave&&i.addEventListener("mouseleave",(e=>{t.onMouseLeave(e,this)})),t.onMouseWheel&&i.addEventListener("wheel",(e=>{t.onMouseWheel(e,this)})),t.onContextMenu&&i.addEventListener("contextmenu",(e=>{t.onContextMenu(e,this),e.preventDefault()})),this._x1=0,this._y1=0,this._x2=0,this._y2=0,this._update()}get _visible(){return"visible"===this._wire.style.visibility}_update(){var e=Math.abs(Math.sqrt((this._x1-this._x2)*(this._x1-this._x2)+(this._y1-this._y2)*(this._y1-this._y2))),t=180*Math.atan2(this._y2-this._y1,this._x2-this._x1)/Math.PI,s=this._wire.style;s.width=Math.round(e)+"px",s.left=Math.round(this._x1)+"px",s.top=Math.round(this._y1)+"px",s["-webkit-transform"]="rotate("+t+"deg)",s["-moz-transform"]="rotate("+t+"deg)",s["-ms-transform"]="rotate("+t+"deg)",s["-o-transform"]="rotate("+t+"deg)",s.transform="rotate("+t+"deg)";var n=this._wireClickable.style;n.width=Math.round(e)+"px",n.left=Math.round(this._x1)+"px",n.top=Math.round(this._y1)+"px",n["-webkit-transform"]="rotate("+t+"deg)",n["-moz-transform"]="rotate("+t+"deg)",n["-ms-transform"]="rotate("+t+"deg)",n["-o-transform"]="rotate("+t+"deg)",n.transform="rotate("+t+"deg)"}setStartAndEnd(e,t,s,n){this._x1=e,this._y1=t,this._x2=s,this._y2=n,this._update()}setColor(e){this._color=e||"black",this._wire.style.border="solid "+this._thickness+"px "+this._color}setOpacity(e){this._wire.style.opacity=e}setVisible(e){e=!!e,this._visible!==e&&(this._wire.style.visibility=e?"visible":"hidden")}setClickable(e){this._wireClickable.style["pointer-events"]=e?"all":"none"}setHighlighted(e){this._highlighted!==e&&(this._highlighted=!!e,this._highlighted?this._wire.classList.add(this._highlightClass):this._wire.classList.remove(this._highlightClass))}destroy(e){this._wire.parentElement&&this._wire.parentElement.removeChild(this._wire),this._wireClickable.parentElement&&this._wireClickable.parentElement.removeChild(this._wireClickable)}}class q{constructor(e,t={}){this._highlightClass="viewer-ruler-dot-highlighted",this._x=0,this._y=0,this._visible=!0,this._dot=document.createElement("div"),this._dot.className+=this._dot.className?" viewer-ruler-dot":"viewer-ruler-dot",this._dotClickable=document.createElement("div"),this._dotClickable.className+=this._dotClickable.className?" viewer-ruler-dot-clickable":"viewer-ruler-dot-clickable";var s=this._dot,n=s.style;n["border-radius"]="25px",n.border="solid 2px white",n.background="lightgreen",n.position="absolute",n["z-index"]=void 0===t.zIndex?"40000005":t.zIndex,n.width="8px",n.height="8px",n.visibility=!1!==t.visible?"visible":"hidden",n.top="0px",n.left="0px",n["box-shadow"]="0 2px 5px 0 #182A3D;",n.opacity=1,n["pointer-events"]="none",t.onContextMenu,e.appendChild(s);var i=this._dotClickable,a=i.style;a["border-radius"]="35px",a.border="solid 10px white",a.position="absolute",a["z-index"]=void 0===t.zIndex?"40000007":t.zIndex+1,a.width="8px",a.height="8px",a.visibility="visible",a.top="0px",a.left="0px",a.opacity=0,a["pointer-events"]="none",t.onContextMenu,e.appendChild(i),t.onMouseOver&&i.addEventListener("mouseover",(e=>{t.onMouseOver(e,this)})),t.onMouseLeave&&i.addEventListener("mouseleave",(e=>{t.onMouseLeave(e,this)})),t.onMouseWheel&&i.addEventListener("wheel",(e=>{t.onMouseWheel(e,this)})),t.onContextMenu&&i.addEventListener("contextmenu",(e=>{t.onContextMenu(e,this),e.preventDefault()})),this.setPos(t.x||0,t.y||0),this.setFillColor(t.fillColor),this.setBorderColor(t.borderColor)}setPos(e,t){this._x=e,this._y=t;var s=this._dot.style;s.left=Math.round(e)-4+"px",s.top=Math.round(t)-4+"px";var n=this._dotClickable.style;n.left=Math.round(e)-9+"px",n.top=Math.round(t)-9+"px"}setFillColor(e){this._dot.style.background=e||"lightgreen"}setBorderColor(e){this._dot.style.border="solid 2px"+(e||"black")}setOpacity(e){this._dot.style.opacity=e}setVisible(e){this._visible!==e&&(this._visible=!!e,this._dot.style.visibility=this._visible?"visible":"hidden")}setClickable(e){this._dotClickable.style["pointer-events"]=e?"all":"none"}setHighlighted(e){this._highlighted!==e&&(this._highlighted=!!e,this._highlighted?this._dot.classList.add(this._highlightClass):this._dot.classList.remove(this._highlightClass))}destroy(){this.setVisible(!1),this._dot.parentElement&&this._dot.parentElement.removeChild(this._dot),this._dotClickable.parentElement&&this._dotClickable.parentElement.removeChild(this._dotClickable)}}class J{constructor(e,t={}){this._highlightClass="viewer-ruler-label-highlighted",this._prefix=t.prefix||"",this._x=0,this._y=0,this._visible=!0,this._culled=!1,this._label=document.createElement("div"),this._label.className+=this._label.className?" viewer-ruler-label":"viewer-ruler-label";var s=this._label,n=s.style;n["border-radius"]="5px",n.color="white",n.padding="4px",n.border="solid 1px",n.background="lightgreen",n.position="absolute",n["z-index"]=void 0===t.zIndex?"5000005":t.zIndex,n.width="auto",n.height="auto",n.visibility="visible",n.top="0px",n.left="0px",n["pointer-events"]="all",n.opacity=1,t.onContextMenu,s.innerText="",e.appendChild(s),this.setPos(t.x||0,t.y||0),this.setFillColor(t.fillColor),this.setBorderColor(t.fillColor),this.setText(t.text),t.onMouseOver&&s.addEventListener("mouseover",(e=>{t.onMouseOver(e,this),e.preventDefault()})),t.onMouseLeave&&s.addEventListener("mouseleave",(e=>{t.onMouseLeave(e,this),e.preventDefault()})),t.onMouseWheel&&s.addEventListener("wheel",(e=>{t.onMouseWheel(e,this)})),t.onContextMenu&&s.addEventListener("contextmenu",(e=>{t.onContextMenu(e,this),e.preventDefault()}))}setPos(e,t){this._x=e,this._y=t;var s=this._label.style;s.left=Math.round(e)-20+"px",s.top=Math.round(t)-12+"px"}setPosOnWire(e,t,s,n){var i=e+.5*(s-e),a=t+.5*(n-t),r=this._label.style;r.left=Math.round(i)-20+"px",r.top=Math.round(a)-12+"px"}setPosBetweenWires(e,t,s,n,i,a){var r=(e+s+i)/3,l=(t+n+a)/3,o=this._label.style;o.left=Math.round(r)-20+"px",o.top=Math.round(l)-12+"px"}setText(e){this._label.innerHTML=this._prefix+(e||"")}setFillColor(e){this._fillColor=e||"lightgreen",this._label.style.background=this._fillColor}setBorderColor(e){this._borderColor=e||"black",this._label.style.border="solid 1px "+this._borderColor}setOpacity(e){this._label.style.opacity=e}setVisible(e){this._visible!==e&&(this._visible=!!e,this._label.style.visibility=this._visible&&!this._culled?"visible":"hidden")}setCulled(e){this._culled!==e&&(this._culled=!!e,this._label.style.visibility=this._visible&&!this._culled?"visible":"hidden")}setHighlighted(e){this._highlighted!==e&&(this._highlighted=!!e,this._highlighted?this._label.classList.add(this._highlightClass):this._label.classList.remove(this._highlightClass))}setClickable(e){this._label.style["pointer-events"]=e?"all":"none"}destroy(){this._label.parentElement&&this._label.parentElement.removeChild(this._label)}}var Z=h.vec3(),$=h.vec3();class ee extends C{constructor(e,t={}){if(super(e.viewer.scene,t),this.plugin=e,this._container=t.container,!this._container)throw"config missing: container";this._color=t.color||e.defaultColor;var s=this.plugin.viewer.scene;this._originMarker=new Y(s,t.origin),this._cornerMarker=new Y(s,t.corner),this._targetMarker=new Y(s,t.target),this._originWorld=h.vec3(),this._cornerWorld=h.vec3(),this._targetWorld=h.vec3(),this._wp=new Float64Array(12),this._vp=new Float64Array(12),this._pp=new Float64Array(12),this._cp=new Int16Array(6);const n=t.onMouseOver?e=>{t.onMouseOver(e,this)}:null,i=t.onMouseLeave?e=>{t.onMouseLeave(e,this)}:null,a=t.onContextMenu?e=>{t.onContextMenu(e,this)}:null,r=e=>{this.plugin.viewer.scene.canvas.canvas.dispatchEvent(new WheelEvent("wheel",e))};this._originDot=new q(this._container,{fillColor:this._color,zIndex:void 0!==e.zIndex?e.zIndex+2:void 0,onMouseOver:n,onMouseLeave:i,onMouseWheel:r,onContextMenu:a}),this._cornerDot=new q(this._container,{fillColor:this._color,zIndex:void 0!==e.zIndex?e.zIndex+2:void 0,onMouseOver:n,onMouseLeave:i,onMouseWheel:r,onContextMenu:a}),this._targetDot=new q(this._container,{fillColor:this._color,zIndex:void 0!==e.zIndex?e.zIndex+2:void 0,onMouseOver:n,onMouseLeave:i,onMouseWheel:r,onContextMenu:a}),this._originWire=new X(this._container,{color:this._color||"blue",thickness:1,zIndex:e.zIndex,onMouseOver:n,onMouseLeave:i,onMouseWheel:r,onContextMenu:a}),this._targetWire=new X(this._container,{color:this._color||"red",thickness:1,zIndex:void 0!==e.zIndex?e.zIndex+1:void 0,onMouseOver:n,onMouseLeave:i,onMouseWheel:r,onContextMenu:a}),this._angleLabel=new J(this._container,{fillColor:this._color||"#00BBFF",prefix:"",text:"",zIndex:e.zIndex+2,onMouseOver:n,onMouseLeave:i,onMouseWheel:r,onContextMenu:a}),this._wpDirty=!1,this._vpDirty=!1,this._cpDirty=!1,this._visible=!1,this._originVisible=!1,this._cornerVisible=!1,this._targetVisible=!1,this._originWireVisible=!1,this._targetWireVisible=!1,this._angleVisible=!1,this._labelsVisible=!1,this._clickable=!1,this._originMarker.on("worldPos",(e=>{this._originWorld.set(e||[0,0,0]),this._wpDirty=!0,this._needUpdate(0)})),this._cornerMarker.on("worldPos",(e=>{this._cornerWorld.set(e||[0,0,0]),this._wpDirty=!0,this._needUpdate(0)})),this._targetMarker.on("worldPos",(e=>{this._targetWorld.set(e||[0,0,0]),this._wpDirty=!0,this._needUpdate(0)})),this._onViewMatrix=s.camera.on("viewMatrix",(()=>{this._vpDirty=!0,this._needUpdate(0)})),this._onProjMatrix=s.camera.on("projMatrix",(()=>{this._cpDirty=!0,this._needUpdate()})),this._onCanvasBoundary=s.canvas.on("boundary",(()=>{this._cpDirty=!0,this._needUpdate(0)})),this.approximate=t.approximate,this.visible=t.visible,this.originVisible=t.originVisible,this.cornerVisible=t.cornerVisible,this.targetVisible=t.targetVisible,this.originWireVisible=t.originWireVisible,this.targetWireVisible=t.targetWireVisible,this.angleVisible=t.angleVisible,this.labelsVisible=t.labelsVisible}_update(){if(!this._visible)return;const e=this.plugin.viewer.scene;if(this._wpDirty&&(this._wp[0]=this._originWorld[0],this._wp[1]=this._originWorld[1],this._wp[2]=this._originWorld[2],this._wp[3]=1,this._wp[4]=this._cornerWorld[0],this._wp[5]=this._cornerWorld[1],this._wp[6]=this._cornerWorld[2],this._wp[7]=1,this._wp[8]=this._targetWorld[0],this._wp[9]=this._targetWorld[1],this._wp[10]=this._targetWorld[2],this._wp[11]=1,this._wpDirty=!1,this._vpDirty=!0),this._vpDirty&&(h.transformPositions4(e.camera.viewMatrix,this._wp,this._vp),this._vp[3]=1,this._vp[7]=1,this._vp[11]=1,this._vpDirty=!1,this._cpDirty=!0),this._cpDirty){const A=-.3,d=this._originMarker.viewPos[2],f=this._cornerMarker.viewPos[2],I=this._targetMarker.viewPos[2];if(d>A||f>A||I>A)return this._originDot.setVisible(!1),this._cornerDot.setVisible(!1),this._targetDot.setVisible(!1),this._originWire.setVisible(!1),this._targetWire.setVisible(!1),void this._angleLabel.setCulled(!0);h.transformPositions4(e.camera.project.matrix,this._vp,this._pp);var t=this._pp,s=this._cp,n=e.canvas.canvas.getBoundingClientRect();const y=this._container.getBoundingClientRect();for(var i=n.top-y.top,a=n.left-y.left,r=e.canvas.boundary,l=r[2],o=r[3],c=0,u=0,p=t.length;u<p;u+=4)s[c]=a+Math.floor((1+t[u+0]/t[u+3])*l/2),s[c+1]=i+Math.floor((1-t[u+1]/t[u+3])*o/2),c+=2;if(this._originDot.setPos(s[0],s[1]),this._cornerDot.setPos(s[2],s[3]),this._targetDot.setPos(s[4],s[5]),this._originWire.setStartAndEnd(s[0],s[1],s[2],s[3]),this._targetWire.setStartAndEnd(s[2],s[3],s[4],s[5]),this._angleLabel.setPosBetweenWires(s[0],s[1],s[2],s[3],s[4],s[5]),h.subVec3(this._originWorld,this._cornerWorld,Z),h.subVec3(this._targetWorld,this._cornerWorld,$),!(0===Z[0]&&0===Z[1]&&0===Z[2]||0===$[0]&&0===$[1]&&0===$[2])){const e=this._approximate?" ~ ":" = ";h.normalizeVec3(Z),h.normalizeVec3($);const t=Math.abs(h.angleVec3(Z,$));this._angle=t/h.DEGTORAD,this._angleLabel.setText(e+this._angle.toFixed(2)+"°")}else this._angleLabel.setText("");this._originDot.setVisible(this._visible&&this._originVisible),this._cornerDot.setVisible(this._visible&&this._cornerVisible),this._targetDot.setVisible(this._visible&&this._targetVisible),this._originWire.setVisible(this._visible&&this._originWireVisible),this._targetWire.setVisible(this._visible&&this._targetWireVisible),this._angleLabel.setCulled(!(this._visible&&this._angleVisible&&this.labelsVisible)),this._cpDirty=!1}}set approximate(e){e=!1!==e,this._approximate!==e&&(this._approximate=e,this._cpDirty=!0,this._needUpdate(0))}get approximate(){return this._approximate}get origin(){return this._originMarker}get corner(){return this._cornerMarker}get target(){return this._targetMarker}get angle(){return this._update(),this._angle}get color(){return this._color}set color(e){this._originDot.setFillColor(e),this._cornerDot.setFillColor(e),this._targetDot.setFillColor(e),this._originWire.setColor(e||"blue"),this._targetWire.setColor(e||"red"),this._angleLabel.setFillColor(e||"#00BBFF"),this._color=e}set visible(e){e=!1!==e,this._visible=e,this._originDot.setVisible(this._visible&&this._originVisible),this._cornerDot.setVisible(this._visible&&this._cornerVisible),this._targetDot.setVisible(this._visible&&this._targetVisible),this._originWire.setVisible(this._visible&&this._originWireVisible),this._targetWire.setVisible(this._visible&&this._targetWireVisible),this._angleLabel.setVisible(this._visible&&this._angleVisible),this._cpDirty=!0,this._needUpdate()}get visible(){return this._visible}set originVisible(e){e=!1!==e,this._originVisible=e,this._originDot.setVisible(this._visible&&this._originVisible),this._cpDirty=!0,this._needUpdate()}get originVisible(){return this._originVisible}set cornerVisible(e){e=!1!==e,this._cornerVisible=e,this._cornerDot.setVisible(this._visible&&this._cornerVisible),this._cpDirty=!0,this._needUpdate()}get cornerVisible(){return this._cornerVisible}set targetVisible(e){e=!1!==e,this._targetVisible=e,this._targetDot.setVisible(this._visible&&this._targetVisible),this._cpDirty=!0,this._needUpdate()}get targetVisible(){return this._targetVisible}set originWireVisible(e){e=!1!==e,this._originWireVisible=e,this._originWire.setVisible(this._visible&&this._originWireVisible),this._cpDirty=!0,this._needUpdate()}get originWireVisible(){return this._originWireVisible}set targetWireVisible(e){e=!1!==e,this._targetWireVisible=e,this._targetWire.setVisible(this._visible&&this._targetWireVisible),this._cpDirty=!0,this._needUpdate()}get targetWireVisible(){return this._targetWireVisible}set angleVisible(e){e=!1!==e,this._angleVisible=e,this._angleLabel.setVisible(this._visible&&this._angleVisible),this._cpDirty=!0,this._needUpdate()}get angleVisible(){return this._angleVisible}set labelsVisible(e){e=void 0!==e?Boolean(e):this.plugin.defaultLabelsVisible,this._labelsVisible=e;var t=this._visible&&this._labelsVisible;this._angleLabel.setVisible(t),this._cpDirty=!0,this._needUpdate()}get labelsVisible(){return this._labelsVisible}setHighlighted(e){this._originDot.setHighlighted(e),this._cornerDot.setHighlighted(e),this._targetDot.setHighlighted(e),this._originWire.setHighlighted(e),this._targetWire.setHighlighted(e),this._angleLabel.setHighlighted(e)}set clickable(e){e=!!e,this._clickable=e,this._originDot.setClickable(this._clickable),this._cornerDot.setClickable(this._clickable),this._targetDot.setClickable(this._clickable),this._originWire.setClickable(this._clickable),this._targetWire.setClickable(this._clickable),this._angleLabel.setClickable(this._clickable)}get clickable(){return this._clickable}destroy(){const e=this.plugin.viewer.scene;this._onViewMatrix&&e.camera.off(this._onViewMatrix),this._onProjMatrix&&e.camera.off(this._onProjMatrix),this._onCanvasBoundary&&e.canvas.off(this._onCanvasBoundary),this._originDot.destroy(),this._cornerDot.destroy(),this._targetDot.destroy(),this._originWire.destroy(),this._targetWire.destroy(),this._angleLabel.destroy(),super.destroy()}}class te extends C{constructor(e){super(e.viewer.scene),this.plugin=e,this._active=!1,this._state=0,this._currentAngleMeasurement=null;const t=document.createElement("div"),s=this.scene.canvas.canvas;s.parentNode.insertBefore(t,s),t.style.background="black",t.style.border="2px solid blue",t.style.borderRadius="10px",t.style.width="5px",t.style.height="5px",t.style.margin="-200px -200px",t.style.zIndex="100",t.style.position="absolute",t.style.pointerEvents="none",this.markerDiv=t,this._onMouseHoverSurface=null,this._onHoverNothing=null,this._onMouseHoverOff=null,this._onPickedNothing=null,this._onPickedSurface=null,this._onInputMouseDown=null,this._onInputMouseUp=null,this._onCanvasTouchStart=null,this._onCanvasTouchEnd=null}get active(){return this._active}activate(){if(this._active)return;const e=this.plugin,t=this.scene,s=e.viewer.cameraControl,n=t.canvas.canvas,i=t.input;let a=!1,r=null,l=h.vec3();const o=h.vec2();let c,u;const p=h.vec2(),A=h.vec2();h.vec3(),this._onMouseHoverSurface=s.on("hoverSnapOrSurface",(e=>{switch(a=!0,r=e.entity,l.set(e.worldPos),o.set(e.canvasPos),this._state){case 0:this.markerDiv.style.marginLeft=e.canvasPos[0]-5+"px",this.markerDiv.style.marginTop=e.canvasPos[1]-5+"px",this.markerDiv.style.background="pink",this.markerDiv.style.border="2px solid red";break;case 1:this._currentAngleMeasurement&&(this._currentAngleMeasurement.originWireVisible=!0,this._currentAngleMeasurement.targetWireVisible=!1,this._currentAngleMeasurement.cornerVisible=!0,this._currentAngleMeasurement.angleVisible=!1,this._currentAngleMeasurement.corner.worldPos=e.worldPos),this.markerDiv.style.marginLeft="-10000px",this.markerDiv.style.marginTop="-10000px",n.style.cursor="pointer";break;case 2:this._currentAngleMeasurement&&(this._currentAngleMeasurement.targetWireVisible=!0,this._currentAngleMeasurement.targetVisible=!0,this._currentAngleMeasurement.angleVisible=!0,this._currentAngleMeasurement.target.worldPos=e.worldPos),this.markerDiv.style.marginLeft="-10000px",this.markerDiv.style.marginTop="-10000px",n.style.cursor="pointer"}})),this._onInputMouseDown=i.on("mousedown",(e=>{c=e[0],u=e[1]})),this._onInputMouseUp=i.on("mouseup",(e=>{if(!(e[0]>c+5||e[0]<c-5||e[1]>u+5||e[1]<u-5))switch(this._state){case 0:a&&(this._currentAngleMeasurement=this.plugin.createMeasurement({id:h.createUUID(),origin:{entity:r,worldPos:l},corner:{entity:r,worldPos:l},target:{entity:r,worldPos:l},approximate:!0}),this._currentAngleMeasurement.originVisible=!0,this._currentAngleMeasurement.originWireVisible=!0,this._currentAngleMeasurement.cornerVisible=!1,this._currentAngleMeasurement.targetWireVisible=!1,this._currentAngleMeasurement.targetVisible=!1,this._currentAngleMeasurement.angleVisible=!1,this._state=1,this.fire("measurementStart",this._currentAngleMeasurement));break;case 1:a?(this._currentAngleMeasurement.targetWireVisible=!1,this._currentAngleMeasurement.targetVisible=!0,this._currentAngleMeasurement.angleVisible=!0,this._state=2):this._currentAngleMeasurement&&(this._currentAngleMeasurement.destroy(),this._currentAngleMeasurement=null,this._state=0,this.fire("measurementCancel",this._currentAngleMeasurement));break;case 2:a?(this._currentAngleMeasurement.targetVisible=!0,this._currentAngleMeasurement.angleVisible=!0,this.fire("measurementEnd",this._currentAngleMeasurement),this._currentAngleMeasurement=null,this._state=0):this._currentAngleMeasurement&&(this._currentAngleMeasurement.destroy(),this._currentAngleMeasurement=null,this._state=0,this.fire("measurementCancel",this._currentAngleMeasurement))}})),this._onMouseHoverOff=s.on("hoverSnapOrSurfaceOff",(e=>{if(a=!1,this.markerDiv.style.marginLeft="-100px",this.markerDiv.style.marginTop="-100px",this._currentAngleMeasurement){switch(this._state){case 0:this._currentAngleMeasurement.originVisible=!1;break;case 1:this._currentAngleMeasurement.cornerVisible=!1,this._currentAngleMeasurement.originWireVisible=!1,this._currentAngleMeasurement.targetVisible=!1,this._currentAngleMeasurement.targetWireVisible=!1,this._currentAngleMeasurement.angleVisible=!1;break;case 2:this._currentAngleMeasurement.targetVisible=!1,this._currentAngleMeasurement.targetWireVisible=!1,this._currentAngleMeasurement.angleVisible=!1}n.style.cursor="default"}})),n.addEventListener("touchstart",this._onCanvasTouchStart=e=>{const t=e.touches,s=e.changedTouches;1===t.length&&1===s.length&&se(t[0],p)},{passive:!0}),n.addEventListener("touchend",this._onCanvasTouchEnd=e=>{const s=e.touches,n=e.changedTouches;if(0===s.length&&1===n.length){if(se(n[0],A),A[0]>p[0]+5||A[0]<p[0]-5||A[1]>p[1]+5||A[1]<p[1]-5)return;const e=t.pick({canvasPos:A,pickSurface:!0});if(e&&e.worldPos)switch(this._state){case 0:this._currentAngleMeasurement=this.plugin.createMeasurement({id:h.createUUID(),origin:{entity:e.entity,worldPos:e.worldPos},corner:{entity:e.entity,worldPos:e.worldPos},target:{entity:e.entity,worldPos:e.worldPos},approximate:!0}),this._currentAngleMeasurement.originVisible=!0,this._currentAngleMeasurement.originWireVisible=!0,this._currentAngleMeasurement.cornerVisible=!1,this._currentAngleMeasurement.targetWireVisible=!1,this._currentAngleMeasurement.targetVisible=!1,this._currentAngleMeasurement.angleVisible=!1,this._currentAngleMeasurement.clickable=!1,this._state=1,this.fire("measurementStart",this._currentAngleMeasurement);break;case 1:this._currentAngleMeasurement.corner.worldPos=e.worldPos,this._currentAngleMeasurement.targetWireVisible=!1,this._currentAngleMeasurement.targetVisible=!0,this._currentAngleMeasurement.angleVisible=!0,this._state=2;break;case 2:this._currentAngleMeasurement.target.worldPos=e.worldPos,this._currentAngleMeasurement.targetVisible=!0,this._currentAngleMeasurement.angleVisible=!0,this._currentAngleMeasurement.clickable=!0,this.fire("measurementEnd",this._currentAngleMeasurement),this._currentAngleMeasurement=null,this._state=0}else this._currentAngleMeasurement&&(this._currentAngleMeasurement.destroy(),this._currentAngleMeasurement=null,this._state=0,this.fire("measurementCancel",this._currentAngleMeasurement))}},{passive:!0}),this._active=!0}deactivate(){if(!this._active)return;this.reset();const e=this.plugin.viewer.scene.input,t=this.plugin.viewer.cameraControl,s=this.plugin.viewer.scene.canvas.canvas;e.off(this._onInputMouseDown),e.off(this._onInputMouseUp),t.off(this._onMouseHoverSurface),t.off(this._onPickedSurface),t.off(this._onHoverNothing),t.off(this._onPickedNothing),s.removeEventListener("touchstart",this._onCanvasTouchStart),s.removeEventListener("touchend",this._onCanvasTouchEnd),this._currentAngleMeasurement=null,this._active=!1}reset(){this._active&&(this._currentAngleMeasurement&&(this._currentAngleMeasurement.destroy(),this._currentAngleMeasurement=null),this._state=0)}destroy(){this.deactivate(),super.destroy()}}const se=function(e,t){if(e){let s=e.target,n=0,i=0;for(;s.offsetParent;)n+=s.offsetLeft,i+=s.offsetTop,s=s.offsetParent;t[0]=e.pageX-n,t[1]=e.pageY-i}else e=window.event,t[0]=e.x,t[1]=e.y;return t};class ne extends Y{constructor(e,t){if(super(e,t),this.plugin=t.plugin,this._container=t.container,!this._container)throw"config missing: container";if(!t.markerElement&&!t.markerHTML)throw"config missing: need either markerElement or markerHTML";if(!t.labelElement&&!t.labelHTML)throw"config missing: need either labelElement or labelHTML";this._htmlDirty=!1,t.markerElement?(this._marker=t.markerElement,this._marker.addEventListener("click",this._onMouseClickedExternalMarker=()=>{this.plugin.fire("markerClicked",this)}),this._marker.addEventListener("mouseenter",this._onMouseEnterExternalMarker=()=>{this.plugin.fire("markerMouseEnter",this)}),this._marker.addEventListener("mouseleave",this._onMouseLeaveExternalMarker=()=>{this.plugin.fire("markerMouseLeave",this)}),this._markerExternal=!0):(this._markerHTML=t.markerHTML,this._htmlDirty=!0,this._markerExternal=!1),t.labelElement?(this._label=t.labelElement,this._labelExternal=!0):(this._labelHTML=t.labelHTML,this._htmlDirty=!0,this._labelExternal=!1),this._markerShown=!!t.markerShown,this._labelShown=!!t.labelShown,this._values=t.values||{},this._layoutDirty=!0,this._visibilityDirty=!0,this._buildHTML(),this._onTick=this.scene.on("tick",(()=>{this._htmlDirty&&(this._buildHTML(),this._htmlDirty=!1,this._layoutDirty=!0,this._visibilityDirty=!0),(this._layoutDirty||this._visibilityDirty)&&(this._markerShown||this._labelShown)&&(this._updatePosition(),this._layoutDirty=!1),this._visibilityDirty&&(this._marker.style.visibility=this.visible&&this._markerShown?"visible":"hidden",this._label.style.visibility=this.visible&&this._markerShown&&this._labelShown?"visible":"hidden",this._visibilityDirty=!1)})),this.on("canvasPos",(()=>{this._layoutDirty=!0})),this.on("visible",(()=>{this._visibilityDirty=!0})),this.setMarkerShown(!1!==t.markerShown),this.setLabelShown(t.labelShown),this.eye=t.eye?t.eye.slice():null,this.look=t.look?t.look.slice():null,this.up=t.up?t.up.slice():null,this.projection=t.projection}_buildHTML(){if(!this._markerExternal){this._marker&&(this._container.removeChild(this._marker),this._marker=null);let e=this._markerHTML||"<p></p>";y.isArray(e)&&(e=e.join("")),e=this._renderTemplate(e);const t=document.createRange().createContextualFragment(e);this._marker=t.firstChild,this._container.appendChild(this._marker),this._marker.style.visibility=this._markerShown?"visible":"hidden",this._marker.addEventListener("click",(()=>{this.plugin.fire("markerClicked",this)})),this._marker.addEventListener("mouseenter",(()=>{this.plugin.fire("markerMouseEnter",this)})),this._marker.addEventListener("mouseleave",(()=>{this.plugin.fire("markerMouseLeave",this)})),this._marker.addEventListener("wheel",(e=>{this.plugin.viewer.scene.canvas.canvas.dispatchEvent(new WheelEvent("wheel",e))}))}if(!this._labelExternal){this._label&&(this._container.removeChild(this._label),this._label=null);let e=this._labelHTML||"<p></p>";y.isArray(e)&&(e=e.join("")),e=this._renderTemplate(e);const t=document.createRange().createContextualFragment(e);this._label=t.firstChild,this._container.appendChild(this._label),this._label.style.visibility=this._markerShown&&this._labelShown?"visible":"hidden",this._label.addEventListener("wheel",(e=>{this.plugin.viewer.scene.canvas.canvas.dispatchEvent(new WheelEvent("wheel",e))}))}}_updatePosition(){const e=this.scene.canvas.boundary,t=e[0],s=e[1],n=this.canvasPos;this._marker.style.left=Math.floor(t+n[0])-12+"px",this._marker.style.top=Math.floor(s+n[1])-12+"px",this._marker.style["z-index"]=90005+Math.floor(this._viewPos[2])+1;this._label.style.left=20+Math.floor(t+n[0]+20)+"px",this._label.style.top=Math.floor(s+n[1]+-17)+"px",this._label.style["z-index"]=90005+Math.floor(this._viewPos[2])+1}_renderTemplate(e){for(var t in this._values)if(this._values.hasOwnProperty(t)){const s=this._values[t];e=e.replace(new RegExp("{{"+t+"}}","g"),s)}return e}setMarkerShown(e){e=!!e,this._markerShown!==e&&(this._markerShown=e,this._visibilityDirty=!0)}getMarkerShown(){return this._markerShown}setLabelShown(e){e=!!e,this._labelShown!==e&&(this._labelShown=e,this._visibilityDirty=!0)}getLabelShown(){return this._labelShown}setField(e,t){this._values[e]=t||"",this._htmlDirty=!0}getField(e){return this._values[e]}setValues(e){for(var t in e)if(e.hasOwnProperty(t)){const s=e[t];this.setField(t,s)}}getValues(){return this._values}destroy(){this._marker&&(this._markerExternal?(this._marker.removeEventListener("click",this._onMouseClickedExternalMarker),this._marker.removeEventListener("mouseenter",this._onMouseEnterExternalMarker),this._marker.removeEventListener("mouseleave",this._onMouseLeaveExternalMarker),this._marker=null):this._marker.parentNode.removeChild(this._marker)),this._label&&(this._labelExternal||this._label.parentNode.removeChild(this._label),this._label=null),this.scene.off(this._onTick),super.destroy()}}const ie=h.vec3(),ae=h.vec3(),re=h.vec3();class le extends C{get type(){return"Spinner"}constructor(e,t={}){super(e,t),this._canvas=t.canvas,this._element=null,this._isCustom=!1,t.elementId&&(this._element=document.getElementById(t.elementId),this._element?this._adjustPosition():this.error("Can't find given Spinner HTML element: '"+t.elementId+"' - will automatically create default element")),this._element||this._createDefaultSpinner(),this.processes=0}_createDefaultSpinner(){this._injectDefaultCSS();const e=document.createElement("div"),t=e.style;t["z-index"]="9000",t.position="absolute",e.innerHTML='<div class="sk-fading-circle"> <div class="sk-circle1 sk-circle"></div> <div class="sk-circle2 sk-circle"></div> <div class="sk-circle3 sk-circle"></div> <div class="sk-circle4 sk-circle"></div> <div class="sk-circle5 sk-circle"></div> <div class="sk-circle6 sk-circle"></div> <div class="sk-circle7 sk-circle"></div> <div class="sk-circle8 sk-circle"></div> <div class="sk-circle9 sk-circle"></div> <div class="sk-circle10 sk-circle"></div> <div class="sk-circle11 sk-circle"></div> <div class="sk-circle12 sk-circle"></div> </div>',this._canvas.parentElement.appendChild(e),this._element=e,this._isCustom=!1,this._adjustPosition()}_injectDefaultCSS(){const e="xeokit-spinner-css";if(document.getElementById(e))return;const t=document.createElement("style");t.innerHTML=".sk-fading-circle { background: transparent; margin: 20px auto; width: 50px; height:50px; position: relative; } .sk-fading-circle .sk-circle { width: 120%; height: 120%; position: absolute; left: 0; top: 0; } .sk-fading-circle .sk-circle:before { content: ''; display: block; margin: 0 auto; width: 15%; height: 15%; background-color: #ff8800; border-radius: 100%; -webkit-animation: sk-circleFadeDelay 1.2s infinite ease-in-out both; animation: sk-circleFadeDelay 1.2s infinite ease-in-out both; } .sk-fading-circle .sk-circle2 { -webkit-transform: rotate(30deg); -ms-transform: rotate(30deg); transform: rotate(30deg); } .sk-fading-circle .sk-circle3 { -webkit-transform: rotate(60deg); -ms-transform: rotate(60deg); transform: rotate(60deg); } .sk-fading-circle .sk-circle4 { -webkit-transform: rotate(90deg); -ms-transform: rotate(90deg); transform: rotate(90deg); } .sk-fading-circle .sk-circle5 { -webkit-transform: rotate(120deg); -ms-transform: rotate(120deg); transform: rotate(120deg); } .sk-fading-circle .sk-circle6 { -webkit-transform: rotate(150deg); -ms-transform: rotate(150deg); transform: rotate(150deg); } .sk-fading-circle .sk-circle7 { -webkit-transform: rotate(180deg); -ms-transform: rotate(180deg); transform: rotate(180deg); } .sk-fading-circle .sk-circle8 { -webkit-transform: rotate(210deg); -ms-transform: rotate(210deg); transform: rotate(210deg); } .sk-fading-circle .sk-circle9 { -webkit-transform: rotate(240deg); -ms-transform: rotate(240deg); transform: rotate(240deg); } .sk-fading-circle .sk-circle10 { -webkit-transform: rotate(270deg); -ms-transform: rotate(270deg); transform: rotate(270deg); } .sk-fading-circle .sk-circle11 { -webkit-transform: rotate(300deg); -ms-transform: rotate(300deg); transform: rotate(300deg); } .sk-fading-circle .sk-circle12 { -webkit-transform: rotate(330deg); -ms-transform: rotate(330deg); transform: rotate(330deg); } .sk-fading-circle .sk-circle2:before { -webkit-animation-delay: -1.1s; animation-delay: -1.1s; } .sk-fading-circle .sk-circle3:before { -webkit-animation-delay: -1s; animation-delay: -1s; } .sk-fading-circle .sk-circle4:before { -webkit-animation-delay: -0.9s; animation-delay: -0.9s; } .sk-fading-circle .sk-circle5:before { -webkit-animation-delay: -0.8s; animation-delay: -0.8s; } .sk-fading-circle .sk-circle6:before { -webkit-animation-delay: -0.7s; animation-delay: -0.7s; } .sk-fading-circle .sk-circle7:before { -webkit-animation-delay: -0.6s; animation-delay: -0.6s; } .sk-fading-circle .sk-circle8:before { -webkit-animation-delay: -0.5s; animation-delay: -0.5s; } .sk-fading-circle .sk-circle9:before { -webkit-animation-delay: -0.4s; animation-delay: -0.4s; } .sk-fading-circle .sk-circle10:before { -webkit-animation-delay: -0.3s; animation-delay: -0.3s; } .sk-fading-circle .sk-circle11:before { -webkit-animation-delay: -0.2s; animation-delay: -0.2s; } .sk-fading-circle .sk-circle12:before { -webkit-animation-delay: -0.1s; animation-delay: -0.1s; } @-webkit-keyframes sk-circleFadeDelay { 0%, 39%, 100% { opacity: 0; } 40% { opacity: 1; } } @keyframes sk-circleFadeDelay { 0%, 39%, 100% { opacity: 0; } 40% { opacity: 1; } }",t.id=e,document.body.appendChild(t)}_adjustPosition(){if(this._isCustom)return;const e=this._canvas,t=this._element,s=t.style;s.left=e.offsetLeft+.5*e.clientWidth-.5*t.clientWidth+"px",s.top=e.offsetTop+.5*e.clientHeight-.5*t.clientHeight+"px"}set processes(e){if(e=e||0,this._processes===e)return;if(e<0)return;const t=this._processes;this._processes=e;const s=this._element;s&&(s.style.visibility=this._processes>0?"visible":"hidden"),this.fire("processes",this._processes),0===this._processes&&this._processes!==t&&this.fire("zeroProcesses",this._processes)}get processes(){return this._processes}_destroy(){this._element&&!this._isCustom&&(this._element.parentNode.removeChild(this._element),this._element=null);const e=document.getElementById("xeokit-spinner-css");e&&e.parentNode.removeChild(e)}}const oe={WEBGL:!1,SUPPORTED_EXTENSIONS:{}},ce=document.createElement("canvas");if(ce){const e=ce.getContext("webgl",{antialias:!0})||ce.getContext("experimental-webgl",{antialias:!0});oe.WEBGL=!!e,oe.WEBGL&&(oe.ANTIALIAS=e.getContextAttributes().antialias,e.getShaderPrecisionFormat?e.getShaderPrecisionFormat(e.FRAGMENT_SHADER,e.HIGH_FLOAT).precision>0?oe.FS_MAX_FLOAT_PRECISION="highp":e.getShaderPrecisionFormat(e.FRAGMENT_SHADER,e.MEDIUM_FLOAT).precision>0?oe.FS_MAX_FLOAT_PRECISION="mediump":oe.FS_MAX_FLOAT_PRECISION="lowp":oe.FS_MAX_FLOAT_PRECISION="mediump",oe.DEPTH_BUFFER_BITS=e.getParameter(e.DEPTH_BITS),oe.MAX_TEXTURE_SIZE=e.getParameter(e.MAX_TEXTURE_SIZE),oe.MAX_CUBE_MAP_SIZE=e.getParameter(e.MAX_CUBE_MAP_TEXTURE_SIZE),oe.MAX_RENDERBUFFER_SIZE=e.getParameter(e.MAX_RENDERBUFFER_SIZE),oe.MAX_TEXTURE_UNITS=e.getParameter(e.MAX_COMBINED_TEXTURE_IMAGE_UNITS),oe.MAX_TEXTURE_IMAGE_UNITS=e.getParameter(e.MAX_TEXTURE_IMAGE_UNITS),oe.MAX_VERTEX_ATTRIBS=e.getParameter(e.MAX_VERTEX_ATTRIBS),oe.MAX_VERTEX_UNIFORM_VECTORS=e.getParameter(e.MAX_VERTEX_UNIFORM_VECTORS),oe.MAX_FRAGMENT_UNIFORM_VECTORS=e.getParameter(e.MAX_FRAGMENT_UNIFORM_VECTORS),oe.MAX_VARYING_VECTORS=e.getParameter(e.MAX_VARYING_VECTORS),e.getSupportedExtensions().forEach((function(e){oe.SUPPORTED_EXTENSIONS[e]=!0})))}const ue=["webgl2","experimental-webgl","webkit-3d","moz-webgl","moz-glweb20"];class he extends C{constructor(e,t={}){super(e,t),this._backgroundColor=h.vec3([t.backgroundColor?t.backgroundColor[0]:1,t.backgroundColor?t.backgroundColor[1]:1,t.backgroundColor?t.backgroundColor[2]:1]),this._backgroundColorFromAmbientLight=!!t.backgroundColorFromAmbientLight,this.canvas=t.canvas,this.gl=null,this.webgl2=!1,this.transparent=!!t.transparent,this.contextAttr=t.contextAttr||{},this.contextAttr.alpha=this.transparent,this.contextAttr.preserveDrawingBuffer=!!this.contextAttr.preserveDrawingBuffer,this.contextAttr.stencil=!1,this.contextAttr.premultipliedAlpha=!!this.contextAttr.premultipliedAlpha,this.contextAttr.antialias=!1!==this.contextAttr.antialias,this.resolutionScale=t.resolutionScale,this.canvas.width=Math.round(this.canvas.clientWidth*this._resolutionScale),this.canvas.height=Math.round(this.canvas.clientHeight*this._resolutionScale),this.boundary=[this.canvas.offsetLeft,this.canvas.offsetTop,this.canvas.clientWidth,this.canvas.clientHeight],this._initWebGL(t);const s=this;this.canvas.addEventListener("webglcontextlost",this._webglcontextlostListener=function(e){console.time("webglcontextrestored"),s.scene._webglContextLost(),s.fire("webglcontextlost"),e.preventDefault()},!1),this.canvas.addEventListener("webglcontextrestored",this._webglcontextrestoredListener=function(e){s._initWebGL(),s.gl&&(s.scene._webglContextRestored(s.gl),s.fire("webglcontextrestored",s.gl),e.preventDefault()),console.timeEnd("webglcontextrestored")},!1);let n=!0;new ResizeObserver((e=>{for(const t of e)t.contentBoxSize&&(n=!0)})).observe(this.canvas),this._tick=this.scene.on("tick",(()=>{n&&(n=!1,s.canvas.width=Math.round(s.canvas.clientWidth*s._resolutionScale),s.canvas.height=Math.round(s.canvas.clientHeight*s._resolutionScale),s.boundary[0]=s.canvas.offsetLeft,s.boundary[1]=s.canvas.offsetTop,s.boundary[2]=s.canvas.clientWidth,s.boundary[3]=s.canvas.clientHeight,s.fire("boundary",s.boundary))})),this._spinner=new le(this.scene,{canvas:this.canvas,elementId:t.spinnerElementId})}get type(){return"Canvas"}get backgroundColorFromAmbientLight(){return this._backgroundColorFromAmbientLight}set backgroundColorFromAmbientLight(e){this._backgroundColorFromAmbientLight=!1!==e,this.glRedraw()}get backgroundColor(){return this._backgroundColor}set backgroundColor(e){e?(this._backgroundColor[0]=e[0],this._backgroundColor[1]=e[1],this._backgroundColor[2]=e[2]):(this._backgroundColor[0]=1,this._backgroundColor[1]=1,this._backgroundColor[2]=1),this.glRedraw()}get resolutionScale(){return this._resolutionScale}set resolutionScale(e){if((e=e||1)===this._resolutionScale)return;this._resolutionScale=e;const t=this.canvas;t.width=Math.round(t.clientWidth*this._resolutionScale),t.height=Math.round(t.clientHeight*this._resolutionScale),this.glRedraw()}get spinner(){return this._spinner}_createCanvas(){const e="xeokit-canvas-"+h.createUUID(),t=document.getElementsByTagName("body")[0],s=document.createElement("div"),n=s.style;n.height="100%",n.width="100%",n.padding="0",n.margin="0",n.background="rgba(0,0,0,0);",n.float="left",n.left="0",n.top="0",n.position="absolute",n.opacity="1.0",n["z-index"]="-10000",s.innerHTML+='<canvas id="'+e+'" style="width: 100%; height: 100%; float: left; margin: 0; padding: 0;"></canvas>',t.appendChild(s),this.canvas=document.getElementById(e)}_getElementXY(e){let t=0,s=0;for(;e;)t+=e.offsetLeft-e.scrollLeft,s+=e.offsetTop-e.scrollTop,e=e.offsetParent;return{x:t,y:s}}_initWebGL(){if(!this.gl)for(let e=0;!this.gl&&e<ue.length;e++)try{this.gl=this.canvas.getContext(ue[e],this.contextAttr)}catch(e){}this.gl||(this.error("Failed to get a WebGL context"),this.fire("webglContextFailed",!0,!0));{const e=this.gl;let t="__",s=e.activeTexture;e.activeTexture=function(e){t!==e&&(t=e,s.call(this,e))};let n={},i=e.bindTexture;e.bindTexture=function(e,s){n[t]!==s&&(n[t]=s,i.call(this,e,s))}}this.gl&&this.webgl2&&(this.gl.hint(this.gl.FRAGMENT_SHADER_DERIVATIVE_HINT,this.gl.FASTEST),this.gl,WebGL2RenderingContext)}getSnapshot(e){throw"Canvas#getSnapshot() has been replaced by Viewer#getSnapshot() - use that method instead."}readPixels(e,t,s,n){return this.scene._renderer.readPixels(e,t,s,n)}loseWebGLContext(){this.canvas.loseContext&&this.canvas.loseContext()}destroy(){this.scene.off(this._tick),this._spinner._destroy(),this.canvas.removeEventListener("webglcontextlost",this._webglcontextlostListener),this.canvas.removeEventListener("webglcontextrestored",this._webglcontextrestoredListener),this.gl=null,super.destroy()}}class pe{constructor(e){this._scene=e,this._matPool=[],this._matPoolNextFreeIndex=0,this._rtcViewMats={},this._rtcPickViewMats={},this.reset()}reset(){this._matPoolNextFreeIndex=0,this._rtcViewMats={},this._rtcPickViewMats={},this.gl=this._scene.canvas.gl,this.lastProgramId=null,this.pbrEnabled=!1,this.colorTextureEnabled=!1,this.withSAO=!1,this.backfaces=!1,this.frontface=!0,this.textureUnit=0,this.drawElements=0,this.drawArrays=0,this.useProgram=0,this.bindTexture=0,this.bindArray=0,this.pass=0,this.shadowViewMatrix=null,this.shadowProjMatrix=null,this.pickViewMatrix=null,this.pickProjMatrix=null,this.pickZNear=.01,this.pickZFar=5e3,this.pickInvisible=!1,this.pickElementsCount=null,this.pickElementsOffset=null,this.lineWidth=1,this.snapPickLayerParams={},this.snapPickLayerNumber=0,this.snapPickCoordinateScale=h.vec3(),this.snapPickOrigin=h.vec3()}getRTCViewMatrix(e,t){let s=this._rtcViewMats[e];return s||(s=this._getNewMat(),B(this._scene.camera.viewMatrix,t,s),this._rtcViewMats[e]=s),s}getRTCPickViewMatrix(e,t){let s=this._rtcPickViewMats[e];if(!s){s=this._getNewMat();const n=this.pickViewMatrix||this._scene.camera.viewMatrix;B(n,t,s),this._rtcPickViewMats[e]=s}return s}_getNewMat(){let e=this._matPool[this._matPoolNextFreeIndex];return e||(e=h.mat4(),this._matPool[this._matPoolNextFreeIndex]=e),this._matPoolNextFreeIndex++,e}}class Ae{constructor(){this.entity=null,this.primitive=null,this.primIndex=-1,this.pickSurfacePrecision=!1,this.touchInput=!1,this._canvasPos=new Int16Array([0,0]),this._origin=new Float64Array([0,0,0]),this._direction=new Float64Array([0,0,0]),this._indices=new Int32Array(3),this._localPos=new Float64Array([0,0,0]),this._worldPos=new Float64Array([0,0,0]),this._viewPos=new Float64Array([0,0,0]),this._bary=new Float64Array([0,0,0]),this._worldNormal=new Float64Array([0,0,0]),this._uv=new Float64Array([0,0]),this.reset()}get canvasPos(){return this._gotCanvasPos?this._canvasPos:null}set canvasPos(e){e?(this._canvasPos[0]=e[0],this._canvasPos[1]=e[1],this._gotCanvasPos=!0):this._gotCanvasPos=!1}get origin(){return this._gotOrigin?this._origin:null}set origin(e){e?(this._origin[0]=e[0],this._origin[1]=e[1],this._origin[2]=e[2],this._gotOrigin=!0):this._gotOrigin=!1}get direction(){return this._gotDirection?this._direction:null}set direction(e){e?(this._direction[0]=e[0],this._direction[1]=e[1],this._direction[2]=e[2],this._gotDirection=!0):this._gotDirection=!1}get indices(){return this.entity&&this._gotIndices?this._indices:null}set indices(e){e?(this._indices[0]=e[0],this._indices[1]=e[1],this._indices[2]=e[2],this._gotIndices=!0):this._gotIndices=!1}get localPos(){return this.entity&&this._gotLocalPos?this._localPos:null}set localPos(e){e?(this._localPos[0]=e[0],this._localPos[1]=e[1],this._localPos[2]=e[2],this._gotLocalPos=!0):this._gotLocalPos=!1}get worldPos(){return this._gotWorldPos?this._worldPos:null}set worldPos(e){e?(this._worldPos[0]=e[0],this._worldPos[1]=e[1],this._worldPos[2]=e[2],this._gotWorldPos=!0):this._gotWorldPos=!1}get viewPos(){return this.entity&&this._gotViewPos?this._viewPos:null}set viewPos(e){e?(this._viewPos[0]=e[0],this._viewPos[1]=e[1],this._viewPos[2]=e[2],this._gotViewPos=!0):this._gotViewPos=!1}get bary(){return this.entity&&this._gotBary?this._bary:null}set bary(e){e?(this._bary[0]=e[0],this._bary[1]=e[1],this._bary[2]=e[2],this._gotBary=!0):this._gotBary=!1}get worldNormal(){return this.entity&&this._gotWorldNormal?this._worldNormal:null}set worldNormal(e){e?(this._worldNormal[0]=e[0],this._worldNormal[1]=e[1],this._worldNormal[2]=e[2],this._gotWorldNormal=!0):this._gotWorldNormal=!1}get uv(){return this.entity&&this._gotUV?this._uv:null}set uv(e){e?(this._uv[0]=e[0],this._uv[1]=e[1],this._gotUV=!0):this._gotUV=!1}reset(){this.entity=null,this.primIndex=-1,this.primitive=null,this.pickSurfacePrecision=!1,this._gotCanvasPos=!1,this._gotOrigin=!1,this._gotDirection=!1,this._gotIndices=!1,this._gotLocalPos=!1,this._gotWorldPos=!1,this._gotViewPos=!1,this._gotBary=!1,this._gotWorldNormal=!1,this._gotUV=!1,this.touchInput=!1}}class de{constructor(e,t,s){if(this.allocated=!1,this.compiled=!1,this.handle=e.createShader(t),this.handle){if(this.allocated=!0,e.shaderSource(this.handle,s),e.compileShader(this.handle),this.compiled=e.getShaderParameter(this.handle,e.COMPILE_STATUS),!this.compiled&&!e.isContextLost()){const t=s.split("\n"),n=[];for(let e=0;e<t.length;e++)n.push(e+1+": "+t[e]+"\n");this.errors=[],this.errors.push(""),this.errors.push(e.getShaderInfoLog(this.handle)),this.errors=this.errors.concat(n.join(""))}}else this.errors=["Failed to allocate"]}destroy(){}}class fe{constructor(e,t){this.bindTexture=function(s,n){return!!s.bind(n)&&(e.uniform1i(t,n),!0)}}}class Ie{constructor(e,t){this._gl=e,this.location=t}bindArrayBuffer(e){e&&(e.bind(),this._gl.enableVertexAttribArray(this.location),this._gl.vertexAttribPointer(this.location,e.itemSize,e.itemType,e.normalized,e.stride,e.offset))}}const ye=new e({});function me(e){const t=[];let s,n;for(let i=0,a=e.length;i<a;i++)s=e[i],n=s.indexOf("/"),n>0&&"/"===s.charAt(n+1)&&(s=s.substring(0,n)),t.push(s);return t.join("\n")}function ve(e){console.error(e.join("\n"))}class we{constructor(e,t){this.id=ye.addItem({}),this.source=t,this.init(e)}init(e){if(this.gl=e,this.allocated=!1,this.compiled=!1,this.linked=!1,this.validated=!1,this.errors=null,this.uniforms={},this.samplers={},this.attributes={},this._vertexShader=new de(e,e.VERTEX_SHADER,me(this.source.vertex)),this._fragmentShader=new de(e,e.FRAGMENT_SHADER,me(this.source.fragment)),!this._vertexShader.allocated)return this.errors=["Vertex shader failed to allocate"].concat(this._vertexShader.errors),void ve(this.errors);if(!this._fragmentShader.allocated)return this.errors=["Fragment shader failed to allocate"].concat(this._fragmentShader.errors),void ve(this.errors);if(this.allocated=!0,!this._vertexShader.compiled)return this.errors=["Vertex shader failed to compile"].concat(this._vertexShader.errors),void ve(this.errors);if(!this._fragmentShader.compiled)return this.errors=["Fragment shader failed to compile"].concat(this._fragmentShader.errors),void ve(this.errors);let t,s,n,i,a;if(this.compiled=!0,this.handle=e.createProgram(),!this.handle)return void(this.errors=["Failed to allocate program"]);if(e.attachShader(this.handle,this._vertexShader.handle),e.attachShader(this.handle,this._fragmentShader.handle),e.linkProgram(this.handle),this.linked=e.getProgramParameter(this.handle,e.LINK_STATUS),this.validated=!0,!this.linked||!this.validated)return this.errors=[],this.errors.push(""),this.errors.push(e.getProgramInfoLog(this.handle)),this.errors.push("\nVertex shader:\n"),this.errors=this.errors.concat(this.source.vertex),this.errors.push("\nFragment shader:\n"),this.errors=this.errors.concat(this.source.fragment),void ve(this.errors);const r=e.getProgramParameter(this.handle,e.ACTIVE_UNIFORMS);for(s=0;s<r;++s)n=e.getActiveUniform(this.handle,s),n&&(i=n.name,"\0"===i[i.length-1]&&(i=i.substr(0,i.length-1)),a=e.getUniformLocation(this.handle,i),n.type===e.SAMPLER_2D||n.type===e.SAMPLER_CUBE||35682===n.type||e instanceof WebGL2RenderingContext&&(n.type===e.UNSIGNED_INT_SAMPLER_2D||n.type===e.INT_SAMPLER_2D)?this.samplers[i]=new fe(e,a):this.uniforms[i]=a);const l=e.getProgramParameter(this.handle,e.ACTIVE_ATTRIBUTES);for(s=0;s<l;s++)t=e.getActiveAttrib(this.handle,s),t&&(a=e.getAttribLocation(this.handle,t.name),this.attributes[t.name]=new Ie(e,a));this.allocated=!0}bind(){this.allocated&&this.gl.useProgram(this.handle)}getLocation(e){if(this.allocated)return this.uniforms[e]}getAttribute(e){if(this.allocated)return this.attributes[e]}bindTexture(e,t,s){if(!this.allocated)return!1;const n=this.samplers[e];return!!n&&n.bindTexture(t,s)}destroy(){this.allocated&&(ye.removeItem(this.id),this.gl.deleteProgram(this.handle),this.gl.deleteShader(this._vertexShader.handle),this.gl.deleteShader(this._fragmentShader.handle),this.handle=null,this.attributes=null,this.uniforms=null,this.samplers=null,this.allocated=!1)}}class ge{constructor(e,t,s,n,i,a,r,l,o){switch(this._gl=e,this.type=t,this.allocated=!1,s.constructor){case Uint8Array:this.itemType=e.UNSIGNED_BYTE,this.itemByteSize=1;break;case Int8Array:this.itemType=e.BYTE,this.itemByteSize=1;break;case Uint16Array:this.itemType=e.UNSIGNED_SHORT,this.itemByteSize=2;break;case Int16Array:this.itemType=e.SHORT,this.itemByteSize=2;break;case Uint32Array:this.itemType=e.UNSIGNED_INT,this.itemByteSize=4;break;case Int32Array:this.itemType=e.INT,this.itemByteSize=4;break;default:this.itemType=e.FLOAT,this.itemByteSize=4}this.usage=a,this.length=0,this.dataLength=n,this.numItems=0,this.itemSize=i,this.normalized=!!r,this.stride=l||0,this.offset=o||0,this._allocate(s)}_allocate(e){if(this.allocated=!1,this._handle=this._gl.createBuffer(),!this._handle)throw"Failed to allocate WebGL ArrayBuffer";this._handle&&(this._gl.bindBuffer(this.type,this._handle),this._gl.bufferData(this.type,e.length>this.dataLength?e.slice(0,this.dataLength):e,this.usage),this._gl.bindBuffer(this.type,null),this.length=e.length,this.numItems=this.length/this.itemSize,this.allocated=!0)}setData(e,t){this.allocated&&(e.length+(t||0)>this.length?(this.destroy(),this._allocate(e)):(this._gl.bindBuffer(this.type,this._handle),t||0===t?this._gl.bufferSubData(this.type,t*this.itemByteSize,e):this._gl.bufferData(this.type,e,this.usage),this._gl.bindBuffer(this.type,null)))}bind(){this.allocated&&this._gl.bindBuffer(this.type,this._handle)}unbind(){this.allocated&&this._gl.bindBuffer(this.type,null)}destroy(){this.allocated&&(this._gl.deleteBuffer(this._handle),this._handle=null,this.allocated=!1)}}class Te{constructor(e,t){this.scene=e,this.aabb=h.AABB3(),this.origin=h.vec3(t),this.originHash=this.origin.join(),this.numMarkers=0,this.markers={},this.markerList=[],this.markerIndices={},this.positions=[],this.indices=[],this.positionsBuf=null,this.lenPositionsBuf=0,this.indicesBuf=null,this.sectionPlanesActive=[],this.culledBySectionPlanes=!1,this.occlusionTestList=[],this.lenOcclusionTestList=0,this.pixels=[],this.aabbDirty=!1,this.markerListDirty=!1,this.positionsDirty=!0,this.occlusionTestListDirty=!1}addMarker(e){this.markers[e.id]=e,this.markerListDirty=!0,this.numMarkers++}markerWorldPosUpdated(e){if(!this.markers[e.id])return;const t=this.markerIndices[e.id];this.positions[3*t+0]=e.worldPos[0],this.positions[3*t+1]=e.worldPos[1],this.positions[3*t+2]=e.worldPos[2],this.positionsDirty=!0}removeMarker(e){delete this.markers[e.id],this.markerListDirty=!0,this.numMarkers--}update(){this.markerListDirty&&(this._buildMarkerList(),this.markerListDirty=!1,this.positionsDirty=!0,this.occlusionTestListDirty=!0),this.positionsDirty&&(this._buildPositions(),this.positionsDirty=!1,this.aabbDirty=!0,this.vbosDirty=!0),this.aabbDirty&&(this._buildAABB(),this.aabbDirty=!1),this.vbosDirty&&(this._buildVBOs(),this.vbosDirty=!1),this.occlusionTestListDirty&&this._buildOcclusionTestList(),this._updateActiveSectionPlanes()}_buildMarkerList(){for(var e in this.numMarkers=0,this.markers)this.markers.hasOwnProperty(e)&&(this.markerList[this.numMarkers]=this.markers[e],this.markerIndices[e]=this.numMarkers,this.numMarkers++);this.markerList.length=this.numMarkers}_buildPositions(){let e=0;for(let t=0;t<this.numMarkers;t++)if(this.markerList[t]){const s=this.markerList[t].worldPos;this.positions[e++]=s[0],this.positions[e++]=s[1],this.positions[e++]=s[2],this.indices[t]=t}this.positions.length=3*this.numMarkers,this.indices.length=this.numMarkers}_buildAABB(){const e=this.aabb;h.collapseAABB3(e),h.expandAABB3Points3(e,this.positions);const t=this.origin;e[0]+=t[0],e[1]+=t[1],e[2]+=t[2],e[3]+=t[0],e[4]+=t[1],e[5]+=t[2]}_buildVBOs(){if(this.positionsBuf){if(this.lenPositionsBuf===this.positions.length)return void this.positionsBuf.setData(this.positions);this.positionsBuf.destroy(),this.positionsBuf=null,this.indicesBuf.destroy(),this.indicesBuf=null}const e=this.scene.canvas.gl,t=3*this.numMarkers,s=this.numMarkers;this.positionsBuf=new ge(e,e.ARRAY_BUFFER,new Float32Array(this.positions),t,3,e.STATIC_DRAW),this.indicesBuf=new ge(e,e.ELEMENT_ARRAY_BUFFER,new Uint16Array(this.indices),s,1,e.STATIC_DRAW),this.lenPositionsBuf=this.positions.length}_buildOcclusionTestList(){const e=this.scene.canvas,t=this.scene.camera.perspective.near,s=e.boundary,n=s[2],i=s[3];let a=0;this.lenOcclusionTestList=0;for(let e=0;e<this.numMarkers;e++){const s=this.markerList[e];if(s.viewPos[2]>-t){s._setVisible(!1);continue}const r=s.canvasPos,l=r[0],o=r[1];l+10<0||o+10<0||l-10>n||o-10>i?s._setVisible(!1):!s.entity||s.entity.visible?s.occludable?(this.occlusionTestList[this.lenOcclusionTestList++]=s,this.pixels[a++]=l,this.pixels[a++]=o):s._setVisible(!0):s._setVisible(!1)}}_updateActiveSectionPlanes(){const e=this.scene._sectionPlanesState.sectionPlanes,t=e.length;if(t>0)for(let s=0;s<t;s++){const t=e[s];if(t.active){const e=h.planeAABB3Intersect(t.dir,t.dist,this.aabb);if(-1===e)return void(this.culledBySectionPlanes=!0);const n=0===e;this.sectionPlanesActive[s]=n}else this.sectionPlanesActive[s]=!1}this.culledBySectionPlanes=!1}destroy(){this.markers={},this.markerList.length=0,this.positionsBuf&&this.positionsBuf.destroy(),this.indicesBuf&&this.indicesBuf.destroy()}}const Ee=h.vec3([1,0,0]),be=h.vec3();class De{constructor(e,t){this._scene=e,this._renderBufferManager=t,this._occlusionLayers={},this._occlusionLayersList=[],this._occlusionLayersListDirty=!1,this._shaderSource=null,this._program=null,this._shaderSourceHash=null,this._shaderSourceDirty=!0,this._programDirty=!1,this._markersToOcclusionLayersMap={},this._onCameraViewMatrix=e.camera.on("viewMatrix",(()=>{this._occlusionTestListDirty=!0})),this._onCameraProjMatrix=e.camera.on("projMatrix",(()=>{this._occlusionTestListDirty=!0})),this._onCanvasBoundary=e.canvas.on("boundary",(()=>{this._occlusionTestListDirty=!0}))}addMarker(e){const t=e.origin.join();let s=this._occlusionLayers[t];s||(s=new Te(this._scene,e.origin),this._occlusionLayers[s.originHash]=s,this._occlusionLayersListDirty=!0),s.addMarker(e),this._markersToOcclusionLayersMap[e.id]=s,this._occlusionTestListDirty=!0}markerWorldPosUpdated(e){const t=this._markersToOcclusionLayersMap[e.id];if(!t)return void e.error("Marker has not been added to OcclusionTester");const s=e.origin.join();if(s!==t.originHash){1===t.numMarkers?(t.destroy(),delete this._occlusionLayers[t.originHash],this._occlusionLayersListDirty=!0):t.removeMarker(e);let n=this._occlusionLayers[s];n||(n=new Te(this._scene,e.origin),this._occlusionLayers[s]=t,this._occlusionLayersListDirty=!0),n.addMarker(e),this._markersToOcclusionLayersMap[e.id]=n}else t.markerWorldPosUpdated(e)}removeMarker(e){const t=e.origin.join();let s=this._occlusionLayers[t];s&&(1===s.numMarkers?(s.destroy(),delete this._occlusionLayers[s.originHash],this._occlusionLayersListDirty=!0):s.removeMarker(e),delete this._markersToOcclusionLayersMap[e.id])}get needOcclusionTest(){return this._occlusionTestListDirty}bindRenderBuf(){const e=[this._scene.canvas.canvas.id,this._scene._sectionPlanesState.getHash()].join(";");if(e!==this._shaderSourceHash&&(this._shaderSourceHash=e,this._shaderSourceDirty=!0),this._shaderSourceDirty&&(this._buildShaderSource(),this._shaderSourceDirty=!1,this._programDirty=!0),this._programDirty&&(this._buildProgram(),this._programDirty=!1,this._occlusionTestListDirty=!0),this._occlusionLayersListDirty&&(this._buildOcclusionLayersList(),this._occlusionLayersListDirty=!1),this._occlusionTestListDirty){for(let e=0,t=this._occlusionLayersList.length;e<t;e++){this._occlusionLayersList[e].occlusionTestListDirty=!0}this._occlusionTestListDirty=!1}this._readPixelBuf=this._renderBufferManager.getRenderBuffer("occlusionReadPix"),this._readPixelBuf.bind(),this._readPixelBuf.clear()}_buildOcclusionLayersList(){let e=0;for(let t in this._occlusionLayers)this._occlusionLayers.hasOwnProperty(t)&&(this._occlusionLayersList[e++]=this._occlusionLayers[t]);this._occlusionLayersList.length=e}_buildShaderSource(){this._shaderSource={vertex:this._buildVertexShaderSource(),fragment:this._buildFragmentShaderSource()}}_buildVertexShaderSource(){const e=this._scene,t=e._sectionPlanesState.sectionPlanes.length>0,s=[];return s.push("#version 300 es"),s.push("// OcclusionTester vertex shader"),s.push("in vec3 position;"),s.push("uniform mat4 modelMatrix;"),s.push("uniform mat4 viewMatrix;"),s.push("uniform mat4 projMatrix;"),e.logarithmicDepthBufferEnabled&&(s.push("uniform float logDepthBufFC;"),s.push("out float vFragDepth;")),t&&s.push("out vec4 vWorldPosition;"),s.push("void main(void) {"),s.push("vec4 worldPosition = vec4(position, 1.0); "),s.push(" vec4 viewPosition = viewMatrix * worldPosition;"),t&&s.push(" vWorldPosition = worldPosition;"),s.push(" vec4 clipPos = projMatrix * viewPosition;"),s.push(" gl_PointSize = 20.0;"),e.logarithmicDepthBufferEnabled?s.push("vFragDepth = 1.0 + clipPos.w;"):s.push("clipPos.z += -0.001;"),s.push(" gl_Position = clipPos;"),s.push("}"),s}_buildFragmentShaderSource(){const e=this._scene,t=e._sectionPlanesState,s=t.sectionPlanes.length>0,n=[];if(n.push("#version 300 es"),n.push("// OcclusionTester fragment shader"),n.push("#ifdef GL_FRAGMENT_PRECISION_HIGH"),n.push("precision highp float;"),n.push("precision highp int;"),n.push("#else"),n.push("precision mediump float;"),n.push("precision mediump int;"),n.push("#endif"),e.logarithmicDepthBufferEnabled&&(n.push("uniform float logDepthBufFC;"),n.push("in float vFragDepth;")),s){n.push("in vec4 vWorldPosition;");for(let e=0;e<t.sectionPlanes.length;e++)n.push("uniform bool sectionPlaneActive"+e+";"),n.push("uniform vec3 sectionPlanePos"+e+";"),n.push("uniform vec3 sectionPlaneDir"+e+";")}if(n.push("out vec4 outColor;"),n.push("void main(void) {"),s){n.push(" float dist = 0.0;");for(var i=0;i<t.sectionPlanes.length;i++)n.push("if (sectionPlaneActive"+i+") {"),n.push(" dist += clamp(dot(-sectionPlaneDir"+i+".xyz, vWorldPosition.xyz - sectionPlanePos"+i+".xyz), 0.0, 1000.0);"),n.push("}");n.push(" if (dist > 0.0) { discard; }")}return e.logarithmicDepthBufferEnabled&&n.push("gl_FragDepth = log2( vFragDepth ) * logDepthBufFC * 0.5;"),n.push(" outColor = vec4(1.0, 0.0, 0.0, 1.0); "),n.push("}"),n}_buildProgram(){this._program&&this._program.destroy();const e=this._scene,t=e.canvas.gl,s=e._sectionPlanesState;if(this._program=new we(t,this._shaderSource),this._program.errors)return void(this.errors=this._program.errors);const n=this._program;this._uViewMatrix=n.getLocation("viewMatrix"),this._uProjMatrix=n.getLocation("projMatrix"),this._uSectionPlanes=[];for(let e=0,t=s.sectionPlanes.length;e<t;e++)this._uSectionPlanes.push({active:n.getLocation("sectionPlaneActive"+e),pos:n.getLocation("sectionPlanePos"+e),dir:n.getLocation("sectionPlaneDir"+e)});this._aPosition=n.getAttribute("position"),e.logarithmicDepthBufferEnabled&&(this._uLogDepthBufFC=n.getLocation("logDepthBufFC"))}drawMarkers(){const e=this._scene,t=e.canvas.gl,s=this._program,n=e._sectionPlanesState,i=e.camera,a=e.camera.project;if(s.bind(),t.uniformMatrix4fv(this._uProjMatrix,!1,i._project._state.matrix),e.logarithmicDepthBufferEnabled){const e=2/(Math.log(a.far+1)/Math.LN2);t.uniform1f(this._uLogDepthBufFC,e)}for(let e=0,s=this._occlusionLayersList.length;e<s;e++){const s=this._occlusionLayersList[e];if(s.update(),s.culledBySectionPlanes)continue;const a=s.origin;t.uniformMatrix4fv(this._uViewMatrix,!1,B(i.viewMatrix,a));const r=n.sectionPlanes.length;if(r>0){const e=n.sectionPlanes;for(let n=0;n<r;n++){const i=this._uSectionPlanes[n];if(i){const r=s.sectionPlanesActive[n];if(t.uniform1i(i.active,r?1:0),r){const s=e[n];t.uniform3fv(i.pos,N(s.dist,s.dir,a,be)),t.uniform3fv(i.dir,s.dir)}}}}this._aPosition.bindArrayBuffer(s.positionsBuf);const l=s.indicesBuf;l.bind(),t.drawElements(t.POINTS,l.numItems,l.itemType,0)}}doOcclusionTest(){{const e=this._scene.canvas.resolutionScale,t=255*Ee[0],s=255*Ee[1],n=255*Ee[2];for(let i=0,a=this._occlusionLayersList.length;i<a;i++){const a=this._occlusionLayersList[i];for(let i=0;i<a.lenOcclusionTestList;i++){const r=a.occlusionTestList[i],l=2*i,o=this._readPixelBuf.read(Math.round(a.pixels[l]*e),Math.round(a.pixels[l+1]*e)),c=o[0]===t&&o[1]===s&&o[2]===n;r._setVisible(c)}}}}unbindRenderBuf(){this._readPixelBuf.unbind()}destroy(){if(!this.destroyed){for(let e=0,t=this._occlusionLayersList.length;e<t;e++){this._occlusionLayersList[e].destroy()}this._program&&this._program.destroy(),this._scene.camera.off(this._onCameraViewMatrix),this._scene.camera.off(this._onCameraProjMatrix),this._scene.canvas.off(this._onCanvasBoundary),this.destroyed=!0}}}const Pe=h.vec2();class Re{constructor(e){this._scene=e,this._numSamples=null,this._program=null,this._programError=!1,this._aPosition=null,this._aUV=null,this._uDepthTexture="uDepthTexture",this._uCameraNear=null,this._uCameraFar=null,this._uCameraProjectionMatrix=null,this._uCameraInverseProjectionMatrix=null,this._uScale=null,this._uIntensity=null,this._uBias=null,this._uKernelRadius=null,this._uMinResolution=null,this._uRandomSeed=null,this._uvBuf=null,this._positionsBuf=null,this._indicesBuf=null}render(e){if(this._build(),this._programError)return;this._getInverseProjectMat||(this._getInverseProjectMat=(()=>{let e=!0;this._scene.camera.on("projMatrix",(function(){e=!0}));const t=h.mat4();return()=>(e&&h.inverseMat4(n.camera.projMatrix,t),t)})());const t=this._scene.canvas.gl,s=this._program,n=this._scene,i=n.sao,a=t.drawingBufferWidth,r=t.drawingBufferHeight,l=n.camera.project._state,o=l.near,c=l.far,u=l.matrix,p=this._getInverseProjectMat(),A=Math.random(),d="perspective"===n.camera.projection;Pe[0]=a,Pe[1]=r,t.viewport(0,0,a,r),t.clearColor(0,0,0,1),t.disable(t.DEPTH_TEST),t.disable(t.BLEND),t.frontFace(t.CCW),t.clear(t.COLOR_BUFFER_BIT),s.bind(),t.uniform1f(this._uCameraNear,o),t.uniform1f(this._uCameraFar,c),t.uniformMatrix4fv(this._uCameraProjectionMatrix,!1,u),t.uniformMatrix4fv(this._uCameraInverseProjectionMatrix,!1,p),t.uniform1i(this._uPerspective,d),t.uniform1f(this._uScale,i.scale*(c/5)),t.uniform1f(this._uIntensity,i.intensity),t.uniform1f(this._uBias,i.bias),t.uniform1f(this._uKernelRadius,i.kernelRadius),t.uniform1f(this._uMinResolution,i.minResolution),t.uniform2fv(this._uViewport,Pe),t.uniform1f(this._uRandomSeed,A);const f=e.getDepthTexture();s.bindTexture(this._uDepthTexture,f,0),this._aUV.bindArrayBuffer(this._uvBuf),this._aPosition.bindArrayBuffer(this._positionsBuf),this._indicesBuf.bind(),t.drawElements(t.TRIANGLES,this._indicesBuf.numItems,this._indicesBuf.itemType,0)}_build(){let e=!1;const t=this._scene.sao;if(t.numSamples!==this._numSamples&&(this._numSamples=Math.floor(t.numSamples),e=!0),!e)return;const s=this._scene.canvas.gl;if(this._program&&(this._program.destroy(),this._program=null),this._program=new we(s,{vertex:["#version 300 es\n precision highp float;\n precision highp int;\n \n in vec3 aPosition;\n in vec2 aUV; \n \n out vec2 vUV;\n \n void main () {\n gl_Position = vec4(aPosition, 1.0);\n vUV = aUV;\n }"],fragment:[`#version 300 es \n precision highp float;\n precision highp int; \n \n #define NORMAL_TEXTURE 0\n #define PI 3.14159265359\n #define PI2 6.28318530718\n #define EPSILON 1e-6\n #define NUM_SAMPLES ${this._numSamples}\n #define NUM_RINGS 4 \n \n in vec2 vUV;\n \n uniform sampler2D uDepthTexture;\n \n uniform float uCameraNear;\n uniform float uCameraFar;\n uniform mat4 uProjectMatrix;\n uniform mat4 uInverseProjectMatrix;\n \n uniform bool uPerspective;\n\n uniform float uScale;\n uniform float uIntensity;\n uniform float uBias;\n uniform float uKernelRadius;\n uniform float uMinResolution;\n uniform vec2 uViewport;\n uniform float uRandomSeed;\n\n float pow2( const in float x ) { return x*x; }\n \n highp float rand( const in vec2 uv ) {\n const highp float a = 12.9898, b = 78.233, c = 43758.5453;\n highp float dt = dot( uv.xy, vec2( a,b ) ), sn = mod( dt, PI );\n return fract(sin(sn) * c);\n }\n\n vec3 packNormalToRGB( const in vec3 normal ) {\n return normalize( normal ) * 0.5 + 0.5;\n }\n\n vec3 unpackRGBToNormal( const in vec3 rgb ) {\n return 2.0 * rgb.xyz - 1.0;\n }\n\n const float packUpscale = 256. / 255.;\n const float unpackDownScale = 255. / 256.; \n\n const vec3 packFactors = vec3( 256. * 256. * 256., 256. * 256., 256. );\n const vec4 unPackFactors = unpackDownScale / vec4( packFactors, 1. ); \n\n const float shiftRights = 1. / 256.;\n\n vec4 packFloatToRGBA( const in float v ) {\n vec4 r = vec4( fract( v * packFactors ), v );\n r.yzw -= r.xyz * shiftRights; \n return r * packUpscale;\n }\n\n float unpackRGBAToFloat( const in vec4 v ) { \n return dot( floor( v * 255.0 + 0.5 ) / 255.0, unPackFactors );\n }\n \n float perspectiveDepthToViewZ( const in float invClipZ, const in float near, const in float far ) {\n return ( near * far ) / ( ( far - near ) * invClipZ - far );\n }\n\n float orthographicDepthToViewZ( const in float linearClipZ, const in float near, const in float far ) {\n return linearClipZ * ( near - far ) - near;\n }\n \n float getDepth( const in vec2 screenPosition ) {\n return vec4(texture(uDepthTexture, screenPosition)).r;\n }\n\n float getViewZ( const in float depth ) {\n if (uPerspective) {\n return perspectiveDepthToViewZ( depth, uCameraNear, uCameraFar );\n } else {\n return orthographicDepthToViewZ( depth, uCameraNear, uCameraFar );\n }\n }\n\n vec3 getViewPos( const in vec2 screenPos, const in float depth, const in float viewZ ) {\n \tfloat clipW = uProjectMatrix[2][3] * viewZ + uProjectMatrix[3][3];\n \tvec4 clipPosition = vec4( ( vec3( screenPos, depth ) - 0.5 ) * 2.0, 1.0 );\n \tclipPosition *= clipW; \n \treturn ( uInverseProjectMatrix * clipPosition ).xyz;\n }\n\n vec3 getViewNormal( const in vec3 viewPosition, const in vec2 screenPos ) { \n return normalize( cross( dFdx( viewPosition ), dFdy( viewPosition ) ) );\n }\n\n float scaleDividedByCameraFar;\n float minResolutionMultipliedByCameraFar;\n\n float getOcclusion( const in vec3 centerViewPosition, const in vec3 centerViewNormal, const in vec3 sampleViewPosition ) {\n \tvec3 viewDelta = sampleViewPosition - centerViewPosition;\n \tfloat viewDistance = length( viewDelta );\n \tfloat scaledScreenDistance = scaleDividedByCameraFar * viewDistance;\n \treturn max(0.0, (dot(centerViewNormal, viewDelta) - minResolutionMultipliedByCameraFar) / scaledScreenDistance - uBias) / (1.0 + pow2( scaledScreenDistance ) );\n }\n\n const float ANGLE_STEP = PI2 * float( NUM_RINGS ) / float( NUM_SAMPLES );\n const float INV_NUM_SAMPLES = 1.0 / float( NUM_SAMPLES );\n\n float getAmbientOcclusion( const in vec3 centerViewPosition ) {\n \n \tscaleDividedByCameraFar = uScale / uCameraFar;\n \tminResolutionMultipliedByCameraFar = uMinResolution * uCameraFar;\n \tvec3 centerViewNormal = getViewNormal( centerViewPosition, vUV );\n\n \tfloat angle = rand( vUV + uRandomSeed ) * PI2;\n \tvec2 radius = vec2( uKernelRadius * INV_NUM_SAMPLES ) / uViewport;\n \tvec2 radiusStep = radius;\n\n \tfloat occlusionSum = 0.0;\n \tfloat weightSum = 0.0;\n\n \tfor( int i = 0; i < NUM_SAMPLES; i ++ ) {\n \t\tvec2 sampleUv = vUV + vec2( cos( angle ), sin( angle ) ) * radius;\n \t\tradius += radiusStep;\n \t\tangle += ANGLE_STEP;\n\n \t\tfloat sampleDepth = getDepth( sampleUv );\n \t\tif( sampleDepth >= ( 1.0 - EPSILON ) ) {\n \t\t\tcontinue;\n \t\t}\n\n \t\tfloat sampleViewZ = getViewZ( sampleDepth );\n \t\tvec3 sampleViewPosition = getViewPos( sampleUv, sampleDepth, sampleViewZ );\n \t\tocclusionSum += getOcclusion( centerViewPosition, centerViewNormal, sampleViewPosition );\n \t\tweightSum += 1.0;\n \t}\n\n \tif( weightSum == 0.0 ) discard;\n\n \treturn occlusionSum * ( uIntensity / weightSum );\n }\n\n out vec4 outColor;\n \n void main() {\n \n \tfloat centerDepth = getDepth( vUV );\n \t\n \tif( centerDepth >= ( 1.0 - EPSILON ) ) {\n \t\tdiscard;\n \t}\n\n \tfloat centerViewZ = getViewZ( centerDepth );\n \tvec3 viewPosition = getViewPos( vUV, centerDepth, centerViewZ );\n\n \tfloat ambientOcclusion = getAmbientOcclusion( viewPosition );\n \n \toutColor = packFloatToRGBA( 1.0- ambientOcclusion );\n }`]}),this._program.errors)return console.error(this._program.errors.join("\n")),void(this._programError=!0);const n=new Float32Array([1,1,0,1,0,0,1,0]),i=new Float32Array([1,1,0,-1,1,0,-1,-1,0,1,-1,0]),a=new Uint32Array([0,1,2,0,2,3]);this._positionsBuf=new ge(s,s.ARRAY_BUFFER,i,i.length,3,s.STATIC_DRAW),this._uvBuf=new ge(s,s.ARRAY_BUFFER,n,n.length,2,s.STATIC_DRAW),this._indicesBuf=new ge(s,s.ELEMENT_ARRAY_BUFFER,a,a.length,1,s.STATIC_DRAW),this._program.bind(),this._uCameraNear=this._program.getLocation("uCameraNear"),this._uCameraFar=this._program.getLocation("uCameraFar"),this._uCameraProjectionMatrix=this._program.getLocation("uProjectMatrix"),this._uCameraInverseProjectionMatrix=this._program.getLocation("uInverseProjectMatrix"),this._uPerspective=this._program.getLocation("uPerspective"),this._uScale=this._program.getLocation("uScale"),this._uIntensity=this._program.getLocation("uIntensity"),this._uBias=this._program.getLocation("uBias"),this._uKernelRadius=this._program.getLocation("uKernelRadius"),this._uMinResolution=this._program.getLocation("uMinResolution"),this._uViewport=this._program.getLocation("uViewport"),this._uRandomSeed=this._program.getLocation("uRandomSeed"),this._aPosition=this._program.getAttribute("aPosition"),this._aUV=this._program.getAttribute("aUV"),this._dirty=!1}destroy(){this._program&&(this._program.destroy(),this._program=null)}}const Ce=new Float32Array(xe(17,[0,1])),_e=new Float32Array(xe(17,[1,0])),Be=new Float32Array(function(e,t){const s=[];for(let n=0;n<=e;n++)s.push(Ne(n,t));return s}(17,4)),Oe=new Float32Array(2);class Se{constructor(e){this._scene=e,this._program=null,this._programError=!1,this._aPosition=null,this._aUV=null,this._uDepthTexture="uDepthTexture",this._uOcclusionTexture="uOcclusionTexture",this._uViewport=null,this._uCameraNear=null,this._uCameraFar=null,this._uCameraProjectionMatrix=null,this._uCameraInverseProjectionMatrix=null,this._uvBuf=null,this._positionsBuf=null,this._indicesBuf=null,this.init()}init(){const e=this._scene.canvas.gl;if(this._program=new we(e,{vertex:["#version 300 es\n precision highp float;\n precision highp int;\n \n in vec3 aPosition;\n in vec2 aUV;\n uniform vec2 uViewport;\n out vec2 vUV;\n out vec2 vInvSize;\n void main () {\n vUV = aUV;\n vInvSize = 1.0 / uViewport;\n gl_Position = vec4(aPosition, 1.0);\n }"],fragment:["#version 300 es\n precision highp float;\n precision highp int;\n \n #define PI 3.14159265359\n #define PI2 6.28318530718\n #define EPSILON 1e-6\n\n #define KERNEL_RADIUS 16\n\n in vec2 vUV;\n in vec2 vInvSize;\n \n uniform sampler2D uDepthTexture;\n uniform sampler2D uOcclusionTexture; \n \n uniform float uCameraNear;\n uniform float uCameraFar; \n uniform float uDepthCutoff;\n\n uniform vec2 uSampleOffsets[ KERNEL_RADIUS + 1 ];\n uniform float uSampleWeights[ KERNEL_RADIUS + 1 ];\n\n const float unpackDownscale = 255. / 256.; \n\n const vec3 packFactors = vec3( 256. * 256. * 256., 256. * 256., 256. );\n const vec4 unpackFactors = unpackDownscale / vec4( packFactors, 1. ); \n\n const float packUpscale = 256. / 255.;\n \n const float shiftRights = 1. / 256.;\n \n float unpackRGBAToFloat( const in vec4 v ) {\n return dot( floor( v * 255.0 + 0.5 ) / 255.0, unpackFactors );\n } \n\n vec4 packFloatToRGBA( const in float v ) {\n vec4 r = vec4( fract( v * packFactors ), v );\n r.yzw -= r.xyz * shiftRights; \n return r * packUpscale;\n }\n\n float viewZToOrthographicDepth( const in float viewZ) {\n return ( viewZ + uCameraNear ) / ( uCameraNear - uCameraFar );\n }\n \n float orthographicDepthToViewZ( const in float linearClipZ) {\n return linearClipZ * ( uCameraNear - uCameraFar ) - uCameraNear;\n }\n\n float viewZToPerspectiveDepth( const in float viewZ) {\n return (( uCameraNear + viewZ ) * uCameraFar ) / (( uCameraFar - uCameraNear ) * viewZ );\n }\n \n float perspectiveDepthToViewZ( const in float invClipZ) {\n return ( uCameraNear * uCameraFar ) / ( ( uCameraFar - uCameraNear ) * invClipZ - uCameraFar );\n }\n\n float getDepth( const in vec2 screenPosition ) {\n return vec4(texture(uDepthTexture, screenPosition)).r;\n }\n\n float getViewZ( const in float depth ) {\n return perspectiveDepthToViewZ( depth );\n }\n\n out vec4 outColor;\n \n void main() {\n \n float depth = getDepth( vUV );\n if( depth >= ( 1.0 - EPSILON ) ) {\n discard;\n }\n\n float centerViewZ = -getViewZ( depth );\n bool rBreak = false;\n bool lBreak = false;\n\n float weightSum = uSampleWeights[0];\n float occlusionSum = unpackRGBAToFloat(texture( uOcclusionTexture, vUV )) * weightSum;\n\n for( int i = 1; i <= KERNEL_RADIUS; i ++ ) {\n\n float sampleWeight = uSampleWeights[i];\n vec2 sampleUVOffset = uSampleOffsets[i] * vInvSize;\n\n vec2 sampleUV = vUV + sampleUVOffset;\n float viewZ = -getViewZ( getDepth( sampleUV ) );\n\n if( abs( viewZ - centerViewZ ) > uDepthCutoff ) {\n rBreak = true;\n }\n\n if( ! rBreak ) {\n occlusionSum += unpackRGBAToFloat(texture( uOcclusionTexture, sampleUV )) * sampleWeight;\n weightSum += sampleWeight;\n }\n\n sampleUV = vUV - sampleUVOffset;\n viewZ = -getViewZ( getDepth( sampleUV ) );\n\n if( abs( viewZ - centerViewZ ) > uDepthCutoff ) {\n lBreak = true;\n }\n\n if( ! lBreak ) {\n occlusionSum += unpackRGBAToFloat(texture( uOcclusionTexture, sampleUV )) * sampleWeight;\n weightSum += sampleWeight;\n }\n }\n\n outColor = packFloatToRGBA(occlusionSum / weightSum);\n }"]}),this._program.errors)return console.error(this._program.errors.join("\n")),void(this._programError=!0);const t=new Float32Array([1,1,0,1,0,0,1,0]),s=new Float32Array([1,1,0,-1,1,0,-1,-1,0,1,-1,0]),n=new Uint32Array([0,1,2,0,2,3]);this._positionsBuf=new ge(e,e.ARRAY_BUFFER,s,s.length,3,e.STATIC_DRAW),this._uvBuf=new ge(e,e.ARRAY_BUFFER,t,t.length,2,e.STATIC_DRAW),this._indicesBuf=new ge(e,e.ELEMENT_ARRAY_BUFFER,n,n.length,1,e.STATIC_DRAW),this._program.bind(),this._uViewport=this._program.getLocation("uViewport"),this._uCameraNear=this._program.getLocation("uCameraNear"),this._uCameraFar=this._program.getLocation("uCameraFar"),this._uDepthCutoff=this._program.getLocation("uDepthCutoff"),this._uSampleOffsets=e.getUniformLocation(this._program.handle,"uSampleOffsets"),this._uSampleWeights=e.getUniformLocation(this._program.handle,"uSampleWeights"),this._aPosition=this._program.getAttribute("aPosition"),this._aUV=this._program.getAttribute("aUV")}render(e,t,s){if(this._programError)return;this._getInverseProjectMat||(this._getInverseProjectMat=(()=>{let e=!0;this._scene.camera.on("projMatrix",(function(){e=!0}));const t=h.mat4();return()=>(e&&h.inverseMat4(a.camera.projMatrix,t),t)})());const n=this._scene.canvas.gl,i=this._program,a=this._scene,r=n.drawingBufferWidth,l=n.drawingBufferHeight,o=a.camera.project._state,c=o.near,u=o.far;n.viewport(0,0,r,l),n.clearColor(0,0,0,1),n.enable(n.DEPTH_TEST),n.disable(n.BLEND),n.frontFace(n.CCW),n.clear(n.COLOR_BUFFER_BIT|n.DEPTH_BUFFER_BIT),i.bind(),Oe[0]=r,Oe[1]=l,n.uniform2fv(this._uViewport,Oe),n.uniform1f(this._uCameraNear,c),n.uniform1f(this._uCameraFar,u),n.uniform1f(this._uDepthCutoff,.01),0===s?n.uniform2fv(this._uSampleOffsets,_e):n.uniform2fv(this._uSampleOffsets,Ce),n.uniform1fv(this._uSampleWeights,Be);const p=e.getDepthTexture(),A=t.getTexture();i.bindTexture(this._uDepthTexture,p,0),i.bindTexture(this._uOcclusionTexture,A,1),this._aUV.bindArrayBuffer(this._uvBuf),this._aPosition.bindArrayBuffer(this._positionsBuf),this._indicesBuf.bind(),n.drawElements(n.TRIANGLES,this._indicesBuf.numItems,this._indicesBuf.itemType,0)}destroy(){this._program.destroy()}}function Ne(e,t){return Math.exp(-e*e/(t*t*2))/(Math.sqrt(2*Math.PI)*t)}function xe(e,t){const s=[];for(let n=0;n<=e;n++)s.push(t[0]*n),s.push(t[1]*n);return s}class Le{constructor(e,t,s){s=s||{},this.gl=t,this.allocated=!1,this.canvas=e,this.buffer=null,this.bound=!1,this.size=s.size,this._hasDepthTexture=!!s.depthTexture}setSize(e){this.size=e}webglContextRestored(e){this.gl=e,this.buffer=null,this.allocated=!1,this.bound=!1}bind(e=null){if(this._touch(e),this.bound)return;const t=this.gl;t.bindFramebuffer(t.FRAMEBUFFER,this.buffer.framebuf),this.bound=!0}_touch(e=null){let t,s;const n=this.gl;if(this.size?(t=this.size[0],s=this.size[1]):(t=n.drawingBufferWidth,s=n.drawingBufferHeight),this.buffer){if(this.buffer.width===t&&this.buffer.height===s)return;n.deleteTexture(this.buffer.texture),n.deleteFramebuffer(this.buffer.framebuf),n.deleteRenderbuffer(this.buffer.renderbuf)}const i=n.createTexture();let a;n.bindTexture(n.TEXTURE_2D,i),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_MIN_FILTER,n.NEAREST),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_MAG_FILTER,n.NEAREST),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_WRAP_S,n.CLAMP_TO_EDGE),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_WRAP_T,n.CLAMP_TO_EDGE),e?n.texStorage2D(n.TEXTURE_2D,1,e,t,s):n.texImage2D(n.TEXTURE_2D,0,n.RGBA,t,s,0,n.RGBA,n.UNSIGNED_BYTE,null),this._hasDepthTexture&&(a=n.createTexture(),n.bindTexture(n.TEXTURE_2D,a),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_MAG_FILTER,n.NEAREST),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_MIN_FILTER,n.NEAREST),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_WRAP_S,n.CLAMP_TO_EDGE),n.texParameteri(n.TEXTURE_2D,n.TEXTURE_WRAP_T,n.CLAMP_TO_EDGE),n.texImage2D(n.TEXTURE_2D,0,n.DEPTH_COMPONENT32F,t,s,0,n.DEPTH_COMPONENT,n.FLOAT,null));const r=n.createRenderbuffer();n.bindRenderbuffer(n.RENDERBUFFER,r),n.renderbufferStorage(n.RENDERBUFFER,n.DEPTH_COMPONENT32F,t,s);const l=n.createFramebuffer();if(n.bindFramebuffer(n.FRAMEBUFFER,l),n.framebufferTexture2D(n.FRAMEBUFFER,n.COLOR_ATTACHMENT0,n.TEXTURE_2D,i,0),this._hasDepthTexture?n.framebufferTexture2D(n.FRAMEBUFFER,n.DEPTH_ATTACHMENT,n.TEXTURE_2D,a,0):n.framebufferRenderbuffer(n.FRAMEBUFFER,n.DEPTH_ATTACHMENT,n.RENDERBUFFER,r),n.bindTexture(n.TEXTURE_2D,null),n.bindRenderbuffer(n.RENDERBUFFER,null),n.bindFramebuffer(n.FRAMEBUFFER,null),n.bindFramebuffer(n.FRAMEBUFFER,l),!n.isFramebuffer(l))throw"Invalid framebuffer";n.bindFramebuffer(n.FRAMEBUFFER,null);const o=n.checkFramebufferStatus(n.FRAMEBUFFER);switch(o){case n.FRAMEBUFFER_COMPLETE:break;case n.FRAMEBUFFER_INCOMPLETE_ATTACHMENT:throw"Incomplete framebuffer: FRAMEBUFFER_INCOMPLETE_ATTACHMENT";case n.FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:throw"Incomplete framebuffer: FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT";case n.FRAMEBUFFER_INCOMPLETE_DIMENSIONS:throw"Incomplete framebuffer: FRAMEBUFFER_INCOMPLETE_DIMENSIONS";case n.FRAMEBUFFER_UNSUPPORTED:throw"Incomplete framebuffer: FRAMEBUFFER_UNSUPPORTED";default:throw"Incomplete framebuffer: "+o}this.buffer={framebuf:l,renderbuf:r,texture:i,depthTexture:a,width:t,height:s},this.bound=!1}clear(){if(!this.bound)throw"Render buffer not bound";const e=this.gl;e.clear(e.COLOR_BUFFER_BIT|e.DEPTH_BUFFER_BIT)}read(e,t,s=null,n=null,i=Uint8Array,a=4){const r=e,l=(this.buffer.height||this.gl.drawingBufferHeight)-t,o=new i(a),c=this.gl;return c.readPixels(r,l,1,1,s||c.RGBA,n||c.UNSIGNED_BYTE,o,0),o}readArray(e=null,t=null,s=Uint8Array,n=4){const i=new s(this.buffer.width*this.buffer.height*n),a=this.gl;return a.readPixels(0,0,this.buffer.width,this.buffer.height,e||a.RGBA,t||a.UNSIGNED_BYTE,i,0),i}readImageAsCanvas(){const e=this.gl,t=this._getImageDataCache(),s=t.pixelData,n=t.canvas,i=t.imageData,a=t.context;e.readPixels(0,0,this.buffer.width,this.buffer.height,e.RGBA,e.UNSIGNED_BYTE,s);const r=this.buffer.width,l=this.buffer.height,o=l/2|0,c=4*r,u=new Uint8Array(4*r);for(let e=0;e<o;++e){const t=e*c,n=(l-e-1)*c;u.set(s.subarray(t,t+c)),s.copyWithin(t,n,n+c),s.set(u,n)}return i.data.set(s),a.putImageData(i,0,0),n}readImage(e){const t=this.gl,s=this._getImageDataCache(),n=s.pixelData,i=s.canvas,a=s.imageData,r=s.context,{width:l,height:o}=this.buffer;t.readPixels(0,0,l,o,t.RGBA,t.UNSIGNED_BYTE,n),a.data.set(n),r.putImageData(a,0,0),r.save(),r.globalCompositeOperation="copy",r.scale(1,-1),r.drawImage(i,0,-o,l,o),r.restore();let c=e.format||"png";return"jpeg"!==c&&"png"!==c&&"bmp"!==c&&(console.error("Unsupported image format: '"+c+"' - supported types are 'jpeg', 'bmp' and 'png' - defaulting to 'png'"),c="png"),i.toDataURL(`image/${c}`)}_getImageDataCache(e=Uint8Array,t=4){const s=this.buffer.width,n=this.buffer.height;let i=this._imageDataCache;if(i&&(i.width===s&&i.height===n||(this._imageDataCache=null,i=null)),!i){const a=document.createElement("canvas"),r=a.getContext("2d");a.width=s,a.height=n,i={pixelData:new e(s*n*t),canvas:a,context:r,imageData:r.createImageData(s,n),width:s,height:n},this._imageDataCache=i}return i.context.resetTransform(),i}unbind(){const e=this.gl;e.bindFramebuffer(e.FRAMEBUFFER,null),this.bound=!1}getTexture(){const e=this;return this._texture||(this._texture={renderBuffer:this,bind:function(t){return!(!e.buffer||!e.buffer.texture)&&(e.gl.activeTexture(e.gl["TEXTURE"+t]),e.gl.bindTexture(e.gl.TEXTURE_2D,e.buffer.texture),!0)},unbind:function(t){e.buffer&&e.buffer.texture&&(e.gl.activeTexture(e.gl["TEXTURE"+t]),e.gl.bindTexture(e.gl.TEXTURE_2D,null))}})}hasDepthTexture(){return this._hasDepthTexture}getDepthTexture(){if(!this._hasDepthTexture)return null;const e=this;return this._depthTexture||(this._dethTexture={renderBuffer:this,bind:function(t){return!(!e.buffer||!e.buffer.depthTexture)&&(e.gl.activeTexture(e.gl["TEXTURE"+t]),e.gl.bindTexture(e.gl.TEXTURE_2D,e.buffer.depthTexture),!0)},unbind:function(t){e.buffer&&e.buffer.depthTexture&&(e.gl.activeTexture(e.gl["TEXTURE"+t]),e.gl.bindTexture(e.gl.TEXTURE_2D,null))}})}destroy(){if(this.allocated){const e=this.gl;e.deleteTexture(this.buffer.texture),e.deleteTexture(this.buffer.depthTexture),e.deleteFramebuffer(this.buffer.framebuf),e.deleteRenderbuffer(this.buffer.renderbuf),this.allocated=!1,this.buffer=null,this.bound=!1}this._imageDataCache=null,this._texture=null,this._depthTexture=null}}class Me{constructor(e){this.scene=e,this._renderBuffersBasic={},this._renderBuffersScaled={}}getRenderBuffer(e,t){const s=1===this.scene.canvas.resolutionScale?this._renderBuffersBasic:this._renderBuffersScaled;let n=s[e];return n||(n=new Le(this.scene.canvas.canvas,this.scene.canvas.gl,t),s[e]=n),n}destroy(){for(let e in this._renderBuffersBasic)this._renderBuffersBasic[e].destroy();for(let e in this._renderBuffersScaled)this._renderBuffersScaled[e].destroy()}}function Fe(e,t){if(void 0===e._cachedExtensions&&(e._cachedExtensions={}),void 0!==e._cachedExtensions[t])return e._cachedExtensions[t];let s;switch(t){case"WEBGL_depth_texture":s=e.getExtension("WEBGL_depth_texture")||e.getExtension("MOZ_WEBGL_depth_texture")||e.getExtension("WEBKIT_WEBGL_depth_texture");break;case"EXT_texture_filter_anisotropic":s=e.getExtension("EXT_texture_filter_anisotropic")||e.getExtension("MOZ_EXT_texture_filter_anisotropic")||e.getExtension("WEBKIT_EXT_texture_filter_anisotropic");break;case"WEBGL_compressed_texture_s3tc":s=e.getExtension("WEBGL_compressed_texture_s3tc")||e.getExtension("MOZ_WEBGL_compressed_texture_s3tc")||e.getExtension("WEBKIT_WEBGL_compressed_texture_s3tc");break;case"WEBGL_compressed_texture_pvrtc":s=e.getExtension("WEBGL_compressed_texture_pvrtc")||e.getExtension("WEBKIT_WEBGL_compressed_texture_pvrtc");break;default:s=e.getExtension(t)}return e._cachedExtensions[t]=s,s}const He=function(t,s){s=s||{};const n=new pe(t),i=t.canvas.canvas,a=t.canvas.gl,r=!!s.transparent,l=s.alphaDepthMask,o=new e({});let c={},u={},p=!0,d=!0,f=!0,I=!0,y=!0,m=!0,v=!0,w=!0;const g=new Me(t);let T=!1;const E=new Re(t),b=new Se(t);function D(){p&&(!function(){for(let e in c)if(c.hasOwnProperty(e)){const t=c[e],s=t.drawableMap,n=t.drawableListPreCull;let i=0;for(let e in s)s.hasOwnProperty(e)&&(n[i++]=s[e]);n.length=i}}(),p=!1,d=!0),d&&(!function(){for(let e in c)if(c.hasOwnProperty(e)){const t=c[e];t.isStateSortable&&t.drawableListPreCull.sort(t.stateSortCompare)}}(),d=!1,f=!0),f&&function(){for(let e in c)if(c.hasOwnProperty(e)){const t=c[e],s=t.drawableListPreCull,n=t.drawableList;let i=0;for(let e=0,t=s.length;e<t;e++){const t=s[e];t.rebuildRenderFlags(),t.renderFlags.culled||(n[i++]=t)}n.length=i}}()}function P(e){if(!e.castsShadow)return;const t=e.getShadowRenderBuf();if(t){t.bind(),n.reset(),n.backfaces=!0,n.frontface=!0,n.shadowViewMatrix=e.getShadowViewMatrix(),n.shadowProjMatrix=e.getShadowProjMatrix(),a.viewport(0,0,a.drawingBufferWidth,a.drawingBufferHeight),a.clearColor(0,0,0,1),a.enable(a.DEPTH_TEST),a.disable(a.BLEND),a.clear(a.COLOR_BUFFER_BIT|a.DEPTH_BUFFER_BIT);for(let e in c)if(c.hasOwnProperty(e)){const t=c[e].drawableList;for(let e=0,s=t.length;e<s;e++){const s=t[e];!1!==s.visible&&s.castsShadow&&s.drawShadow&&(s.renderFlags.colorOpaque&&s.drawShadow(n))}}t.unbind()}}this.scene=t,this._occlusionTester=null,this.capabilities={astcSupported:!!Fe(a,"WEBGL_compressed_texture_astc"),etc1Supported:!0,etc2Supported:!!Fe(a,"WEBGL_compressed_texture_etc"),dxtSupported:!!Fe(a,"WEBGL_compressed_texture_s3tc"),bptcSupported:!!Fe(a,"EXT_texture_compression_bptc"),pvrtcSupported:!(!Fe(a,"WEBGL_compressed_texture_pvrtc")&&!Fe(a,"WEBKIT_WEBGL_compressed_texture_pvrtc"))},this.setTransparentEnabled=function(e){I=e,f=!0},this.setEdgesEnabled=function(e){y=e,f=!0},this.setSAOEnabled=function(e){m=e,f=!0},this.setPBREnabled=function(e){v=e,f=!0},this.setColorTextureEnabled=function(e){w=e,f=!0},this.needStateSort=function(){d=!0},this.shadowsDirty=function(){},this.imageDirty=function(){f=!0},this.webglContextLost=function(){},this.webglContextRestored=function(e){E.init(),b.init(),f=!0},this.addDrawable=function(e,t){const s=t.type;if(!s)return void console.error("Renderer#addDrawable() : drawable with ID "+e+" has no 'type' - ignoring");let n=c[s];n||(n={type:t.type,count:0,isStateSortable:t.isStateSortable,stateSortCompare:t.stateSortCompare,drawableMap:{},drawableListPreCull:[],drawableList:[]},c[s]=n),n.count++,n.drawableMap[e]=t,u[e]=t,p=!0},this.removeDrawable=function(e){const t=u[e];if(!t)return void console.error("Renderer#removeDrawable() : drawable not found with ID "+e+" - ignoring");const s=t.type,n=c[s];--n.count<=0?delete c[s]:delete n.drawableMap[e],delete u[e],p=!0},this.getPickID=function(e){return o.addItem(e)},this.putPickID=function(e){o.removeItem(e)},this.clear=function(e){if(a.viewport(0,0,a.drawingBufferWidth,a.drawingBufferHeight),r)a.clearColor(1,1,1,1);else{const e=t.canvas.backgroundColorFromAmbientLight?this.lights.getAmbientColorAndIntensity():t.canvas.backgroundColor;a.clearColor(e[0],e[1],e[2],1)}a.clear(a.COLOR_BUFFER_BIT|a.DEPTH_BUFFER_BIT)},this.needsRender=function(){return f||p||d},this.render=function(e){(e=e||{}).force&&(f=!0),D(),f&&(!function(e){const s=t.sao;m&&s.possible&&function(e){const s=t.sao,i=g.getRenderBuffer("saoDepth",{depthTexture:!0});i.bind(),i.clear(),function(e){n.reset(),n.pass=e.pass,a.viewport(0,0,a.drawingBufferWidth,a.drawingBufferHeight),a.clearColor(0,0,0,0),a.enable(a.DEPTH_TEST),a.frontFace(a.CCW),a.enable(a.CULL_FACE),a.depthMask(!0),!1!==e.clear&&a.clear(a.COLOR_BUFFER_BIT|a.DEPTH_BUFFER_BIT);for(let e in c)if(c.hasOwnProperty(e)){const t=c[e].drawableList;for(let e=0,s=t.length;e<s;e++){const s=t[e];!0!==s.culled&&!1!==s.visible&&s.drawDepth&&s.saoEnabled&&(s.renderFlags.colorOpaque&&s.drawDepth(n))}}}(e),i.unbind();const r=g.getRenderBuffer("saoOcclusion");if(r.bind(),r.clear(),E.render(i),r.unbind(),s.blur){const e=g.getRenderBuffer("saoOcclusion2");e.bind(),e.clear(),b.render(i,r,0),e.unbind(),r.bind(),r.clear(),b.render(i,e,1),r.unbind()}}(e);(function(){let e=t._lightsState.lights;for(let t=0,s=e.length;t<s;t++){const s=e[t];s.castsShadow&&P(s)}})(),function(e){const s=[],i=[],o=[],u=[],h=[],p=[],d=[],f=[],T=[],E=[],b=[],D=[],P=[],R=[],C=[],_=[],B=t._lightsState.getAmbientColorAndIntensity();if(n.reset(),n.pass=e.pass,n.withSAO=!1,n.pbrEnabled=v&&!!t.pbrEnabled,n.colorTextureEnabled=w&&!!t.colorTextureEnabled,a.viewport(0,0,a.drawingBufferWidth,a.drawingBufferHeight),r)a.clearColor(0,0,0,0);else{const e=t.canvas.backgroundColorFromAmbientLight?B:t.canvas.backgroundColor;a.clearColor(e[0],e[1],e[2],1)}a.enable(a.DEPTH_TEST),a.frontFace(a.CCW),a.enable(a.CULL_FACE),a.depthMask(!0),a.lineWidth(1),n.lineWidth=1;const O=t.sao.possible;if(m&&O){const e=g.getRenderBuffer("saoOcclusion");n.occlusionTexture=e?e.getTexture():null}else n.occlusionTexture=null;let S,N,x;const L=Date.now();!1!==e.clear&&a.clear(a.COLOR_BUFFER_BIT|a.DEPTH_BUFFER_BIT);let M=0,F=0,H=0,U=0,G=0,V=0,j=0,k=0,Q=0,W=0,z=0,K=0,Y=0,X=0,q=0,J=0;for(let e in c)if(c.hasOwnProperty(e)){const t=c[e].drawableList;for(S=0,N=t.length;S<N;S++){if(x=t[S],!0===x.culled||!1===x.visible)continue;const e=x.renderFlags;e.colorOpaque&&(m&&O&&x.saoEnabled?s[M++]=x:x.drawColorOpaque(n)),I&&e.colorTransparent&&(o[H++]=x),e.xrayedSilhouetteTransparent&&(d[j++]=x),e.xrayedSilhouetteOpaque&&(h[G++]=x),e.highlightedSilhouetteTransparent&&(b[z++]=x),e.highlightedSilhouetteOpaque&&(T[Q++]=x),e.selectedSilhouetteTransparent&&(C[q++]=x),e.selectedSilhouetteOpaque&&(P[Y++]=x),y&&(e.edgesOpaque&&(i[F++]=x),e.edgesTransparent&&(u[U++]=x)),e.selectedEdgesTransparent&&(_[J++]=x),e.selectedEdgesOpaque&&(R[X++]=x),e.xrayedEdgesTransparent&&(f[k++]=x),e.xrayedEdgesOpaque&&(p[V++]=x),e.highlightedEdgesTransparent&&(D[K++]=x),e.highlightedEdgesOpaque&&(E[W++]=x)}}if(M>0)for(n.withSAO=!0,S=0;S<M;S++)s[S].drawColorOpaque(n);if(F>0)for(S=0;S<F;S++)i[S].drawEdgesColorOpaque(n);if(G>0)for(S=0;S<G;S++)h[S].drawSilhouetteXRayed(n);if(V>0)for(S=0;S<V;S++)p[S].drawEdgesXRayed(n);if(j>0||k>0||H>0||U>0){if(a.enable(a.CULL_FACE),a.enable(a.BLEND),r?(a.blendEquation(a.FUNC_ADD),a.blendFuncSeparate(a.SRC_ALPHA,a.ONE_MINUS_SRC_ALPHA,a.ONE,a.ONE_MINUS_SRC_ALPHA)):(a.blendEquation(a.FUNC_ADD),a.blendFunc(a.SRC_ALPHA,a.ONE_MINUS_SRC_ALPHA)),n.backfaces=!1,l||a.depthMask(!1),(H>0||U>0)&&a.blendFunc(a.SRC_ALPHA,a.ONE_MINUS_SRC_ALPHA),U>0)for(S=0;S<U;S++)x=u[S],x.drawEdgesColorTransparent(n);if(H>0)for(S=0;S<H;S++)x=o[S],x.drawColorTransparent(n);if(k>0)for(S=0;S<k;S++)f[S].drawEdgesXRayed(n);if(j>0)for(S=0;S<j;S++)d[S].drawSilhouetteXRayed(n);a.disable(a.BLEND),l||a.depthMask(!0)}if(Q>0||W>0){if(n.lastProgramId=null,t.highlightMaterial.glowThrough&&a.clear(a.DEPTH_BUFFER_BIT),W>0)for(S=0;S<W;S++)E[S].drawEdgesHighlighted(n);if(Q>0)for(S=0;S<Q;S++)T[S].drawSilhouetteHighlighted(n)}if(z>0||K>0||Q>0){if(n.lastProgramId=null,t.selectedMaterial.glowThrough&&a.clear(a.DEPTH_BUFFER_BIT),a.enable(a.BLEND),r?(a.blendEquation(a.FUNC_ADD),a.blendFuncSeparate(a.SRC_ALPHA,a.ONE_MINUS_SRC_ALPHA,a.ONE,a.ONE_MINUS_SRC_ALPHA)):a.blendFunc(a.SRC_ALPHA,a.ONE_MINUS_SRC_ALPHA),a.enable(a.CULL_FACE),K>0)for(S=0;S<K;S++)D[S].drawEdgesHighlighted(n);if(z>0)for(S=0;S<z;S++)b[S].drawSilhouetteHighlighted(n);a.disable(a.BLEND)}if(Y>0||X>0){if(n.lastProgramId=null,t.selectedMaterial.glowThrough&&a.clear(a.DEPTH_BUFFER_BIT),X>0)for(S=0;S<X;S++)R[S].drawEdgesSelected(n);if(Y>0)for(S=0;S<Y;S++)P[S].drawSilhouetteSelected(n)}if(q>0||J>0){if(n.lastProgramId=null,t.selectedMaterial.glowThrough&&a.clear(a.DEPTH_BUFFER_BIT),a.enable(a.CULL_FACE),a.enable(a.BLEND),r?(a.blendEquation(a.FUNC_ADD),a.blendFuncSeparate(a.SRC_ALPHA,a.ONE_MINUS_SRC_ALPHA,a.ONE,a.ONE_MINUS_SRC_ALPHA)):a.blendFunc(a.SRC_ALPHA,a.ONE_MINUS_SRC_ALPHA),J>0)for(S=0;S<J;S++)_[S].drawEdgesSelected(n);if(q>0)for(S=0;S<q;S++)C[S].drawSilhouetteSelected(n);a.disable(a.BLEND)}const Z=Date.now(),$=A.frame;$.renderTime=(Z-L)/1e3,$.drawElements=n.drawElements,$.drawArrays=n.drawArrays,$.useProgram=n.useProgram,$.bindTexture=n.bindTexture,$.bindArray=n.bindArray;const ee=oe.MAX_TEXTURE_IMAGE_UNITS;for(let e=0;e<ee;e++)a.activeTexture(a.TEXTURE0+e);a.bindTexture(a.TEXTURE_CUBE_MAP,null),a.bindTexture(a.TEXTURE_2D,null);const te=oe.MAX_VERTEX_ATTRIBS;for(let e=0;e<te;e++)a.disableVertexAttribArray(e)}(e)}(e),A.frame.frameCount++,f=!1)},this.pick=function(){const e=h.vec3(),s=h.mat4(),r=h.mat4(),l=h.vec3(),u=h.vec3([0,1,0]),p=new Ae,A=h.vec2(),d=h.vec3(),f=h.vec3(),I=h.vec3();return h.vec3(),h.vec3(),function(y,m=p){let v;m.reset(),D();let w=null,T=null;if(m.pickSurface=y.pickSurface,y.canvasPos)d[0]=y.canvasPos[0],d[1]=y.canvasPos[1],w=t.camera.viewMatrix,T=t.camera.projMatrix,m.canvasPos=y.canvasPos;else{const n=h.frustumMat4(-1,1,-1,1,.01,t.camera.project.far,s);y.matrix?(w=y.matrix,T=n):(f.set(y.origin||[0,0,0]),I.set(y.direction||[0,0,1]),v=h.addVec3(f,I,e),l[0]=Math.random(),l[1]=Math.random(),l[2]=Math.random(),h.normalizeVec3(l),h.cross3Vec3(I,l,u),w=h.lookAtMat4v(f,v,u,r),T=n,m.origin=f,m.direction=I),d[0]=.5*i.clientWidth,d[1]=.5*i.clientHeight}for(let e in c)if(c.hasOwnProperty(e)){const t=c[e].drawableList;for(let e=0,s=t.length;e<s;e++){const s=t[e];s.setPickMatrices&&s.setPickMatrices(w,T)}}const E=g.getRenderBuffer("pick");E.bind();const b=function(e,s,i,r,l,u){n.reset(),n.backfaces=!0,n.frontface=!0,n.pickOrigin=u.origin,n.pickViewMatrix=i,n.pickProjMatrix=r,n.pickInvisible=!!l.pickInvisible,a.viewport(0,0,a.drawingBufferWidth,a.drawingBufferHeight),a.clearColor(0,0,0,0),a.enable(a.DEPTH_TEST),a.disable(a.CULL_FACE),a.disable(a.BLEND),a.clear(a.COLOR_BUFFER_BIT|a.DEPTH_BUFFER_BIT);const h=l.includeEntityIds,p=l.excludeEntityIds;for(let e in c)if(c.hasOwnProperty(e)){const t=c[e].drawableList;for(let e=0,s=t.length;e<s;e++){const s=t[e];!s.drawPickMesh||!0!==l.pickInvisible&&!1===s.visible||!1===s.pickable||(h&&!h[s.id]||p&&p[s.id]||s.drawPickMesh(n))}}const A=t.canvas.resolutionScale,d=e.read(Math.round(s[0]*A),Math.round(s[1]*A));let f=d[0]+256*d[1]+256*d[2]*256+256*d[3]*256*256;if(f<0)return;return o.items[f]}(E,d,w,T,y,m);if(!b)return E.unbind(),null;const P=b.delegatePickedEntity?b.delegatePickedEntity():b;return P?(y.pickSurface&&(b.canPickTriangle&&b.canPickTriangle()?(!function(e,s,i,r,l,o){if(!s.drawPickTriangles)return;n.reset(),n.backfaces=!0,n.frontface=!0,n.pickOrigin=o.origin,n.pickViewMatrix=r,n.pickProjMatrix=l,a.viewport(0,0,a.drawingBufferWidth,a.drawingBufferHeight),a.clearColor(0,0,0,0),a.enable(a.DEPTH_TEST),a.disable(a.CULL_FACE),a.disable(a.BLEND),a.clear(a.COLOR_BUFFER_BIT|a.DEPTH_BUFFER_BIT),s.drawPickTriangles(n);const c=t.canvas.resolutionScale,u=e.read(Math.round(i[0]*c),Math.round(i[1]*c));let h=u[0]+256*u[1]+256*u[2]*256+256*u[3]*256*256;h*=3,o.primIndex=h}(E,b,d,w,T,m),b.pickTriangleSurface(w,T,m),m.pickSurfacePrecision=!1):b.canPickWorldPos&&b.canPickWorldPos()&&(A[0]=t.camera.project.near,A[1]=t.camera.project.far,R(E,b,d,w,T,A,m),!1!==y.pickSurfaceNormal&&function(e,s,i,r,l,o){n.reset(),n.backfaces=!0,n.frontface=!0,n.pickOrigin=o.origin,n.pickViewMatrix=r,n.pickProjMatrix=l,a.viewport(0,0,a.drawingBufferWidth,a.drawingBufferHeight),a.clearColor(0,0,0,0),a.enable(a.DEPTH_TEST),a.disable(a.CULL_FACE),a.disable(a.BLEND),a.clear(a.COLOR_BUFFER_BIT|a.DEPTH_BUFFER_BIT),s.drawPickNormals(n);const c=t.canvas.resolutionScale,u=e.read(Math.round(i[0]*c),Math.round(i[1]*c)),p=[u[0]/256-.5,u[1]/256-.5,u[2]/256-.5];h.normalizeVec3(p),o.worldNormal=p}(E,b,d,w,T,m),m.pickSurfacePrecision=!1)),E.unbind(),m.entity=P,m):(E.unbind(),null)}}();const R=function(){const e=h.vec4(),s=h.vec4(),r=h.vec4(),l=h.vec4(),o=h.vec4(),c=h.mat4(),u=h.mat4(),p=h.mat4();return function(A,d,f,I,y,m,v){n.reset(),n.backfaces=!0,n.frontface=!0,n.pickOrigin=v.origin,n.pickViewMatrix=I,n.pickProjMatrix=y,n.pickZNear=m[0],n.pickZFar=m[1],n.pickElementsCount=d.pickElementsCount,n.pickElementsOffset=d.pickElementsOffset,a.viewport(0,0,a.drawingBufferWidth,a.drawingBufferHeight),a.clearColor(0,0,0,0),a.enable(a.DEPTH_TEST),a.disable(a.CULL_FACE),a.disable(a.BLEND),a.clear(a.COLOR_BUFFER_BIT|a.DEPTH_BUFFER_BIT),d.drawPickDepths(n);const w=t.canvas.resolutionScale,g=function(e){const t=[e[0]/256,e[1]/256,e[2]/256,e[3]/256],s=[1/16777216,1/65536,1/256,1];return h.dotVec4(t,s)}(A.read(Math.round(f[0]*w),Math.round(f[1]*w))),T=(f[0]-i.clientWidth/2)/(i.clientWidth/2),E=-(f[1]-i.clientHeight/2)/(i.clientHeight/2),b=d.origin;let D;if(b){const e=B(I,b,c);D=h.mulMat4(y,e,u)}else D=h.mulMat4(y,I,u);const P=h.inverseMat4(D,p);e[0]=T,e[1]=E,e[2]=-1,e[3]=1;let R=h.transformVec4(P,e);R=h.mulVec4Scalar(R,1/R[3]),s[0]=T,s[1]=E,s[2]=1,s[3]=1;let C=h.transformVec4(P,s);C=h.mulVec4Scalar(C,1/C[3]);const _=h.subVec3(C,R,r),O=h.addVec3(R,h.mulVec4Scalar(_,g,l),o);b&&h.addVec3(O,b),v.worldPos=O}}();function C(e,t){return 1-e/t*2}this.snapPick=function(e,s=50,i="vertex"){n.reset(),n.backfaces=!0,n.frontface=!0,n.pickZNear=t.camera.project.near,n.pickZFar=t.camera.project.far;let r=g.getRenderBuffer("uniquePickColors-aabs",{depthTexture:!0,size:[2*s+1,2*s+1]});var l,o;n.snapVectorA=[(l=e[0],o=a.drawingBufferWidth,l/o*2-1),C(e[1],a.drawingBufferHeight)],n.snapInvVectorAB=[a.drawingBufferWidth/(2*s),a.drawingBufferHeight/(2*s)],n.snapMode=i,r.bind(a.RGBA32I),a.viewport(0,0,r.size[0],r.size[1]),a.enable(a.DEPTH_TEST),a.frontFace(a.CCW),a.disable(a.CULL_FACE),a.depthMask(!0),a.disable(a.BLEND),a.depthFunc(a.LESS),a.clear(a.DEPTH_BUFFER_BIT),a.clearBufferiv(a.COLOR,0,new Int32Array([0,0,0,0]));const u=t.camera.viewMatrix,h=t.camera.projMatrix;for(let e in c)if(c.hasOwnProperty(e)){const t=c[e].drawableList;for(let e=0,s=t.length;e<s;e++){const s=t[e];s.setPickMatrices&&s.setPickMatrices(u,h)}}const p=function(e){e.snapPickLayerParams={},e.snapPickLayerNumber=0;for(let t in c){const s=c[t].drawableList;for(let t=0,n=s.length;t<n;t++){const n=s[t];n.drawSnapInitDepthBuf&&!n.culled&&n.visible&&n.pickable&&n.drawSnapInitDepthBuf(e)}}return e.snapPickLayerParams}(n),A=function(e){e.snapPickLayerParams={},e.snapPickLayerNumber=0;for(let t in c){const s=c[t].drawableList;for(let t=0,n=s.length;t<n;t++){const n=s[t];n.drawSnapDepths&&!n.culled&&n.visible&&n.pickable&&n.drawSnapDepths(e)}}return e.snapPickLayerParams}(n),d=r.readArray(a.RGBA_INTEGER,a.INT,Int32Array,4);r.unbind();let f=null;const I=4*s+s*r.size[0]*4,y=d.slice(I,I+4);if(0!==y[3]){const e=p[Math.abs(y[3])],t=e.origin,s=e.coordinateScale;f=[y[0]*s[0]+t[0],y[1]*s[1]+t[1],y[2]*s[2]+t[2]]}let m=[];for(let e=0;e<d.length;e+=4)if(d[e+3]>0){const t=Math.floor(e/4),s=r.size[0];r.size[1];const n=t%s-Math.floor(s/2),i=Math.floor(t/s)-Math.floor(s/2),a=Math.sqrt(Math.pow(n,2)+Math.pow(i,2));m.push({x:n,y:i,dist:a,result:[d[e+0],d[e+1],d[e+2],d[e+3]]})}let v=null;if(m.length>0){m.sort(((e,t)=>e.dist-t.dist)),m=m[0].result;const e=A[m[3]],t=e.origin,s=e.coordinateScale;v=[m[0]*s[0]+t[0],m[1]*s[1]+t[1],m[2]*s[2]+t[2]]}if(null===f&&null==v)return null;let w=null;return null!==v&&(w=t.camera.projectWorldPos(v)),{worldPos:f,snappedWorldPos:v,snappedCanvasPos:w}},this.addMarker=function(e){this._occlusionTester=this._occlusionTester||new De(t,g),this._occlusionTester.addMarker(e),t.occlusionTestCountdown=0},this.markerWorldPosUpdated=function(e){this._occlusionTester.markerWorldPosUpdated(e)},this.removeMarker=function(e){this._occlusionTester.removeMarker(e)},this.doOcclusionTest=function(){if(this._occlusionTester&&this._occlusionTester.needOcclusionTest){D(),this._occlusionTester.bindRenderBuf(),n.reset(),n.backfaces=!0,n.frontface=!0,a.viewport(0,0,a.drawingBufferWidth,a.drawingBufferHeight),a.clearColor(0,0,0,0),a.enable(a.DEPTH_TEST),a.disable(a.CULL_FACE),a.disable(a.BLEND),a.clear(a.COLOR_BUFFER_BIT|a.DEPTH_BUFFER_BIT);for(let e in c)if(c.hasOwnProperty(e)){const t=c[e].drawableList;for(let e=0,s=t.length;e<s;e++){const s=t[e];s.drawOcclusion&&!0!==s.culled&&!1!==s.visible&&!1!==s.pickable&&s.drawOcclusion(n)}}this._occlusionTester.drawMarkers(n),this._occlusionTester.doOcclusionTest(),this._occlusionTester.unbindRenderBuf()}},this.readPixels=function(e,t,s,n){const i=g.getRenderBuffer("snapshot");let a,r,l,o;for(i.bind(),i.clear(),this.render({force:!0,opaqueOnly:n}),r=0;r<s;r++)l=2*r,o=4*r,a=i.read(e[l],e[l+1]),t[o]=a[0],t[o+1]=a[1],t[o+2]=a[2],t[o+3]=a[3];i.unbind(),f=!0},this.beginSnapshot=function(e={}){const t=g.getRenderBuffer("snapshot");e.width&&e.height&&t.setSize([e.width,e.height]),t.bind(),t.clear(),T=!0},this.renderSnapshot=function(){if(!T)return;g.getRenderBuffer("snapshot").clear(),this.render({force:!0,opaqueOnly:!1}),f=!0},this.readSnapshot=function(e){return g.getRenderBuffer("snapshot").readImage(e)},this.readSnapshotAsCanvas=function(){return g.getRenderBuffer("snapshot").readImageAsCanvas()},this.endSnapshot=function(){if(!T)return;g.getRenderBuffer("snapshot").unbind(),T=!1},this.destroy=function(){c={},u={},g.destroy(),E.destroy(),b.destroy(),this._occlusionTester&&this._occlusionTester.destroy()}};class Ue extends C{constructor(e,t={}){super(e,t),this.KEY_BACKSPACE=8,this.KEY_TAB=9,this.KEY_ENTER=13,this.KEY_SHIFT=16,this.KEY_CTRL=17,this.KEY_ALT=18,this.KEY_PAUSE_BREAK=19,this.KEY_CAPS_LOCK=20,this.KEY_ESCAPE=27,this.KEY_PAGE_UP=33,this.KEY_PAGE_DOWN=34,this.KEY_END=35,this.KEY_HOME=36,this.KEY_LEFT_ARROW=37,this.KEY_UP_ARROW=38,this.KEY_RIGHT_ARROW=39,this.KEY_DOWN_ARROW=40,this.KEY_INSERT=45,this.KEY_DELETE=46,this.KEY_NUM_0=48,this.KEY_NUM_1=49,this.KEY_NUM_2=50,this.KEY_NUM_3=51,this.KEY_NUM_4=52,this.KEY_NUM_5=53,this.KEY_NUM_6=54,this.KEY_NUM_7=55,this.KEY_NUM_8=56,this.KEY_NUM_9=57,this.KEY_A=65,this.KEY_B=66,this.KEY_C=67,this.KEY_D=68,this.KEY_E=69,this.KEY_F=70,this.KEY_G=71,this.KEY_H=72,this.KEY_I=73,this.KEY_J=74,this.KEY_K=75,this.KEY_L=76,this.KEY_M=77,this.KEY_N=78,this.KEY_O=79,this.KEY_P=80,this.KEY_Q=81,this.KEY_R=82,this.KEY_S=83,this.KEY_T=84,this.KEY_U=85,this.KEY_V=86,this.KEY_W=87,this.KEY_X=88,this.KEY_Y=89,this.KEY_Z=90,this.KEY_LEFT_WINDOW=91,this.KEY_RIGHT_WINDOW=92,this.KEY_SELECT_KEY=93,this.KEY_NUMPAD_0=96,this.KEY_NUMPAD_1=97,this.KEY_NUMPAD_2=98,this.KEY_NUMPAD_3=99,this.KEY_NUMPAD_4=100,this.KEY_NUMPAD_5=101,this.KEY_NUMPAD_6=102,this.KEY_NUMPAD_7=103,this.KEY_NUMPAD_8=104,this.KEY_NUMPAD_9=105,this.KEY_MULTIPLY=106,this.KEY_ADD=107,this.KEY_SUBTRACT=109,this.KEY_DECIMAL_POINT=110,this.KEY_DIVIDE=111,this.KEY_F1=112,this.KEY_F2=113,this.KEY_F3=114,this.KEY_F4=115,this.KEY_F5=116,this.KEY_F6=117,this.KEY_F7=118,this.KEY_F8=119,this.KEY_F9=120,this.KEY_F10=121,this.KEY_F11=122,this.KEY_F12=123,this.KEY_NUM_LOCK=144,this.KEY_SCROLL_LOCK=145,this.KEY_SEMI_COLON=186,this.KEY_EQUAL_SIGN=187,this.KEY_COMMA=188,this.KEY_DASH=189,this.KEY_PERIOD=190,this.KEY_FORWARD_SLASH=191,this.KEY_GRAVE_ACCENT=192,this.KEY_OPEN_BRACKET=219,this.KEY_BACK_SLASH=220,this.KEY_CLOSE_BRACKET=221,this.KEY_SINGLE_QUOTE=222,this.KEY_SPACE=32,this.element=t.element,this.altDown=!1,this.ctrlDown=!1,this.mouseDownLeft=!1,this.mouseDownMiddle=!1,this.mouseDownRight=!1,this.keyDown=[],this.enabled=!0,this.keyboardEnabled=!0,this.mouseover=!1,this.mouseCanvasPos=h.vec2(),this._keyboardEventsElement=t.keyboardEventsElement||document,this._bindEvents()}_bindEvents(){if(!this._eventsBound){this._keyboardEventsElement.addEventListener("keydown",this._keyDownListener=e=>{this.enabled&&this.keyboardEnabled&&"INPUT"!==e.target.tagName&&"TEXTAREA"!==e.target.tagName&&(e.keyCode===this.KEY_CTRL?this.ctrlDown=!0:e.keyCode===this.KEY_ALT?this.altDown=!0:e.keyCode===this.KEY_SHIFT&&(this.shiftDown=!0),this.keyDown[e.keyCode]=!0,this.fire("keydown",e.keyCode,!0))},!1),this._keyboardEventsElement.addEventListener("keyup",this._keyUpListener=e=>{this.enabled&&this.keyboardEnabled&&"INPUT"!==e.target.tagName&&"TEXTAREA"!==e.target.tagName&&(e.keyCode===this.KEY_CTRL?this.ctrlDown=!1:e.keyCode===this.KEY_ALT?this.altDown=!1:e.keyCode===this.KEY_SHIFT&&(this.shiftDown=!1),this.keyDown[e.keyCode]=!1,this.fire("keyup",e.keyCode,!0))}),this.element.addEventListener("mouseenter",this._mouseEnterListener=e=>{this.enabled&&(this.mouseover=!0,this._getMouseCanvasPos(e),this.fire("mouseenter",this.mouseCanvasPos,!0))}),this.element.addEventListener("mouseleave",this._mouseLeaveListener=e=>{this.enabled&&(this.mouseover=!1,this._getMouseCanvasPos(e),this.fire("mouseleave",this.mouseCanvasPos,!0))}),this.element.addEventListener("mousedown",this._mouseDownListener=e=>{if(this.enabled){switch(e.which){case 1:this.mouseDownLeft=!0;break;case 2:this.mouseDownMiddle=!0;break;case 3:this.mouseDownRight=!0}this._getMouseCanvasPos(e),this.element.focus(),this.fire("mousedown",this.mouseCanvasPos,!0),this.mouseover&&e.preventDefault()}}),document.addEventListener("mouseup",this._mouseUpListener=e=>{if(this.enabled){switch(e.which){case 1:this.mouseDownLeft=!1;break;case 2:this.mouseDownMiddle=!1;break;case 3:this.mouseDownRight=!1}this.fire("mouseup",this.mouseCanvasPos,!0)}},!0),document.addEventListener("click",this._clickListener=e=>{if(this.enabled){switch(e.which){case 1:case 3:this.mouseDownLeft=!1,this.mouseDownRight=!1;break;case 2:this.mouseDownMiddle=!1}this._getMouseCanvasPos(e),this.fire("click",this.mouseCanvasPos,!0),this.mouseover&&e.preventDefault()}}),document.addEventListener("dblclick",this._dblClickListener=e=>{if(this.enabled){switch(e.which){case 1:case 3:this.mouseDownLeft=!1,this.mouseDownRight=!1;break;case 2:this.mouseDownMiddle=!1}this._getMouseCanvasPos(e),this.fire("dblclick",this.mouseCanvasPos,!0),this.mouseover&&e.preventDefault()}}),this.element.addEventListener("mousemove",this._mouseMoveListener=e=>{this.enabled&&(this._getMouseCanvasPos(e),this.fire("mousemove",this.mouseCanvasPos,!0),this.mouseover&&e.preventDefault())}),this.element.addEventListener("wheel",this._mouseWheelListener=(e,t)=>{if(!this.enabled)return;const s=Math.max(-1,Math.min(1,40*-e.deltaY));this.fire("mousewheel",s,!0)},{passive:!0});{let e,t;const s=2;this.on("mousedown",(s=>{e=s[0],t=s[1]})),this.on("mouseup",(n=>{e>=n[0]-s&&e<=n[0]+s&&t>=n[1]-s&&t<=n[1]+s&&this.fire("mouseclicked",n,!0)}))}this._eventsBound=!0}}_unbindEvents(){this._eventsBound&&(this._keyboardEventsElement.removeEventListener("keydown",this._keyDownListener),this._keyboardEventsElement.removeEventListener("keyup",this._keyUpListener),this.element.removeEventListener("mouseenter",this._mouseEnterListener),this.element.removeEventListener("mouseleave",this._mouseLeaveListener),this.element.removeEventListener("mousedown",this._mouseDownListener),document.removeEventListener("mouseup",this._mouseDownListener),document.removeEventListener("click",this._clickListener),document.removeEventListener("dblclick",this._dblClickListener),this.element.removeEventListener("mousemove",this._mouseMoveListener),this.element.removeEventListener("wheel",this._mouseWheelListener),window.OrientationChangeEvent&&window.removeEventListener("orientationchange",this._orientationchangedListener),window.DeviceMotionEvent&&window.removeEventListener("devicemotion",this._deviceMotionListener),window.DeviceOrientationEvent&&window.removeEventListener("deviceorientation",this._deviceOrientListener),this._eventsBound=!1)}_getMouseCanvasPos(e){if(e){let t=e.target,s=0,n=0;for(;t.offsetParent;)s+=t.offsetLeft,n+=t.offsetTop,t=t.offsetParent;this.mouseCanvasPos[0]=e.pageX-s,this.mouseCanvasPos[1]=e.pageY-n}else e=window.event,this.mouseCanvasPos[0]=e.x,this.mouseCanvasPos[1]=e.y}setEnabled(e){this.enabled!==e&&this.fire("enabled",this.enabled=e)}getEnabled(){return this.enabled}setKeyboardEnabled(e){this.keyboardEnabled=e}getKeyboardEnabled(){return this.keyboardEnabled}destroy(){super.destroy(),this._unbindEvents()}}const Ge=new e({});class Ve{constructor(e){this.id=Ge.addItem({});for(const t in e)e.hasOwnProperty(t)&&(this[t]=e[t])}destroy(){Ge.removeItem(this.id)}}class je extends C{get type(){return"Viewport"}constructor(e,t={}){super(e,t),this._state=new Ve({boundary:[0,0,100,100]}),this.boundary=t.boundary,this.autoBoundary=t.autoBoundary}set boundary(e){if(!this._autoBoundary){if(!e){const t=this.scene.canvas.boundary;e=[0,0,t[2],t[3]]}this._state.boundary=e,this.glRedraw(),this.fire("boundary",this._state.boundary)}}get boundary(){return this._state.boundary}set autoBoundary(e){(e=!!e)!==this._autoBoundary&&(this._autoBoundary=e,this._autoBoundary?this._onCanvasSize=this.scene.canvas.on("boundary",(function(e){const t=e[2],s=e[3];this._state.boundary=[0,0,t,s],this.glRedraw(),this.fire("boundary",this._state.boundary)}),this):this._onCanvasSize&&(this.scene.canvas.off(this._onCanvasSize),this._onCanvasSize=null),this.fire("autoBoundary",this._autoBoundary))}get autoBoundary(){return this._autoBoundary}_getState(){return this._state}destroy(){super.destroy(),this._state.destroy()}}class ke extends C{get type(){return"Perspective"}constructor(e,t={}){super(e,t),this.camera=e,this._state=new Ve({matrix:h.mat4(),inverseMatrix:h.mat4(),transposedMatrix:h.mat4(),near:.1,far:2e3}),this._inverseMatrixDirty=!0,this._transposedMatrixDirty=!0,this._fov=60,this._canvasResized=this.scene.canvas.on("boundary",this._needUpdate,this),this.fov=t.fov,this.fovAxis=t.fovAxis,this.near=t.near,this.far=t.far}_update(){const e=this.scene.canvas.boundary,t=e[2]/e[3],s=this._fovAxis;let n=this._fov;("x"===s||"min"===s&&t<1||"max"===s&&t>1)&&(n/=t),n=Math.min(n,120),h.perspectiveMat4(n*(Math.PI/180),t,this._state.near,this._state.far,this._state.matrix),this._inverseMatrixDirty=!0,this._transposedMatrixDirty=!0,this.glRedraw(),this.camera._updateScheduled=!0,this.fire("matrix",this._state.matrix)}set fov(e){(e=null!=e?e:60)!==this._fov&&(this._fov=e,this._needUpdate(0),this.fire("fov",this._fov))}get fov(){return this._fov}set fovAxis(e){e=e||"min",this._fovAxis!==e&&("x"!==e&&"y"!==e&&"min"!==e&&(this.error("Unsupported value for 'fovAxis': "+e+" - defaulting to 'min'"),e="min"),this._fovAxis=e,this._needUpdate(0),this.fire("fovAxis",this._fovAxis))}get fovAxis(){return this._fovAxis}set near(e){const t=null!=e?e:.1;this._state.near!==t&&(this._state.near=t,this._needUpdate(0),this.fire("near",this._state.near))}get near(){return this._state.near}set far(e){const t=null!=e?e:2e3;this._state.far!==t&&(this._state.far=t,this._needUpdate(0),this.fire("far",this._state.far))}get far(){return this._state.far}get matrix(){return this._updateScheduled&&this._doUpdate(),this._state.matrix}get inverseMatrix(){return this._updateScheduled&&this._doUpdate(),this._inverseMatrixDirty&&(h.inverseMat4(this._state.matrix,this._state.inverseMatrix),this._inverseMatrixDirty=!1),this._state.inverseMatrix}get transposedMatrix(){return this._updateScheduled&&this._doUpdate(),this._transposedMatrixDirty&&(h.transposeMat4(this._state.matrix,this._state.transposedMatrix),this._transposedMatrixDirty=!1),this._state.transposedMatrix}unproject(e,t,s,n,i){const a=this.scene.canvas.canvas,r=a.offsetWidth/2,l=a.offsetHeight/2;return s[0]=(e[0]-r)/r,s[1]=(e[1]-l)/l,s[2]=t,s[3]=1,h.mulMat4v4(this.inverseMatrix,s,n),h.mulVec3Scalar(n,1/n[3]),n[3]=1,n[1]*=-1,h.mulMat4v4(this.camera.inverseViewMatrix,n,i),i}destroy(){super.destroy(),this._state.destroy(),this.scene.canvas.off(this._canvasResized)}}class Qe extends C{get type(){return"Ortho"}constructor(e,t={}){super(e,t),this.camera=e,this._state=new Ve({matrix:h.mat4(),inverseMatrix:h.mat4(),transposedMatrix:h.mat4(),near:.1,far:2e3}),this._inverseMatrixDirty=!0,this._transposedMatrixDirty=!0,this.scale=t.scale,this.near=t.near,this.far=t.far,this._onCanvasBoundary=this.scene.canvas.on("boundary",this._needUpdate,this)}_update(){const e=this.scene,t=.5*this._scale,s=e.canvas.boundary,n=s[2],i=s[3],a=n/i;let r,l,o,c;n>i?(r=-t,l=t,o=t/a,c=-t/a):(r=-t*a,l=t*a,o=t,c=-t),h.orthoMat4c(r,l,c,o,this._state.near,this._state.far,this._state.matrix),this._inverseMatrixDirty=!0,this._transposedMatrixDirty=!0,this.glRedraw(),this.fire("matrix",this._state.matrix)}set scale(e){null==e&&(e=1),e<=0&&(e=.01),this._scale=e,this._needUpdate(0),this.fire("scale",this._scale)}get scale(){return this._scale}set near(e){const t=null!=e?e:.1;this._state.near!==t&&(this._state.near=t,this._needUpdate(0),this.fire("near",this._state.near))}get near(){return this._state.near}set far(e){const t=null!=e?e:2e3;this._state.far!==t&&(this._state.far=t,this._needUpdate(0),this.fire("far",this._state.far))}get far(){return this._state.far}get matrix(){return this._updateScheduled&&this._doUpdate(),this._state.matrix}get inverseMatrix(){return this._updateScheduled&&this._doUpdate(),this._inverseMatrixDirty&&(h.inverseMat4(this._state.matrix,this._state.inverseMatrix),this._inverseMatrixDirty=!1),this._state.inverseMatrix}get transposedMatrix(){return this._updateScheduled&&this._doUpdate(),this._transposedMatrixDirty&&(h.transposeMat4(this._state.matrix,this._state.transposedMatrix),this._transposedMatrixDirty=!1),this._state.transposedMatrix}unproject(e,t,s,n,i){const a=this.scene.canvas.canvas,r=a.offsetWidth/2,l=a.offsetHeight/2;return s[0]=(e[0]-r)/r,s[1]=(e[1]-l)/l,s[2]=t,s[3]=1,h.mulMat4v4(this.inverseMatrix,s,n),h.mulVec3Scalar(n,1/n[3]),n[3]=1,n[1]*=-1,h.mulMat4v4(this.camera.inverseViewMatrix,n,i),i}destroy(){super.destroy(),this._state.destroy(),this.scene.canvas.off(this._onCanvasBoundary)}}class We extends C{get type(){return"Frustum"}constructor(e,t={}){super(e,t),this.camera=e,this._state=new Ve({matrix:h.mat4(),inverseMatrix:h.mat4(),transposedMatrix:h.mat4(),near:.1,far:1e4}),this._left=-1,this._right=1,this._bottom=-1,this._top=1,this._inverseMatrixDirty=!0,this._transposedMatrixDirty=!0,this.left=t.left,this.right=t.right,this.bottom=t.bottom,this.top=t.top,this.near=t.near,this.far=t.far}_update(){h.frustumMat4(this._left,this._right,this._bottom,this._top,this._state.near,this._state.far,this._state.matrix),this._inverseMatrixDirty=!0,this._transposedMatrixDirty=!0,this.glRedraw(),this.fire("matrix",this._state.matrix)}set left(e){this._left=null!=e?e:-1,this._needUpdate(0),this.fire("left",this._left)}get left(){return this._left}set right(e){this._right=null!=e?e:1,this._needUpdate(0),this.fire("right",this._right)}get right(){return this._right}set top(e){this._top=null!=e?e:1,this._needUpdate(0),this.fire("top",this._top)}get top(){return this._top}set bottom(e){this._bottom=null!=e?e:-1,this._needUpdate(0),this.fire("bottom",this._bottom)}get bottom(){return this._bottom}set near(e){this._state.near=null!=e?e:.1,this._needUpdate(0),this.fire("near",this._state.near)}get near(){return this._state.near}set far(e){this._state.far=null!=e?e:1e4,this._needUpdate(0),this.fire("far",this._state.far)}get far(){return this._state.far}get matrix(){return this._updateScheduled&&this._doUpdate(),this._state.matrix}get inverseMatrix(){return this._updateScheduled&&this._doUpdate(),this._inverseMatrixDirty&&(h.inverseMat4(this._state.matrix,this._state.inverseMatrix),this._inverseMatrixDirty=!1),this._state.inverseMatrix}get transposedMatrix(){return this._updateScheduled&&this._doUpdate(),this._transposedMatrixDirty&&(h.transposeMat4(this._state.matrix,this._state.transposedMatrix),this._transposedMatrixDirty=!1),this._state.transposedMatrix}unproject(e,t,s,n,i){const a=this.scene.canvas.canvas,r=a.offsetWidth/2,l=a.offsetHeight/2;return s[0]=(e[0]-r)/r,s[1]=(e[1]-l)/l,s[2]=t,s[3]=1,h.mulMat4v4(this.inverseMatrix,s,n),h.mulVec3Scalar(n,1/n[3]),n[3]=1,n[1]*=-1,h.mulMat4v4(this.camera.inverseViewMatrix,n,i),i}destroy(){super.destroy(),this._state.destroy(),super.destroy()}}class ze extends C{get type(){return"CustomProjection"}constructor(e,t={}){super(e,t),this.camera=e,this._state=new Ve({matrix:h.mat4(),inverseMatrix:h.mat4(),transposedMatrix:h.mat4()}),this._inverseMatrixDirty=!0,this._transposedMatrixDirty=!1,this.matrix=t.matrix}set matrix(e){this._state.matrix.set(e||[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]),this._inverseMatrixDirty=!0,this._transposedMatrixDirty=!0,this.glRedraw(),this.fire("matrix",this._state.matrix)}get matrix(){return this._state.matrix}get inverseMatrix(){return this._updateScheduled&&this._doUpdate(),this._inverseMatrixDirty&&(h.inverseMat4(this._state.matrix,this._state.inverseMatrix),this._inverseMatrixDirty=!1),this._state.inverseMatrix}get transposedMatrix(){return this._updateScheduled&&this._doUpdate(),this._transposedMatrixDirty&&(h.transposeMat4(this._state.matrix,this._state.transposedMatrix),this._transposedMatrixDirty=!1),this._state.transposedMatrix}unproject(e,t,s,n,i){const a=this.scene.canvas.canvas,r=a.offsetWidth/2,l=a.offsetHeight/2;return s[0]=(e[0]-r)/r,s[1]=(e[1]-l)/l,s[2]=t,s[3]=1,h.mulMat4v4(this.inverseMatrix,s,n),h.mulVec3Scalar(n,1/n[3]),n[3]=1,n[1]*=-1,h.mulMat4v4(this.camera.inverseViewMatrix,n,i),i}destroy(){super.destroy(),this._state.destroy()}}const Ke=h.vec3(),Ye=h.vec3(),Xe=h.vec3(),qe=h.vec3(),Je=h.vec3(),Ze=h.vec3(),$e=h.vec4(),et=h.vec4(),tt=h.vec4(),st=h.mat4(),nt=h.mat4(),it=h.vec3(),at=h.vec3(),rt=h.vec3(),lt=h.vec3();class ot extends C{get type(){return"Camera"}constructor(e,t={}){super(e,t),this._state=new Ve({deviceMatrix:h.mat4(),hasDeviceMatrix:!1,matrix:h.mat4(),normalMatrix:h.mat4(),inverseMatrix:h.mat4()}),this._perspective=new ke(this),this._ortho=new Qe(this),this._frustum=new We(this),this._customProjection=new ze(this),this._project=this._perspective,this._eye=h.vec3([0,0,10]),this._look=h.vec3([0,0,0]),this._up=h.vec3([0,1,0]),this._worldUp=h.vec3([0,1,0]),this._worldRight=h.vec3([1,0,0]),this._worldForward=h.vec3([0,0,-1]),this.deviceMatrix=t.deviceMatrix,this.eye=t.eye,this.look=t.look,this.up=t.up,this.worldAxis=t.worldAxis,this.gimbalLock=t.gimbalLock,this.constrainPitch=t.constrainPitch,this.projection=t.projection,this._perspective.on("matrix",(()=>{"perspective"===this._projectionType&&this.fire("projMatrix",this._perspective.matrix)})),this._ortho.on("matrix",(()=>{"ortho"===this._projectionType&&this.fire("projMatrix",this._ortho.matrix)})),this._frustum.on("matrix",(()=>{"frustum"===this._projectionType&&this.fire("projMatrix",this._frustum.matrix)})),this._customProjection.on("matrix",(()=>{"customProjection"===this._projectionType&&this.fire("projMatrix",this._customProjection.matrix)}))}_update(){const e=this._state;let t;"ortho"===this.projection?(h.subVec3(this._eye,this._look,it),h.normalizeVec3(it,at),h.mulVec3Scalar(at,1e3,rt),h.addVec3(this._look,rt,lt),t=lt):t=this._eye,e.hasDeviceMatrix?(h.lookAtMat4v(t,this._look,this._up,nt),h.mulMat4(e.deviceMatrix,nt,e.matrix)):h.lookAtMat4v(t,this._look,this._up,e.matrix),h.inverseMat4(this._state.matrix,this._state.inverseMatrix),h.transposeMat4(this._state.inverseMatrix,this._state.normalMatrix),this.glRedraw(),this.fire("matrix",this._state.matrix),this.fire("viewMatrix",this._state.matrix)}orbitYaw(e){let t=h.subVec3(this._eye,this._look,Ke);h.rotationMat4v(.0174532925*e,this._gimbalLock?this._worldUp:this._up,st),t=h.transformPoint3(st,t,Ye),this.eye=h.addVec3(this._look,t,Xe),this.up=h.transformPoint3(st,this._up,qe)}orbitPitch(e){if(this._constrainPitch&&(e=h.dotVec3(this._up,this._worldUp)/h.DEGTORAD)<1)return;let t=h.subVec3(this._eye,this._look,Ke);const s=h.cross3Vec3(h.normalizeVec3(t,Ye),h.normalizeVec3(this._up,Xe));h.rotationMat4v(.0174532925*e,s,st),t=h.transformPoint3(st,t,qe),this.up=h.transformPoint3(st,this._up,Je),this.eye=h.addVec3(t,this._look,Ze)}yaw(e){let t=h.subVec3(this._look,this._eye,Ke);h.rotationMat4v(.0174532925*e,this._gimbalLock?this._worldUp:this._up,st),t=h.transformPoint3(st,t,Ye),this.look=h.addVec3(t,this._eye,Xe),this._gimbalLock&&(this.up=h.transformPoint3(st,this._up,qe))}pitch(e){if(this._constrainPitch&&(e=h.dotVec3(this._up,this._worldUp)/h.DEGTORAD)<1)return;let t=h.subVec3(this._look,this._eye,Ke);const s=h.cross3Vec3(h.normalizeVec3(t,Ye),h.normalizeVec3(this._up,Xe));h.rotationMat4v(.0174532925*e,s,st),this.up=h.transformPoint3(st,this._up,Ze),t=h.transformPoint3(st,t,qe),this.look=h.addVec3(t,this._eye,Je)}pan(e){const t=h.subVec3(this._eye,this._look,Ke),s=[0,0,0];let n;if(0!==e[0]){const i=h.cross3Vec3(h.normalizeVec3(t,[]),h.normalizeVec3(this._up,Ye));n=h.mulVec3Scalar(i,e[0]),s[0]+=n[0],s[1]+=n[1],s[2]+=n[2]}0!==e[1]&&(n=h.mulVec3Scalar(h.normalizeVec3(this._up,Xe),e[1]),s[0]+=n[0],s[1]+=n[1],s[2]+=n[2]),0!==e[2]&&(n=h.mulVec3Scalar(h.normalizeVec3(t,qe),e[2]),s[0]+=n[0],s[1]+=n[1],s[2]+=n[2]),this.eye=h.addVec3(this._eye,s,Je),this.look=h.addVec3(this._look,s,Ze)}zoom(e){const t=h.subVec3(this._eye,this._look,Ke),s=Math.abs(h.lenVec3(t,Ye)),n=Math.abs(s+e);if(n<.5)return;const i=h.normalizeVec3(t,Xe);this.eye=h.addVec3(this._look,h.mulVec3Scalar(i,n),qe)}set eye(e){this._eye.set(e||[0,0,10]),this._needUpdate(0),this.fire("eye",this._eye)}get eye(){return this._eye}set look(e){this._look.set(e||[0,0,0]),this._needUpdate(0),this.fire("look",this._look)}get look(){return this._look}set up(e){this._up.set(e||[0,1,0]),this._needUpdate(0),this.fire("up",this._up)}get up(){return this._up}set deviceMatrix(e){this._state.deviceMatrix.set(e||[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]),this._state.hasDeviceMatrix=!!e,this._needUpdate(0),this.fire("deviceMatrix",this._state.deviceMatrix)}get deviceMatrix(){return this._state.deviceMatrix}set worldAxis(e){e=e||[1,0,0,0,1,0,0,0,1],this._worldAxis?this._worldAxis.set(e):this._worldAxis=h.vec3(e),this._worldRight[0]=this._worldAxis[0],this._worldRight[1]=this._worldAxis[1],this._worldRight[2]=this._worldAxis[2],this._worldUp[0]=this._worldAxis[3],this._worldUp[1]=this._worldAxis[4],this._worldUp[2]=this._worldAxis[5],this._worldForward[0]=this._worldAxis[6],this._worldForward[1]=this._worldAxis[7],this._worldForward[2]=this._worldAxis[8],this.fire("worldAxis",this._worldAxis)}get worldAxis(){return this._worldAxis}get worldUp(){return this._worldUp}get xUp(){return this._worldUp[0]>this._worldUp[1]&&this._worldUp[0]>this._worldUp[2]}get yUp(){return this._worldUp[1]>this._worldUp[0]&&this._worldUp[1]>this._worldUp[2]}get zUp(){return this._worldUp[2]>this._worldUp[0]&&this._worldUp[2]>this._worldUp[1]}get worldRight(){return this._worldRight}get worldForward(){return this._worldForward}set gimbalLock(e){this._gimbalLock=!1!==e,this.fire("gimbalLock",this._gimbalLock)}get gimbalLock(){return this._gimbalLock}set constrainPitch(e){this._constrainPitch=!!e,this.fire("constrainPitch",this._constrainPitch)}get eyeLookDist(){return h.lenVec3(h.subVec3(this._look,this._eye,Ke))}get matrix(){return this._updateScheduled&&this._doUpdate(),this._state.matrix}get viewMatrix(){return this._updateScheduled&&this._doUpdate(),this._state.matrix}get normalMatrix(){return this._updateScheduled&&this._doUpdate(),this._state.normalMatrix}get viewNormalMatrix(){return this._updateScheduled&&this._doUpdate(),this._state.normalMatrix}get inverseViewMatrix(){return this._updateScheduled&&this._doUpdate(),this._state.inverseMatrix}get projMatrix(){return this[this.projection].matrix}get perspective(){return this._perspective}get ortho(){return this._ortho}get frustum(){return this._frustum}get customProjection(){return this._customProjection}set projection(e){e=e||"perspective",this._projectionType!==e&&("perspective"===e?this._project=this._perspective:"ortho"===e?this._project=this._ortho:"frustum"===e?this._project=this._frustum:"customProjection"===e?this._project=this._customProjection:(this.error("Unsupported value for 'projection': "+e+" defaulting to 'perspective'"),this._project=this._perspective,e="perspective"),this._project._update(),this._projectionType=e,this.glRedraw(),this._update(),this.fire("dirty"),this.fire("projection",this._projectionType),this.fire("projMatrix",this._project.matrix))}get projection(){return this._projectionType}get project(){return this._project}projectWorldPos(e){const t=$e,s=et,n=tt;t[0]=e[0],t[1]=e[1],t[2]=e[2],t[3]=1,h.mulMat4v4(this.viewMatrix,t,s),h.mulMat4v4(this.projMatrix,s,n),h.mulVec3Scalar(n,1/n[3]),n[3]=1,n[1]*=-1;const i=this.scene.canvas.canvas,a=i.offsetWidth/2,r=i.offsetHeight/2;return[n[0]*a+a,n[1]*r+r]}destroy(){super.destroy(),this._state.destroy()}}class ct extends C{get type(){return"Light"}get isLight(){return!0}constructor(e,t={}){super(e,t)}}class ut extends ct{get type(){return"DirLight"}constructor(e,t={}){super(e,t),this._shadowRenderBuf=null,this._shadowViewMatrix=null,this._shadowProjMatrix=null,this._shadowViewMatrixDirty=!0,this._shadowProjMatrixDirty=!0;const s=this.scene.camera,n=this.scene.canvas;this._onCameraViewMatrix=s.on("viewMatrix",(()=>{this._shadowViewMatrixDirty=!0})),this._onCameraProjMatrix=s.on("projMatrix",(()=>{this._shadowProjMatrixDirty=!0})),this._onCanvasBoundary=n.on("boundary",(()=>{this._shadowProjMatrixDirty=!0})),this._state=new Ve({type:"dir",dir:h.vec3([1,1,1]),color:h.vec3([.7,.7,.8]),intensity:1,space:t.space||"view",castsShadow:!1,getShadowViewMatrix:()=>{if(this._shadowViewMatrixDirty){this._shadowViewMatrix||(this._shadowViewMatrix=h.identityMat4());const e=this.scene.camera,t=this._state.dir,s=e.look,n=[s[0]-t[0],s[1]-t[1],s[2]-t[2]],i=[0,1,0];h.lookAtMat4v(n,s,i,this._shadowViewMatrix),this._shadowViewMatrixDirty=!1}return this._shadowViewMatrix},getShadowProjMatrix:()=>(this._shadowProjMatrixDirty&&(this._shadowProjMatrix||(this._shadowProjMatrix=h.identityMat4()),h.orthoMat4c(-40,40,-40,40,-40,80,this._shadowProjMatrix),this._shadowProjMatrixDirty=!1),this._shadowProjMatrix),getShadowRenderBuf:()=>(this._shadowRenderBuf||(this._shadowRenderBuf=new Le(this.scene.canvas.canvas,this.scene.canvas.gl,{size:[1024,1024]})),this._shadowRenderBuf)}),this.dir=t.dir,this.color=t.color,this.intensity=t.intensity,this.castsShadow=t.castsShadow,this.scene._lightCreated(this)}set dir(e){this._state.dir.set(e||[1,1,1]),this._shadowViewMatrixDirty=!0,this.glRedraw()}get dir(){return this._state.dir}set color(e){this._state.color.set(e||[.7,.7,.8]),this.glRedraw()}get color(){return this._state.color}set intensity(e){e=void 0!==e?e:1,this._state.intensity=e,this.glRedraw()}get intensity(){return this._state.intensity}set castsShadow(e){e=!!e,this._state.castsShadow!==e&&(this._state.castsShadow=e,this._shadowViewMatrixDirty=!0,this.glRedraw())}get castsShadow(){return this._state.castsShadow}destroy(){const e=this.scene.camera,t=this.scene.canvas;e.off(this._onCameraViewMatrix),e.off(this._onCameraProjMatrix),t.off(this._onCanvasBoundary),super.destroy(),this._state.destroy(),this._shadowRenderBuf&&this._shadowRenderBuf.destroy(),this.scene._lightDestroyed(this),this.glRedraw()}}class ht extends ct{get type(){return"AmbientLight"}constructor(e,t={}){super(e,t),this._state={type:"ambient",color:h.vec3([.7,.7,.7]),intensity:1},this.color=t.color,this.intensity=t.intensity,this.scene._lightCreated(this)}set color(e){this._state.color.set(e||[.7,.7,.8]),this.glRedraw()}get color(){return this._state.color}set intensity(e){this._state.intensity=void 0!==e?e:1,this.glRedraw()}get intensity(){return this._state.intensity}destroy(){super.destroy(),this.scene._lightDestroyed(this)}}class pt extends C{get type(){return"Geometry"}get isGeometry(){return!0}constructor(e,t={}){super(e,t),A.memory.meshes++}destroy(){super.destroy(),A.memory.meshes--}}var At=function(){const e=[],t=[],s=[],n=[],i=[];let a=0;const r=new Uint16Array(3),l=new Uint16Array(3),o=new Uint16Array(3),c=h.vec3(),u=h.vec3(),p=h.vec3(),A=h.vec3(),d=h.vec3(),f=h.vec3(),I=h.vec3();return function(y,m,v,w){!function(i,a){const r={};let l,o,c,u;const h=Math.pow(10,4);let p,A,d=0;for(p=0,A=i.length;p<A;p+=3)l=i[p],o=i[p+1],c=i[p+2],u=Math.round(l*h)+"_"+Math.round(o*h)+"_"+Math.round(c*h),void 0===r[u]&&(r[u]=d/3,e[d++]=l,e[d++]=o,e[d++]=c),t[p/3]=r[u];for(p=0,A=a.length;p<A;p++)n[p]=t[a[p]],s[n[p]]=a[p]}(y,m),function(t,s){a=0;for(let y=0,m=t;y<m;y+=3){const t=3*n[y],m=3*n[y+1],v=3*n[y+2];s?(r[0]=e[t],r[1]=e[t+1],r[2]=e[t+2],l[0]=e[m],l[1]=e[m+1],l[2]=e[m+2],o[0]=e[v],o[1]=e[v+1],o[2]=e[v+2],h.decompressPosition(r,s,c),h.decompressPosition(l,s,u),h.decompressPosition(o,s,p)):(c[0]=e[t],c[1]=e[t+1],c[2]=e[t+2],u[0]=e[m],u[1]=e[m+1],u[2]=e[m+2],p[0]=e[v],p[1]=e[v+1],p[2]=e[v+2]),h.subVec3(p,u,A),h.subVec3(c,u,d),h.cross3Vec3(A,d,f),h.normalizeVec3(f,I);const w=i[a]||(i[a]={normal:h.vec3()});w.normal[0]=I[0],w.normal[1]=I[1],w.normal[2]=I[2],a++}}(m.length,v);const g=[],T=Math.cos(h.DEGTORAD*w),E={};let b,D,P,R,C,_,B,O,S,N,x,L=!1;for(let e=0,t=m.length;e<t;e+=3){const t=e/3;for(let s=0;s<3;s++)b=n[e+s],D=n[e+(s+1)%3],P=Math.min(b,D),R=Math.max(b,D),C=P+","+R,void 0===E[C]?E[C]={index1:P,index2:R,face1:t,face2:void 0}:E[C].face2=t}for(C in E)_=E[C],void 0!==_.face2&&(B=i[_.face1].normal,O=i[_.face2].normal,S=h.dotVec3(B,O),S>T)||(N=s[_.index1],x=s[_.index2],(!L&&N>65535||x>65535)&&(L=!0),g.push(N),g.push(x));return L?new Uint32Array(g):new Uint16Array(g)}}();const dt=function(){const e=h.mat4(),t=h.mat4();return function(s,n){n=n||h.mat4();const i=s[0],a=s[1],r=s[2],l=s[3]-i,o=s[4]-a,c=s[5]-r,u=65535;return h.identityMat4(e),h.translationMat4v(s,e),h.identityMat4(t),h.scalingMat4v([l/u,o/u,c/u],t),h.mulMat4(e,t,n),n}}();var ft=function(){const e=h.mat4(),t=h.mat4();return function(s,n,i){const a=new Uint16Array(s.length),r=new Float32Array([i[0]!==n[0]?65535/(i[0]-n[0]):0,i[1]!==n[1]?65535/(i[1]-n[1]):0,i[2]!==n[2]?65535/(i[2]-n[2]):0]);let l;for(l=0;l<s.length;l+=3)a[l+0]=Math.max(0,Math.min(65535,Math.floor((s[l+0]-n[0])*r[0]))),a[l+1]=Math.max(0,Math.min(65535,Math.floor((s[l+1]-n[1])*r[1]))),a[l+2]=Math.max(0,Math.min(65535,Math.floor((s[l+2]-n[2])*r[2])));h.identityMat4(e),h.translationMat4v(n,e),h.identityMat4(t),h.scalingMat4v([(i[0]-n[0])/65535,(i[1]-n[1])/65535,(i[2]-n[2])/65535],t);return{quantized:a,decodeMatrix:h.mulMat4(e,t,h.identityMat4())}}}();var It=function(){const e=h.mat3(),t=h.mat3();return function(s,n,i){const a=new Uint16Array(s.length),r=new Float32Array([65535/(i[0]-n[0]),65535/(i[1]-n[1])]);let l;for(l=0;l<s.length;l+=2)a[l+0]=Math.max(0,Math.min(65535,Math.floor((s[l+0]-n[0])*r[0]))),a[l+1]=Math.max(0,Math.min(65535,Math.floor((s[l+1]-n[1])*r[1])));h.identityMat3(e),h.translationMat3v(n,e),h.identityMat3(t),h.scalingMat3v([(i[0]-n[0])/65535,(i[1]-n[1])/65535],t);return{quantized:a,decodeMatrix:h.mulMat3(e,t,h.identityMat3())}}}();function yt(e,t,s,n){let i=e[t]/(Math.abs(e[t])+Math.abs(e[t+1])+Math.abs(e[t+2])),a=e[t+1]/(Math.abs(e[t])+Math.abs(e[t+1])+Math.abs(e[t+2]));if(e[t+2]<0){let e=(1-Math.abs(a))*(i>=0?1:-1),t=(1-Math.abs(i))*(a>=0?1:-1);i=e,a=t}return new Int8Array([Math[s](127.5*i+(i<0?-1:0)),Math[n](127.5*a+(a<0?-1:0))])}function mt(e){let t=e[0],s=e[1];t/=t<0?127:128,s/=s<0?127:128;const n=1-Math.abs(t)-Math.abs(s);n<0&&(t=(1-Math.abs(s))*(t>=0?1:-1),s=(1-Math.abs(t))*(s>=0?1:-1));const i=Math.sqrt(t*t+s*s+n*n);return[t/i,s/i,n/i]}function vt(e,t,s){return e[t]*s[0]+e[t+1]*s[1]+e[t+2]*s[2]}const wt={getPositionsBounds:function(e){const t=new Float32Array(3),s=new Float32Array(3);let n,i;for(n=0;n<3;n++)t[n]=Number.MAX_VALUE,s[n]=-Number.MAX_VALUE;for(n=0;n<e.length;n+=3)for(i=0;i<3;i++)t[i]=Math.min(t[i],e[n+i]),s[i]=Math.max(s[i],e[n+i]);return{min:t,max:s}},createPositionsDecodeMatrix:dt,compressPositions:ft,compressPosition:function(e,t,s){const n=new Float32Array([t[3]!==t[0]?65535/(t[3]-t[0]):0,t[4]!==t[1]?65535/(t[4]-t[1]):0,t[5]!==t[2]?65535/(t[5]-t[2]):0]);s[0]=Math.max(0,Math.min(65535,Math.floor((e[0]-t[0])*n[0]))),s[1]=Math.max(0,Math.min(65535,Math.floor((e[1]-t[1])*n[1]))),s[2]=Math.max(0,Math.min(65535,Math.floor((e[2]-t[2])*n[2])))},decompressPositions:function(e,t,s=new Float32Array(e.length)){for(let n=0,i=e.length;n<i;n+=3)s[n+0]=e[n+0]*t[0]+t[12],s[n+1]=e[n+1]*t[5]+t[13],s[n+2]=e[n+2]*t[10]+t[14];return s},decompressPosition:function(e,t,s){return s[0]=e[0]*t[0]+t[12],s[1]=e[1]*t[5]+t[13],s[2]=e[2]*t[10]+t[14],s},decompressAABB:function(e,t,s=e){return s[0]=e[0]*t[0]+t[12],s[1]=e[1]*t[5]+t[13],s[2]=e[2]*t[10]+t[14],s[3]=e[3]*t[0]+t[12],s[4]=e[4]*t[5]+t[13],s[5]=e[5]*t[10]+t[14],s},getUVBounds:function(e){const t=new Float32Array(2),s=new Float32Array(2);let n,i;for(n=0;n<2;n++)t[n]=Number.MAX_VALUE,s[n]=-Number.MAX_VALUE;for(n=0;n<e.length;n+=2)for(i=0;i<2;i++)t[i]=Math.min(t[i],e[n+i]),s[i]=Math.max(s[i],e[n+i]);return{min:t,max:s}},compressUVs:It,decompressUVs:function(e,t,s=new Float32Array(e.length)){for(let n=0,i=e.length;n<i;n+=3)s[n+0]=e[n+0]*t[0]+t[6],s[n+1]=e[n+1]*t[4]+t[7];return s},decompressUV:function(e,t,s){s[0]=e[0]*t[0]+t[6],s[1]=e[1]*t[4]+t[7]},compressNormals:function(e){const t=new Int8Array(e.length);let s,n,i,a,r;for(let l=0;l<e.length;l+=3)i=s=yt(e,l,"floor","floor"),n=mt(s),a=r=vt(e,l,n),s=yt(e,l,"ceil","floor"),n=mt(s),a=vt(e,l,n),a>r&&(i=s,r=a),s=yt(e,l,"floor","ceil"),n=mt(s),a=vt(e,l,n),a>r&&(i=s,r=a),s=yt(e,l,"ceil","ceil"),n=mt(s),a=vt(e,l,n),a>r&&(i=s,r=a),t[l]=i[0],t[l+1]=i[1];return t},decompressNormals:function(e,t){for(let s=0,n=0,i=e.length;s<i;s+=2){let i=e[s+0],a=e[s+1];i=(2*i+1)/255,a=(2*a+1)/255;const r=1-Math.abs(i)-Math.abs(a);r<0&&(i=(1-Math.abs(a))*(i>=0?1:-1),a=(1-Math.abs(i))*(a>=0?1:-1));const l=Math.sqrt(i*i+a*a+r*r);t[n+0]=i/l,t[n+1]=a/l,t[n+2]=r/l,n+=3}return t},decompressNormal:function(e,t){let s=e[0],n=e[1];s=(2*s+1)/255,n=(2*n+1)/255;const i=1-Math.abs(s)-Math.abs(n);i<0&&(s=(1-Math.abs(n))*(s>=0?1:-1),n=(1-Math.abs(s))*(n>=0?1:-1));const a=Math.sqrt(s*s+n*n+i*i);return t[0]=s/a,t[1]=n/a,t[2]=i/a,t}},gt=A.memory,Tt=h.AABB3();class Et extends pt{get type(){return"ReadableGeometry"}get isReadableGeometry(){return!0}constructor(e,t={}){super(e,t),this._state=new Ve({compressGeometry:!!t.compressGeometry,primitive:null,primitiveName:null,positions:null,normals:null,colors:null,uv:null,indices:null,positionsDecodeMatrix:null,uvDecodeMatrix:null,positionsBuf:null,normalsBuf:null,colorsbuf:null,uvBuf:null,indicesBuf:null,hash:""}),this._numTriangles=0,this._edgeThreshold=t.edgeThreshold||10,this._edgeIndicesBuf=null,this._pickTrianglePositionsBuf=null,this._pickTriangleColorsBuf=null,this._aabbDirty=!0,this._boundingSphere=!0,this._aabb=null,this._aabbDirty=!0,this._obb=null,this._obbDirty=!0;const s=this._state,n=this.scene.canvas.gl;switch(t.primitive=t.primitive||"triangles",t.primitive){case"points":s.primitive=n.POINTS,s.primitiveName=t.primitive;break;case"lines":s.primitive=n.LINES,s.primitiveName=t.primitive;break;case"line-loop":s.primitive=n.LINE_LOOP,s.primitiveName=t.primitive;break;case"line-strip":s.primitive=n.LINE_STRIP,s.primitiveName=t.primitive;break;case"triangles":s.primitive=n.TRIANGLES,s.primitiveName=t.primitive;break;case"triangle-strip":s.primitive=n.TRIANGLE_STRIP,s.primitiveName=t.primitive;break;case"triangle-fan":s.primitive=n.TRIANGLE_FAN,s.primitiveName=t.primitive;break;default:this.error("Unsupported value for 'primitive': '"+t.primitive+"' - supported values are 'points', 'lines', 'line-loop', 'line-strip', 'triangles', 'triangle-strip' and 'triangle-fan'. Defaulting to 'triangles'."),s.primitive=n.TRIANGLES,s.primitiveName=t.primitive}if(t.positions)if(this._state.compressGeometry){const e=wt.getPositionsBounds(t.positions),n=wt.compressPositions(t.positions,e.min,e.max);s.positions=n.quantized,s.positionsDecodeMatrix=n.decodeMatrix}else s.positions=t.positions.constructor===Float32Array?t.positions:new Float32Array(t.positions);if(t.colors&&(s.colors=t.colors.constructor===Float32Array?t.colors:new Float32Array(t.colors)),t.uv)if(this._state.compressGeometry){const e=wt.getUVBounds(t.uv),n=wt.compressUVs(t.uv,e.min,e.max);s.uv=n.quantized,s.uvDecodeMatrix=n.decodeMatrix}else s.uv=t.uv.constructor===Float32Array?t.uv:new Float32Array(t.uv);t.normals&&(this._state.compressGeometry?s.normals=wt.compressNormals(t.normals):s.normals=t.normals.constructor===Float32Array?t.normals:new Float32Array(t.normals)),t.indices&&(s.indices=t.indices.constructor===Uint32Array||t.indices.constructor===Uint16Array?t.indices:new Uint32Array(t.indices),"triangles"===this._state.primitiveName&&(this._numTriangles=t.indices.length/3)),this._buildHash(),gt.meshes++,this._buildVBOs()}_buildVBOs(){const e=this._state,t=this.scene.canvas.gl;if(e.indices&&(e.indicesBuf=new ge(t,t.ELEMENT_ARRAY_BUFFER,e.indices,e.indices.length,1,t.STATIC_DRAW),gt.indices+=e.indicesBuf.numItems),e.positions&&(e.positionsBuf=new ge(t,t.ARRAY_BUFFER,e.positions,e.positions.length,3,t.STATIC_DRAW),gt.positions+=e.positionsBuf.numItems),e.normals){let s=e.compressGeometry;e.normalsBuf=new ge(t,t.ARRAY_BUFFER,e.normals,e.normals.length,3,t.STATIC_DRAW,s),gt.normals+=e.normalsBuf.numItems}e.colors&&(e.colorsBuf=new ge(t,t.ARRAY_BUFFER,e.colors,e.colors.length,4,t.STATIC_DRAW),gt.colors+=e.colorsBuf.numItems),e.uv&&(e.uvBuf=new ge(t,t.ARRAY_BUFFER,e.uv,e.uv.length,2,t.STATIC_DRAW),gt.uvs+=e.uvBuf.numItems)}_buildHash(){const e=this._state,t=["/g"];t.push("/"+e.primitive+";"),e.positions&&t.push("p"),e.colors&&t.push("c"),(e.normals||e.autoVertexNormals)&&t.push("n"),e.uv&&t.push("u"),e.compressGeometry&&t.push("cp"),t.push(";"),e.hash=t.join("")}_getEdgeIndices(){return this._edgeIndicesBuf||this._buildEdgeIndices(),this._edgeIndicesBuf}_getPickTrianglePositions(){return this._pickTrianglePositionsBuf||this._buildPickTriangleVBOs(),this._pickTrianglePositionsBuf}_getPickTriangleColors(){return this._pickTriangleColorsBuf||this._buildPickTriangleVBOs(),this._pickTriangleColorsBuf}_buildEdgeIndices(){const e=this._state;if(!e.positions||!e.indices)return;const t=this.scene.canvas.gl,s=At(e.positions,e.indices,e.positionsDecodeMatrix,this._edgeThreshold);this._edgeIndicesBuf=new ge(t,t.ELEMENT_ARRAY_BUFFER,s,s.length,1,t.STATIC_DRAW),gt.indices+=this._edgeIndicesBuf.numItems}_buildPickTriangleVBOs(){const e=this._state;if(!e.positions||!e.indices)return;const t=this.scene.canvas.gl,s=h.buildPickTriangles(e.positions,e.indices,e.compressGeometry),n=s.positions,i=s.colors;this._pickTrianglePositionsBuf=new ge(t,t.ARRAY_BUFFER,n,n.length,3,t.STATIC_DRAW),this._pickTriangleColorsBuf=new ge(t,t.ARRAY_BUFFER,i,i.length,4,t.STATIC_DRAW,!0),gt.positions+=this._pickTrianglePositionsBuf.numItems,gt.colors+=this._pickTriangleColorsBuf.numItems}_buildPickVertexVBOs(){}_webglContextLost(){this._sceneVertexBufs&&this._sceneVertexBufs.webglContextLost()}_webglContextRestored(){this._sceneVertexBufs&&this._sceneVertexBufs.webglContextRestored(),this._buildVBOs(),this._edgeIndicesBuf=null,this._pickVertexPositionsBuf=null,this._pickTrianglePositionsBuf=null,this._pickTriangleColorsBuf=null,this._pickVertexPositionsBuf=null,this._pickVertexColorsBuf=null}get primitive(){return this._state.primitiveName}get compressGeometry(){return this._state.compressGeometry}get positions(){return this._state.positions?this._state.compressGeometry?(this._decompressedPositions||(this._decompressedPositions=new Float32Array(this._state.positions.length),wt.decompressPositions(this._state.positions,this._state.positionsDecodeMatrix,this._decompressedPositions)),this._decompressedPositions):this._state.positions:null}set positions(e){const t=this._state,s=t.positions;if(s)if(s.length===e.length){if(this._state.compressGeometry){const s=wt.getPositionsBounds(e),n=wt.compressPositions(e,s.min,s.max);e=n.quantized,t.positionsDecodeMatrix=n.decodeMatrix}s.set(e),t.positionsBuf&&t.positionsBuf.setData(s),this._setAABBDirty(),this.glRedraw()}else this.error("can't update geometry positions - new positions are wrong length");else this.error("can't update geometry positions - geometry has no positions")}get normals(){if(this._state.normals){if(!this._state.compressGeometry)return this._state.normals;if(!this._decompressedNormals){const e=this._state.normals.length,t=e+e/2;this._decompressedNormals=new Float32Array(t),wt.decompressNormals(this._state.normals,this._decompressedNormals)}return this._decompressedNormals}}set normals(e){if(this._state.compressGeometry)return void this.error("can't update geometry normals - quantized geometry is immutable");const t=this._state,s=t.normals;s?s.length===e.length?(s.set(e),t.normalsBuf&&t.normalsBuf.setData(s),this.glRedraw()):this.error("can't update geometry normals - new normals are wrong length"):this.error("can't update geometry normals - geometry has no normals")}get uv(){return this._state.uv?this._state.compressGeometry?(this._decompressedUV||(this._decompressedUV=new Float32Array(this._state.uv.length),wt.decompressUVs(this._state.uv,this._state.uvDecodeMatrix,this._decompressedUV)),this._decompressedUV):this._state.uv:null}set uv(e){if(this._state.compressGeometry)return void this.error("can't update geometry UVs - quantized geometry is immutable");const t=this._state,s=t.uv;s?s.length===e.length?(s.set(e),t.uvBuf&&t.uvBuf.setData(s),this.glRedraw()):this.error("can't update geometry UVs - new UVs are wrong length"):this.error("can't update geometry UVs - geometry has no UVs")}get colors(){return this._state.colors}set colors(e){if(this._state.compressGeometry)return void this.error("can't update geometry colors - quantized geometry is immutable");const t=this._state,s=t.colors;s?s.length===e.length?(s.set(e),t.colorsBuf&&t.colorsBuf.setData(s),this.glRedraw()):this.error("can't update geometry colors - new colors are wrong length"):this.error("can't update geometry colors - geometry has no colors")}get indices(){return this._state.indices}get aabb(){return this._aabbDirty&&(this._aabb||(this._aabb=h.AABB3()),h.positions3ToAABB3(this._state.positions,this._aabb,this._state.positionsDecodeMatrix),this._aabbDirty=!1),this._aabb}get obb(){return this._obbDirty&&(this._obb||(this._obb=h.OBB3()),h.positions3ToAABB3(this._state.positions,Tt,this._state.positionsDecodeMatrix),h.AABB3ToOBB3(Tt,this._obb),this._obbDirty=!1),this._obb}get numTriangles(){return this._numTriangles}_setAABBDirty(){this._aabbDirty||(this._aabbDirty=!0,this._aabbDirty=!0,this._obbDirty=!0)}_getState(){return this._state}destroy(){super.destroy();const e=this._state;e.indicesBuf&&e.indicesBuf.destroy(),e.positionsBuf&&e.positionsBuf.destroy(),e.normalsBuf&&e.normalsBuf.destroy(),e.uvBuf&&e.uvBuf.destroy(),e.colorsBuf&&e.colorsBuf.destroy(),this._edgeIndicesBuf&&this._edgeIndicesBuf.destroy(),this._pickTrianglePositionsBuf&&this._pickTrianglePositionsBuf.destroy(),this._pickTriangleColorsBuf&&this._pickTriangleColorsBuf.destroy(),this._pickVertexPositionsBuf&&this._pickVertexPositionsBuf.destroy(),this._pickVertexColorsBuf&&this._pickVertexColorsBuf.destroy(),e.destroy(),gt.meshes--}}function bt(e={}){let t=e.xSize||1;t<0&&(console.error("negative xSize not allowed - will invert"),t*=-1);let s=e.ySize||1;s<0&&(console.error("negative ySize not allowed - will invert"),s*=-1);let n=e.zSize||1;n<0&&(console.error("negative zSize not allowed - will invert"),n*=-1);const i=e.center,a=i?i[0]:0,r=i?i[1]:0,l=i?i[2]:0,o=-t+a,c=-s+r,u=-n+l,h=t+a,p=s+r,A=n+l;return y.apply(e,{positions:[h,p,A,o,p,A,o,c,A,h,c,A,h,p,A,h,c,A,h,c,u,h,p,u,h,p,A,h,p,u,o,p,u,o,p,A,o,p,A,o,p,u,o,c,u,o,c,A,o,c,u,h,c,u,h,c,A,o,c,A,h,c,u,o,c,u,o,p,u,h,p,u],normals:[0,0,1,0,0,1,0,0,1,0,0,1,1,0,0,1,0,0,1,0,0,1,0,0,0,1,0,0,1,0,0,1,0,0,1,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,0,-1,0,0,-1,0,0,-1,0,0,-1],uv:[1,0,0,0,0,1,1,1,0,0,0,1,1,1,1,0,1,1,1,0,0,0,0,1,1,0,0,0,0,1,1,1,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0],indices:[0,1,2,0,2,3,4,5,6,4,6,7,8,9,10,8,10,11,12,13,14,12,14,15,16,17,18,16,18,19,20,21,22,20,22,23]})}class Dt extends C{get type(){return"Material"}constructor(e,t={}){super(e,t),A.memory.materials++}destroy(){super.destroy(),A.memory.materials--}}const Pt={opaque:0,mask:1,blend:2},Rt=["opaque","mask","blend"];class Ct extends Dt{get type(){return"PhongMaterial"}constructor(e,t={}){super(e,t),this._state=new Ve({type:"PhongMaterial",ambient:h.vec3([1,1,1]),diffuse:h.vec3([1,1,1]),specular:h.vec3([1,1,1]),emissive:h.vec3([0,0,0]),alpha:null,shininess:null,reflectivity:null,alphaMode:null,alphaCutoff:null,lineWidth:null,pointSize:null,backfaces:null,frontface:null,hash:null}),this.ambient=t.ambient,this.diffuse=t.diffuse,this.specular=t.specular,this.emissive=t.emissive,this.alpha=t.alpha,this.shininess=t.shininess,this.reflectivity=t.reflectivity,this.lineWidth=t.lineWidth,this.pointSize=t.pointSize,t.ambientMap&&(this._ambientMap=this._checkComponent("Texture",t.ambientMap)),t.diffuseMap&&(this._diffuseMap=this._checkComponent("Texture",t.diffuseMap)),t.specularMap&&(this._specularMap=this._checkComponent("Texture",t.specularMap)),t.emissiveMap&&(this._emissiveMap=this._checkComponent("Texture",t.emissiveMap)),t.alphaMap&&(this._alphaMap=this._checkComponent("Texture",t.alphaMap)),t.reflectivityMap&&(this._reflectivityMap=this._checkComponent("Texture",t.reflectivityMap)),t.normalMap&&(this._normalMap=this._checkComponent("Texture",t.normalMap)),t.occlusionMap&&(this._occlusionMap=this._checkComponent("Texture",t.occlusionMap)),t.diffuseFresnel&&(this._diffuseFresnel=this._checkComponent("Fresnel",t.diffuseFresnel)),t.specularFresnel&&(this._specularFresnel=this._checkComponent("Fresnel",t.specularFresnel)),t.emissiveFresnel&&(this._emissiveFresnel=this._checkComponent("Fresnel",t.emissiveFresnel)),t.alphaFresnel&&(this._alphaFresnel=this._checkComponent("Fresnel",t.alphaFresnel)),t.reflectivityFresnel&&(this._reflectivityFresnel=this._checkComponent("Fresnel",t.reflectivityFresnel)),this.alphaMode=t.alphaMode,this.alphaCutoff=t.alphaCutoff,this.backfaces=t.backfaces,this.frontface=t.frontface,this._makeHash()}_makeHash(){const e=this._state,t=["/p"];this._normalMap&&(t.push("/nm"),this._normalMap.hasMatrix&&t.push("/mat")),this._ambientMap&&(t.push("/am"),this._ambientMap.hasMatrix&&t.push("/mat"),t.push("/"+this._ambientMap.encoding)),this._diffuseMap&&(t.push("/dm"),this._diffuseMap.hasMatrix&&t.push("/mat"),t.push("/"+this._diffuseMap.encoding)),this._specularMap&&(t.push("/sm"),this._specularMap.hasMatrix&&t.push("/mat")),this._emissiveMap&&(t.push("/em"),this._emissiveMap.hasMatrix&&t.push("/mat"),t.push("/"+this._emissiveMap.encoding)),this._alphaMap&&(t.push("/opm"),this._alphaMap.hasMatrix&&t.push("/mat")),this._reflectivityMap&&(t.push("/rm"),this._reflectivityMap.hasMatrix&&t.push("/mat")),this._occlusionMap&&(t.push("/ocm"),this._occlusionMap.hasMatrix&&t.push("/mat")),this._diffuseFresnel&&t.push("/df"),this._specularFresnel&&t.push("/sf"),this._emissiveFresnel&&t.push("/ef"),this._alphaFresnel&&t.push("/of"),this._reflectivityFresnel&&t.push("/rf"),t.push(";"),e.hash=t.join("")}set ambient(e){let t=this._state.ambient;if(t){if(e&&t[0]===e[0]&&t[1]===e[1]&&t[2]===e[2])return}else t=this._state.ambient=new Float32Array(3);e?(t[0]=e[0],t[1]=e[1],t[2]=e[2]):(t[0]=.2,t[1]=.2,t[2]=.2),this.glRedraw()}get ambient(){return this._state.ambient}set diffuse(e){let t=this._state.diffuse;if(t){if(e&&t[0]===e[0]&&t[1]===e[1]&&t[2]===e[2])return}else t=this._state.diffuse=new Float32Array(3);e?(t[0]=e[0],t[1]=e[1],t[2]=e[2]):(t[0]=1,t[1]=1,t[2]=1),this.glRedraw()}get diffuse(){return this._state.diffuse}set specular(e){let t=this._state.specular;if(t){if(e&&t[0]===e[0]&&t[1]===e[1]&&t[2]===e[2])return}else t=this._state.specular=new Float32Array(3);e?(t[0]=e[0],t[1]=e[1],t[2]=e[2]):(t[0]=1,t[1]=1,t[2]=1),this.glRedraw()}get specular(){return this._state.specular}set emissive(e){let t=this._state.emissive;if(t){if(e&&t[0]===e[0]&&t[1]===e[1]&&t[2]===e[2])return}else t=this._state.emissive=new Float32Array(3);e?(t[0]=e[0],t[1]=e[1],t[2]=e[2]):(t[0]=0,t[1]=0,t[2]=0),this.glRedraw()}get emissive(){return this._state.emissive}set alpha(e){e=null!=e?e:1,this._state.alpha!==e&&(this._state.alpha=e,this.glRedraw())}get alpha(){return this._state.alpha}set shininess(e){this._state.shininess=void 0!==e?e:80,this.glRedraw()}get shininess(){return this._state.shininess}set lineWidth(e){this._state.lineWidth=e||1,this.glRedraw()}get lineWidth(){return this._state.lineWidth}set pointSize(e){this._state.pointSize=e||1,this.glRedraw()}get pointSize(){return this._state.pointSize}set reflectivity(e){this._state.reflectivity=void 0!==e?e:1,this.glRedraw()}get reflectivity(){return this._state.reflectivity}get normalMap(){return this._normalMap}get ambientMap(){return this._ambientMap}get diffuseMap(){return this._diffuseMap}get specularMap(){return this._specularMap}get emissiveMap(){return this._emissiveMap}get alphaMap(){return this._alphaMap}get reflectivityMap(){return this._reflectivityMap}get occlusionMap(){return this._occlusionMap}get diffuseFresnel(){return this._diffuseFresnel}get specularFresnel(){return this._specularFresnel}get emissiveFresnel(){return this._emissiveFresnel}get alphaFresnel(){return this._alphaFresnel}get reflectivityFresnel(){return this._reflectivityFresnel}set alphaMode(e){let t=Pt[e=e||"opaque"];void 0===t&&(this.error("Unsupported value for 'alphaMode': "+e+" - defaulting to 'opaque'"),t="opaque"),this._state.alphaMode!==t&&(this._state.alphaMode=t,this.glRedraw())}get alphaMode(){return Rt[this._state.alphaMode]}set alphaCutoff(e){null==e&&(e=.5),this._state.alphaCutoff!==e&&(this._state.alphaCutoff=e)}get alphaCutoff(){return this._state.alphaCutoff}set backfaces(e){e=!!e,this._state.backfaces!==e&&(this._state.backfaces=e,this.glRedraw())}get backfaces(){return this._state.backfaces}set frontface(e){e="cw"!==e,this._state.frontface!==e&&(this._state.frontface=e,this.glRedraw())}get frontface(){return this._state.frontface?"ccw":"cw"}destroy(){super.destroy(),this._state.destroy()}}const _t={default:{fill:!0,fillColor:[.4,.4,.4],fillAlpha:.2,edges:!0,edgeColor:[.2,.2,.2],edgeAlpha:.5,edgeWidth:1},defaultWhiteBG:{fill:!0,fillColor:[1,1,1],fillAlpha:.6,edgeColor:[.2,.2,.2],edgeAlpha:1,edgeWidth:1},defaultLightBG:{fill:!0,fillColor:[.4,.4,.4],fillAlpha:.2,edges:!0,edgeColor:[.2,.2,.2],edgeAlpha:.5,edgeWidth:1},defaultDarkBG:{fill:!0,fillColor:[.4,.4,.4],fillAlpha:.2,edges:!0,edgeColor:[.5,.5,.5],edgeAlpha:.5,edgeWidth:1},phosphorous:{fill:!0,fillColor:[0,0,0],fillAlpha:.4,edges:!0,edgeColor:[.9,.9,.9],edgeAlpha:.5,edgeWidth:2},sunset:{fill:!0,fillColor:[.9,.9,.6],fillAlpha:.2,edges:!0,edgeColor:[.9,.9,.9],edgeAlpha:.5,edgeWidth:1},vectorscope:{fill:!0,fillColor:[0,0,0],fillAlpha:.7,edges:!0,edgeColor:[.2,1,.2],edgeAlpha:1,edgeWidth:2},battlezone:{fill:!0,fillColor:[0,0,0],fillAlpha:1,edges:!0,edgeColor:[.2,1,.2],edgeAlpha:1,edgeWidth:3},sepia:{fill:!0,fillColor:[.970588207244873,.7965892553329468,.6660899519920349],fillAlpha:.4,edges:!0,edgeColor:[.529411792755127,.4577854573726654,.4100345969200134],edgeAlpha:1,edgeWidth:1},yellowHighlight:{fill:!0,fillColor:[1,1,0],fillAlpha:.5,edges:!0,edgeColor:[.529411792755127,.4577854573726654,.4100345969200134],edgeAlpha:1,edgeWidth:1},greenSelected:{fill:!0,fillColor:[0,1,0],fillAlpha:.5,edges:!0,edgeColor:[.4577854573726654,.529411792755127,.4100345969200134],edgeAlpha:1,edgeWidth:1},gamegrid:{fill:!0,fillColor:[.2,.2,.7],fillAlpha:.9,edges:!0,edgeColor:[.4,.4,1.6],edgeAlpha:.8,edgeWidth:3}};class Bt extends Dt{get type(){return"EmphasisMaterial"}get presets(){return _t}constructor(e,t={}){super(e,t),this._state=new Ve({type:"EmphasisMaterial",fill:null,fillColor:null,fillAlpha:null,edges:null,edgeColor:null,edgeAlpha:null,edgeWidth:null,backfaces:!0,glowThrough:!0}),this._preset="default",t.preset?(this.preset=t.preset,void 0!==t.fill&&(this.fill=t.fill),t.fillColor&&(this.fillColor=t.fillColor),void 0!==t.fillAlpha&&(this.fillAlpha=t.fillAlpha),void 0!==t.edges&&(this.edges=t.edges),t.edgeColor&&(this.edgeColor=t.edgeColor),void 0!==t.edgeAlpha&&(this.edgeAlpha=t.edgeAlpha),void 0!==t.edgeWidth&&(this.edgeWidth=t.edgeWidth),void 0!==t.backfaces&&(this.backfaces=t.backfaces),void 0!==t.glowThrough&&(this.glowThrough=t.glowThrough)):(this.fill=t.fill,this.fillColor=t.fillColor,this.fillAlpha=t.fillAlpha,this.edges=t.edges,this.edgeColor=t.edgeColor,this.edgeAlpha=t.edgeAlpha,this.edgeWidth=t.edgeWidth,this.backfaces=t.backfaces,this.glowThrough=t.glowThrough)}set fill(e){e=!1!==e,this._state.fill!==e&&(this._state.fill=e,this.glRedraw())}get fill(){return this._state.fill}set fillColor(e){let t=this._state.fillColor;if(t){if(e&&t[0]===e[0]&&t[1]===e[1]&&t[2]===e[2])return}else t=this._state.fillColor=new Float32Array(3);e?(t[0]=e[0],t[1]=e[1],t[2]=e[2]):(t[0]=.4,t[1]=.4,t[2]=.4),this.glRedraw()}get fillColor(){return this._state.fillColor}set fillAlpha(e){e=null!=e?e:.2,this._state.fillAlpha!==e&&(this._state.fillAlpha=e,this.glRedraw())}get fillAlpha(){return this._state.fillAlpha}set edges(e){e=!1!==e,this._state.edges!==e&&(this._state.edges=e,this.glRedraw())}get edges(){return this._state.edges}set edgeColor(e){let t=this._state.edgeColor;if(t){if(e&&t[0]===e[0]&&t[1]===e[1]&&t[2]===e[2])return}else t=this._state.edgeColor=new Float32Array(3);e?(t[0]=e[0],t[1]=e[1],t[2]=e[2]):(t[0]=.2,t[1]=.2,t[2]=.2),this.glRedraw()}get edgeColor(){return this._state.edgeColor}set edgeAlpha(e){e=null!=e?e:.5,this._state.edgeAlpha!==e&&(this._state.edgeAlpha=e,this.glRedraw())}get edgeAlpha(){return this._state.edgeAlpha}set edgeWidth(e){this._state.edgeWidth=e||1,this.glRedraw()}get edgeWidth(){return this._state.edgeWidth}set backfaces(e){e=!!e,this._state.backfaces!==e&&(this._state.backfaces=e,this.glRedraw())}get backfaces(){return this._state.backfaces}set glowThrough(e){e=!1!==e,this._state.glowThrough!==e&&(this._state.glowThrough=e,this.glRedraw())}get glowThrough(){return this._state.glowThrough}set preset(e){if(e=e||"default",this._preset===e)return;const t=_t[e];t?(this.fill=t.fill,this.fillColor=t.fillColor,this.fillAlpha=t.fillAlpha,this.edges=t.edges,this.edgeColor=t.edgeColor,this.edgeAlpha=t.edgeAlpha,this.edgeWidth=t.edgeWidth,this.glowThrough=t.glowThrough,this._preset=e):this.error("unsupported preset: '"+e+"' - supported values are "+Object.keys(_t).join(", "))}get preset(){return this._preset}destroy(){super.destroy(),this._state.destroy()}}const Ot={default:{edgeColor:[0,0,0],edgeAlpha:1,edgeWidth:1},defaultWhiteBG:{edgeColor:[.2,.2,.2],edgeAlpha:1,edgeWidth:1},defaultLightBG:{edgeColor:[.2,.2,.2],edgeAlpha:1,edgeWidth:1},defaultDarkBG:{edgeColor:[.5,.5,.5],edgeAlpha:1,edgeWidth:1}};class St extends Dt{get type(){return"EdgeMaterial"}get presets(){return Ot}constructor(e,t={}){super(e,t),this._state=new Ve({type:"EdgeMaterial",edges:null,edgeColor:null,edgeAlpha:null,edgeWidth:null}),this._preset="default",t.preset?(this.preset=t.preset,t.edgeColor&&(this.edgeColor=t.edgeColor),void 0!==t.edgeAlpha&&(this.edgeAlpha=t.edgeAlpha),void 0!==t.edgeWidth&&(this.edgeWidth=t.edgeWidth)):(this.edgeColor=t.edgeColor,this.edgeAlpha=t.edgeAlpha,this.edgeWidth=t.edgeWidth),this.edges=!1!==t.edges}set edges(e){e=!1!==e,this._state.edges!==e&&(this._state.edges=e,this.glRedraw())}get edges(){return this._state.edges}set edgeColor(e){let t=this._state.edgeColor;if(t){if(e&&t[0]===e[0]&&t[1]===e[1]&&t[2]===e[2])return}else t=this._state.edgeColor=new Float32Array(3);e?(t[0]=e[0],t[1]=e[1],t[2]=e[2]):(t[0]=.2,t[1]=.2,t[2]=.2),this.glRedraw()}get edgeColor(){return this._state.edgeColor}set edgeAlpha(e){e=null!=e?e:1,this._state.edgeAlpha!==e&&(this._state.edgeAlpha=e,this.glRedraw())}get edgeAlpha(){return this._state.edgeAlpha}set edgeWidth(e){this._state.edgeWidth=e||1,this.glRedraw()}get edgeWidth(){return this._state.edgeWidth}set preset(e){if(e=e||"default",this._preset===e)return;const t=Ot[e];t?(this.edgeColor=t.edgeColor,this.edgeAlpha=t.edgeAlpha,this.edgeWidth=t.edgeWidth,this._preset=e):this.error("unsupported preset: '"+e+"' - supported values are "+Object.keys(Ot).join(", "))}get preset(){return this._preset}destroy(){super.destroy(),this._state.destroy()}}const Nt={meters:{abbrev:"m"},metres:{abbrev:"m"},centimeters:{abbrev:"cm"},centimetres:{abbrev:"cm"},millimeters:{abbrev:"mm"},millimetres:{abbrev:"mm"},yards:{abbrev:"yd"},feet:{abbrev:"ft"},inches:{abbrev:"in"}};class xt extends C{constructor(e,t={}){super(e,t),this._units="meters",this._scale=1,this._origin=h.vec3([0,0,0]),this.units=t.units,this.scale=t.scale,this.origin=t.origin}get unitsInfo(){return Nt}set units(e){e||(e="meters");Nt[e]||(this.error("Unsupported value for 'units': "+e+" defaulting to 'meters'"),e="meters"),this._units=e,this.fire("units",this._units)}get units(){return this._units}set scale(e){(e=e||1)<=0?this.error("scale value should be larger than zero"):(this._scale=e,this.fire("scale",this._scale))}get scale(){return this._scale}set origin(e){if(!e)return this._origin[0]=0,this._origin[1]=0,void(this._origin[2]=0);this._origin[0]=e[0],this._origin[1]=e[1],this._origin[2]=e[2],this.fire("origin",this._origin)}get origin(){return this._origin}worldToRealPos(e,t=h.vec3(3)){t[0]=this._origin[0]+this._scale*e[0],t[1]=this._origin[1]+this._scale*e[1],t[2]=this._origin[2]+this._scale*e[2]}realToWorldPos(e,t=h.vec3(3)){return t[0]=(e[0]-this._origin[0])/this._scale,t[1]=(e[1]-this._origin[1])/this._scale,t[2]=(e[2]-this._origin[2])/this._scale,t}}class Lt extends C{constructor(e,t={}){super(e,t),this._supported=oe.SUPPORTED_EXTENSIONS.OES_standard_derivatives,this.enabled=t.enabled,this.kernelRadius=t.kernelRadius,this.intensity=t.intensity,this.bias=t.bias,this.scale=t.scale,this.minResolution=t.minResolution,this.numSamples=t.numSamples,this.blur=t.blur,this.blendCutoff=t.blendCutoff,this.blendFactor=t.blendFactor}get supported(){return this._supported}set enabled(e){e=!!e,this._enabled!==e&&(this._enabled=e,this.glRedraw())}get enabled(){return this._enabled}get possible(){if(!this._supported)return!1;if(!this._enabled)return!1;const e=this.scene.camera.projection;return"customProjection"!==e&&"frustum"!==e}get active(){return this._active}set kernelRadius(e){null==e&&(e=100),this._kernelRadius!==e&&(this._kernelRadius=e,this.glRedraw())}get kernelRadius(){return this._kernelRadius}set intensity(e){null==e&&(e=.15),this._intensity!==e&&(this._intensity=e,this.glRedraw())}get intensity(){return this._intensity}set bias(e){null==e&&(e=.5),this._bias!==e&&(this._bias=e,this.glRedraw())}get bias(){return this._bias}set scale(e){null==e&&(e=1),this._scale!==e&&(this._scale=e,this.glRedraw())}get scale(){return this._scale}set minResolution(e){null==e&&(e=0),this._minResolution!==e&&(this._minResolution=e,this.glRedraw())}get minResolution(){return this._minResolution}set numSamples(e){null==e&&(e=10),this._numSamples!==e&&(this._numSamples=e,this.glRedraw())}get numSamples(){return this._numSamples}set blur(e){e=!1!==e,this._blur!==e&&(this._blur=e,this.glRedraw())}get blur(){return this._blur}set blendCutoff(e){null==e&&(e=.3),this._blendCutoff!==e&&(this._blendCutoff=e,this.glRedraw())}get blendCutoff(){return this._blendCutoff}set blendFactor(e){null==e&&(e=1),this._blendFactor!==e&&(this._blendFactor=e,this.glRedraw())}get blendFactor(){return this._blendFactor}destroy(){super.destroy()}}const Mt={default:{pointSize:4,roundPoints:!0,perspectivePoints:!0},square:{pointSize:4,roundPoints:!1,perspectivePoints:!0},round:{pointSize:4,roundPoints:!0,perspectivePoints:!0}};class Ft extends Dt{get type(){return"PointsMaterial"}get presets(){return Mt}constructor(e,t={}){super(e,t),this._state=new Ve({type:"PointsMaterial",pointSize:null,roundPoints:null,perspectivePoints:null,minPerspectivePointSize:null,maxPerspectivePointSize:null,filterIntensity:null,minIntensity:null,maxIntensity:null}),t.preset?(this.preset=t.preset,void 0!==t.pointSize&&(this.pointSize=t.pointSize),void 0!==t.roundPoints&&(this.roundPoints=t.roundPoints),void 0!==t.perspectivePoints&&(this.perspectivePoints=t.perspectivePoints),void 0!==t.minPerspectivePointSize&&(this.minPerspectivePointSize=t.minPerspectivePointSize),void 0!==t.maxPerspectivePointSize&&(this.maxPerspectivePointSize=t.minPerspectivePointSize)):(this._preset="default",this.pointSize=t.pointSize,this.roundPoints=t.roundPoints,this.perspectivePoints=t.perspectivePoints,this.minPerspectivePointSize=t.minPerspectivePointSize,this.maxPerspectivePointSize=t.maxPerspectivePointSize),this.filterIntensity=t.filterIntensity,this.minIntensity=t.minIntensity,this.maxIntensity=t.maxIntensity}set pointSize(e){this._state.pointSize=e||2,this.glRedraw()}get pointSize(){return this._state.pointSize}set roundPoints(e){e=!1!==e,this._state.roundPoints!==e&&(this._state.roundPoints=e,this.scene._needRecompile=!0,this.glRedraw())}get roundPoints(){return this._state.roundPoints}set perspectivePoints(e){e=!1!==e,this._state.perspectivePoints!==e&&(this._state.perspectivePoints=e,this.scene._needRecompile=!0,this.glRedraw())}get perspectivePoints(){return this._state.perspectivePoints}set minPerspectivePointSize(e){this._state.minPerspectivePointSize=e||1,this.scene._needRecompile=!0,this.glRedraw()}get minPerspectivePointSize(){return this._state.minPerspectivePointSize}set maxPerspectivePointSize(e){this._state.maxPerspectivePointSize=e||6,this.scene._needRecompile=!0,this.glRedraw()}get maxPerspectivePointSize(){return this._state.maxPerspectivePointSize}set filterIntensity(e){e=!1!==e,this._state.filterIntensity!==e&&(this._state.filterIntensity=e,this.scene._needRecompile=!0,this.glRedraw())}get filterIntensity(){return this._state.filterIntensity}set minIntensity(e){this._state.minIntensity=null!=e?e:0,this.glRedraw()}get minIntensity(){return this._state.minIntensity}set maxIntensity(e){this._state.maxIntensity=null!=e?e:1,this.glRedraw()}get maxIntensity(){return this._state.maxIntensity}set preset(e){if(e=e||"default",this._preset===e)return;const t=Mt[e];t?(this.pointSize=t.pointSize,this.roundPoints=t.roundPoints,this.perspectivePoints=t.perspectivePoints,this.minPerspectivePointSize=t.minPerspectivePointSize,this.maxPerspectivePointSize=t.maxPerspectivePointSize,this._preset=e):this.error("unsupported preset: '"+e+"' - supported values are "+Object.keys(Mt).join(", "))}get preset(){return this._preset}get hash(){return[this.pointSize,this.roundPoints,this.perspectivePoints,this.minPerspectivePointSize,this.maxPerspectivePointSize,this.filterIntensity].join(";")}destroy(){super.destroy(),this._state.destroy()}}const Ht={default:{lineWidth:1},thick:{lineWidth:2},thicker:{lineWidth:4}};class Ut extends Dt{get type(){return"LinesMaterial"}get presets(){return Ht}constructor(e,t={}){super(e,t),this._state=new Ve({type:"LinesMaterial",lineWidth:null}),t.preset?(this.preset=t.preset,void 0!==t.lineWidth&&(this.lineWidth=t.lineWidth)):(this._preset="default",this.lineWidth=t.lineWidth)}set lineWidth(e){this._state.lineWidth=e||1,this.glRedraw()}get lineWidth(){return this._state.lineWidth}set preset(e){if(e=e||"default",this._preset===e)return;const t=Ht[e];t?(this.lineWidth=t.lineWidth,this._preset=e):this.error("unsupported preset: '"+e+"' - supported values are "+Object.keys(Ht).join(", "))}get preset(){return this._preset}get hash(){return[""+this.lineWidth].join(";")}destroy(){super.destroy(),this._state.destroy()}}const Gt=h.vec3(),Vt=h.vec3(),jt=h.mat4();class kt{constructor(){this.normal=h.vec3(),this.offset=0,this.testVertex=h.vec3()}set(e,t,s,n){const i=1/Math.sqrt(e*e+t*t+s*s);this.normal[0]=e*i,this.normal[1]=t*i,this.normal[2]=s*i,this.offset=n*i,this.testVertex[0]=this.normal[0]>=0?1:0,this.testVertex[1]=this.normal[1]>=0?1:0,this.testVertex[2]=this.normal[2]>=0?1:0}}class Qt{constructor(){this.planes=[new kt,new kt,new kt,new kt,new kt,new kt]}}function Wt(e,t,s){const n=h.mulMat4(s,t,jt),i=n[0],a=n[1],r=n[2],l=n[3],o=n[4],c=n[5],u=n[6],p=n[7],A=n[8],d=n[9],f=n[10],I=n[11],y=n[12],m=n[13],v=n[14],w=n[15];e.planes[0].set(l-i,p-o,I-A,w-y),e.planes[1].set(l+i,p+o,I+A,w+y),e.planes[2].set(l-a,p-c,I-d,w-m),e.planes[3].set(l+a,p+c,I+d,w+m),e.planes[4].set(l-r,p-u,I-f,w-v),e.planes[5].set(l+r,p+u,I+f,w+v)}function zt(e,t){let s=Qt.INSIDE;const n=Gt,i=Vt;n[0]=t[0],n[1]=t[1],n[2]=t[2],i[0]=t[3],i[1]=t[4],i[2]=t[5];const a=[n,i];for(let t=0;t<6;++t){const n=e.planes[t];if(n.normal[0]*a[n.testVertex[0]][0]+n.normal[1]*a[n.testVertex[1]][1]+n.normal[2]*a[n.testVertex[2]][2]+n.offset<0)return Qt.OUTSIDE;n.normal[0]*a[1-n.testVertex[0]][0]+n.normal[1]*a[1-n.testVertex[1]][1]+n.normal[2]*a[1-n.testVertex[2]][2]+n.offset<0&&(s=Qt.INTERSECT)}return s}Qt.INSIDE=0,Qt.INTERSECT=1,Qt.OUTSIDE=2;class Kt{constructor(e,t){this.primLODLevels=e,this.entitiesInLOD={},this.primCountInLOD={},this.targetFps=t,this.lodLevelIndex=0,this.consecutiveFramesWithTargetFps=0,this.consecutiveFramesWithoutTargetFps=0}initializeLodState(e){const t=Object.values(e.objects);if(0===t.length)return;const s={},n={};for(let e=0,i=t.length;e<i;e++){const i=t[e];i.numPrimitives,h.getAABB3Diag(i.aabb);let a,r=0;for(r=0,a=this.primLODLevels.length;r<a&&!(i.numPrimitives>=this.primLODLevels[r]);r++);const l=this.primLODLevels[r]||0;l in s||(s[l]=[]),s[l].push(i),l in n||(n[l]=0),n[l]+=i.numPrimitives}this.entitiesInLOD=s,this.primCountInLOD=n}}class Yt{constructor(e,t,s,n){this.id=t.id,this.scene=e,this.sceneModel=t,this.lodState=new Kt(s,n),this.lodState.initializeLodState(t)}_increaseLODLevelIndex(){const e=this.lodState;if(e.lodLevelIndex===e.primLODLevels.length)return!1;const t=e.entitiesInLOD[e.primLODLevels[e.lodLevelIndex]]||[];for(let e=0,s=t.length;e<s;e++)t[e].culledLOD=!0;return e.lodLevelIndex++,!0}_decreaseLODLevelIndex(){const e=this.lodState;if(0===e.lodLevelIndex)return!1;const t=e.entitiesInLOD[e.primLODLevels[e.lodLevelIndex-1]]||[];for(let e=0,s=t.length;e<s;e++)t[e].culledLOD=!1;return e.lodLevelIndex--,!0}applyLodCulling(e){let t=this.lodState,s=!1;return e<t.targetFps?++t.consecutiveFramesWithoutTargetFps>0&&(t.consecutiveFramesWithoutTargetFps=0,s=this._increaseLODLevelIndex()):e>t.targetFps+4&&++t.consecutiveFramesWithTargetFps>1&&(t.consecutiveFramesWithTargetFps=0,s=this._decreaseLODLevelIndex()),s}resetLodCulling(){let e=!1,t=!1;do{e|=t=this._decreaseLODLevelIndex()}while(t);return e}}class Xt extends C{constructor(e,t={}){super(e,t),this._scene=e,this._lodLevels=[2e3,600,150,80,20],this._lodManagers={},this._lodManagerList=[],this.enabled=t.enabled,this.targetFPS=t.targetFPS,this._init()}_init(){const e=new Array(4);let t=0,s=-1,n=Date.now(),i=0,a=0,r=a;this._scene.on("rendering",(()=>{if(-1!==s)for(let e=0,t=this._lodManagerList.length;e<t;e++)this._lodManagerList[e].applyLodCulling(s)})),this._scene.on("rendered",(()=>{n=Date.now(),window.requestAnimationFrame((()=>{t++;const a=Date.now();i=a-n,n=a,e[t%4]=i;let r=0;if(t>4){for(let t=0;t<4;t++)r+=e[t];s=4/r*1e3}}))})),this._scene.camera.on("matrix",(()=>{r=a})),this._scene.on("tick",(()=>{if(a-r>3)for(let e=0,t=this._lodManagerList.length;e<t;e++)this._lodManagerList[e].resetLodCulling();a++}))}set enabled(e){e=!!e,this._enabled!==e&&(this._enabled=e,this.glRedraw())}get enabled(){return this._enabled}set targetFPS(e){null==e&&(e=30),this._targetFPS!==e&&(this._targetFPS=e,this.glRedraw())}get targetFPS(){return this._targetFPS}getLODManager(e){const t=new Yt(this.scene,e,this._lodLevels,this._targetFPS);return this._lodManagers[t.id]=t,this._lodManagerList=Object.values(this._lodManagers),t}putLODManager(e){delete this._lodManagers[e.id],this._lodManagerList=Object.values(this._lodManagers)}destroy(){super.destroy()}}var qt={},Jt={};!function(e){if("object"==typeof Jt&&void 0!==qt)qt.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).RBush3D=e()}}((function(){return function e(t,s,n){function i(r,l){if(!s[r]){if(!t[r]){var o="function"==typeof require&&require;if(!l&&o)return o(r,!0);if(a)return a(r,!0);var c=new Error("Cannot find module '"+r+"'");throw c.code="MODULE_NOT_FOUND",c}var u=s[r]={exports:{}};t[r][0].call(u.exports,(function(e){return i(t[r][1][e]||e)}),u,u.exports,e,t,s,n)}return s[r].exports}for(var a="function"==typeof require&&require,r=0;r<n.length;r++)i(n[r]);return i}({1:[function(e,t,s){Object.defineProperty(s,"__esModule",{value:!0});var n=e("quickselect"),i=[],a=function(e){return i.push(e)},r=function(e){e&&(a(e),u(e)||e.children.forEach(r))},l=function(e){var t=i.pop();return t?(t.children=e,t.height=1,t.leaf=!0,t.minX=1/0,t.minY=1/0,t.minZ=1/0,t.maxX=-1/0,t.maxY=-1/0,t.maxZ=-1/0):t={children:e,height:1,leaf:!0,minX:1/0,minY:1/0,minZ:1/0,maxX:-1/0,maxY:-1/0,maxZ:-1/0},t},o=[],c=function(e,t){var s=o.pop();return s?(s.dist=e,s.node=t):s={dist:e,node:t},s},u=function(e){return e.leaf},h=function(e,t){return e.leaf},p=function(e,t,s){if(!s)return t.indexOf(e);for(var n=0;n<t.length;n++)if(s(e,t[n]))return n;return-1},A=function(e){d(e,0,e.children.length,e)},d=function(e,t,s,n){var i=n;i?(i.minX=1/0,i.minY=1/0,i.minZ=1/0,i.maxX=-1/0,i.maxY=-1/0,i.maxZ=-1/0):i=l([]);for(var a=t,r=void 0;a<s;a++)r=e.children[a],f(i,r);return i},f=function(e,t){return e.minX=Math.min(e.minX,t.minX),e.minY=Math.min(e.minY,t.minY),e.minZ=Math.min(e.minZ,t.minZ),e.maxX=Math.max(e.maxX,t.maxX),e.maxY=Math.max(e.maxY,t.maxY),e.maxZ=Math.max(e.maxZ,t.maxZ),e},I=function(e){return(e.maxX-e.minX)*(e.maxY-e.minY)*(e.maxZ-e.minZ)},y=function(e){return e.maxX-e.minX+(e.maxY-e.minY)+(e.maxZ-e.minZ)},m=function(e,t){var s=Math.min(e.minX,t.minX),n=Math.min(e.minY,t.minY),i=Math.min(e.minZ,t.minZ);return(Math.max(e.maxX,t.maxX)-s)*(Math.max(e.maxY,t.maxY)-n)*(Math.max(e.maxZ,t.maxZ)-i)},v=function(e,t){var s=Math.max(e.minX,t.minX),n=Math.max(e.minY,t.minY),i=Math.max(e.minZ,t.minZ),a=Math.min(e.maxX,t.maxX),r=Math.min(e.maxY,t.maxY),l=Math.min(e.maxZ,t.maxZ);return Math.max(0,a-s)*Math.max(0,r-n)*Math.max(0,l-i)},w=function(e,t){return e.minX<=t.minX&&e.minY<=t.minY&&e.minZ<=t.minZ&&t.maxX<=e.maxX&&t.maxY<=e.maxY&&t.maxZ<=e.maxZ};s.intersects=function(e,t){return t.minX<=e.maxX&&t.minY<=e.maxY&&t.minZ<=e.maxZ&&t.maxX>=e.minX&&t.maxY>=e.minY&&t.maxZ>=e.minZ},s.boxRayIntersects=function(e,t,s,n,i,a,r){var l=(e.minX-t)*i,o=(e.maxX-t)*i,c=(e.minY-s)*a,u=(e.maxY-s)*a,h=(e.minZ-n)*r,p=(e.maxZ-n)*r,A=Math.min(h,p),d=Math.max(h,p),f=Math.min(c,u),I=Math.max(c,u),y=Math.min(l,o),m=Math.max(l,o),v=Math.max(0,y,f,A);return Math.min(m,I,d)>=v?v:1/0};var g=function(e,t,s,i,a){for(var r,l=[t,s];l.length;)(s=l.pop())-(t=l.pop())<=i||(r=t+Math.ceil((s-t)/i/2)*i,n(e,r,t,s,a),l.push(t,r,r,s))},T=function(e,t){return e.minX-t.minX},E=function(e,t){return e.minY-t.minY},b=function(e,t){return e.minZ-t.minZ},D=function(){function e(e){void 0===e&&(e=16),this.maxEntries=Math.max(e,8),this.minEntries=Math.max(4,Math.ceil(.4*this.maxEntries)),this.clear()}return e.alloc=function(){return this.pool.pop()||new this},e.free=function(e){e.clear(),this.pool.push(e)},e.prototype.searchCustom=function(e,t){var s=this.data,n=[];if(!e(s,h(s)))return n;for(var i=[];s;){for(var a=0,r=s.children.length;a<r;a++){var l=s.children[a];e(l,h(s))&&(h(s)?n.push(l):t(l)?this._all(l,n):i.push(l))}s=i.pop()}return n},e.prototype.analyzeTriangles=function(e){void 0===e&&(e=this.data);var t=0;if(u(e))for(var s=0,n=e.children.length;s<n;s++)t+=e.children[s].numPrimitives;else for(s=0,n=e.children.length;s<n;s++)t+=this.analyzeTriangles(e.children[s]);return e.totalTriangles=t},e.prototype.groupNodesByNumTrianglesThreshold=function(e,t){void 0===t&&(this.analyzeTriangles(),t=this.data);for(var s=[],n=0,i=t.children.length;n<i;n++){var a=t.children[n];s.push({item:a,triangles:a.totalTriangles||a.numPrimitives})}s.sort((function(e,t){return-(e.item.triangles-t.item.triangles)}));var r=[],l=[],o=0;for(n=0,i=s.length;n<i;n++){var c=s[n];if(o+c.triangles<e)l.push(c),o+=c.triangles;else if(l.length&&(r.push(l),l=[],o=0),o+c.triangles<e)n--;else if(h(t,c.item))r.push([c]);else{for(var u=this.groupNodesByNumTrianglesThreshold(e,c.item),p=[],A=0,d=0,f=u.length;d<f-1;d++)r.push(u[n]);if(u.length>1){for(d=0,f=(u=u[u.length-1]).length;d<f;d++)(A+=u[d].triangles)<e?p.push(u[d]):(0==A&&p.push(u[d]),r.push(p),A=0,p=[]);p.forEach((function(e){l.push(e),o+=e.triangles}))}}}return l.length&&r.push(l),r},e.prototype.search=function(e){var t=this.data,n=[];if(!s.intersects(e,t))return n;for(var i=[];t;){for(var a=0,r=t.children.length;a<r;a++){var l=t.children[a];s.intersects(e,l)&&(h(t)?n.push(l):w(e,l)?this._all(l,n):i.push(l))}t=i.pop()}return n},e.prototype.collides=function(e){var t=this.data;if(!s.intersects(e,t))return!1;for(var n=[];t;){for(var i=0,a=t.children.length;i<a;i++){var r=t.children[i];if(s.intersects(e,r)){if(h(t)||w(e,r))return!0;n.push(r)}}t=n.pop()}return!1},e.prototype.raycastInv=function(e,t,n,i,a,r,l){void 0===l&&(l=1/0);var u=this.data;if(i===1/0&&a===1/0&&r===1/0)return c(1/0,void 0);if(s.boxRayIntersects(u,e,t,n,i,a,r)===1/0)return c(1/0,void 0);for(var p,A=[c(0,u)],d=function(e,t){var s=A[e];A[e]=A[t],A[t]=s},f=function(){var e=A[0],t=A.length-1;A[0]=A[t],A.length=t;for(var s=0;;){var n=s<<1|1;if(n>=t)break;var i=n+1;if(i<t&&A[i].dist<A[n].dist&&(n=i),A[s].dist<A[n].dist)break;d(s,n),s=n}return function(e){o.push(e)}(e),e.node},I=function(e,t){var s=A.length;for(A.push(c(e,t));s>0;){var n=s-1>>1;if(A[n].dist<=A[s].dist)break;d(s,n),s=n}},y=l;A.length&&A[0].dist<y;)for(var m=0,v=(u=f()).children.length;m<v;m++){var w=u.children[m],g=s.boxRayIntersects(w,e,t,n,i,a,r);if(h(u)){if(g<y){if(0===g)return c(g,w);y=g,p=w}}else I(g,w)}return c(y<l?y:1/0,p)},e.prototype.raycast=function(e,t,s,n,i,a,r){return void 0===r&&(r=1/0),this.raycastInv(e,t,s,1/n,1/i,1/a,r)},e.prototype.all=function(){return this._all(this.data,[])},e.prototype.load=function(e){if(!e||!e.length)return this;if(e.length<this.minEntries){for(var t=0,s=e.length;t<s;t++)this.insert(e[t]);return this}var n=this.build(e.slice(),0,e.length-1,0);if(this.data.children.length)if(this.data.height===n.height)this.splitRoot(this.data,n);else{if(this.data.height<n.height){var i=this.data;this.data=n,n=i}this._insert(n,this.data.height-n.height-1,!0)}else this.data=n;return this},e.prototype.insert=function(e){return e&&this._insert(e,this.data.height-1),this},e.prototype.clear=function(){return this.data&&r(this.data),this.data=l([]),this},e.prototype.remove=function(e,t){if(!e)return this;for(var s,n,i=this.data,a=0,r=!1,l=[],o=[];i||l.length;){if(i||(i=l.pop(),a=o.pop(),n=l[l.length-1],r=!0),u(i)&&-1!==(s=p(e,i.children,t)))return i.children.splice(s,1),l.push(i),this.condense(l),this;r||u(i)||!w(i,e)?n?(a++,i=n.children[a],r=!1):i=void 0:(l.push(i),o.push(a),a=0,n=i,i=i.children[0])}return this},e.prototype.toJSON=function(){return this.data},e.prototype.fromJSON=function(e){return r(this.data),this.data=e,this},e.prototype.build=function(e,t,s,n){var i,a=s-t+1,r=this.maxEntries;if(a<=r)return i=l(e.slice(t,s+1)),A(i),i;n||(n=Math.ceil(Math.log(a)/Math.log(r)),r=Math.ceil(a/Math.pow(r,n-1))),(i=l([])).leaf=!1,i.height=n;var o=Math.ceil(a/r),c=o*Math.ceil(Math.pow(r,2/3)),u=o*Math.ceil(Math.pow(r,1/3));g(e,t,s,u,T);for(var h=t;h<=s;h+=u){var p=Math.min(h+u-1,s);g(e,h,p,c,E);for(var d=h;d<=p;d+=c){var f=Math.min(d+c-1,p);g(e,d,f,o,b);for(var I=d;I<=f;I+=o){var y=Math.min(I+o-1,f);i.children.push(this.build(e,I,y,n-1))}}}return A(i),i},e.prototype._all=function(e,t){for(var s=[];e;)u(e)?t.push.apply(t,e.children):s.push.apply(s,e.children),e=s.pop();return t},e.prototype.chooseSubtree=function(e,t,s,n){for(var i,a,r;n.push(t),!u(t)&&n.length-1!==s;){i=a=1/0;for(var l=0,o=t.children.length;l<o;l++){var c=t.children[l],h=I(c),p=m(e,c)-h;p<a?(a=p,i=h<i?h:i,r=c):p===a&&h<i&&(i=h,r=c)}t=r||t.children[0]}return t},e.prototype.split=function(e,t){var s=e[t],n=s.children.length,i=this.minEntries;this.chooseSplitAxis(s,i,n);var a=this.chooseSplitIndex(s,i,n),r=l(s.children.splice(a,s.children.length-a));r.height=s.height,r.leaf=s.leaf,A(s),A(r),t?e[t-1].children.push(r):this.splitRoot(s,r)},e.prototype.splitRoot=function(e,t){this.data=l([e,t]),this.data.height=e.height+1,this.data.leaf=!1,A(this.data)},e.prototype.chooseSplitIndex=function(e,t,s){for(var n,i=1/0,a=1/0,r=t;r<=s-t;r++){var l=d(e,0,r),o=d(e,r,s),c=v(l,o),u=I(l)+I(o);c<i?(i=c,n=r,a=u<a?u:a):c===i&&u<a&&(a=u,n=r)}return n},e.prototype.chooseSplitAxis=function(e,t,s){var n=this.allDistMargin(e,t,s,T),i=this.allDistMargin(e,t,s,E),a=this.allDistMargin(e,t,s,b);n<i&&n<a?e.children.sort(T):i<n&&i<a&&e.children.sort(E)},e.prototype.allDistMargin=function(e,t,s,n){e.children.sort(n);for(var i=d(e,0,t),a=d(e,s-t,s),r=y(i)+y(a),l=t;l<s-t;l++){var o=e.children[l];f(i,o),r+=y(i)}for(l=s-t-1;l>=t;l--){o=e.children[l];f(a,o),r+=y(a)}return r},e.prototype.adjustParentBBoxes=function(e,t,s){for(var n=s;n>=0;n--)f(t[n],e)},e.prototype.condense=function(e){for(var t=e.length-1,s=void 0;t>=0;t--)0===e[t].children.length?t>0?((s=e[t-1].children).splice(s.indexOf(e[t]),1),a(e[t])):this.clear():A(e[t])},e.prototype._insert=function(e,t,s){var n=[],i=this.chooseSubtree(e,this.data,t,n);for(i.children.push(e),f(i,e);t>=0&&n[t].children.length>this.maxEntries;)this.split(n,t),t--;this.adjustParentBBoxes(e,n,t)},e.pool=[],e}();s.RBush3D=D},{quickselect:2}],2:[function(e,t,s){!function(e,n){"object"==typeof s&&void 0!==t?t.exports=n():e.quickselect=n()}(this,(function(){function e(s,n,i,a,r){for(;a>i;){if(a-i>600){var l=a-i+1,o=n-i+1,c=Math.log(l),u=.5*Math.exp(2*c/3),h=.5*Math.sqrt(c*u*(l-u)/l)*(o-l/2<0?-1:1);e(s,n,Math.max(i,Math.floor(n-o*u/l+h)),Math.min(a,Math.floor(n+(l-o)*u/l+h)),r)}var p=s[n],A=i,d=a;for(t(s,i,n),r(s[a],p)>0&&t(s,i,a);A<d;){for(t(s,A,d),A++,d--;r(s[A],p)<0;)A++;for(;r(s[d],p)>0;)d--}0===r(s[i],p)?t(s,i,d):t(s,++d,a),d<=n&&(i=d+1),n<=d&&(a=d-1)}}function t(e,t,s){var n=e[t];e[t]=e[s],e[s]=n}function s(e,t){return e<t?-1:e>t?1:0}return function(t,n,i,a,r){e(t,n,i||0,a||t.length-1,r||s)}}))},{}]},{},[1])(1)}));var Zt=qt.exports.RBush3D,$t=function(e){function t(e){var t=0;return e.meshes.forEach((function(e){t+=e.numPrimitives})),t}function s(e,t){var s={pos:{x:0,y:0},left:0,right:e,top:0,bottom:t,dir:0};function n(){(0==s.dir&&s.pos.x+1>=s.right||1==s.dir&&s.pos.y+1>=s.bottom||2==s.dir&&s.pos.x-1<=s.left-1||3==s.dir&&s.pos.y-1<=s.top-1)&&(s.dir=(s.dir+1)%4,0==s.dir&&s.left++,1==s.dir&&s.top++,2==s.dir&&s.right--,3==s.dir&&s.bottom--),0==s.dir&&s.pos.x++,1==s.dir&&s.pos.y++,2==s.dir&&s.pos.x--,3==s.dir&&s.pos.y--}for(var i=[],a=e*t;i.length<a;n())i.push(s.pos.x+"_"+s.pos.y);return i}var n,i,a,r,l=0,o=(i=function(t,s){for(var n=s||function(e){return!0},i=(e.aabbTree.data.maxX-e.aabbTree.data.minX)/10,a=(e.aabbTree.data.maxZ-e.aabbTree.data.minZ)/10,r=[],l=e.aabbTree.data.minX,o=0;o<10;o++,l+=i)for(var c=e.aabbTree.data.minZ,u=0;u<10;u++,c+=a)r.push({minX:l,maxX:l+i,minY:-1e8,maxY:1e8,minZ:c,maxZ:c+a,indexX:o,indexZ:u});var h={cellsByEntity:{},entitiesByCell:{},cellsX:10,cellsZ:10};return(r||[]).forEach((function(t){e.aabbTree.search(t).filter((function(e){return n(e.entity)})).forEach((function(e){var s=e.entity.id;h.cellsByEntity[s]=h.cellsByEntity[s]||{cells:[],entity:e.entity},h.cellsByEntity[s].cells.push(t);var n=t.indexX+"_"+t.indexZ;h.entitiesByCell[n]=h.entitiesByCell[n]||{entities:[],cell:t},h.entitiesByCell[n].entities.push(e.entity)}))})),h}((n={cellSideInMeters:.05,entityFilterFunc:function(e){return!0},maxPolygonsPerCluster:9e4,minPercentOfClustredPolygons:.9}).cellSideInMeters,n.entityFilterFunc),a=function(e,s){var n={},i=0;Object.keys(s).forEach((function(e){var a=s[e].cells.length,r=t(s[e].entity);n[a]=(n[a]||0)+r,i+=r}));var a=Object.keys(n);a.sort((function(e,t){return e-t}));for(var r=0,l=0,o=0;o<a.length&&l<e;o++)r=a[o],l+=n[r]/i;return{maxCellsPerEntity:r,polygonStats:{percentClustered:l,numberClustered:Math.round(l*i),numberUnclustered:i-Math.round(l*i)}}}(n.minPercentOfClustredPolygons,i.cellsByEntity),r=function(e,n,i,a,r,l){var o={},c={accumEntities:[]},u=[];s(e,n).forEach((function(e){var s=function(e,t,s,n){return(s[e]||{entities:[]}).entities.filter((function(e){return n[e.id].cells.length<=t}))}(e,i,a,r).filter((function(e){return!(e.id in o)}));s.forEach((function(e){o[e.id]=!0})),s.sort((function(e,s){return t(e)-t(s)}));var n=c.accumEntities.concat(s),h=[],p=l,A=0;do{for(;A<n.length;A++){var d=n[A],f=t(d);f>p&&(u.push(h),h=[],p=l),h.push(d),p-=f}}while(A<n.length);c.accumEntities=h})),c.accumEntities.length&&u.push(c.accumEntities);var h=[];Object.keys(r).forEach((function(e){e in o||h.push(r[e].entity)}));var p=l,A=[];h.forEach((function(e){var s=t(e);s>p&&(u.push(A),A=[],p=l),A.push(e),p-=s})),A.length&&u.push(A);var d={};return u.forEach((function(e,t){e.forEach((function(e){d[e.id]=t}))})),{clusters:u,entityIdToClusterIdMapping:d}}(i.cellsX,i.cellsZ,a.maxCellsPerEntity,i.entitiesByCell,i.cellsByEntity,n.maxPolygonsPerCluster),{clusters:{total:r.clusters.length},clusteringResult:r});l+=o.clusters.total,console.log("Total clusters: "+l);var c=[];return o.clusteringResult.clusters.forEach((function(e){e.forEach((function(e){c.push(e.id)}))})),o.orderedEntityIds=c,o};
|
|
2
2
|
/**
|
|
3
3
|
* @author https://github.com/tmarti, with support from https://tribia.com/
|
|
4
4
|
* @license MIT
|