统一从网格中删除顶点重复项

时间:2019-01-26 19:40:28

标签: c# unity3d mesh

我需要从单一网格中删除重复的顶点,其复杂度要优于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)

1 个答案:

答案 0 :(得分:0)

您可以实现自己的IComparer<Vector3>并将其传递给SortedList构造函数。