Search Unity

How to Merge Intersecting Meshes on Runtime

Discussion in 'Scripting' started by ADZ_, Jul 16, 2019.

  1. ADZ_

    ADZ_

    Joined:
    Jun 14, 2019
    Posts:
    7
    Hi, I am trying to make a 3D editor and I just wanted to ask is it possible to combine intersecting meshes, then remove intersecting faces on runtime and if so how? I am aware of Combine Instances and have tried using those to some extent already. Please see attached image for a better understanding of what I mean (the normals are flipped).


    Demo.jpg
     
    Santa likes this.
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Last edited: Jul 16, 2019
  3. ADZ_

    ADZ_

    Joined:
    Jun 14, 2019
    Posts:
    7
    Thank you very much.
     
  4. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,997
    palex is having a little joke on you. Anything is _possible_. Merging shapes like that, with no hidden faces, is fairly complicated code from 3D modeling software. Writing even a simple "merge verts within this radius" is harder than combineInstances (more if you haven't used that tool). That link to the Mesh class is merely the API to give Unity the completed mesh. You're 100% on your own actually making it.
     
  5. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Are you looking specifically for meshes to merge? Or do you want the effect to simply show up, because a gamer isn't going to notice the difference if you simply have colliders stick together without any fancy trickery and it will be cheaper resources wise for your computer to run it as well.

    I've done some heavy research on this topic just recently and found that metaballs were the answer to my problem.

    https://forum.unity.com/threads/how-to-make-the-black-and-white-border.694372/

    http://patomkin.com/blog/metaball-tutorial/ - Tutorial I found that explains how to make them.

    There are also 3D versions out there I believe as others have said where the meshes themselves blend but it's down to what you're trying to do for your game.
     
  6. ADZ_

    ADZ_

    Joined:
    Jun 14, 2019
    Posts:
    7
    @Lethn Thank you for your response. Yes, I am specifically trying to merge meshes, however I don't think I made myself clear enough to begin with. I am not aiming to use metaballs, but either way, thanks for the tutorial (it's bound to come in handy one day).

    What I am aiming for is less of an effect and more of a game mechanic. The meshes displayed in the above image are supposed to be 'rooms' that the player can drag into the scene. Once a 'room' mesh comes into contact with another mesh, it will merge to form one big 'room'.

    Yes, I realise this is quite a difficult thing to achieve but I'm willing to try anyway. At the moment, all I'm trying to do is combine two simple meshes with intersecting normals to form a larger mesh (without the faces that intersect with faces in other meshes).
     
  7. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    In that case if you're looking to merge rooms you'll need to research the marching cubes algorithm which is a really big project.



    Oh before I forget he did a tutorial series on this topic.

     
  8. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    For this purpose it is easier to cut meshes apart in 3D editor like 9scale sprite but in 3d, with caps from 6 sides and corners. When user drags a room into scene, check what sides match and remove caps. After that merge meshes just by copyint vertices.
     
    MadeFromPolygons likes this.
  9. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,977
    You can probably use this library to do it:

    https://github.com/gradientspace/geometry3Sharp

    Its not unity specific but it is compatible with unity and can do a whole host of other things too.

    Alternatively you could reverse engineer this to work out how it splits up the mesh into 2:
    https://github.com/hugoscurti/mesh-cutter

    And then use similar to get an intersection, and effectively cut the intersecting mesh into 2 and discard one half and stitch the other half on / remake the mesh using the left over points and wind new triangles.

    Just 2 off top of my head, but itll be a lot of work either way. But certainly doable and not outside what 1 person can achieve with correct use of additional libraries
     
    Santa likes this.
  10. ADZ_

    ADZ_

    Joined:
    Jun 14, 2019
    Posts:
    7
    Thank you everyone for your support!
     
    MadeFromPolygons likes this.