1

尝试从大量 SCNVector3 位置渲染一些多边形时,我收到以下错误消息。我真的需要渲染多边形,我不知道为什么会这样。如果有人知道另一种渲染多边形的方法,我将很高兴对其进行测试!

       [MTLDebugDevice validateNewBufferArgs:options:]
:467: failed assertion Cannot create buffer of zero length.

以下代码用于创建自定义 SCNGeometry。

extension SCNGeometry {
    static func multiPolygon (vertices: [SCNVector3]) -> SCNGeometry {
        var indices: [Int32] = [Int32(vertices.count)]
        indices.append(contentsOf: generateIndices(max: vertices.count))
        let vertexSource = SCNGeometrySource(vertices: vertices )
        let indexData = Data(bytes: indices,
                             count: indices.count * MemoryLayout<Int32>.size)
        let element = SCNGeometryElement(data: indexData,
                                         primitiveType: .polygon,
                                         primitiveCount: 1,
                                         bytesPerIndex: MemoryLayout<Int32>.size)
        return SCNGeometry(sources: [vertexSource], elements: [element])
    }

    static private func generateIndices(max maxIndexValue: Int) -> [Int32]{
        var counter: Int = 0
        var output: [Int32] = []
        while counter < maxIndexValue {
            output.append(Int32(counter))
            counter += 1
        }
        return output
    }
}
4

0 回答 0