Red
Red

Reputation: 2246

Weird Runtime SceneKit error when dealing with Swift (C3DRendererContextBindMeshElement)

So, when I run my program, I keep getting a weird error whenever the camera looks (I think) at a custom geometry I am trying to render. I create a bunch of triangles based of the uniform sampling of a parametric surface (position and normals) Here is the error:

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

It prints it out a bunch of times in the console. I am having a hard time finding any real context on this online and it is a bit cryptic based on the code. Here is the code:

    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)

Where parametric vertex is:

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

I have no idea where I am going wrong or what the error is even trying to tell me. Any help would be greatly appreciated.

Upvotes: 1

Views: 307

Answers (1)

mnuages
mnuages

Reputation: 13462

you are using 64 bits integers (Int, 8 bytes) for your indices and that's not supported. You can declare indices as an array of UInt16 to solve this issue.

Upvotes: 4

Related Questions