I have a method that locates all mesh filters attached to a transform and its children and aggregates its vertices and triangles. A copy of the code is further down.
[Edit: Based on skovacs1's answer and further troubleshooting.] The below code works as expected. The mirroring that is mentioned was due to the display method used for debugging. So the only remaining question is whether there is a more efficient way of aggregating meshes for purposes other than display.
The problem I'm having is that the resulting aggregate mesh is mirrored along the x-axis. So only if I view the aggregate mesh with Scale X = -1 is everything good.
The answer was hiding in plain sight.
First, a review of the requirements implicit in the question:
Another requirement is that the vertices be a flattened array, rather than an array of Vector3 structures. But that requirement effects the detail of the implementation, not the answer.
And the answer is to use Mesh.CombineMeshes.
Most of the documentation involving the Mesh.CombineMeshes function discusses using it to combine meshes that share the same material. But, if all you need are the vertices and triangles, then it can be used to combine meshes with different materials.
Just prepare and use Mesh.CombineMeshes as shown in the function's example code, then extract the vertices and triangles from mesh.vertices and mesh.triangles.
The 64K vertices limit for a single Mesh object applies. But that can be worked around using a batching process:
As far as the performance goes: The aggregation took over 2 minutes to complete using the original algorithm. (See question.) It took less than 50 milliseconds using Mesh.CombineMeshes.
A slight performance boost.
answered Nov 19 '10 at 01:59 AM
It is possible that there is a problem with that portion of code which you use to make this data viewable.
What is going on with currentIndex and indexMap? You go through a lot of work with currentIndex, but all you really want to do is add your triangles to the aggregate triangles, right? wouldn't that simply entail getting the offset for the vertices in the aggregate vertices?
Here's a simpler version of what your code does (each vertex is still split into separate components and the triangle indices index to the start index of each vertex):