从人脸数组中删除重复的人脸 - Three.js

Remove duplicate faces from an Array of faces - Three.js

我想从面孔数组中删除重复的面孔 - 我尝试了下面的一些代码,但我不确定如何完成它。

首先我惊讶地发现:

new THREE.Vector3(0,0,0) == new THREE.Vector3(0,0,0)

产生假(我希望它产生真)而且下面的代码也产生假(我再次希望它产生真)。

    var triangleGeometry = new THREE.Geometry(); 
    triangleGeometry.vertices.push(new THREE.Vector3( 0.0,  1.0, 0.0)); 
    triangleGeometry.vertices.push(new THREE.Vector3(-1.0, -1.0, 0.0)); 
    triangleGeometry.vertices.push(new THREE.Vector3( 1.0, -1.0, 0.0)); 
    triangleGeometry.faces.push(new THREE.Face3(0, 1, 2)); 

    var triangleGeometry2 = new THREE.Geometry(); 
    triangleGeometry2.vertices.push(new THREE.Vector3( 0.0,  1.0, 0.0)); 
    triangleGeometry2.vertices.push(new THREE.Vector3(-1.0, -1.0, 0.0)); 
    triangleGeometry2.vertices.push(new THREE.Vector3( 1.0, -1.0, 0.0)); 
    triangleGeometry2.faces.push(new THREE.Face3(0, 1, 2));

    triangleGeometry2.faces[0] === triangleGeometry.faces[0] - yields false

至于我的代码来确定一个人脸是否已经在一个人脸数组中,我写了以下代码:

            function faceInArray(arrayOfFaces,face)
            {   // 
                // Determine whether a face is in an array of faces
                // The ES6 Set object does not have any compare methods or custom compare extensibility.
                // For this reason this function will be called before adding an face to an array of faces 
                // to ensure that duplicate faces are not placed in an array

                for(let i = 0; i < arrayOfFaces.length; i++)
                {
                    vertexaFaceFromArray = buildingGeometry.vertices[arrayOfFaces[i].a]
                    vertexbFaceFromArray = buildingGeometry.vertices[arrayOfFaces[i].b]
                    vertexcFaceFromArray = buildingGeometry.vertices[arrayOfFaces[i].c]

                    vertexaFace = buildingGeometry.vertices[face.a]
                    vertexbFace = buildingGeometry.vertices[face.b]
                    vertexcFace = buildingGeometry.vertices[face.c]

                    // Compare the vertices in each face I'm not sure how to do this?


                }
            }

现在我不确定如何从这里开始,因为简单地检查 vertex1 == vertex2 不起作用,正如我在第一段代码中演示的那样。比较它们时,我真的需要为每张脸提取 x、y 和 z 坐标吗?此外,顶点的顺序重要吗?

这不起作用的原因:new THREE.Vector3(0,0,0) == new THREE.Vector3(0,0,0) == 在这种情况下检查两个值是否是对同一对象的引用。但是你的向量是不同的对象,它们恰好具有相同的 x、y 和 z 值。您应该改为在 Vector3:

上使用 three.js equals 函数
new THREE.Vector3(0,0,0).equals(new THREE.Vector3(0,0,0))

所以你的函数可以这样工作:

function faceInArray(arrayOfFaces, face) {
    for(let i = 0; i < arrayOfFaces.length; i++) {
        vertexaFaceFromArray = buildingGeometry.vertices[arrayOfFaces[i].a]
        vertexbFaceFromArray = buildingGeometry.vertices[arrayOfFaces[i].b]
        vertexcFaceFromArray = buildingGeometry.vertices[arrayOfFaces[i].c]

        vertexaFace = buildingGeometry.vertices[face.a]
        vertexbFace = buildingGeometry.vertices[face.b]
        vertexcFace = buildingGeometry.vertices[face.c]

        if (vertexaFaceFromArray.equals(vertexaFace) && 
            vertexbFaceFromArray.equals(vertexbFace) && 
            vertexcFaceFromArray.equals(vertexcFace)) {
            return true;
        }
    }
    return false;
}

但是,当然,这只会检查每个面的顶点顺序是否与输入面的顺序完全相同。这取决于你要用它做什么,但原则上面 (1, 2, 3) 与面 (2, 3, 1) 和 (3, 1, 2) 相同。

此外,如果你的脸是两个面的,那么它也与顶点的任何顺序相同。 IE。 (3, 2, 1), (2, 1, 3) 和 (1, 3, 2)。因此,您可能希望扩展代码以另外检查这些情况。