"매 instancing 의 진짜 한계는 frustum culling 과 per-instance state 다". InstancedMesh2 는 Three.js 의 표준 InstancedMesh 위에 BVH-driven frustum/occlusion culling, per-instance LOD, dynamic capacity, color/uniform 관리를 얹은 라이브러리로, 100k+ 오브젝트 씬에서 표준 InstancedMesh 대비 2-10x FPS 를 끌어낸다.
mesh.computeBVH({margin: 0});// renderer 가 매 프레임 BVH 로 visible 인스턴스만 GPU 에 업로드.
// margin: BVH 노드 padding (모션이 큰 경우 늘림).
4. per-instance LOD
constlodGeoMid=newTHREE.BoxGeometry(1,1,1,2,2,2);constlodGeoLow=newTHREE.BoxGeometry(1,1,1);mesh.addLOD(lodGeoMid,material,50);// 50m 부터 mid
mesh.addLOD(lodGeoLow,material,200);// 200m 부터 low
// 200m 이후 자동 cull (default)
5. per-instance update (애니메이션)
functionanimate(t: number){mesh.updateInstances((obj,idx)=>{obj.position.y=Math.sin(t*0.001+idx*0.01)*5;// obj.updateMatrix() 자동 호출됨
});renderer.render(scene,camera);}
6. 인스턴스 hide / show
mesh.instances[42].visible=false;// 단일
// bulk
for(constinstofmesh.instances){if(inst.position.distanceTo(camera.position)>500)inst.visible=false;}
7. raycasting (BVH 가속)
constraycaster=newTHREE.Raycaster();raycaster.setFromCamera(mouse,camera);consthits=raycaster.intersectObject(mesh);// hits[0].instanceId 로 인스턴스 식별
8. transparent sorting
constmat=newTHREE.MeshStandardMaterial({transparent: true,opacity: 0.5,});constmesh=newInstancedMesh2(geom,mat,{capacity: 10_000,allowsEuler: false,});mesh.sortObjects=true;// distance sort each frame