# Using CSG with MergeMeshes

**URL:** <https://forum.babylonjs.com/t/using-csg-with-mergemeshes/50685>\
**Category:** Questions\
**Tags:** csg, mergemeshes\
**Created:** [May 20, 2024, 7:47am UTC](https://forum.babylonjs.com/t/using-csg-with-mergemeshes/50685 "2024-05-20T07:47:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kzhsw](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@kzhsw](https://forum.babylonjs.com/u/kzhsw)\
**Post date:** [May 20, 2024, 7:47am UTC](https://forum.babylonjs.com/t/using-csg-with-mergemeshes/50685/1 "2024-05-20T07:47:54Z")

</div>

Ver: 7.7.0  
Playground:

> **[Babylon.js Playground](https://playground.babylonjs.com/#Y6UIUT%2310)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

Desc:  
I’m building a scene that digs a hole when the button at the top right of the scene clicked, and when the wireframe mesh moved and the btn clicked again, it digs another hole.  
Firstly I tried to use `subtractInPlace` again and again on the same csg, but it goes too much slower after 3 clicks.  
Then I looked at `MergeMeshes`, expecting using it to get a merged mesh of all holes to dig, and `subtract` the csg with it being faster, but the result looks wrong.  
Also, it’s also acceptable if a way is found making csg faster with multiple `subtractInPlace`.

Here are playgrounds tuned for faster reproduction of this:  
Expected(but very slow):

> **[Babylon.js Playground](https://playground.babylonjs.com/#Y6UIUT%2312)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/1/a/1a32008363acad5c7b1a1d4365124c49b881cca7.jpeg)

Actual:

> **[Babylon.js Playground](https://playground.babylonjs.com/#Y6UIUT%2311)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/5/2/52192b8f8d783450c44c03f09db8ff5bf613fc76.jpeg)

---

<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:** [May 20, 2024, 9:28am UTC](https://forum.babylonjs.com/t/using-csg-with-mergemeshes/50685/2 "2024-05-20T09:28:56Z")

</div>

There’s unfortunately nothing that can be done (except maybe using another algorithm to compute CSG). CSG creates many faces very quickly, so after you’ve applied CSG a few times, your mesh can contain a lot of faces and it becomes exponentially slower to apply new CSG operations.

---

<div class="post-metadata">

**Author:** ![kzhsw](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@kzhsw](https://forum.babylonjs.com/u/kzhsw)\
**Post date:** [May 20, 2024, 9:35am UTC](https://forum.babylonjs.com/t/using-csg-with-mergemeshes/50685/3 "2024-05-20T09:35:45Z")

</div>

So, do it mean that the performance of csg can not be improved? What about the mergemeshes? Any ideas of making merged mesh work with csg?

---

<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:** [May 20, 2024, 9:50am UTC](https://forum.babylonjs.com/t/using-csg-with-mergemeshes/50685/4 "2024-05-20T09:50:12Z")

</div>

I don’t know if the current CSG algorithm can be improved, it’s an external contribution I think, so it’s hard to know. But we already had this problem in the past with no good solution.

I’m not sure what you mean by having merge meshes work with CSG… Merge meshes simply merge the vertex buffers of multiple meshes into a single mesh, so in the end you end up with a regular mesh.

---

<div class="post-metadata">

**Author:** ![Joe\_Kerr](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/joe_kerr/32/39017_2.png) [@Joe\_Kerr](https://forum.babylonjs.com/u/Joe_Kerr)\
**Post date:** [May 20, 2024, 9:51am UTC](https://forum.babylonjs.com/t/using-csg-with-mergemeshes/50685/5 "2024-05-20T09:51:42Z")

</div>

Even if it is just a single mesh, your poly count will go through the roof too quickly.

You need to look into “destructible environments” and see what approaches fit your project/purpose.

If you need e.g. dynamic caves, google for how “Red Faction 1” or “Deep Rock Galactic” did it. Or Minecraft.

---

<div class="post-metadata">

**Author:** ![kzhsw](https://avatars.discourse-cdn.com/v4/letter/k/848f3c/32.png) [@kzhsw](https://forum.babylonjs.com/u/kzhsw)\
**Post date:** [May 21, 2024, 1:01am UTC](https://forum.babylonjs.com/t/using-csg-with-mergemeshes/50685/6 "2024-05-21T01:01:41Z")

</div>

> [@Evgeni\_Popov](#):
>
> I’m not sure what you mean by having merge meshes work with CSG…

I made a merged mesh from meshes in different positions, expecting that `subtract` a csg created from the merged mesh would result like running `subtractInPlace` multiple on csgs from meshes in different positions, but fails.

---

<div class="post-metadata">

**Author:** ![dsaffo](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/dsaffo/32/31907_2.png) [@dsaffo](https://forum.babylonjs.com/u/dsaffo)\
**Post date:** [May 21, 2024, 1:52am UTC](https://forum.babylonjs.com/t/using-csg-with-mergemeshes/50685/7 "2024-05-21T01:52:03Z")

</div>

Perhaps check out the solution in a past thread with a related issue:

> [@CSG alternatives](https://forum.babylonjs.com/t/csg-alternatives/28137):
>
> We are using CSG successfully here to cut meshes but the time it takes to perform these cuts depends greatly on the complexity of the meshes. Given that we can’t control for the mesh complexity (the meshes are coming from an external source), I’m exploring different options for how we can perform the CSG operations. We have currently offloaded the cutting to our NodeJS server and are using a NullEngine, but the process isn’t any faster, it’s just non-blocking at that point. What I’d like to do…
