"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("three"),b=require("bvh.js"),xt=require("three/examples/jsm/utils/SortUtils.js");class E{constructor(t,e,n){this.isInstanceEntity=!0,this.position=new c.Vector3,this.scale=new c.Vector3(1,1,1),this.id=e,this.owner=t;const i=this.quaternion=new c.Quaternion;if(n){const s=this.rotation=new c.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 color(){return this.owner.getColorAt(this.id)}set color(t){this.owner.setColorAt(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 H;const t=this.owner,e=this.position,n=this.quaternion,i=this.scale,s=t._matrixArray,a=this.id,o=a*16,h=n._x,u=n._y,l=n._z,d=n._w,p=h+h,f=u+u,_=l+l,g=h*p,w=h*f,T=h*_,q=u*f,V=u*_,k=l*_,W=d*p,P=d*f,Y=d*_,B=i.x,j=i.y,R=i.z;s[o+0]=(1-(q+k))*B,s[o+1]=(w+Y)*B,s[o+2]=(T-P)*B,s[o+3]=0,s[o+4]=(w-Y)*j,s[o+5]=(1-(g+k))*j,s[o+6]=(V+W)*j,s[o+7]=0,s[o+8]=(T+P)*R,s[o+9]=(V-W)*R,s[o+10]=(1-(g+q))*R,s[o+11]=0,s[o+12]=e.x,s[o+13]=e.y,s[o+14]=e.z,s[o+15]=1,t.matricesTexture.needsUpdate=!0,(H=t.bvh)==null||H.move(a)}updateMatrixPosition(){var a;const t=this.owner,e=this.position,n=t._matrixArray,i=this.id,s=i*16;n[s+12]=e.x,n[s+13]=e.y,n[s+14]=e.z,t.matricesTexture.needsUpdate=!0,(a=t.bvh)==null||a.move(i)}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 O.setFromAxisAngle(t,e),this.quaternion.multiply(O),this}rotateOnWorldAxis(t,e){return O.setFromAxisAngle(t,e),this.quaternion.premultiply(O),this}rotateX(t){return this.rotateOnAxis(X,t)}rotateY(t){return this.rotateOnAxis(Q,t)}rotateZ(t){return this.rotateOnAxis($,t)}translateOnAxis(t,e){return Z.copy(t).applyQuaternion(this.quaternion),this.position.add(Z.multiplyScalar(e)),this}translateX(t){return this.translateOnAxis(X,t)}translateY(t){return this.translateOnAxis(Q,t)}translateZ(t){return this.translateOnAxis($,t)}}const O=new c.Quaternion,Z=new c.Vector3,X=new c.Vector3(1,0,0),Q=new c.Vector3(0,1,0),$=new c.Vector3(0,0,1);function pt(r){const t=Math.ceil(Math.sqrt(r)),e=new Float32Array(t*t),n=new c.DataTexture(e,t,t,c.RedFormat,c.FloatType);return n.needsUpdate=!0,n}function ft(r){const t=Math.ceil(Math.sqrt(r)),e=new Uint32Array(t*t),n=new c.DataTexture(e,t,t,c.RedIntegerFormat,c.UnsignedIntType);return n.needsUpdate=!0,n}function mt(r){const t=Math.ceil(Math.sqrt(r)),e=new Float32Array(t*t*2),n=new c.DataTexture(e,t,t,c.RGFormat,c.FloatType);return n.needsUpdate=!0,n}function ot(r){const t=Math.ceil(Math.sqrt(r)),e=new Float32Array(t*t*4),n=new c.DataTexture(e,t,t,c.RGBAFormat,c.FloatType);return n.needsUpdate=!0,n}function _t(r){let t=Math.sqrt(r*3);t=Math.ceil(t/3)*3,t=Math.max(t,3);const e=new Float32Array(t*t*4),n=new c.DataTexture(e,t,t,c.RGBAFormat,c.FloatType);return n.needsUpdate=!0,n}function at(r){let t=Math.sqrt(r*4);t=Math.ceil(t/4)*4,t=Math.max(t,4);const e=new Float32Array(t*t*4),n=new c.DataTexture(e,t,t,c.RGBAFormat,c.FloatType);return n.needsUpdate=!0,n}class ct extends c.GLBufferAttribute{constructor(t,e,n,i,s,a=1){const o=t.createBuffer();super(o,e,n,i,s.length/n),this.isInstancedBufferAttribute=!0,this.isGLInstancedBufferAttribute=!0,this._needsUpdate=!1,this.meshPerAttribute=a,this.array=s,t.bindBuffer(t.ARRAY_BUFFER,o),t.bufferData(t.ARRAY_BUFFER,s,t.DYNAMIC_DRAW)}update(t,e){if(!this._needsUpdate)return;const n=t.getContext();n.bindBuffer(n.ARRAY_BUFFER,this.buffer),n.bufferSubData(n.ARRAY_BUFFER,0,this.array,0,e),this._needsUpdate=!1}clone(){return this}}function ht(r,t,e){const n=r*16,i=t[n+0],s=t[n+1],a=t[n+2],o=t[n+4],h=t[n+5],u=t[n+6],l=t[n+8],d=t[n+9],p=t[n+10],f=i*i+s*s+a*a,_=o*o+h*h+u*u,g=l*l+d*d+p*p;return e.maxScale=Math.sqrt(Math.max(f,_,g)),e.centerX=t[n+12],e.centerY=t[n+13],e.centerZ=t[n+14],e}function S(r,t,e){const n=r*16;return e.x=t[n+12],e.y=t[n+13],e.z=t[n+14],e}function U(r,t){const e=r*16,n=t[e+0],i=t[e+1],s=t[e+2],a=n*n+i*i+s*s,o=t[e+4],h=t[e+5],u=t[e+6],l=o*o+h*h+u*u,d=t[e+8],p=t[e+9],f=t[e+10],_=d*d+p*p+f*f;return Math.sqrt(Math.max(a,l,_))}class ut{constructor(t,e=0,n=!1,i=!1){this.nodesMap=new Map,this.LODsMap=new Map,this._geoBoundingSphere=null,this._sphereTarget=null,this._margin=e,this.target=t;const s=t._geometry;if(s.boundingBox||s.computeBoundingBox(),this.geoBoundingBox=s.boundingBox,i){s.boundingSphere||s.computeBoundingSphere();const a=s.boundingSphere.center;a.x===0&&a.y===0&&a.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.'),i=!1)}this._arrayType=n?Float64Array:Float32Array,this.bvh=new b.BVH(new b.HybridBuilder(n),b.WebGLCoordinateSystem),this._origin=new this._arrayType(3),this._dir=new this._arrayType(3),this._cameraPos=new this._arrayType(3),this._getBoxFromSphere=i}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)})}insert(t){const e=this.bvh.insert(t,this.getBox(t,new this._arrayType(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.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 this._arrayType(n.length));const s=this.LODsMap.get(n);for(let o=0;o0?this.bvh.frustumCullingLOD(t.elements,a,s,(o,h,u,l)=>{u.isIntersectedMargin(o.box,l,this._margin)&&i(o,h)}):this.bvh.frustumCullingLOD(t.elements,a,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 this._arrayType(6));const n=this._boxArray;return b.box3ToArray(t,n),this.bvh.intersectsBox(n,e)}getBox(t,e){if(this._getBoxFromSphere){const{centerX:n,centerY:i,centerZ:s,maxScale:a}=ht(t,this.target._matrixArray,this._sphereTarget),o=this._geoBoundingSphere.radius*a;e[0]=n-o,e[1]=n+o,e[2]=i-o,e[3]=i+o,e[4]=s-o,e[5]=s+o}else J.copy(this.geoBoundingBox).applyMatrix4(this.target.getMatrixAt(t)),b.box3ToArray(J,e);return e}}const J=new c.Box3;class x extends c.Mesh{constructor(t,e,n,i,s,a=!1){if(!e||e<0)throw new Error('"count" must be greater than 0.');super(n,i),this.type="InstancedMesh2",this.isInstancedMesh2=!0,this.instances=null,this.colorsTexture=null,this.morphTexture=null,this.boundingBox=null,this.boundingSphere=null,this.bvh=null,this.customSort=null,this.raycastOnlyFrustum=!1,this.infoLOD=null,this._renderer=null,this._colorArray=null,this._perObjectFrustumCulled=!0,this._sortObjects=!1,this._visibilityChanged=!1,this._uniformsSetCallback=new Map,this.customDepthMaterial=new c.MeshDepthMaterial({depthPacking:c.RGBADepthPacking}),this.customDistanceMaterial=new c.MeshDistanceMaterial,this.isInstancedMesh=!0,this.instanceMatrix=new c.InstancedBufferAttribute(new Float32Array(0),16),this.instanceColor=null,this._renderer=t,this._instancesUseEuler=a,this._instance=new E(this,-1,a),this.instancesCount=e,this._maxCount=e,this._count=e,this._geometry=n,this._material=i,this._parentLOD=s,this.visibilityArray=(s==null?void 0:s.visibilityArray)??new Array(e).fill(!0),this.initIndexArray(),this.initIndexAttribute(),this.initMatricesTexture(),this.patchMaterial(this.customDepthMaterial),this.patchMaterial(this.customDistanceMaterial)}get count(){return this._count}get maxCount(){return this._maxCount}get perObjectFrustumCulled(){return this._perObjectFrustumCulled}set perObjectFrustumCulled(t){this._perObjectFrustumCulled=t,this._visibilityChanged=!0}get sortObjects(){return this._sortObjects}set sortObjects(t){this._sortObjects=t,this._visibilityChanged=!0}get geometry(){return this._geometry}set geometry(t){this._geometry=t,this.patchGeometry(t)}get material(){return this._material}set material(t){this._material=t,this.patchMaterials(t)}onBeforeShadow(t,e,n,i,s,a,o){this.instanceIndex&&this.performFrustumCulling(i,n)}onBeforeRender(t,e,n,i,s,a){this.instanceIndex?this.performFrustumCulling(n):this._renderer=t}onAfterRender(t,e,n,i,s,a){this.instanceIndex||this.initIndexAttribute()}initIndexArray(){const t=this._maxCount,e=new Uint32Array(t);for(let n=0;n{if(e&&e(n,i),!!n.instancing&&(n.instancing=!1,n.instancingColor=!1,n.uniforms.matricesTexture={value:this.matricesTexture},n.defines||(n.defines={}),n.defines.USE_INSTANCING_INDIRECT="",this.colorsTexture!==null)){if(!n.fragmentShader.includes("#include "))return;n.uniforms.colorsTexture={value:this.colorsTexture},n.defines.USE_INSTANCING_COLOR_INDIRECT="",n.fragmentShader=n.fragmentShader.replace("#include ",`#define USE_COLOR #include `)}},t.isInstancedMeshPatched=!0}updateInstances(t){var s;const e=this.instancesCount;if(this.instances){const a=this.instances;for(let o=0;oa[o]=h;else{const o=i.format===c.RGFormat?2:4;s=(h,u)=>u.toArray(a,h*o)}this._uniformsSetCallback.set(e,s)}s(t,n),i.needsUpdate=!0}getMorphAt(t,e=gt){const n=e.morphTargetInfluences,i=this.morphTexture.source.data.data,s=n.length+1,a=t*s+1;for(let o=0;oe.depthSort,aux:new Array(r.maxCount),reversed:null};return function(n){const i=r._material;if(!i.isMaterial)throw new Error("Multi material is not supported.");t.reversed=i.transparent,r.maxCount>t.aux.length&&(t.aux.length=r.maxCount);let s=1/0,a=-1/0;for(const{depth:u}of n)u>a&&(a=u),u=n.length&&n.push({depth:null,index:null,depthSort:null});const a=n[s];a.depth=t,a.index=e,i.push(a)}reset(){this.list.length=0}}const N=new c.Frustum,y=new dt,C=new c.Matrix4,A=new c.Matrix4,F=new c.Vector3,M=new c.Vector3,v=new c.Vector3,I=new c.Vector3,m=new c.Sphere;x.prototype.performFrustumCulling=function(r,t=r){const e=this.infoLOD,n=r!==t;let i;if(e){i=n?e.shadowRender??e.render:e.render;for(const s of e.objects)s===this?s._count=0:s.visible=!1}(i==null?void 0:i.levels.length)>0?this.frustumCullingLOD(i,e.objects,r,t):this._parentLOD||this.frustumCulling(r),this.instanceIndex.update(this._renderer,this._count)};x.prototype.frustumCulling=function(r){var i;const t=this._sortObjects,e=this._perObjectFrustumCulled,n=this._indexArray;if(this.instanceIndex._needsUpdate=!0,!e&&!t){this.updateIndexArray();return}if(t&&(A.copy(this.matrixWorld).invert(),M.setFromMatrixPosition(r.matrixWorld).applyMatrix4(A),F.set(0,0,-1).transformDirection(r.matrixWorld).transformDirection(A)),e?(C.multiplyMatrices(r.projectionMatrix,r.matrixWorldInverse).multiply(this.matrixWorld),this.bvh?this.BVHCulling():this.linearCulling()):this.updateRenderList(),t){const s=this.customSort;s===null?y.list.sort((i=this._material)!=null&&i.transparent?G:z):s(y.list);const a=y.list,o=a.length;for(let h=0;h{const a=s.object;if(a_&&(f++,_=((u=s[f+1])==null?void 0:u.distance)??1/0),p[f][i[f]++]=T.index}y.reset()}for(let l=0;l0,d._count=i[l]}};x.prototype.BVHCullingLOD=function(r,t){const{count:e,indexes:n,levels:i}=r,s=this._matrixArray,a=this.instancesCount,o=this.visibilityArray;t?this.bvh.frustumCulling(C,h=>{const u=h.object;if(u{const l=h.object;if(l0;e--){const n=r[e],i=n.distance-n.distance*n.hysteresis;if(t>=i)return e}return 0};x.prototype.setFirstLODDistance=function(r=0,t=0){if(this._parentLOD){console.error("Cannot create LOD for this InstancedMesh2.");return}return this.infoLOD||(this.infoLOD={render:null,shadowRender:null,objects:[this]}),this.infoLOD.render||(this.infoLOD.render={levels:[{distance:r,hysteresis:t,object:this}],indexes:[this._indexArray],count:[0]}),this};x.prototype.addLOD=function(r,t,e=0,n=0){var i;if(this._parentLOD){console.error("Cannot create LOD for this InstancedMesh2.");return}if(!((i=this.infoLOD)!=null&&i.render)&&e===0){console.error('Cannot set distance to 0 for the first LOD. Use "setFirstLODDistance" before use "addLOD".');return}else this.setFirstLODDistance(0,n);return this.addLevel(this.infoLOD.render,r,t,e,n),this};x.prototype.addShadowLOD=function(r,t=0,e=0){if(this._parentLOD){console.error("Cannot create LOD for this InstancedMesh2.");return}this.infoLOD||(this.infoLOD={render:null,shadowRender:null,objects:[this]}),this.infoLOD.shadowRender||(this.infoLOD.shadowRender={levels:[],indexes:[],count:[]});const n=this.addLevel(this.infoLOD.shadowRender,r,null,t,e);return n.castShadow=!0,this.castShadow=!0,this};x.prototype.addLevel=function(r,t,e,n,i){const s=this.infoLOD.objects,a=r.levels;let o,h;n=n**2;const u=s.findIndex(l=>l.geometry===t);for(u===-1?(h=new x(this._renderer,this._maxCount,t,e??new c.ShaderMaterial,this),s.push(h),this.add(h)):(h=s[u],e&&(h.material=e)),o=0;othis.checkObjectIntersection(r,o,t));else{if(this.boundingSphere===null&&this.computeBoundingSphere(),rt.copy(this.boundingSphere),!r.ray.intersectsSphere(rt))return;const o=this._indexArray,h=e?this._count:this.instancesCount;for(let u=0;uthis.instancesCount||!this.getVisibilityAt(t))){this.getMatrixAt(t,D.matrixWorld),D.raycast(r,L);for(const n of L)n.instanceId=t,n.object=this,e.push(n);L.length=0}};const At=` float getFloatFromTexture( sampler2D texture, const in uint i ) { int size = textureSize( texture, 0 ).x; int j = int( i ); int x = j % size; int y = j / size; return float( texelFetch( texture, ivec2( x, y ), 0 ).r ); } vec2 getVec2FromTexture( sampler2D texture, const in uint i ) { int size = textureSize( texture, 0 ).x; int j = int( i ); int x = j % size; int y = j / size; return texelFetch( texture, ivec2( x, y ), 0 ).rg; } vec3 getVec3FromTexture( sampler2D texture, const in uint i ) { int size = textureSize( texture, 0 ).x; int j = int( i ); int x = j % size; int y = j / size; return texelFetch( texture, ivec2( x, y ), 0 ).rgb; } vec4 getVec4FromTexture( sampler2D texture, const in uint i ) { int size = textureSize( texture, 0 ).x; int j = int( i ); int x = j % size; int y = j / size; return texelFetch( texture, ivec2( x, y ), 0 ); } mat3 getMat3FromTexture( sampler2D texture, const in uint i ) { int size = textureSize( texture, 0 ).x; int j = int( i ) * 3; int x = j % size; int y = j / size; vec4 v1 = texelFetch( texture, ivec2( x, y ), 0 ); vec4 v2 = texelFetch( texture, ivec2( x + 1, y ), 0 ); vec4 v3 = texelFetch( texture, ivec2( x + 2, y ), 0 ); return mat3( v1, v2, v3); } mat4 getMat4FromTexture( sampler2D texture, const in uint i ) { int size = textureSize( texture, 0 ).x; int j = int( i ) * 4; int x = j % size; int y = j / size; vec4 v1 = texelFetch( texture, ivec2( x, y ), 0 ); vec4 v2 = texelFetch( texture, ivec2( x + 1, y ), 0 ); vec4 v3 = texelFetch( texture, ivec2( x + 2, y ), 0 ); vec4 v4 = texelFetch( texture, ivec2( x + 3, y ), 0 ); return mat4( v1, v2, v3, v4 ); } `,St=` #ifdef USE_INSTANCING_INDIRECT attribute uint instanceIndex; uniform highp sampler2D matricesTexture; #ifdef USE_INSTANCING_COLOR_INDIRECT uniform highp sampler2D colorsTexture; #endif #endif `,It=` #ifdef USE_INSTANCING_INDIRECT mat4 instanceMatrix = getMat4FromTexture( matricesTexture, instanceIndex ); #ifdef USE_INSTANCING_COLOR_INDIRECT vec3 instanceColor = getVec3FromTexture( colorsTexture, instanceIndex ); vColor.xyz *= instanceColor.xyz; #endif #endif `;c.ShaderChunk.get_from_texture=At;c.ShaderChunk.instanced_pars_vertex=St;c.ShaderChunk.instanced_vertex=It;c.ShaderChunk.project_vertex=c.ShaderChunk.project_vertex.replace("#ifdef USE_INSTANCING","#if defined USE_INSTANCING || defined USE_INSTANCING_INDIRECT");c.ShaderChunk.worldpos_vertex=c.ShaderChunk.worldpos_vertex.replace("#ifdef USE_INSTANCING","#if defined USE_INSTANCING || defined USE_INSTANCING_INDIRECT");c.ShaderChunk.defaultnormal_vertex=c.ShaderChunk.defaultnormal_vertex.replace("#ifdef USE_INSTANCING","#if defined USE_INSTANCING || defined USE_INSTANCING_INDIRECT");c.ShaderChunk.color_pars_vertex=c.ShaderChunk.color_pars_vertex.replace("defined( USE_INSTANCING_COLOR )","defined( USE_INSTANCING_COLOR ) || defined( USE_INSTANCING_COLOR_INDIRECT )");c.ShaderChunk.color_vertex=c.ShaderChunk.color_vertex.replace("defined( USE_INSTANCING_COLOR )","defined( USE_INSTANCING_COLOR ) || defined( USE_INSTANCING_COLOR_INDIRECT )");c.ShaderChunk.common=c.ShaderChunk.common.concat(` #include `);c.ShaderChunk.batching_pars_vertex=c.ShaderChunk.batching_pars_vertex.concat(` #include `);c.ShaderChunk.batching_vertex=c.ShaderChunk.batching_vertex.concat(` #include `);function Tt(r){return r.replace("#ifdef USE_INSTANCING","#if defined USE_INSTANCING || defined USE_INSTANCING_INDIRECT").replace("#ifdef USE_INSTANCING_COLOR","#if defined USE_INSTANCING_COLOR || defined USE_INSTANCING_COLOR_INDIRECT").replace("defined( USE_INSTANCING_COLOR )","defined( USE_INSTANCING_COLOR ) || defined( USE_INSTANCING_COLOR_INDIRECT )")}exports.GLInstancedBufferAttribute=ct;exports.InstancedEntity=E;exports.InstancedMesh2=x;exports.InstancedMeshBVH=ut;exports.InstancedRenderList=dt;exports.ascSortIntersection=lt;exports.createRadixSort=bt;exports.createTexture_float=pt;exports.createTexture_mat3=_t;exports.createTexture_mat4=at;exports.createTexture_uint=ft;exports.createTexture_vec2=mt;exports.createTexture_vec4=ot;exports.getMaxScaleOnAxisAt=U;exports.getPositionAt=S;exports.getSphereFromMatrix_centeredGeometry=ht;exports.patchShader=Tt;exports.sortOpaque=z;exports.sortTransparent=G; //# sourceMappingURL=index.cjs.map