I’m making an editor tool that separates a mesh into smaller meshes based on timestamp data in UV2. The tool is for Open Brush’s Unity SDK. Open Brush is a VR painting game, and the SDK allows a unity developer to take Open Brush paintings into their Unity project. When you export from Open Brush, the exporter will group strokes together. This tool can be used to get the original strokes.
Example use case: https://youtu.be/krXYTtJ8Mk8
The reason for this post: My first approach had several problems in terms of memory usage, and I’m worried that my improved approach has other problems I’m not aware of.
In my first approach, I use a method GetMeshSubset(Mesh originalMesh, int[] Triangles)
to create the new meshes. The caller will fill Triangles
with only the necessary triangles for this child mesh:
public static Mesh GetMeshSubset(Mesh OriginalMesh, int[] Triangles) {
Mesh newMesh = new Mesh();
newMesh.name = OriginalMesh.name;
newMesh.vertices = OriginalMesh.vertices;
newMesh.triangles = Triangles;
newMesh.uv = OriginalMesh.uv;
newMesh.uv2 = OriginalMesh.uv2;
newMesh.uv3 = OriginalMesh.uv3;
newMesh.colors = OriginalMesh.colors;
newMesh.subMeshCount = OriginalMesh.subMeshCount;
newMesh.normals = OriginalMesh.normals;
return newMesh;
}
There are 2 problems with this approach:
-
The new mesh data is saved into the scene file because the meshes aren’t saved as assets.
-
The method unnecessarily copies all vertex, UV, and other data from the original mesh, even if only a small subset of the data is needed for the new mesh.
Improved approach:
public static Mesh GetMeshSubset(Mesh OriginalMesh, int[] Triangles, Vector3[] vertices = null, Vector2[] uv = null, Vector3[] normals = null,
Color[] colors = null, int index = 0) {
Mesh newMesh = new Mesh();
newMesh.name = OriginalMesh.name;
newMesh.vertices = vertices ?? OriginalMesh.vertices;
newMesh.triangles = Triangles;
newMesh.uv = uv ?? OriginalMesh.uv;
newMesh.uv3 = OriginalMesh.uv3;
newMesh.colors = colors ?? OriginalMesh.colors;
newMesh.subMeshCount = OriginalMesh.subMeshCount;
newMesh.normals = normals ?? OriginalMesh.normals;
AssetDatabase.CreateAsset(newMesh, "Assets/"+OriginalMesh.name+"_submesh["+index+"].asset");
return newMesh;
}
The reason for this post, is because I’m worried that I have again made a mistake somewhere. Those 2 problems explained above, I didn’t know they would be problems before I encountered them.
So my question: in my currently improved approach - where I create a new asset for each mesh - am I making any mistakes memory/storage-wise?