So I’m building a mesh in my game procedurally and my goal is to strip the duplicate vertices for performance.
I can do this in probuilder with the collapsesharedvertices method.
public static void CollapseSharedVertices(Mesh mesh, Vertex[] vertices = null)
{
bool flag = vertices != null;
if (vertices == null)
{
vertices = mesh.GetVertices();
}
int subMeshCount = mesh.subMeshCount;
List<Dictionary<Vertex, int>> list = new List<Dictionary<Vertex, int>>();
int[][] array = new int[subMeshCount][];
int num = 0;
for (int i = 0; i < subMeshCount; i++)
{
array[i] = mesh.GetTriangles(i);
Dictionary<Vertex, int> dictionary = new Dictionary<Vertex, int>();
for (int j = 0; j < array[i].Length; j++)
{
Vertex key = vertices[array[i][j]];
if (dictionary.TryGetValue(key, out var value))
{
array[i][j] = value;
continue;
}
array[i][j] = num;
dictionary.Add(key, num);
num++;
}
list.Add(dictionary);
Debug.Log(i);
}
Vertex[] array2 = list.SelectMany((Dictionary<Vertex, int> x) => x.Keys).ToArray();
if (flag | (array2.Length != vertices.Length))
{
Vertex.SetMesh(mesh, array2);
mesh.subMeshCount = subMeshCount;
for (int k = 0; k < subMeshCount; k++)
{
mesh.SetTriangles(array[k], k);
}
}
}
But that function is designed to work with probuildermeshes. Does anyone know of a way to convert that function so I can use it in final build? It looks like the only thing that function uses is “Vertex” so if I can figure out how to convert that it might work. Or if there is another way to go about it?