Technique for blending/combining nearby meshes?

I know meshes can be combined - just ran some tests on that overlapping some cubes. They formed a single mesh but were no less complex. The material was translucent, and you could see through the cubes to the inside - the overlapping parts were still there, just inside the new “blob” of cubes. I want to do something different.

I’m looking for either a tool or some creative ideas here. My test app has a bunch of basic 3D cubes somewhat randomly scattered about the scene. What I’d like to do is take nearby cubes (looping through them, testing distance to other cubes) and if 2 or more are touching/overlapping, or nearby each other, merge them into a single mesh, blending them together (connecting vertexes, I guess would be the way to go?) and connecting the cubes that are nearby but not touching. Has to be something done during runtime.

Is there a tool for something like this? If not, anyone have any ideas on how this might best be accomplished, before I start trying to manually build meshes from all the vertexes of the cubes?

As I write this… finding which 2 faces are closest then connecting the corners together might be a good way to go. The cubes won’t be rotated, so the faces will always be either parallel or perpendicular to each other.

Well before I dive down the rabbit hole of wasted time… anyone know of a tool? Done something like this before?

First you need to understand the structure of Meshes in Unity before you can think about this.

Meshes in Unity don’t have anything called a “face”. At the most fundamental level they have only vertices:
https://docs.unity3d.com/ScriptReference/Mesh-vertices.html
triangles:
https://docs.unity3d.com/ScriptReference/Mesh-triangles.html
and normals:

(for now we’ll ignore other things like UVs, vertex colors, etc…)

On a cube, a given face is going to made of at least two triangles, if not more. Actually merging two cubes can be done in a pretty much infinite number of ways (creating new surfaces that connect between the two), but would most likely involve discarding some of the old triangles and creating new ones connecting the two groups of vertices. You’d then have to also go back and fix your normals etc…

Start reading up on how triangles work and how they actually form the structure of the mesh.

1 Like

looks like CSG boolean operations, also voxel then remeshing, i’m not sure you want to go that way anyway, i mean merging cube, probably will generate more tris, trash triangles, and various nasties. i would go for voxel anyway, simpler, imho, show a picture of your case.

1 Like

Yes, what @neoshaman said… CSG (constructive solid geometry) is what you’re seeking to do, and it should cut down on overdraw to some extent. But the general case of dynamic CSG is not really a simple topic. There is some CSG utilities in Unity’s ProBuilder package but I haven’t used them at runtime.

Thanks for the tips. I started messing with voxels last night. Since I’m working with cubes, aligning them into a grid would make merging them a lot easier, I’m experimenting with that today. I’ll read up on CSG as well… though the voxel method may work out as the easier solution.