创建自定义 SCNGeometry 多边形

Creating a custom SCNGeometry polygon

我正在尝试创建一个可以具有任何多边形形式的自定义 SCNPlane class,因为常规 SCNPlane 只能是矩形。

我试图通过扩展 SCNGeometry 来实现这一点。下面是我使用的代码:

extension SCNGeometry {
    static func polygonPlane(vertices: [SCNVector3]) -> SCNGeometry {
        let src = SCNGeometrySource(vertices: vertices)

        var indices: [UInt32] = []        
        var index: UInt32 = 0
        for _ in vertices {
            indices.append(index)
            index += 1
        }

        let inds = SCNGeometryElement(indices: indices, primitiveType: .polygon)

        return SCNGeometry(sources: [src], elements: [inds])
    }
}

如您所见,该方法将顶点数组作为输入,我希望将这些顶点连接起来,以便使用提供的顶点制作具有指定几何形状的平面。

当在上面的代码中使用 SCNGeometryPrimitiveType.polygon 时,我得到一个奇怪的错误:"Thread 1: EXC_BREAKPOINT (code=1, subcode=0x106362e20)"。 swift 中似乎没有实现此案例的功能,但它适用于 Objective-C?我有什么方法可以使用它吗?

我也尝试使用其他选项作为 primitiveType-value,使用这些值没有崩溃,进一步加强了我的信念,即 .polygon 案例有问题。

使用多边形时,必须使用

  init(data: Data?, primitiveType: SCNGeometryPrimitiveType, primitiveCount: Int, bytesPerIndex: Int)

初始化 SCNGeometryElement。有关详细信息,请参阅苹果文档。