如何从 SCNVector3 在 SceneKit 中生成凹或凸多边形平面

How to generating a concave or convex polygon plane in SceneKit from SCNVector3

我正在尝试以具有自定义形状的平面形式创建自定义 SCNGeometry,它可以放置在 ARKit 会话中。我在以下方法中使用选项 SCNGeometryPrimitiveTypePolygon 似乎工作正常:

extension SCNGeometry {

    class func polygonfrom(vectices: [SCNVector3]) -> SCNGeometry {
        let indices: [Int32] = getIndices(count: vectices.count)
        let indexData = Data(bytes: indices, count: indices.count * MemoryLayout<Int32>.size)
        let source = SCNGeometrySource(vertices: vectices)
        let element = SCNGeometryElement(data: indexData, primitiveType: .polygon, primitiveCount: 1, bytesPerIndex: MemoryLayout<Int32>.size)
        return SCNGeometry(sources: [source], elements: [element])
    }

    class private func getIndices(count: Int) -> [Int32] {
        var indices: [Int32] = []
        indices.append(Int32(count))
        for i in 0..<count{
            indices.append(Int32(i))
        }
        return indices
    }
}

不幸的是,它不适合凹多边形:

results

SCNShape class 代表您创建了一个合适的三角剖分。