"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){this.isInstanceEntity=!0,this.position=new u.Vector3,this.scale=new u.Vector3(1,1,1),this.id=e,this.owner=t;const i=this.quaternion=new u.Quaternion;if(n){const 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)}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,f=h+h,x=c+c,m=l+l,_=h*f,g=h*x,I=h*m,w=c*x,M=c*m,U=l*m,O=d*f,F=d*x,v=d*m,y=i.x,b=i.y,A=i.z;s[a+0]=(1-(w+U))*y,s[a+1]=(g+v)*y,s[a+2]=(I-F)*y,s[a+3]=0,s[a+4]=(g-v)*b,s[a+5]=(1-(_+U))*b,s[a+6]=(M+O)*b,s[a+7]=0,s[a+8]=(I+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)}}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 gt{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=new Array(t),n=new Uint32Array(t);this.clear();for(let i=0;i{this.nodesMap.set(i.object,i)},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{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=new Map}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;a0&&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],f=e[i+9],x=e[i+10],m=s*s+o*o+a*a,_=h*h+c*c+l*l,g=d*d+f*f+x*x;return n.maxScale=Math.sqrt(Math.max(m,_,g)),n.centerX=e[i+12],n.centerY=e[i+13],n.centerZ=e[i+14],n}}const ut=new u.Box3;class yt 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){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`ezInstancedMesh2_${this.id}_${!!this.colorsTexture}_${!!this.boneTexture}_${!!this.uniformsTexture}`,this._onBeforeCompile=(c,l)=>{if(this._onBeforeCompileBase&&this._onBeforeCompileBase(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:f}=this.uniformsTexture.getUniformsGLSL("uniformsTexture","instanceIndex","uint");c.vertexShader=c.vertexShader.replace("void main() {",d),c.fragmentShader=c.fragmentShader.replace("void main() {",f)}this.colorsTexture&&c.fragmentShader.includes("#include ")&&(c.defines.USE_INSTANCING_COLOR_INDIRECT="",c.uniforms.colorsTexture={value:this.colorsTexture},c.vertexShader=c.vertexShader.replace("",""),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:St;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 count(){return this._count}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)}static createFrom(t,e={}){const n=new p(t.geometry,t.material,e);return t.isSkinnedMesh&&n.initSkeleton(t.skeleton),n}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;io===e?i:s(o))}n.get=this._propertiesGetMap.get(e)}unpatchMaterial(t,e){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 gt(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=wt){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],f=c*c+l*l+d*d,x=i[n+8],m=i[n+9],_=i[n+10],g=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,f,g))}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],f=o[s+5],x=o[s+6],m=o[s+7],_=o[s+8],g=o[s+9],I=o[s+10],w=o[s+11],M=o[s+12],U=o[s+13],O=o[s+14],F=o[s+15],v=e.center,y=n.x,b=n.y,A=n.z,T=1/(l*y+m*b+w*A+F);v.x=(a*y+d*b+_*A+M)*T,v.y=(h*y+f*b+g*A+U)*T,v.z=(c*y+x*b+I*A+O)*T;const D=a*a+h*h+c*c,E=d*d+f*f+x*x,N=_*_+g*g+I*I;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.colorsTexture===null&&this.initColorsTexture(),this._useOpacity=!0,this.colorsTexture._data[t*4+3]=e,this.colorsTexture.enqueueUpdate(t)}getOpacityAt(t){return this.colorsTexture._data[t*4+3]}copyTo(t,e){this.getMatrixAt(t,e.matrix).decompose(e.position,e.quaternion,e.scale)}computeBoundingBox(){const t=this._geometry,e=this._instancesCount;this.boundingBox===null&&(this.boundingBox=new u.Box3),t.boundingBox===null&&t.computeBoundingBox();const n=t.boundingBox,i=this.boundingBox;i.makeEmpty();for(let s=0;st&&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};p.prototype.setInstancesCount=function(r){if(r=r;n--)e.delete(n);this._instancesCount=r;return}if(r>this._capacity){let e=this._capacity+(this._capacity>>1)+512;for(;e>1)+512;this.resizeBuffers(e)}const t=this._instancesCount;this._instancesCount=r,this._createEntities&&this.createEntities(t)};function Mt(r){const t={get:e=>e.depthSort,aux:new Array(r._capacity),reversed:null};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=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,C=new It,L=new u.Matrix4,z=new u.Matrix4,J=new u.Vector3,P=new u.Vector3,R=new u.Vector3,Ut=new u.Vector3,S=new u.Sphere;p.prototype.performFrustumCulling=function(r,t=r){if(!this._parentLOD&&this._instancesCount===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)};p.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?C.array.sort((i=this.material)!=null&&i.transparent?rt:st):s(C.array);const o=C.array,a=o.length;for(let h=0;h{const a=o.object;if(al.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=C.array;let f=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 g=d[m];g.depth>x&&(f++,x=((c=i[f+1])==null?void 0:c.distance)??1/0),a[f][n[f]++]=g.index}C.reset()}for(let l=0;l{const l=c.object;if(l{const d=c.object;if(d0){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._instancesCount=Math.max(s+1,this._instancesCount)}const n=this._instancesCount,i=n+r;this.setInstancesCount(this._instancesCount+r);for(let s=n;s=0&&!this.getActiveAt(n);n--)this._instancesCount--;return this};p.prototype.clearInstances=function(){var r;return this._instancesCount=0,this._freeIds.length=0,(r=this.bvh)==null||r.clear(),this};p.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};p.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};p.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};p.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};p.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 p(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;athis.checkObjectIntersection(r,a,t));else{if(this.boundingSphere===null&&this.computeBoundingSphere(),_t.copy(this.boundingSphere),!r.ray.intersectsSphere(_t))return;const a=this.instanceIndex.array,h=e?this._count:this._instancesCount;for(let c=0;cthis._instancesCount||!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}};p.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()}};p.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;od.size-l.size);const a=[];for(const{name:l,size:d,type:f}of n){const x=this.getUniformOffset(d,a);e.set(l,{offset:x,size:d,type:f})}const h=Math.ceil(t/4);return{channels:Math.min(t,4),pixelsPerInstance:h,uniformMap:e,fetchInFragmentShader:o}};p.prototype.getUniformOffset=function(r,t){if(r<4){for(let n=0;n0;r-=4)t.push(r);return e};p.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}`)}};const Bt=` #ifdef USE_INSTANCING_INDIRECT attribute uint instanceIndex; uniform highp sampler2D matricesTexture; mat4 getInstancedMatrix() { int size = textureSize( matricesTexture, 0 ).x; int j = int( instanceIndex ) * 4; int x = j % size; int y = j / size; vec4 v1 = texelFetch( matricesTexture, ivec2( x, y ), 0 ); vec4 v2 = texelFetch( matricesTexture, ivec2( x + 1, y ), 0 ); vec4 v3 = texelFetch( matricesTexture, ivec2( x + 2, y ), 0 ); vec4 v4 = texelFetch( matricesTexture, ivec2( x + 3, y ), 0 ); return mat4( v1, v2, v3, v4 ); } #endif `,Lt=` #ifdef USE_INSTANCING_COLOR_INDIRECT uniform highp sampler2D colorsTexture; #ifdef USE_COLOR_ALPHA vec4 getColorTexture() { int size = textureSize( colorsTexture, 0 ).x; int j = int( instanceIndex ); int x = j % size; int y = j / size; return texelFetch( colorsTexture, ivec2( x, y ), 0 ); } #else vec3 getColorTexture() { int size = textureSize( colorsTexture, 0 ).x; int j = int( instanceIndex ); int x = j % size; int y = j / size; return texelFetch( colorsTexture, ivec2( x, y ), 0 ).rgb; } #endif #endif `,Dt=` #ifdef USE_INSTANCING_INDIRECT mat4 instanceMatrix = getInstancedMatrix(); #ifdef USE_INSTANCING_COLOR_INDIRECT vColor *= getColorTexture(); #endif #endif `,Et=` #ifdef USE_INSTANCING_COLOR_INDIRECT #ifdef USE_VERTEX_COLOR vColor = color; #else #ifdef USE_COLOR_ALPHA vColor = vec4( 1.0 ); #else vColor = vec3( 1.0 ); #endif #endif #endif `,Nt=` #ifdef USE_SKINNING uniform mat4 bindMatrix; uniform mat4 bindMatrixInverse; uniform highp sampler2D boneTexture; #ifdef USE_INSTANCING_SKINNING uniform int bonesPerInstance; #endif mat4 getBoneMatrix( const in float i ) { int size = textureSize( boneTexture, 0 ).x; #ifdef USE_INSTANCING_SKINNING int j = ( bonesPerInstance * int( instanceIndex ) + int( i ) ) * 4; #else int j = int( i ) * 4; #endif int x = j % size; int y = j / size; vec4 v1 = texelFetch( boneTexture, ivec2( x, y ), 0 ); vec4 v2 = texelFetch( boneTexture, ivec2( x + 1, y ), 0 ); vec4 v3 = texelFetch( boneTexture, ivec2( x + 2, y ), 0 ); vec4 v4 = texelFetch( boneTexture, ivec2( x + 3, y ), 0 ); return mat4( v1, v2, v3, v4 ); } #endif `;u.ShaderChunk.instanced_pars_vertex=Bt;u.ShaderChunk.instanced_color_pars_vertex=Lt;u.ShaderChunk.instanced_vertex=Dt;u.ShaderChunk.instanced_color_vertex=Et;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 `);u.ShaderChunk.color_pars_vertex=u.ShaderChunk.color_pars_vertex.concat(` #include `);u.ShaderChunk.batching_vertex=u.ShaderChunk.batching_vertex.concat(` #include `);u.ShaderChunk.skinning_pars_vertex=Nt;u.ShaderChunk.morphinstance_vertex=u.ShaderChunk.morphinstance_vertex.replaceAll("gl_InstanceID","instanceIndex");exports.GLInstancedBufferAttribute=yt;exports.InstancedEntity=nt;exports.InstancedMesh2=p;exports.InstancedMeshBVH=gt;exports.InstancedRenderList=It;exports.SquareDataTexture=j;exports.ascSortIntersection=At;exports.createRadixSort=Mt;exports.getSquareTextureInfo=bt;exports.getSquareTextureSize=it;exports.patchShader=tt;exports.sortOpaque=st;exports.sortTransparent=rt; //# sourceMappingURL=index.cjs.map