2015-11-04 17 views
1

打交道時,奇怪的運行SceneKit錯誤所以,當我運行我的程序,我一直在一個定製的幾何形狀我試圖渲染得到一個奇怪的錯誤,每當相機的外觀(我認爲)。我創建一批基於參數曲面(位置和法線)的均勻採樣的三角形下面是錯誤:與斯威夫特(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 
} 

我不知道我要去的地方錯了還是什麼錯誤甚至想告訴我。任何幫助將不勝感激。

回答

3

您使用的是64位整數(Int,8字節)爲您的索引和不支持的功能。您可以將indices聲明爲UInt16的數組來解決此問題。

+0

天啊!我不敢相信我沒有看到!非常感謝! – Red