Search Unity

Splitting a Mesh in Two

Discussion in 'Scripting' started by Omni_Owl, Jan 19, 2017.

  1. Omni_Owl

    Omni_Owl

    Joined:
    Nov 26, 2013
    Posts:
    176
    Hey guys

    I've been fascinated with destruction in video games lately. I'm currently working on a game for mobile where the main component is using bombs to achieve your goal. It got me thinking of Metal Gear: Revegeance and how they had a fun bit of flair, that let you cut up pretty much anything into increasingly tiny meshes with a sword.

    I was pondering on how to actually do that sort of mechanic and thought "Why not try, step by step to recreate something like it?"

    So I sat down and tried to think of the most simple first step there is, and that would be to take a simple mesh (like the default Cube that Unity can make) and then cut that mesh in two. Now, how I do that is where I get a bit stumped because from what I understand, I would have do something like this:

    1. Do some sort of action that "cuts" or somehow looks for objects to split. I imagined a raycast could do that for now (keeping it simple).
    2. So if my raycast lands on something I can split (I'd probably put some sort of indicator so the player doesn't cut through the world they are standing on) I have to do the split based on where the raycast hits the mesh.
    3. Then from the point it hit, I'd have to figure out the distance between the point I hit initially and the next available face on the mesh that I hit so that I can make the cut through there.
    4. To keep in line with this being simple, lets just say I can only cut vertically down. Then I have to create two new meshes (in this case) where one is the old mesh with the "split" part subtracted and the other mesh is a new mesh minus the old mesh where it was split. I also have to remove the old mesh from the scene completely.
    5. Now I have to close up their faces so that you don't see the inside of the two new meshes.
    6. Apply a bit of force that separates the two new pieces a part because of the split.
    I've made a little illustration below (using my master paint skillz):



    I don't really know how to begin this or if my method is even that great. My knowledge of Unity's API is not all that good yet, and I hope a project like this can help that and make a fun mechanic while learning :)

    I'd like any pointers on where to start perhaps?
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    What you're describing is (of course) possible, but it is Big Magic that requires not only a good understanding of Unity's APIs, but also quite a bit of fairly thorny math.

    So, if you're already a math whiz, then I encourage you to continue with this project — you'll have a lot of fun with it! If not, then I'd suggest you pick a different challenge. It's definitely a Level 18 monster that will not pursue munchkins of level 4 or below.

    But yeah, if you choose to continue, you are on the right track. You will define a plane where the cut is going to happen, and then for every triangle in the mesh, compute the triangle-plane intersection. Most triangles won't intersect the plane at all; those that do, you will cut in two. On one side, you'll still have a triangle, which you just close off; on the other side, you'll now have a quad, which you need to split into two triangles.

    And then on the open face, as you said, you need to create new triangles, either with a different material (which means adding a submesh) or at least with different UVs (much easier if you have control over how these cuttable models are constructed).
     
  3. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    You also have to consider the edge case where the plane intersects through a vertex. Then it just chops it up into 2 triangles. Though depending on how your quad splitting algorithm is set up you might be able to skip this test and just have the quad splitter able to split a triangle into 2 as well.
     
    Omni_Owl and Ryiah like this.
  4. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,130
    There is an example of mesh splitting on Github along with the author's thesis on the subject. It's under the MIT license.

    https://github.com/DanniSchou/MeshSplitting
     
  5. Omni_Owl

    Omni_Owl

    Joined:
    Nov 26, 2013
    Posts:
    176
    I am no math whiz, but I always stood by the notion that you don't necessarily need to understand the math for things like this, to use it if you understand the overall idea of the concept. I know some trigonometry of course and could probably go dig up my old notes on Vectors, Trajectory and Plane Calculus. I'll have to reconsider I guess. Might be too early still.

    Thank you for that. I had a read through a couple of pages at work today. Sounds like what I wanted to achieve, but perhaps he can lead me to a nice solution that can be built upon later.
     
  6. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    metal gear solid revengeance had a similar mechanic; I have fuzzy memories of the devs putting out blogs and details about the issues they faced with implementing this. Might be worth looking up if its gets sticky and you're in need of some inspiration.
     
    Omni_Owl likes this.