# BatchedMesh 1 milion instances using a BVH

**URL:** <https://discourse.threejs.org/t/batchedmesh-1-milion-instances-using-a-bvh/82028>\
**Category:** Showcase\
**Tags:** batchedmesh, bvh\
**Created:** [May 5, 2025, 12:13pm UTC](https://discourse.threejs.org/t/batchedmesh-1-milion-instances-using-a-bvh/82028 "2025-05-05T12:13:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![agargaro](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/agargaro/32/35529_2.png) [@agargaro](https://discourse.threejs.org/u/agargaro)\
**Post date:** [May 5, 2025, 12:13pm UTC](https://discourse.threejs.org/t/batchedmesh-1-milion-instances-using-a-bvh/82028/1 "2025-05-05T12:13:48Z")

</div>

Hi guys, I added to my npm package [`@three.ez/batched-mesh-extensions`](https://www.npmjs.com/package/@three.ez/batched-mesh-extensions) an extension to create and use a BVH to speed up raycasting and frustum culling of `BatchedMesh`.

Here is the example (should runs smooth on mobile too):

> **[Glitch Code Editor ･ﾟ✧](https://glitch.com/edit/#!/three-ez-batched-mesh-extensions-bvh?path=main.js)**
>
> Simple, powerful, free tools to create and use millions of apps.

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/4/c/4ca6f247161c61098b4693a2000528b3c423b124.jpeg)

Repository:

> **[GitHub - agargaro/batched-mesh-extensions](https://github.com/agargaro/batched-mesh-extensions)**
>
> Contribute to agargaro/batched-mesh-extensions development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![arpu](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/arpu/32/6583_2.png) [@arpu](https://discourse.threejs.org/u/arpu)\
**Post date:** [May 5, 2025, 6:35pm UTC](https://discourse.threejs.org/t/batchedmesh-1-milion-instances-using-a-bvh/82028/2 "2025-05-05T18:35:58Z")

</div>

can this be used with [GitHub - gkjohnson/three-mesh-bvh: A BVH implementation to speed up raycasting and enable spatial queries against three.js meshes.](https://github.com/gkjohnson/three-mesh-bvh) ? i already have the bvh on my meshes

---

<div class="post-metadata">

**Author:** ![agargaro](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/agargaro/32/35529_2.png) [@agargaro](https://discourse.threejs.org/u/agargaro)\
**Post date:** [May 5, 2025, 6:58pm UTC](https://discourse.threejs.org/t/batchedmesh-1-milion-instances-using-a-bvh/82028/3 "2025-05-05T18:58:57Z")

</div>

Hi @arpu, thanks for asking! 😁

`three-mesh-bvh` creates a BVH to store the geometry triangles (useful for complex geometries).  
My library instead creates a BVH to store the bounding boxes (AABB) of all instances (useful for a lot of instances).

**The two BVHs work perfectly together.**

My bvh filters out the instances (avoiding iterating them all) that need to be checked using the bvh of `three-mesh-bvh`.

I helped @gkjohnson to implement `BatchedMesh` support of `three-mesh-bvh`, so I more or less know how it works (my BVH implementation is based also on his work).

There is just one small complication: both libraries override the `BatchedMesh.raycast` method, so to make them compatible I will have to make some additional changes.

So at the moment they are not compatible, but they will be soon ❤.

---

<div class="post-metadata">

**Author:** ![agargaro](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/agargaro/32/35529_2.png) [@agargaro](https://discourse.threejs.org/u/agargaro)\
**Post date:** [May 6, 2025, 7:37pm UTC](https://discourse.threejs.org/t/batchedmesh-1-milion-instances-using-a-bvh/82028/4 "2025-05-06T19:37:50Z")

</div>

Hi!

I updated the library. It’s now compatible with `three-mesh-bvh` and the `WebGPURenderer` 🥳

> **[Glitch Code Editor ･ﾟ✧](https://glitch.com/edit/#!/three-ez-batched-mesh-extensions-three-mesh-bvh?path=main.js)**
>
> Simple, powerful, free tools to create and use millions of apps.
