#BatchedMesh how to do intersectsBox with three-mesh-bvh

8 messages · Page 1 of 1 (latest)

reef lily
#

bvh installed like this https://github.com/gkjohnson/three-mesh-bvh?tab=readme-ov-file#use

then did

 for (const geometryUuid in geometries) {
      const geometry = geometries[geometryUuid]
      const geoId = batchedMesh.addGeometry(geometry)
      batchedMesh.computeBoundsTree(geoId)
    }

then placed all the instances to their respective locations

now i want to know if a box3 intersects with the entire batched mesh

i tried this but its always returning true

inverseMatrix.copy(batchedMesh.matrixWorld).invert()
for (let i = 0; i < batchedMesh.boundsTrees.length; i++) {
        if (batchedMesh.boundsTrees[i].intersectsBox(box3, inverseMatrix)) {
          hasIntersects = true
          break
        }
}

i also tried a version where instead of batched mesh the default mesh was used , using intersectsBox worked well in this case.

GitHub

A BVH implementation to speed up raycasting and enable spatial queries against three.js meshes. - gkjohnson/three-mesh-bvh

reef lily
#

@placid skiff 👋 ~hi !
is intersectsBox with a batched mesh an expected usecase ?

placid skiff
#

Individual sub-geometry BVHs can be accessed using the geometry id. The BVHs are stored in the local geometry frame so you need to pass the instance matrix into the functions:

for ( let i = 0; i < activeInstances.length; i ++ ) {

  const instanceId = activeInstances[ i ];
  const geometryId = batchedMesh.getGeometryIdAt( instanceId );
  const bvh = batchedMesh.boundsTrees( geometryId );
  batchedMesh.getMatrixAt( instanceId, _matrix );

  // construct transform from target frame into local geometry frame

  bvh.instsectsBox( box3, _localMatrix );

}

Perhaps @ripe remnant's wrappers have some more direct methods for this kind of functionality

ripe remnant
#
const intersected = myBatchedMesh.bvh.intersectBox(myBox, ( instanceId ) => {
   // enter here if two bbox AABB collide
   // now we check if also geometry collides (using three-mesh-bvh)

  const geometryId = batchedMesh.getGeometryIdAt( instanceId );
  const bvh = batchedMesh.boundsTrees( geometryId );
  batchedMesh.getMatrixAt( instanceId, _matrix );
  
  // construct transform from target frame into local geometry frame

  return bvh.instsectsBox( box3, _localMatrix );
});
#

If it would be helpful I can prepare a small example

reef lily