我需要从单一网格中删除重复的顶点,其复杂度要优于O(n ^ 2)。我尝试使用SortedList和Dictionary,但是似乎不能使用Vector3作为索引,因为compare方法存在问题:
int[] triangles = srcMesh.triangles;
int ntriangles = triangles.Length;
Vector3[] vertices = srcMesh.vertices;
SortedList<Vector3, int> distinctVertices = new SortedList<Vector3, int>();
int ind = 0;
for (int i = 0; i < nvertices; i++)
{
if (!distinctVertices.ContainsKey(vertices[i]))
distinctVertices.Add(vertices[i], ind++);
}
for (int i = 0; i < ntriangles; i++)
triangles[i] = distinctVertices[vertices[triangles[i]]];
foreach (Vector3 distinct in distinctVertices.Keys)
vertices[distinctVertices[distinct]] = distinct;
nvertices = distinctVertices.Count;`
ArgumentException: does not implement right interface
System.Collections.Generic.Comparer`1+DefaultComparer[UnityEngine.Vector3].Compare (Vector3 x, Vector3 y) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Comparer.cs:86)
System.Collections.Generic.SortedList`2[UnityEngine.Vector3,System.Int32].Find (Vector3 key)
System.Collections.Generic.SortedList`2[UnityEngine.Vector3,System.Int32].ContainsKey (Vector3 key)
Extrusion.Start () (at Assets/Extrusion.cs:55)
答案 0 :(得分:0)
您可以实现自己的IComparer<Vector3>
并将其传递给SortedList
构造函数。