UNPKG

@three.ez/instanced-mesh

Version:

Enhanced InstancedMesh with frustum culling, fast raycasting (using BVH), sorting, visibility management and more.

112 lines (104 loc) 45.6 kB
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=require("three"),B=require("bvh.js"),vt=require("three/addons/utils/SortUtils.js");class nt{constructor(t,e,n){if(this.isInstanceEntity=!0,this.position=new u.Vector3,this.scale=new u.Vector3(1,1,1),this.quaternion=new u.Quaternion,this.id=e,this.owner=t,n){const i=this.quaternion,s=this.rotation=new u.Euler;s._onChange(()=>i.setFromEuler(s,!1)),i._onChange(()=>s.setFromQuaternion(i,void 0,!1))}}get visible(){return this.owner.getVisibilityAt(this.id)}set visible(t){this.owner.setVisibilityAt(this.id,t)}get active(){return this.owner.getActiveAt(this.id)}set active(t){this.owner.setActiveAt(this.id,t)}get color(){return this.owner.getColorAt(this.id)}set color(t){this.owner.setColorAt(this.id,t)}get opacity(){return this.owner.getOpacityAt(this.id)}set opacity(t){this.owner.setOpacityAt(this.id,t)}get morph(){return this.owner.getMorphAt(this.id)}set morph(t){this.owner.setMorphAt(this.id,t)}get matrix(){return this.owner.getMatrixAt(this.id)}get matrixWorld(){return this.matrix.premultiply(this.owner.matrixWorld)}setMatrixIdentity(){var s;const t=this.owner,e=t.matricesTexture._data,n=this.id,i=n*16;e[i+0]=1,e[i+1]=0,e[i+2]=0,e[i+3]=0,e[i+4]=0,e[i+5]=1,e[i+6]=0,e[i+7]=0,e[i+8]=0,e[i+9]=0,e[i+10]=1,e[i+11]=0,e[i+12]=0,e[i+13]=0,e[i+14]=0,e[i+15]=1,t.matricesTexture.enqueueUpdate(n),(s=t.bvh)==null||s.move(n)}updateMatrix(){var T;const t=this.owner,e=this.position,n=this.quaternion,i=this.scale,s=t.matricesTexture._data,o=this.id,a=o*16,h=n._x,c=n._y,l=n._z,d=n._w,p=h+h,x=c+c,m=l+l,_=h*p,y=h*x,v=h*m,w=c*x,M=c*m,U=l*m,O=d*p,F=d*x,I=d*m,g=i.x,b=i.y,A=i.z;s[a+0]=(1-(w+U))*g,s[a+1]=(y+I)*g,s[a+2]=(v-F)*g,s[a+3]=0,s[a+4]=(y-I)*b,s[a+5]=(1-(_+U))*b,s[a+6]=(M+O)*b,s[a+7]=0,s[a+8]=(v+F)*A,s[a+9]=(M-O)*A,s[a+10]=(1-(_+w))*A,s[a+11]=0,s[a+12]=e.x,s[a+13]=e.y,s[a+14]=e.z,s[a+15]=1,t.matricesTexture.enqueueUpdate(o),(T=t.bvh)==null||T.move(o)}updateMatrixPosition(){var o;const t=this.owner,e=this.position,n=t.matricesTexture._data,i=this.id,s=i*16;n[s+12]=e.x,n[s+13]=e.y,n[s+14]=e.z,t.matricesTexture.enqueueUpdate(i),(o=t.bvh)==null||o.move(i)}getUniform(t,e){return this.owner.getUniformAt(this.id,t,e)}updateBones(t=!0,e){this.owner.setBonesAt(this.id,t,e)}setUniform(t,e){this.owner.setUniformAt(this.id,t,e)}copyTo(t){t.position.copy(this.position),t.scale.copy(this.scale),t.quaternion.copy(this.quaternion),this.rotation&&t.rotation.copy(this.rotation)}applyMatrix4(t){return this.matrix.premultiply(t).decompose(this.position,this.quaternion,this.scale),this}applyQuaternion(t){return this.quaternion.premultiply(t),this}rotateOnAxis(t,e){return X.setFromAxisAngle(t,e),this.quaternion.multiply(X),this}rotateOnWorldAxis(t,e){return X.setFromAxisAngle(t,e),this.quaternion.premultiply(X),this}rotateX(t){return this.rotateOnAxis(at,t)}rotateY(t){return this.rotateOnAxis(ct,t)}rotateZ(t){return this.rotateOnAxis(ht,t)}translateOnAxis(t,e){return ot.copy(t).applyQuaternion(this.quaternion),this.position.add(ot.multiplyScalar(e)),this}translateX(t){return this.translateOnAxis(at,t)}translateY(t){return this.translateOnAxis(ct,t)}translateZ(t){return this.translateOnAxis(ht,t)}remove(){return this.owner.removeInstances(this.id),this}}const X=new u.Quaternion,ot=new u.Vector3,at=new u.Vector3(1,0,0),ct=new u.Vector3(0,1,0),ht=new u.Vector3(0,0,1);class yt{constructor(t,e=0,n=!1,i=!0){this.nodesMap=new Map,this.LODsMap=new Map,this._geoBoundingSphere=null,this._sphereTarget=null,this.target=t,this.accurateCulling=i,this._margin=e;const s=t._geometry;if(s.boundingBox||s.computeBoundingBox(),this.geoBoundingBox=s.boundingBox,n){s.boundingSphere||s.computeBoundingSphere();const o=s.boundingSphere.center;o.x===0&&o.y===0&&o.z===0?(this._geoBoundingSphere=s.boundingSphere,this._sphereTarget={centerX:0,centerY:0,centerZ:0,maxScale:0}):(console.warn('"getBoxFromSphere" is ignored because geometry is not centered.'),n=!1)}this.bvh=new B.BVH(new B.HybridBuilder,B.WebGLCoordinateSystem),this._origin=new Float32Array(3),this._dir=new Float32Array(3),this._cameraPos=new Float32Array(3),this._getBoxFromSphere=n}create(){const t=this.target._instancesCount,e=this.target._instancesArrayCount,n=new Array(t),i=new Uint32Array(t);let s=0;this.clear();for(let o=0;o<e;o++)this.target.getActiveAt(o)&&(n[s]=this.getBox(o,new Float32Array(6)),i[s]=o,s++);this.bvh.createFromArray(i,n,o=>{this.nodesMap.set(o.object,o)},this._margin)}insert(t){const e=this.bvh.insert(t,this.getBox(t,new Float32Array(6)),this._margin);this.nodesMap.set(t,e)}insertRange(t){const e=t.length,n=new Array(e);for(let i=0;i<e;i++)n[i]=this.getBox(t[i],new Float32Array(6));this.bvh.insertRange(t,n,this._margin,i=>{this.nodesMap.set(i.object,i)})}move(t){const e=this.nodesMap.get(t);e&&(this.getBox(t,e.box),this.bvh.move(e,this._margin))}delete(t){const e=this.nodesMap.get(t);e&&(this.bvh.delete(e),this.nodesMap.delete(t))}clear(){this.bvh.clear(),this.nodesMap.clear()}frustumCulling(t,e){this._margin>0&&this.accurateCulling?this.bvh.frustumCulling(t.elements,(n,i,s)=>{i.isIntersectedMargin(n.box,s,this._margin)&&e(n)}):this.bvh.frustumCulling(t.elements,e)}frustumCullingLOD(t,e,n,i){this.LODsMap.has(n)||this.LODsMap.set(n,new Float32Array(n.length));const s=this.LODsMap.get(n);for(let a=0;a<n.length;a++)s[a]=n[a].distance;const o=this._cameraPos;o[0]=e.x,o[1]=e.y,o[2]=e.z,this._margin>0&&this.accurateCulling?this.bvh.frustumCullingLOD(t.elements,o,s,(a,h,c,l)=>{c.isIntersectedMargin(a.box,l,this._margin)&&i(a,h)}):this.bvh.frustumCullingLOD(t.elements,o,s,i)}raycast(t,e){const n=t.ray,i=this._origin,s=this._dir;B.vec3ToArray(n.origin,i),B.vec3ToArray(n.direction,s),this.bvh.rayIntersections(s,i,e,t.near,t.far)}intersectBox(t,e){this._boxArray||(this._boxArray=new Float32Array(6));const n=this._boxArray;return B.box3ToArray(t,n),this.bvh.intersectsBox(n,e)}getBox(t,e){if(this._getBoxFromSphere){const n=this.target.matricesTexture._data,{centerX:i,centerY:s,centerZ:o,maxScale:a}=this.getSphereFromMatrix_centeredGeometry(t,n,this._sphereTarget),h=this._geoBoundingSphere.radius*a;e[0]=i-h,e[1]=i+h,e[2]=s-h,e[3]=s+h,e[4]=o-h,e[5]=o+h}else ut.copy(this.geoBoundingBox).applyMatrix4(this.target.getMatrixAt(t)),B.box3ToArray(ut,e);return e}getSphereFromMatrix_centeredGeometry(t,e,n){const i=t*16,s=e[i+0],o=e[i+1],a=e[i+2],h=e[i+4],c=e[i+5],l=e[i+6],d=e[i+8],p=e[i+9],x=e[i+10],m=s*s+o*o+a*a,_=h*h+c*c+l*l,y=d*d+p*p+x*x;return n.maxScale=Math.sqrt(Math.max(m,_,y)),n.centerX=e[i+12],n.centerY=e[i+13],n.centerZ=e[i+14],n}}const ut=new u.Box3;class gt extends u.GLBufferAttribute{constructor(t,e,n,i,s,o=1){const a=t.createBuffer();super(a,e,n,i,s.length/n),this.isGLInstancedBufferAttribute=!0,this._needsUpdate=!1,this.isInstancedBufferAttribute=!0,this.meshPerAttribute=o,this.array=s,this._cacheArray=s,t.bindBuffer(t.ARRAY_BUFFER,a),t.bufferData(t.ARRAY_BUFFER,s,t.DYNAMIC_DRAW)}update(t,e){if(!this._needsUpdate||e===0)return;const n=t.getContext();n.bindBuffer(n.ARRAY_BUFFER,this.buffer),this.array===this._cacheArray?n.bufferSubData(n.ARRAY_BUFFER,0,this.array,0,e):(n.bufferData(n.ARRAY_BUFFER,this.array,n.DYNAMIC_DRAW),this._cacheArray=this.array),this._needsUpdate=!1}clone(){return this}}function it(r,t){return Math.max(t,Math.ceil(Math.sqrt(r/t))*t)}function bt(r,t,e,n){t===3&&(console.warn('"channels" cannot be 3. Set to 4. More info: https://github.com/mrdoob/three.js/pull/23228'),t=4);const i=it(n,e),s=new r(i*i*t),o=r.name.includes("Float"),a=r.name.includes("Uint"),h=o?u.FloatType:a?u.UnsignedIntType:u.IntType;let c;switch(t){case 1:c=o?u.RedFormat:u.RedIntegerFormat;break;case 2:c=o?u.RGFormat:u.RGIntegerFormat;break;case 4:c=o?u.RGBAFormat:u.RGBAIntegerFormat;break}return{array:s,size:i,type:h,format:c}}class j extends u.DataTexture{constructor(t,e,n,i,s,o){e===3&&(e=4);const{array:a,format:h,size:c,type:l}=bt(t,e,n,i);super(a,c,c,h,l),this.partialUpdate=!0,this.maxUpdateCalls=1/0,this._utils=null,this._needsUpdate=!1,this._lastWidth=null,this._data=a,this._channels=e,this._pixelsPerInstance=n,this._stride=n*e,this._rowToUpdate=new Array(c),this._uniformMap=s,this._fetchUniformsInFragmentShader=o,this.needsUpdate=!0}resize(t){const e=it(t,this._pixelsPerInstance);if(e===this.image.width)return;const n=this._data,i=this._channels;this._rowToUpdate.length=e;const s=n.constructor,o=new s(e*e*i),a=Math.min(n.length,o.length);o.set(new s(n.buffer,0,a)),this.dispose(),this.image={data:o,height:e,width:e},this._data=o}enqueueUpdate(t){if(this._needsUpdate=!0,!this.partialUpdate)return;const e=this.image.width/this._pixelsPerInstance,n=Math.floor(t/e);this._rowToUpdate[n]=!0}update(t){const e=t.properties.get(this),n=this.version>0&&e.__version!==this.version,i=this._lastWidth!==null&&this._lastWidth!==this.image.width;if(!this._needsUpdate||!e.__webglTexture||n||i){this._lastWidth=this.image.width,this._needsUpdate=!1;return}if(this._needsUpdate=!1,!this.partialUpdate){this.needsUpdate=!0;return}const s=this.getUpdateRowsInfo();s.length!==0&&(s.length>this.maxUpdateCalls?this.needsUpdate=!0:this.updateRows(e,t,s),this._rowToUpdate.fill(!1))}getUpdateRowsInfo(){const t=this._rowToUpdate,e=[];for(let n=0,i=t.length;n<i;n++)if(t[n]){const s=n;for(;n<i&&t[n];n++);e.push({row:s,count:n-s})}return e}updateRows(t,e,n){const i=e.state,s=e.getContext();this._utils??(this._utils=new u.WebGLUtils(s,e.extensions,e.capabilities));const o=this._utils.convert(this.format),a=this._utils.convert(this.type),{data:h,width:c}=this.image,l=this._channels;i.bindTexture(s.TEXTURE_2D,t.__webglTexture);const d=u.ColorManagement.getPrimaries(u.ColorManagement.workingColorSpace),p=this.colorSpace===u.NoColorSpace?null:u.ColorManagement.getPrimaries(this.colorSpace),x=this.colorSpace===u.NoColorSpace||d===p?s.NONE:s.BROWSER_DEFAULT_WEBGL;s.pixelStorei(s.UNPACK_FLIP_Y_WEBGL,this.flipY),s.pixelStorei(s.UNPACK_PREMULTIPLY_ALPHA_WEBGL,this.premultiplyAlpha),s.pixelStorei(s.UNPACK_ALIGNMENT,this.unpackAlignment),s.pixelStorei(s.UNPACK_COLORSPACE_CONVERSION_WEBGL,x);for(const{count:m,row:_}of n)s.texSubImage2D(s.TEXTURE_2D,0,0,_,c,m,o,a,h,_*c*l);this.onUpdate&&this.onUpdate(this)}setUniformAt(t,e,n){const{offset:i,size:s}=this._uniformMap.get(e),o=this._stride;s===1?this._data[t*o+i]=n:n.toArray(this._data,t*o+i)}getUniformAt(t,e,n){const{offset:i,size:s}=this._uniformMap.get(e),o=this._stride;return s===1?this._data[t*o+i]:n.fromArray(this._data,t*o+i)}getUniformsGLSL(t,e,n){const i=this.getUniformsVertexGLSL(t,e,n),s=this.getUniformsFragmentGLSL(t,e,n);return{vertex:i,fragment:s}}getUniformsVertexGLSL(t,e,n){if(this._fetchUniformsInFragmentShader)return` flat varying ${n} ez_v${e}; void main() { ez_v${e} = ${e};`;const i=this.texelsFetchGLSL(t,e),s=this.getFromTexelsGLSL(),{assignVarying:o,declareVarying:a}=this.getVarying();return` uniform highp sampler2D ${t}; ${a} void main() { ${i} ${s} ${o}`}getUniformsFragmentGLSL(t,e,n){if(!this._fetchUniformsInFragmentShader){const{declareVarying:o,getVarying:a}=this.getVarying();return` ${o} void main() { ${a}`}const i=this.texelsFetchGLSL(t,`ez_v${e}`),s=this.getFromTexelsGLSL();return` uniform highp sampler2D ${t}; flat varying ${n} ez_v${e}; void main() { ${i} ${s}`}texelsFetchGLSL(t,e){const n=this._pixelsPerInstance;let i=` int size = textureSize(${t}, 0).x; int j = int(${e}) * ${n}; int x = j % size; int y = j / size; `;for(let s=0;s<n;s++)i+=`vec4 ez_texel${s} = texelFetch(${t}, ivec2(x + ${s}, y), 0); `;return i}getFromTexelsGLSL(){const t=this._uniformMap;let e="";for(const[n,{type:i,offset:s,size:o}]of t){const a=Math.floor(s/this._channels);if(i==="mat3")e+=`mat3 ${n} = mat3(ez_texel${a}.rgb, vec3(ez_texel${a}.a, ez_texel${a+1}.rg), vec3(ez_texel${a+1}.ba, ez_texel${a+2}.r)); `;else if(i==="mat4")e+=`mat4 ${n} = mat4(ez_texel${a}, ez_texel${a+1}, ez_texel${a+2}, ez_texel${a+3}); `;else{const h=this.getUniformComponents(s,o);e+=`${i} ${n} = ez_texel${a}.${h}; `}}return e}getVarying(){const t=this._uniformMap;let e="",n="",i="";for(const[s,{type:o}]of t)e+=`flat varying ${o} ez_v${s}; `,n+=`ez_v${s} = ${s}; `,i+=`${o} ${s} = ez_v${s}; `;return{declareVarying:e,assignVarying:n,getVarying:i}}getUniformComponents(t,e){const n=t%this._channels;let i="";for(let s=0;s<e;s++)i+=It[n+s];return i}copy(t){return super.copy(t),this.partialUpdate=t.partialUpdate,this.maxUpdateCalls=t.maxUpdateCalls,this._channels=t._channels,this._pixelsPerInstance=t._pixelsPerInstance,this._stride=t._stride,this._rowToUpdate=t._rowToUpdate,this._uniformMap=t._uniformMap,this._fetchUniformsInFragmentShader=t._fetchUniformsInFragmentShader,this}}const It=["r","g","b","a"];class f extends u.Mesh{constructor(t,e,n={},i){if(!t)throw new Error('"geometry" is mandatory.');if(!e)throw new Error('"material" is mandatory.');const{allowsEuler:s,renderer:o,createEntities:a}=n;super(t,null),this.type="InstancedMesh2",this.isInstancedMesh2=!0,this.instances=null,this.instanceIndex=null,this.colorsTexture=null,this.morphTexture=null,this.boneTexture=null,this.uniformsTexture=null,this.boundingBox=null,this.boundingSphere=null,this.bvh=null,this.customSort=null,this.raycastOnlyFrustum=!1,this.LODinfo=null,this.autoUpdate=!0,this.bindMode=u.AttachedBindMode,this.bindMatrix=null,this.bindMatrixInverse=null,this.skeleton=null,this.onFrustumEnter=null,this._renderer=null,this._instancesCount=0,this._instancesArrayCount=0,this._perObjectFrustumCulled=!0,this._sortObjects=!1,this._indexArrayNeedsUpdate=!1,this._useOpacity=!1,this._currentMaterial=null,this._customProgramCacheKeyBase=null,this._onBeforeCompileBase=null,this._propertiesGetBase=null,this._propertiesGetMap=new WeakMap,this._properties=new WeakMap,this._freeIds=[],this.isInstancedMesh=!0,this.instanceMatrix=new u.InstancedBufferAttribute(new Float32Array(0),16),this.instanceColor=null,this._customProgramCacheKey=()=>`ezInstancedMesh2_${this.id}_${!!this.colorsTexture}_${this._useOpacity}_${!!this.boneTexture}_${!!this.uniformsTexture}_${this._customProgramCacheKeyBase.call(this._currentMaterial)}`,this._onBeforeCompile=(c,l)=>{if(this._onBeforeCompileBase&&this._onBeforeCompileBase.call(this._currentMaterial,c,l),c.instancing=!1,c.defines??(c.defines={}),c.defines.USE_INSTANCING_INDIRECT="",c.uniforms.matricesTexture={value:this.matricesTexture},this.uniformsTexture){c.uniforms.uniformsTexture={value:this.uniformsTexture};const{vertex:d,fragment:p}=this.uniformsTexture.getUniformsGLSL("uniformsTexture","instanceIndex","uint");c.vertexShader=c.vertexShader.replace("void main() {",d),c.fragmentShader=c.fragmentShader.replace("void main() {",p)}this.colorsTexture&&c.fragmentShader.includes("#include <color_pars_fragment>")&&(c.defines.USE_INSTANCING_COLOR_INDIRECT="",c.uniforms.colorsTexture={value:this.colorsTexture},c.vertexShader=c.vertexShader.replace("<color_vertex>","<instanced_color_vertex>"),c.vertexColors&&(c.defines.USE_VERTEX_COLOR=""),this._useOpacity?c.defines.USE_COLOR_ALPHA="":c.defines.USE_COLOR=""),this.boneTexture&&(c.defines.USE_SKINNING="",c.defines.USE_INSTANCING_SKINNING="",c.uniforms.bindMatrix={value:this.bindMatrix},c.uniforms.bindMatrixInverse={value:this.bindMatrixInverse},c.uniforms.bonesPerInstance={value:this.skeleton.bones.length},c.uniforms.boneTexture={value:this.boneTexture})};const h=n.capacity>0?n.capacity:Tt;this._renderer=o,this._capacity=h,this._parentLOD=i,this._geometry=t,this.material=e,this._allowsEuler=s??!1,this._tempInstance=new nt(this,-1,s),this.availabilityArray=(i==null?void 0:i.availabilityArray)??new Array(h*2),this._createEntities=a,this.initIndexAttribute(),this.initMatricesTexture()}get capacity(){return this._capacity}get instancesCount(){return this._instancesCount}get perObjectFrustumCulled(){return this._perObjectFrustumCulled}set perObjectFrustumCulled(t){this._perObjectFrustumCulled=t,this._indexArrayNeedsUpdate=!0}get sortObjects(){return this._sortObjects}set sortObjects(t){this._sortObjects=t,this._indexArrayNeedsUpdate=!0}get geometry(){return this._geometry}set geometry(t){this._geometry=t,this.patchGeometry(t)}onBeforeShadow(t,e,n,i,s,o,a){var h,c,l;this.patchMaterial(t,o),!(!this.instanceIndex||a&&!this.isFirstGroup(a.materialIndex))&&(this.autoUpdate&&this.performFrustumCulling(i,n),this.matricesTexture.update(t),(h=this.colorsTexture)==null||h.update(t),(c=this.uniformsTexture)==null||c.update(t),(l=this.boneTexture)==null||l.update(t))}onBeforeRender(t,e,n,i,s,o){var a,h,c;if(this.patchMaterial(t,s),!this.instanceIndex){this._renderer=t;return}o&&!this.isFirstGroup(o.materialIndex)||(this.autoUpdate&&this.performFrustumCulling(n),this.matricesTexture.update(t),(a=this.colorsTexture)==null||a.update(t),(h=this.uniformsTexture)==null||h.update(t),(c=this.boneTexture)==null||c.update(t))}onAfterShadow(t,e,n,i,s,o,a){this.unpatchMaterial(t,o)}onAfterRender(t,e,n,i,s,o){this.unpatchMaterial(t,s),!(this.instanceIndex||o&&!this.isLastGroup(o.materialIndex))&&this.initIndexAttribute()}isFirstGroup(t){const e=this.material;for(let n=0;n<=t;n++)if(e[n].visible)return n===t}isLastGroup(t){const e=this.material;for(let n=e.length-1;n>=t;n--)if(e[n].visible)return n===t}initIndexAttribute(){if(!this._renderer){this.count=0;return}const t=this._renderer.getContext(),e=this._capacity,n=new Uint32Array(e);for(let i=0;i<e;i++)n[i]=i;this.instanceIndex=new gt(t,t.UNSIGNED_INT,1,4,n),this._geometry.setAttribute("instanceIndex",this.instanceIndex)}initMatricesTexture(){this._parentLOD||(this.matricesTexture=new j(Float32Array,4,4,this._capacity))}initColorsTexture(){this._parentLOD||(this.colorsTexture=new j(Float32Array,4,1,this._capacity),this.colorsTexture.colorSpace=u.ColorManagement.workingColorSpace,this.colorsTexture._data.fill(1),this.materialsNeedsUpdate())}materialsNeedsUpdate(){if(this.material.isMaterial){this.material.needsUpdate=!0;return}for(const t of this.material)t.needsUpdate=!0}patchGeometry(t){const e=t.getAttribute("instanceIndex");if(e){if(e===this.instanceIndex)return;console.warn("The geometry has been cloned because it was already used."),t=t.clone(),t.deleteAttribute("instanceIndex")}this.instanceIndex&&t.setAttribute("instanceIndex",this.instanceIndex)}patchMaterial(t,e){this._currentMaterial=e,this._customProgramCacheKeyBase=e.customProgramCacheKey,this._onBeforeCompileBase=e.onBeforeCompile,e.customProgramCacheKey=this._customProgramCacheKey,e.onBeforeCompile=this._onBeforeCompile;const n=t.properties;if(!this._properties.has(e)){const i={};this._properties.set(e,i);const s=this._propertiesGetBase=n.get;this._propertiesGetMap.set(e,o=>o===e?i:s(o))}n.get=this._propertiesGetMap.get(e)}unpatchMaterial(t,e){this._currentMaterial=null,t.properties.get=this._propertiesGetBase,e.onBeforeCompile=this._onBeforeCompileBase,e.customProgramCacheKey=this._customProgramCacheKeyBase,this._onBeforeCompileBase=null,this._customProgramCacheKeyBase=null}computeBVH(t={}){this.bvh||(this.bvh=new yt(this,t.margin,t.getBBoxFromBSphere,t.accurateCulling)),this.bvh.clear(),this.bvh.create()}disposeBVH(){this.bvh=null}setMatrixAt(t,e){var n;if(e.toArray(this.matricesTexture._data,t*16),this.instances){const i=this.instances[t];e.decompose(i.position,i.quaternion,i.scale)}this.matricesTexture.enqueueUpdate(t),(n=this.bvh)==null||n.move(t)}getMatrixAt(t,e=Ct){return e.fromArray(this.matricesTexture._data,t*16)}getPositionAt(t,e=St){const n=t*16,i=this.matricesTexture._data;return e.x=i[n+12],e.y=i[n+13],e.z=i[n+14],e}getPositionAndMaxScaleOnAxisAt(t,e){const n=t*16,i=this.matricesTexture._data,s=i[n+0],o=i[n+1],a=i[n+2],h=s*s+o*o+a*a,c=i[n+4],l=i[n+5],d=i[n+6],p=c*c+l*l+d*d,x=i[n+8],m=i[n+9],_=i[n+10],y=x*x+m*m+_*_;return e.x=i[n+12],e.y=i[n+13],e.z=i[n+14],Math.sqrt(Math.max(h,p,y))}applyMatrixAtToSphere(t,e,n,i){const s=t*16,o=this.matricesTexture._data,a=o[s+0],h=o[s+1],c=o[s+2],l=o[s+3],d=o[s+4],p=o[s+5],x=o[s+6],m=o[s+7],_=o[s+8],y=o[s+9],v=o[s+10],w=o[s+11],M=o[s+12],U=o[s+13],O=o[s+14],F=o[s+15],I=e.center,g=n.x,b=n.y,A=n.z,T=1/(l*g+m*b+w*A+F);I.x=(a*g+d*b+_*A+M)*T,I.y=(h*g+p*b+y*A+U)*T,I.z=(c*g+x*b+v*A+O)*T;const D=a*a+h*h+c*c,E=d*d+p*p+x*x,N=_*_+y*y+v*v;e.radius=i*Math.sqrt(Math.max(D,E,N))}setVisibilityAt(t,e){this.availabilityArray[t*2]=e,this._indexArrayNeedsUpdate=!0}getVisibilityAt(t){return this.availabilityArray[t*2]}setActiveAt(t,e){this.availabilityArray[t*2+1]=e,this._indexArrayNeedsUpdate=!0}getActiveAt(t){return this.availabilityArray[t*2+1]}getActiveAndVisibilityAt(t){const e=t*2,n=this.availabilityArray;return n[e]&&n[e+1]}setActiveAndVisibilityAt(t,e){const n=t*2,i=this.availabilityArray;i[n]=e,i[n+1]=e,this._indexArrayNeedsUpdate=!0}setColorAt(t,e){this.colorsTexture===null&&this.initColorsTexture(),e.isColor?e.toArray(this.colorsTexture._data,t*4):pt.set(e).toArray(this.colorsTexture._data,t*4),this.colorsTexture.enqueueUpdate(t)}getColorAt(t,e=pt){return e.fromArray(this.colorsTexture._data,t*4)}setOpacityAt(t,e){this._useOpacity||(this.colorsTexture===null?this.initColorsTexture():this.materialsNeedsUpdate(),this._useOpacity=!0),this.colorsTexture._data[t*4+3]=e,this.colorsTexture.enqueueUpdate(t)}getOpacityAt(t){return this._useOpacity?this.colorsTexture._data[t*4+3]:1}copyTo(t,e){this.getMatrixAt(t,e.matrix).decompose(e.position,e.quaternion,e.scale)}computeBoundingBox(){const t=this._geometry,e=this._instancesArrayCount;this.boundingBox??(this.boundingBox=new u.Box3),t.boundingBox===null&&t.computeBoundingBox();const n=t.boundingBox,i=this.boundingBox;i.makeEmpty();for(let s=0;s<e;s++)this.getActiveAt(s)&&(lt.copy(n).applyMatrix4(this.getMatrixAt(s)),i.union(lt))}computeBoundingSphere(){const t=this._geometry,e=this._instancesArrayCount;this.boundingSphere??(this.boundingSphere=new u.Sphere),t.boundingSphere===null&&t.computeBoundingSphere();const n=t.boundingSphere,i=this.boundingSphere;i.makeEmpty();for(let s=0;s<e;s++)this.getActiveAt(s)&&(dt.copy(n).applyMatrix4(this.getMatrixAt(s)),i.union(dt))}clone(t){const e={capacity:this._capacity,renderer:this._renderer,allowsEuler:this._allowsEuler,createEntities:this._createEntities};return new this.constructor(this.geometry,this.material,e).copy(this,t)}copy(t,e){return super.copy(t,e),this.count=t._capacity,this._instancesCount=t._instancesCount,this._instancesArrayCount=t._instancesArrayCount,this._capacity=t._capacity,t.boundingBox!==null&&(this.boundingBox=t.boundingBox.clone()),t.boundingSphere!==null&&(this.boundingSphere=t.boundingSphere.clone()),this.matricesTexture=t.matricesTexture.clone(),this.matricesTexture.image.data=this.matricesTexture.image.data.slice(),t.colorsTexture!==null&&(this.colorsTexture=t.colorsTexture.clone(),this.colorsTexture.image.data=this.colorsTexture.image.data.slice()),t.uniformsTexture!==null&&(this.uniformsTexture=t.uniformsTexture.clone(),this.uniformsTexture.image.data=this.uniformsTexture.image.data.slice()),t.morphTexture!==null&&(this.morphTexture=t.morphTexture.clone(),this.morphTexture.image.data=this.morphTexture.image.data.slice()),t.boneTexture!==null&&(this.boneTexture=t.boneTexture.clone(),this.boneTexture.image.data=this.boneTexture.image.data.slice()),this}dispose(){var t,e,n,i;this.dispatchEvent({type:"dispose"}),this.matricesTexture.dispose(),(t=this.colorsTexture)==null||t.dispose(),(e=this.morphTexture)==null||e.dispose(),(n=this.boneTexture)==null||n.dispose(),(i=this.uniformsTexture)==null||i.dispose()}updateMatrixWorld(t){super.updateMatrixWorld(t),this.bindMatrixInverse&&(this.bindMode===u.AttachedBindMode?this.bindMatrixInverse.copy(this.matrixWorld).invert():this.bindMode===u.DetachedBindMode?this.bindMatrixInverse.copy(this.bindMatrix).invert():console.warn("Unrecognized bindMode: "+this.bindMode))}}const Tt=1e3,lt=new u.Box3,dt=new u.Sphere,Ct=new u.Matrix4,pt=new u.Color,St=new u.Vector3;f.prototype.resizeBuffers=function(r){var n;const t=this._capacity;this._capacity=r;const e=Math.min(r,t);if(this.instanceIndex){const i=new Uint32Array(r);i.set(new Uint32Array(this.instanceIndex.array.buffer,0,e)),this.instanceIndex.array=i}if(this.LODinfo){for(const i of this.LODinfo.objects)if(i._capacity=r,i.instanceIndex){const s=new Uint32Array(r);s.set(new Uint32Array(i.instanceIndex.array.buffer,0,e)),i.instanceIndex.array=s}}if(this.availabilityArray.length=r*2,this.matricesTexture.resize(r),this.colorsTexture&&(this.colorsTexture.resize(r),r>t&&this.colorsTexture._data.fill(1,t*4)),this.morphTexture){const i=this.morphTexture.image.data,s=i.length/t;this.morphTexture.dispose(),this.morphTexture=new u.DataTexture(new Float32Array(s*r),s,r,u.RedFormat,u.FloatType),this.morphTexture.image.data.set(i)}return(n=this.uniformsTexture)==null||n.resize(r),this};f.prototype.setInstancesArrayCount=function(r){if(r<this._instancesArrayCount){const e=this.bvh;if(e)for(let n=this._instancesArrayCount-1;n>=r;n--)this.getActiveAt(n)&&e.delete(n);this._instancesArrayCount=r;return}if(r>this._capacity){let e=this._capacity+(this._capacity>>1)+512;for(;e<r;)e+=(e>>1)+512;this.resizeBuffers(e)}const t=this._instancesArrayCount;this._instancesArrayCount=r,this._createEntities&&this.createEntities(t)};function wt(r){const t={get:e=>e.depthSort,aux:new Array(r._capacity),reversed:!1};return function(n){var h;t.reversed=!!((h=r.material)!=null&&h.transparent),r._capacity>t.aux.length&&(t.aux.length=r._capacity);let i=1/0,s=-1/0;for(const{depth:c}of n)c>s&&(s=c),c<i&&(i=c);const o=s-i,a=(2**32-1)/o;for(const c of n)c.depthSort=(c.depth-i)*a;vt.radixSort(n,t)}}function st(r,t){return r.depth-t.depth}function rt(r,t){return t.depth-r.depth}class At{constructor(){this.array=[],this.pool=[]}push(t,e){const n=this.pool,i=this.array,s=i.length;s>=n.length&&n.push({depth:null,index:null,depthSort:null});const o=n[s];o.depth=t,o.index=e,i.push(o)}reset(){this.array.length=0}}const Z=new u.Frustum,S=new At,L=new u.Matrix4,z=new u.Matrix4,J=new u.Vector3,P=new u.Vector3,R=new u.Vector3,Mt=new u.Vector3,C=new u.Sphere;f.prototype.performFrustumCulling=function(r,t=r){if(!this._parentLOD&&this._instancesArrayCount===0){this.count=0;return}const e=this.LODinfo,n=r!==t;let i;if(e){i=n?e.shadowRender??e.render:e.render;for(const s of e.objects)s.count=0}(i==null?void 0:i.levels.length)>0?this.frustumCullingLOD(i,r,t):this._parentLOD||this.frustumCulling(r),this.instanceIndex.update(this._renderer,this.count)};f.prototype.frustumCulling=function(r){var i;const t=this._sortObjects,e=this._perObjectFrustumCulled,n=this.instanceIndex.array;if(this.instanceIndex._needsUpdate=!0,!e&&!t){this.updateIndexArray();return}if(t&&(z.copy(this.matrixWorld).invert(),P.setFromMatrixPosition(r.matrixWorld).applyMatrix4(z),J.set(0,0,-1).transformDirection(r.matrixWorld).transformDirection(z)),e?(L.multiplyMatrices(r.projectionMatrix,r.matrixWorldInverse).multiply(this.matrixWorld),this.bvh?this.BVHCulling(r):this.linearCulling(r)):this.updateRenderList(),t){const s=this.customSort;s===null?S.array.sort((i=this.material)!=null&&i.transparent?rt:st):s(S.array);const o=S.array,a=o.length;for(let h=0;h<a;h++)n[h]=o[h].index;this.count=a,S.reset()}};f.prototype.updateIndexArray=function(){if(!this._indexArrayNeedsUpdate)return;const r=this.instanceIndex.array,t=this._instancesArrayCount;let e=0;for(let n=0;n<t;n++)this.getActiveAndVisibilityAt(n)&&(r[e++]=n);this.count=e,this._indexArrayNeedsUpdate=!1};f.prototype.updateRenderList=function(){const r=this._instancesArrayCount;for(let t=0;t<r;t++)if(this.getActiveAndVisibilityAt(t)){const e=this.getPositionAt(t).sub(P).dot(J);S.push(e,t)}};f.prototype.BVHCulling=function(r){const t=this.instanceIndex.array,e=this._instancesArrayCount,n=this._sortObjects,i=this.onFrustumEnter;let s=0;this.bvh.frustumCulling(L,o=>{const a=o.object;if(a<e&&this.getVisibilityAt(a)&&(!i||i(a,r)))if(n){const h=this.getPositionAt(a).sub(P).dot(J);S.push(h,a)}else t[s++]=a}),this.count=s};f.prototype.linearCulling=function(r){const t=this.instanceIndex.array;this.geometry.boundingSphere||this.geometry.computeBoundingSphere();const e=this._geometry.boundingSphere,n=e.radius,i=e.center,s=this._instancesArrayCount,o=i.x===0&&i.y===0&&i.z===0,a=this._sortObjects,h=this.onFrustumEnter;let c=0;Z.setFromProjectionMatrix(L);for(let l=0;l<s;l++)if(this.getActiveAndVisibilityAt(l)){if(o){const d=this.getPositionAndMaxScaleOnAxisAt(l,C.center);C.radius=n*d}else this.applyMatrixAtToSphere(l,C,i,n);if(Z.intersectsSphere(C)&&(!h||h(l,r)))if(a){const d=Mt.subVectors(C.center,P).dot(J);S.push(d,l)}else t[c++]=l}this.count=c};f.prototype.frustumCullingLOD=function(r,t,e){var h,c;const{count:n,levels:i}=r,o=!(t!==e)&&this._sortObjects;for(let l=0;l<i.length;l++)n[l]=0,i[l].object.instanceIndex&&(i[l].object.instanceIndex._needsUpdate=!0);L.multiplyMatrices(t.projectionMatrix,t.matrixWorldInverse).multiply(this.matrixWorld),z.copy(this.matrixWorld).invert(),P.setFromMatrixPosition(t.matrixWorld).applyMatrix4(z),R.setFromMatrixPosition(e.matrixWorld).applyMatrix4(z);const a=r.levels.map(l=>l.object.instanceIndex.array);if(this.bvh?this.BVHCullingLOD(r,a,o,t,e):this.linearCullingLOD(r,a,o,t,e),o){const l=this.customSort,d=S.array;let p=0,x=i[1].distance;l===null?d.sort((h=i[0].object.material)!=null&&h.transparent?rt:st):l(d);for(let m=0,_=d.length;m<_;m++){const y=d[m];y.depth>x&&(p++,x=((c=i[p+1])==null?void 0:c.distance)??1/0),a[p][n[p]++]=y.index}S.reset()}for(let l=0;l<i.length;l++){const d=i[l].object;d.count=n[l]}};f.prototype.BVHCullingLOD=function(r,t,e,n,i){const{count:s,levels:o}=r,a=this._instancesArrayCount,h=this.onFrustumEnter;e?this.bvh.frustumCulling(L,c=>{const l=c.object;if(l<a&&this.getVisibilityAt(l)&&(!h||h(l,n,i))){const d=this.getPositionAt(l).distanceToSquared(R);S.push(d,l)}}):this.bvh.frustumCullingLOD(L,R,o,(c,l)=>{const d=c.object;if(d<a&&this.getVisibilityAt(d)){if(l===null){const p=this.getPositionAt(d).distanceToSquared(R);l=this.getObjectLODIndexForDistance(o,p)}(!h||h(d,n,i,l))&&(t[l][s[l]++]=d)}})};f.prototype.linearCullingLOD=function(r,t,e,n,i){const{count:s,levels:o}=r;this.geometry.boundingSphere||this.geometry.computeBoundingSphere();const a=this._geometry.boundingSphere,h=a.radius,c=a.center,l=this._instancesArrayCount,d=c.x===0&&c.y===0&&c.z===0,p=this.onFrustumEnter;Z.setFromProjectionMatrix(L);for(let x=0;x<l;x++)if(this.getActiveAndVisibilityAt(x)){if(d){const m=this.getPositionAndMaxScaleOnAxisAt(x,C.center);C.radius=h*m}else this.applyMatrixAtToSphere(x,C,c,h);if(Z.intersectsSphere(C))if(e){if(!p||p(x,n,i))continue;const m=C.center.distanceToSquared(R);S.push(m,x)}else{const m=C.center.distanceToSquared(R),_=this.getObjectLODIndexForDistance(o,m);(!p||p(x,n,i,_))&&(t[_][s[_]++]=x)}}};f.prototype.clearTempInstance=function(r){const t=this._tempInstance;return t.id=r,this.clearInstance(t)};f.prototype.clearTempInstancePosition=function(r){const t=this._tempInstance;return t.id=r,t.position.set(0,0,0),t};f.prototype.clearInstance=function(r){return r.position.set(0,0,0),r.scale.set(1,1,1),r.quaternion.identity(),r};f.prototype.updateInstances=function(r){const t=this._instancesArrayCount,e=this.instances;for(let n=0;n<t;n++){if(!this.getActiveAt(n))continue;const i=e?e[n]:this.clearTempInstance(n);r(i,n),i.updateMatrix()}return this};f.prototype.updateInstancesPosition=function(r){const t=this._instancesArrayCount,e=this.instances;for(let n=0;n<t;n++){if(!this.getActiveAt(n))continue;const i=e?e[n]:this.clearTempInstancePosition(n);r(i,n),i.updateMatrixPosition()}return this};f.prototype.createEntities=function(r){const t=this._instancesArrayCount;if(!this.instances)this.instances=new Array(t);else if(this.instances.length<t)this.instances.length=t;else return this;const e=this.instances;for(let n=r;n<t;n++)e[n]||(e[n]=new nt(this,n,this._allowsEuler));return this};f.prototype.addInstances=function(r,t){!t&&this.bvh&&console.warn("InstancedMesh2: if `computeBVH()` has already been called, it is better to valorize the instances in the `onCreation` callback for better performance.");const e=this._freeIds;if(e.length>0){let s=-1;const o=Math.min(e.length,r),a=e.length-o;for(let h=e.length-1;h>=a;h--){const c=e[h];c>s&&(s=c),this.addInstance(c,t)}e.length-=o,r-=o,this._instancesArrayCount=Math.max(s+1,this._instancesArrayCount)}const n=this._instancesArrayCount,i=n+r;this.setInstancesArrayCount(i);for(let s=n;s<i;s++)this.addInstance(s,t);return this};f.prototype.addInstance=function(r,t){var n;this._instancesCount++,this.setActiveAndVisibilityAt(r,!0);const e=this.instances?this.clearInstance(this.instances[r]):this.clearTempInstance(r);t?(t(e,r),e.updateMatrix()):e.setMatrixIdentity(),(n=this.bvh)==null||n.insert(r)};f.prototype.removeInstances=function(...r){const t=this._freeIds,e=this.bvh;for(const n of r)n<this._instancesArrayCount&&this.getActiveAt(n)&&(this.setActiveAt(n,!1),t.push(n),e==null||e.delete(n),this._instancesCount--);for(let n=this._instancesArrayCount-1;n>=0&&!this.getActiveAt(n);n--)this._instancesArrayCount--;return this};f.prototype.clearInstances=function(){var r;if(this._instancesCount=0,this._instancesArrayCount=0,this._freeIds.length=0,(r=this.bvh)==null||r.clear(),this.LODinfo)for(const t of this.LODinfo.objects)t.count=0;return this};f.prototype.getObjectLODIndexForDistance=function(r,t){for(let e=r.length-1;e>0;e--){const n=r[e],i=n.distance-n.distance*n.hysteresis;if(t>=i)return e}return 0};f.prototype.setFirstLODDistance=function(r=0,t=0){if(this._parentLOD)throw new Error("Cannot create LOD for this InstancedMesh2.");return this.LODinfo||(this.LODinfo={render:null,shadowRender:null,objects:[this]}),this.LODinfo.render||(this.LODinfo.render={levels:[{distance:r,hysteresis:t,object:this}],count:[0]}),this};f.prototype.addLOD=function(r,t,e=0,n=0){var i;if(this._parentLOD)throw new Error("Cannot create LOD for this InstancedMesh2.");if(!((i=this.LODinfo)!=null&&i.render)&&e===0)throw new Error('Cannot set distance to 0 for the first LOD. Use "setFirstLODDistance" before use "addLOD".');return this.setFirstLODDistance(0,n),this.addLevel(this.LODinfo.render,r,t,e,n),this};f.prototype.addShadowLOD=function(r,t=0,e=0){if(this._parentLOD)throw new Error("Cannot create LOD for this InstancedMesh2.");this.LODinfo||(this.LODinfo={render:null,shadowRender:null,objects:[this]}),this.LODinfo.shadowRender||(this.LODinfo.shadowRender={levels:[],count:[]});const n=this.addLevel(this.LODinfo.shadowRender,r,null,t,e);return n.castShadow=!0,this.castShadow=!0,this};f.prototype.addLevel=function(r,t,e,n,i){const s=this.LODinfo.objects,o=r.levels;let a,h;n=n**2;const c=s.findIndex(l=>l.geometry===t);if(c===-1){const l={capacity:this._capacity,renderer:this._renderer};h=new f(t,e??new u.ShaderMaterial,l,this),h.frustumCulled=!1,this.patchLevel(h),s.push(h),this.add(h)}else h=s[c],e&&(h.material=e);for(a=0;a<o.length&&!(n<o[a].distance);a++);return o.splice(a,0,{distance:n,hysteresis:i,object:h}),r.count.push(0),h};f.prototype.patchLevel=function(r){Object.defineProperty(r,"matricesTexture",{get(){return this._parentLOD.matricesTexture}}),Object.defineProperty(r,"colorsTexture",{get(){return this._parentLOD.colorsTexture}}),Object.defineProperty(r,"uniformsTexture",{get(){return this._parentLOD.uniformsTexture}}),Object.defineProperty(r,"morphTexture",{get(){return this._parentLOD.morphTexture}}),Object.defineProperty(r,"boneTexture",{get(){return this._parentLOD.boneTexture}}),Object.defineProperty(r,"skeleton",{get(){return this._parentLOD.skeleton}}),Object.defineProperty(r,"bindMatrixInverse",{get(){return this._parentLOD.bindMatrixInverse}}),Object.defineProperty(r,"bindMatrix",{get(){return this._parentLOD.bindMatrix}})};const Ut=new u.Mesh;f.prototype.getMorphAt=function(r,t=Ut){const e=t.morphTargetInfluences,n=this.morphTexture.source.data.data,i=e.length+1,s=r*i+1;for(let o=0;o<e.length;o++)e[o]=n[s+o];return t};f.prototype.setMorphAt=function(r,t){const e=t.morphTargetInfluences,n=e.length+1;this.morphTexture===null&&!this._parentLOD&&(this.morphTexture=new u.DataTexture(new Float32Array(n*this._capacity),n,this._capacity,u.RedFormat,u.FloatType));const i=this.morphTexture.source.data.data;let s=0;for(const h of e)s+=h;const o=this._geometry.morphTargetsRelative?1:1-s,a=n*r;i[a]=o,i.set(e,a+1),this.morphTexture.needsUpdate=!0};const et=[],Q=new u.Mesh,Ot=new u.Ray,ft=new u.Vector3,xt=new u.Vector3,mt=new u.Matrix4,_t=new u.Sphere;f.prototype.raycast=function(r,t){if(this._parentLOD||!this.material||this._instancesArrayCount===0||!this.instanceIndex)return;Q.geometry=this._geometry,Q.material=this.material;const e=r.ray,n=r.near,i=r.far;mt.copy(this.matrixWorld).invert(),xt.setFromMatrixScale(this.matrixWorld),ft.copy(r.ray.direction).multiply(xt);const s=ft.length();r.ray=Ot.copy(r.ray).applyMatrix4(mt),r.near/=s,r.far/=s,this.raycastInstances(r,t),r.ray=e,r.near=n,r.far=i};f.prototype.raycastInstances=function(r,t){if(this.bvh)this.bvh.raycast(r,e=>this.checkObjectIntersection(r,e,t));else{if(this.boundingSphere===null&&this.computeBoundingSphere(),_t.copy(this.boundingSphere),!r.ray.intersectsSphere(_t))return;const e=this.instanceIndex.array,i=this.raycastOnlyFrustum&&this._perObjectFrustumCulled?this.count:this._instancesArrayCount;for(let s=0;s<i;s++)this.checkObjectIntersection(r,e[s],t)}};f.prototype.checkObjectIntersection=function(r,t,e){if(!(t>this._instancesArrayCount||!this.getActiveAndVisibilityAt(t))){this.getMatrixAt(t,Q.matrixWorld),Q.raycast(r,et);for(const n of et)n.instanceId=t,n.object=this,e.push(n);et.length=0}};f.prototype.initSkeleton=function(r,t=!0){if(r&&this.skeleton!==r&&!this._parentLOD){const e=r.bones;if(this.skeleton=r,this.bindMatrix=new u.Matrix4,this.bindMatrixInverse=new u.Matrix4,this.boneTexture=new j(Float32Array,4,4*e.length,this._capacity),t)for(const n of e)n.matrixAutoUpdate=!1,n.matrixWorldAutoUpdate=!1;this.materialsNeedsUpdate()}};f.prototype.setBonesAt=function(r,t=!0,e){const n=this.skeleton;if(!n)throw new Error('"setBonesAt" cannot be called before "initSkeleton"');const i=n.bones,s=n.boneInverses;for(let o=0,a=i.length;o<a;o++){const h=i[o];t&&(e!=null&&e.has(h.name)||h.updateMatrix(),h.matrixWorld.multiplyMatrices(h.parent.matrixWorld,h.matrix)),this.multiplyBoneMatricesAt(r,o,h.matrixWorld,s[o])}this.boneTexture.enqueueUpdate(r)};f.prototype.multiplyBoneMatricesAt=function(r,t,e,n){const i=(r*this.skeleton.bones.length+t)*16,s=e.elements,o=n.elements,a=this.boneTexture._data,h=s[0],c=s[4],l=s[8],d=s[12],p=s[1],x=s[5],m=s[9],_=s[13],y=s[2],v=s[6],w=s[10],M=s[14],U=s[3],O=s[7],F=s[11],I=s[15],g=o[0],b=o[4],A=o[8],T=o[12],D=o[1],E=o[5],N=o[9],G=o[13],$=o[2],k=o[6],q=o[10],V=o[14],W=o[3],K=o[7],Y=o[11],H=o[15];a[i+0]=h*g+c*D+l*$+d*W,a[i+4]=h*b+c*E+l*k+d*K,a[i+8]=h*A+c*N+l*q+d*Y,a[i+12]=h*T+c*G+l*V+d*H,a[i+1]=p*g+x*D+m*$+_*W,a[i+5]=p*b+x*E+m*k+_*K,a[i+9]=p*A+x*N+m*q+_*Y,a[i+13]=p*T+x*G+m*V+_*H,a[i+2]=y*g+v*D+w*$+M*W,a[i+6]=y*b+v*E+w*k+M*K,a[i+10]=y*A+v*N+w*q+M*Y,a[i+14]=y*T+v*G+w*V+M*H,a[i+3]=U*g+O*D+F*$+I*W,a[i+7]=U*b+O*E+F*k+I*K,a[i+11]=U*A+O*N+F*q+I*Y,a[i+15]=U*T+O*G+F*V+I*H};f.prototype.getUniformAt=function(r,t,e){if(!this.uniformsTexture)throw new Error(`Before get/set uniform, it's necessary to use "initUniformsPerInstance".`);return this.uniformsTexture.getUniformAt(r,t,e)};f.prototype.setUniformAt=function(r,t,e){if(!this.uniformsTexture)throw new Error(`Before get/set uniform, it's necessary to use "initUniformsPerInstance".`);this.uniformsTexture.setUniformAt(r,t,e),this.uniformsTexture.enqueueUpdate(r)};f.prototype.initUniformsPerInstance=function(r){if(!this._parentLOD){const{channels:t,pixelsPerInstance:e,uniformMap:n,fetchInFragmentShader:i}=this.getUniformSchemaResult(r);this.uniformsTexture=new j(Float32Array,t,e,this._capacity,n,i),this.materialsNeedsUpdate()}};f.prototype.getUniformSchemaResult=function(r){let t=0;const e=new Map,n=[],i=r.vertex??{},s=r.fragment??{};let o=!0;for(const l in i){const d=i[l],p=this.getUniformSize(d);t+=p,n.push({name:l,type:d,size:p}),o=!1}for(const l in s)if(!i[l]){const d=s[l],p=this.getUniformSize(d);t+=p,n.push({name:l,type:d,size:p})}n.sort((l,d)=>d.size-l.size);const a=[];for(const{name:l,size:d,type:p}of n){const x=this.getUniformOffset(d,a);e.set(l,{offset:x,size:d,type:p})}const h=Math.ceil(t/4);return{channels:Math.min(t,4),pixelsPerInstance:h,uniformMap:e,fetchInFragmentShader:o}};f.prototype.getUniformOffset=function(r,t){if(r<4){for(let n=0;n<t.length;n++)if(t[n]+r<=4){const i=n*4+t[n];return t[n]+=r,i}}const e=t.length*4;for(;r>0;r-=4)t.push(r);return e};f.prototype.getUniformSize=function(r){switch(r){case"float":return 1;case"vec2":return 2;case"vec3":return 3;case"vec4":return 4;case"mat3":return 9;case"mat4":return 16;default:throw new Error(`Invalid uniform type: ${r}`)}};var Ft=`#ifdef USE_INSTANCING_INDIRECT\r attribute uint instanceIndex;\r uniform highp sampler2D matricesTexture; mat4 getInstancedMatrix() {\r int size = textureSize( matricesTexture, 0 ).x;\r int j = int( instanceIndex ) * 4;\r int x = j % size;\r int y = j / size;\r vec4 v1 = texelFetch( matricesTexture, ivec2( x, y ), 0 );\r vec4 v2 = texelFetch( matricesTexture, ivec2( x + 1, y ), 0 );\r vec4 v3 = texelFetch( matricesTexture, ivec2( x + 2, y ), 0 );\r vec4 v4 = texelFetch( matricesTexture, ivec2( x + 3, y ), 0 );\r return mat4( v1, v2, v3, v4 );\r }\r #endif`,Bt=`#ifdef USE_INSTANCING_COLOR_INDIRECT\r uniform highp sampler2D colorsTexture; #ifdef USE_COLOR_ALPHA\r vec4 getColorTexture() {\r int size = textureSize( colorsTexture, 0 ).x;\r int j = int( instanceIndex );\r int x = j % size;\r int y = j / size;\r return texelFetch( colorsTexture, ivec2( x, y ), 0 );\r }\r #else\r vec3 getColorTexture() {\r int size = textureSize( colorsTexture, 0 ).x;\r int j = int( instanceIndex );\r int x = j % size;\r int y = j / size;\r return texelFetch( colorsTexture, ivec2( x, y ), 0 ).rgb;\r }\r #endif\r #endif`,Lt=`#ifdef USE_INSTANCING_INDIRECT\r mat4 instanceMatrix = getInstancedMatrix(); #ifdef USE_INSTANCING_COLOR_INDIRECT\r vColor *= getColorTexture();\r #endif\r #endif`,Dt=`#ifdef USE_INSTANCING_COLOR_INDIRECT\r #ifdef USE_VERTEX_COLOR\r vColor = color;\r #else\r #ifdef USE_COLOR_ALPHA\r vColor = vec4( 1.0 );\r #else\r vColor = vec3( 1.0 );\r #endif\r #endif\r #endif`,Et=`#ifdef USE_SKINNING\r uniform mat4 bindMatrix;\r uniform mat4 bindMatrixInverse;\r uniform highp sampler2D boneTexture; #ifdef USE_INSTANCING_SKINNING\r uniform int bonesPerInstance;\r #endif mat4 getBoneMatrix( const in float i ) {\r int size = textureSize( boneTexture, 0 ).x; #ifdef USE_INSTANCING_SKINNING\r int j = ( bonesPerInstance * int( instanceIndex ) + int( i ) ) * 4;\r #else\r int j = int( i ) * 4;\r #endif int x = j % size;\r int y = j / size;\r vec4 v1 = texelFetch( boneTexture, ivec2( x, y ), 0 );\r vec4 v2 = texelFetch( boneTexture, ivec2( x + 1, y ), 0 );\r vec4 v3 = texelFetch( boneTexture, ivec2( x + 2, y ), 0 );\r vec4 v4 = texelFetch( boneTexture, ivec2( x + 3, y ), 0 );\r return mat4( v1, v2, v3, v4 );\r }\r #endif`;u.ShaderChunk.instanced_pars_vertex=Ft;u.ShaderChunk.instanced_color_pars_vertex=Bt;u.ShaderChunk.instanced_vertex=Lt;u.ShaderChunk.instanced_color_vertex=Dt;function tt(r){return r.replace("#ifdef USE_INSTANCING","#if defined USE_INSTANCING || defined USE_INSTANCING_INDIRECT")}u.ShaderChunk.project_vertex=tt(u.ShaderChunk.project_vertex);u.ShaderChunk.worldpos_vertex=tt(u.ShaderChunk.worldpos_vertex);u.ShaderChunk.defaultnormal_vertex=tt(u.ShaderChunk.defaultnormal_vertex);u.ShaderChunk.batching_pars_vertex=u.ShaderChunk.batching_pars_vertex.concat(` #include <instanced_pars_vertex>`);u.ShaderChunk.color_pars_vertex=u.ShaderChunk.color_pars_vertex.concat(` #include <instanced_color_pars_vertex>`);u.ShaderChunk.batching_vertex=u.ShaderChunk.batching_vertex.concat(` #include <instanced_vertex>`);u.ShaderChunk.skinning_pars_vertex=Et;u.ShaderChunk.morphinstance_vertex&&(u.ShaderChunk.morphinstance_vertex=u.ShaderChunk.morphinstance_vertex.replaceAll("gl_InstanceID","instanceIndex"));function Nt(r,t={}){if(r.isSkinnedMesh)return e(r);if(r.isInstancedMesh)return n(r);return new f(r.geometry,r.material,t);function e(i){const s=new f(i.geometry,i.material,t);return s.initSkeleton(i.skeleton),s}function n(i){t.capacity=Math.max(i.count,t.capacity);const s=i.geometry.clone();s.deleteAttribute("instanceIndex"),l();const o=new f(s,i.material,t);return o.position.copy(i.position),o.quaternion.copy(i.quaternion),o.scale.copy(i.scale),a(),h(),c(),o;function a(){o.setInstancesArrayCount(i.count),o._instancesCount=i.count,o.availabilityArray.fill(!0,0,i.count*2)}function h(){o.matricesTexture.image.data.set(i.instanceMatrix.array)}function c(){if(i.instanceColor){o.initColorsTexture();const d=i.instanceColor.array,p=o.colorsTexture.image.data;for(let x=0,m=0;x<d.length;x+=3,m+=4)p[m]=d[x],p[m+1]=d[x+1],p[m+2]=d[x+2],p[m+3]=1}}function l(){const d=s.attributes;for(const p in d)d[p].isInstancedBufferAttribute&&console.warn(`InstancedBufferAttribute "${p}" is not supported. It will be ignored.`)}}}exports.GLInstancedBufferAttribute=gt;exports.InstancedEntity=nt;exports.InstancedMesh2=f;exports.InstancedMeshBVH=yt;exports.InstancedRenderList=At;exports.SquareDataTexture=j;exports.createInstancedMesh2From=Nt;exports.createRadixSort=wt;exports.getSquareTextureInfo=bt;exports.getSquareTextureSize=it;exports.patchShader=tt;exports.sortOpaque=st;exports.sortTransparent=rt; //# sourceMappingURL=index.cjs.map