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

时间:2015-11-04 19:20:47

标签: swift macos graphics rendering scenekit

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

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
}

我不知道我哪里出错了,或者错误甚至试图告诉我。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:4)

您使用64位整数(Int,8个字节)作为索引,并且不支持。您可以将indices声明为UInt16的数组来解决此问题。