How to Merge Intersecting Meshes on Runtime

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).

1 Like

Yes, it is definitely possible. You need to find intersection points, triangulate new surface and fill in mesh vertices, uvs and normals.

Thank you very much.

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.

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.

Metaball tutorial | PATOMKIN - 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.

@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).

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.

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.

1 Like

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

2 Likes

Thank you everyone for your support!

1 Like