Scenekit - Crash when rendering custom SCNGeometry polygon

203 Views Asked by At

I get the following error message when trying to render some polygons from a large amount of SCNVector3 positions. I really need to render polygons and I don't know why this is happening. If anyone know another way to render polygons I'll be glad to test it out!

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

The following code is used to create the custom 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
    }
}
0

There are 0 best solutions below