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?
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:
- Figure out your bisecting plane, and its positional and normal vectors (I suggest starting with either a perfectly horizontal or vertical plane).
- Find the edges that intersect the plane, and use the intersection point to create your new vertices for the sliced meshes.
- 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
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
I have been looking into how to slice meshes in Babylon as well. Any pointers would be great ā¦,
Pinging @Deltakosh
you can even split a mesh in as many parts you want : Use the Solid Particle System - Babylon.js Documentation
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:
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.
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
https://www.babylonjs-playground.com/#B2DP5I#9
But then, we lose the ability to collide with the other cut meshesā¦
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,
-
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.
-
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
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?..
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.