If you haven’t heard of DynamicMesh yet, check out this tweet!
Introduction
With DynamicMesh coming to Roblox, we will soon be able to read and modify the vertex data of mesh objects! So I figured now would be a great time to talk about something I remade recently. Introducing mesh splitting:
Splitting a Triangle
Lets get right into it! Triangles are usually the building blocks of meshes. So to begin, I needed to figure out how to split a single triangle. Here was a simple procedure I quickly drew out, the red line indicates a cut.
A triangle is made of three line segments. Iterating through the edges and using some algebra, we can calculate the two points that intersect a plane cut!
With the two intersections, the triangle can be split into three smaller triangles as shown in the above diagram.
Separating the two polygons will give us what we needed, a split triangle!
Splitting a Box
Moving on to the third dimension, here you can see I constructed a cube made of triangles. Using the same math as before, we can figure out which triangle to split, colored in blue. Yellow and red indicate non-intersecting triangles on the two sides of the plane, which will be grouped into separate models later.
Calculating the intersections of the triangles like before we can separate the mesh into two groups. Looks like its all coming together!
Here is how the box looks like when its filled. I render the mesh using Wedges since DynamicMesh hasn’t been released yet. But we have a problem, the interior of the cut is hollow. We can’t be having that!
Filling in the Cut
To cover it up, we can use the points we calculated to find the intersection of the mesh on the plane cut.
With this collection of edges, you will need to apply some CS to merge them. I recommend constructing a vertex graph and traversing a single cycle to calculate this polygon. Then we can fill in the polygon intersection using triangulation. Now we got something to perfectly cover up the hole!
Final Results
Wrapping it all together with a touch of welding and physics… tada! Mesh splitting!
Cutting an eggdog?
How about cutting a sphere brick, slowed down?
FAQ
Q: How are you reading the mesh data?
A: Since DynamicMesh are not out yet, there is no native way to grab a mesh’s vertex information. Alternatively, you can use CloneTrooper’s Mesh Parser.
Q: How are you rendering the mesh?
A: This is not performant compared to modifying meshes, but I have been using Wedge instances to replicate triangles in the mean time. Here is how they look like in a different color.