# MergeMeshes Optimizations (Subclass, Async etc.)

**URL:** <https://forum.babylonjs.com/t/mergemeshes-optimizations-subclass-async-etc/40151>\
**Category:** Questions\
**Created:** [April 24, 2023, 8:09am UTC](https://forum.babylonjs.com/t/mergemeshes-optimizations-subclass-async-etc/40151 "2023-04-24T08:09:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dennemark](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/dennemark/32/10745_2.png) [@dennemark](https://forum.babylonjs.com/u/dennemark)\
**Post date:** [April 24, 2023, 8:09am UTC](https://forum.babylonjs.com/t/mergemeshes-optimizations-subclass-async-etc/40151/1 "2023-04-24T08:09:10Z")

</div>

Hi,  
does someone have an example or explanation for the meshSubclass parameter in MergeMeshes function? [Mesh | Babylon.js Documentation](https://doc.babylonjs.com/typedoc/classes/BABYLON.Mesh-1)  
We supply the function with an array of meshes, but meshSubclass only has one mesh. Do I have to understand it in a way, that I have an existing mesh and its vertex data will be overwritten by the merged mesh?  
So without its use its a new mesh (immutable approach):  
`const mesh = MergeMeshes([mesh1, mesh2], true, true, undefined)`  
and with it, it will be mutated?  
`MergeMeshes([mesh3, mesh4], true, true, mesh)`

Maybe follow up question: If I have a large mesh with many submeshes, is there an efficient way to replace a submesh even if indices of the new meshes are more than before?  
I would like to reduce recalculation time for a mesh and only replace altered parts.  
Partly answering it myself, looks like @Evgeni_Popov made a CompositeMesh here: [Different meshes that share the same material, batching drawcalls? - #11 by Evgeni\_Popov](https://forum.babylonjs.com/t/different-meshes-that-share-the-same-material-batching-drawcalls/25432/11) Not sure if it is fully covering my question, but seems nice.

And another thing I noticed for MergeMeshesAsync: This function uses a coroutine that needs one frame? for each VertexData type (position, color, uv…), since it yields after each one of them. I was rather expecting a function that would use another thread. Would it make sense to have such a function? Probably would need SharedArrayBuffer for faster I/O, but I also read some time ago SharedArrayBuffers dont have wide support yet. Just some thoughts…

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [April 24, 2023, 12:14pm UTC](https://forum.babylonjs.com/t/mergemeshes-optimizations-subclass-async-etc/40151/2 "2023-04-24T12:14:30Z")

</div>

> [@dennemark](#):
>
> that I have an existing mesh and its vertex data will be overwritten by the merged mesh?

No, if I understand it well, the `meshSubClass`, if provided, will be extended with the new data, existing data won’t be overwritten.

> [@dennemark](#):
>
> If I have a large mesh with many submeshes, is there an efficient way to replace a submesh even if indices of the new meshes are more than before?

`MergeMeshes` is not able to replace data with other data, it can only create a new mesh or add data to an existing mesh.

cc @RaananW or @sebavan regarding the coroutine question.

---

<div class="post-metadata">

**Author:** ![RaananW](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/raananw/32/28955_2.png) [@RaananW](https://forum.babylonjs.com/u/RaananW)\
**Post date:** [April 24, 2023, 12:25pm UTC](https://forum.babylonjs.com/t/mergemeshes-optimizations-subclass-async-etc/40151/3 "2023-04-24T12:25:14Z")

</div>

> [@dennemark](#):
>
> I was rather expecting a function that would use another thread.

I assume you are referring to a worker when you mean thread?

Yes, there is room for improvement here. If you want to run the merge in a worker you will need to first copy the data to the worker, which will also take some time. What also costs a bit of time is bringing the data back and construct a mesh out of it.  
You are right - it runs each one in a single frame (optimally). The idea is to do whatever we can to not block the main (UI) thread, but there are no fixed numbers we can use - i.e., if a mesh has more than 1000 indices it will take longer than a frame, and therefore we should divide it to 2 frames. But again - there ia always room for improvement, and we will love to hear if you have ideas.

---

<div class="post-metadata">

**Author:** ![dennemark](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/dennemark/32/10745_2.png) [@dennemark](https://forum.babylonjs.com/u/dennemark)\
**Post date:** [April 24, 2023, 1:18pm UTC](https://forum.babylonjs.com/t/mergemeshes-optimizations-subclass-async-etc/40151/4 "2023-04-24T13:18:02Z")

</div>

Thanks for the explanations! Yes I am referring to a worker. Maybe I should just try it at some point with a big mesh in a playground and measure the performance. I could imagine that also the coroutine could use some more options concerning i.e. yield after x indices.

@Evgeni_Popov so MeshSubClass would be a rather nice approach for i.e. streaming mesh data?

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [April 24, 2023, 1:19pm UTC](https://forum.babylonjs.com/t/mergemeshes-optimizations-subclass-async-etc/40151/5 "2023-04-24T13:19:48Z")

</div>

Yes, indeed!
