Hi everyone, I’ve been working on a BSP30 importer and due to the way that the data is stored/read there are a lot of separate meshes for each face. To optimise this I added the option to combine meshes by their material which works well, however this also means that occlusion culling doesn’t work well.
What would be the best approach to this? I’m looking for something similar to blender’s separation tool. (I would do this in blender but my goal is to make things like this possible within unity to simplify the workflow of porting levels)
Example of one mesh that could be separated into 4 by loose parts:
The Mesh/MeshData classes are documented here: Unity - Scripting API: Mesh
It’s straightforward: a mesh is an array of vertices, companion arrays for vertex data like UVs, and a list of triangles (which is really a list of three vertex indices per triangle).
If you’re specifically working with submeshes, you will also need to know how submeshes are encoded. Each submesh has a SubMeshDescriptor, which consists of identifying the first triangle of that submesh, the number of triangles in that submesh, and some other data. Unity - Scripting API: Mesh.SetSubMesh
So loop for each submesh in your original, creating new meshes that contain only the data required by that mesh. You’d likely have to make a dictionary of vertices so you don’t copy ALL the vertices over for each separated submesh, just the ones that touch triangles in each submesh.
I think I understand, thank you. I assume this approach would work when not using submeshes?
If you’re just looking to split multiple islands (Blender’s “Loose Parts”), then you’ll have to work out which triangles belong to each island yourself, instead of existing submeshes with different materials. There’s a straightforward approach to figuring that out, as well, involving a big array of island indices, and whenever you find a triangle that involves vertices from two different islands, just merge the two islands into one.