Cutting / Splitting objects into two

Is there a way to cut an object into two? For example when I press the mouse at an object and move it to the end, the objects should be cut into two objects (two halfs). If so, how?

1 Like

Hi @issabln, welcome to the Babylon forum!

Mesh slicing sounds like a fun math problem to solve, you can definitely find solutions implemented in other engines floating around the internet, Like this solver implemented in Unity3D.

Iā€™d assume your algorithm would be:

  1. Figure out your bisecting plane, and its positional and normal vectors (I suggest starting with either a perfectly horizontal or vertical plane).
  2. Find the edges that intersect the plane, and use the intersection point to create your new vertices for the sliced meshes.
  3. Recreate your ā€œtopā€ and ā€œbottomā€ meshes using the vertices on either side of the slicing plane

2 and 3 are probably a lot easier said than done here, but I hope this gives you a head start.

Or, you can always fake it with pre-sliced geometry that you substitute in, if your usecase allows it :wink:

1 Like

Do you need it to be dynamic? If not thereā€™s tons of tools that you can use to swap out the model for any modeling suite, example: YouTube

1 Like

I have been looking into how to slice meshes in Babylon as well. Any pointers would be great ā€¦,

Pinging @Deltakosh

1 Like

you can even split a mesh in as many parts you want : Use the Solid Particle System - Babylon.js Documentation

2 Likes

thats exactly what Iā€™m looking for, but I need it inside babylon.js

@issabln Looks like weā€™re in luck, Babylon does have constructive solid geometry support, hereā€™s a simple mesh slicer I put together that uses CSG:

https://www.babylonjs-playground.com/#B2DP5I#7

7 Likes

Wow, thanks a lot, thatā€™s great!!! Do You know whatā€™s causing the cut parts to jump so weird sometimes and to fall through the ground?

I could not get work ā€¦ when I click on sphere or plane ā€¦ I donā€™t the plane sliced into tiles or prices

It works better with a box

SPS is one of babylonā€™s best features.

1 Like

The jumping is due to me adding physics to the cut pieces, the physics impostor ā€œmeshesā€ are probably overlapping on creation (and donā€™t really match up with the mesh), causing them to explosively decompress as they get out from inside each otheršŸ˜‚.

@sebavan, @trevordev, either of you have any advice to fix that? The only alternative I could think of is to instead use mesh impostors and have my ground be a giant sphere :thinking:

https://www.babylonjs-playground.com/#B2DP5I#9

But then, we lose the ability to collide with the other cut meshesā€¦

2 Likes

Does @issabln need physics?

Hey @Drigax so I have to solve the same problem here with a twist, I need to cut a parented group from point A to Point B and apply both cuts edge rendering to differentiate them.

So the thing is, can I apply this directly to a parented group? it is necessary to make the box slicer? can I use just the parent group to make the slice instead of the box? can I specify a point A and B as a cutting point or vector?

Hi @TUNRAX,

To the best of my knowledge,

  1. CSG needs to be applied per-mesh. You most likely need to iterate through each mesh in your group and apply your subtract/union CSG operations to split each along your desired planes.

  2. Yes, you do need to use a 3 dimensional object for CSG, my demo ā€œslicesā€ via subtracting to create a copy of the mesh where the segments of mesh that are inside the box are removed, (sized large enough to envelop the whole mesh other than what I want to slice away) and ā€œintersectingā€ to create another copy where everything outside the box is removed. Hereā€™s a crude example of the operations:

In short, since you know the axis you want to cut upon for your mesh, so you should be able to compute a plane to use, which you use to compute a bounding volume where you can create a simple cubic mesh to slice on. unfortunately it takes a bit more math, we donā€™t have any APIs that can compute the whole slicing volume from just a vectorā€¦

One thought that takes minimal 3d math is that you can take the worldspace coordinates of your cutting points A and B, and extrapolate those points along the depth axis you want to cut on (say, youā€™re drawing this on a screen, then extrapolate along the cameraā€™s forward axis by some large amount that will envelop the mesh) and youā€™ll then have a plane that you want to cut upon:

Similarly you can then extrapolate those points along the axis resulting from the normal of that plane, and you should have points that make up volume you can use to slice with.

Not straightforward, sure, however its tough to think of a more generic solution that works for all forms of line input and mesh orientation, atleast top of mind :sweat_smile:

My demo doesnā€™t construct this mesh, it just takes a giant box mesh and places it at the click location to slice any picked mesh vertically.

Thinking about this from an alternative approach, you may be able to compute a rotation from your line, such that a box rotated would have its face aligned with your vector.

Then you can place the box at your vector midpoint and if its proportions are ridiculous enough it should be able to slice whatever you throw at it along the angle that you are using?..

5 Likes

Iā€™m just getting started with Babylon. Can you update this demo to use MeshBuilder.CreateBox() instead of the deprecated Mesh.CreateBox()? When I just change Mesh to MeshBuilder, the demo no longer works for me. I saw something about Mesh detecting only camera-object collisions and not object-object collisions. Is that relevant?

Welcome aboard! You have to use the options object to define the size.

    boxSlicer = BABYLON.CreateBox("boxSlicer", { size: boxSlicerSize }, scene);

Would you please create a new topic next time, refer to this one and ask your question in that new topic? Thanks!

Thanks. That works.

1 Like