处理 Swift 时出现奇怪的运行时 SceneKit 错误(C3DRendererContextBindMeshElement)

Weird Runtime SceneKit error when dealing with Swift (C3DRendererContextBindMeshElement)

因此,当我 运行 我的程序时,每当相机看起来(我认为)我试图渲染的自定义几何体时,我总是会收到一个奇怪的错误。我根据参数曲面(位置和法线)的均匀采样创建了一堆三角形 这是错误:

SceneKit: error, C3DRendererContextBindMeshElement unsupported byte per index (8)

它在控制台中打印了很多次。我很难在网上找到任何真实的上下文,而且根据代码,它有点神秘。这是代码:

    let sampling = 5

    //Returns an array of parametricVertex of 25 points (5 by 5) (grid of points on surface)
    let points = object.getParametricVertexArray(sampling, vPoints: sampling)
    print(points.count)

    // Organize the points into triangles.
    var indices = [Int]()
    var stripStart = 0
    for var i = 0; i < (sampling - 1); i++, stripStart += sampling {
        for var j = 0; j < (sampling - 1); j++ {
            let v1 = stripStart + j
            let v2 = stripStart + j + 1
            let v3 = stripStart + (sampling) + j
            let v4 = stripStart + (sampling) + j + 1

            indices.append(v4)
            indices.append(v2)
            indices.append(v3)

            indices.append(v1)
            indices.append(v3)
            indices.append(v2)
        }
    }


    let data = NSData.init(
        bytes: points,
        length: points.count * sizeof(parametricVertex)
    )

    let source = SCNGeometrySource.init(
        data: data,
        semantic: SCNGeometrySourceSemanticVertex,
        vectorCount: points.count,
        floatComponents: true,
        componentsPerVector: 3,
        bytesPerComponent: sizeof(Float),
        dataOffset: 0,
        dataStride: sizeof(parametricVertex)
    )


    let normalSource = SCNGeometrySource.init(
        data: data,
        semantic: SCNGeometrySourceSemanticNormal,
        vectorCount: points.count,
        floatComponents: true,
        componentsPerVector: 3,
        bytesPerComponent: sizeof(Float),
        dataOffset: sizeof(Float) * 3,
        dataStride: sizeof(parametricVertex)
    )

    let element = SCNGeometryElement.init(
        data: NSData.init(
            bytes: indices,
            length: sizeof(Int) * indices.count
        ),
        primitiveType: SCNGeometryPrimitiveType.Triangles,
        primitiveCount: indices.count / 3,
        bytesPerIndex: sizeof(Int)
    )

    let surfaceGeo = SCNGeometry.init(sources: [source, normalSource], elements: [element])
    surfaceGeo.firstMaterial?.doubleSided = true
    let newNode = SCNNode(geometry: surfaceGeo)
    scene.rootNode.addChildNode(newNode)

其中参数顶点是:

struct parametricVertex {
    var x: Float, y: Float, z: Float      //Positions
    var nx: Float, ny: Float, nz: Float   //Normals
}

我不知道我哪里出错了,也不知道这个错误试图告诉我什么。任何帮助将不胜感激。

您正在为索引使用 64 位整数(Int,8 字节),但这是不受支持的。您可以将 indices 声明为 UInt16 的数组来解决此问题。