【发布时间】:2015-03-14 07:01:02
【问题描述】:
在此处跟进我之前在 Objective-C 中提出的问题(具体来说,我正在尝试将我使用 Objective-C 提供的答案翻译成 Swift):
Drawing a line between two points using SceneKit
问题:SceneKit 统计数据显示正在绘制线条,但它没有出现在屏幕上。
小例子:
- 使用 Swift 和 SceneKit 在 Xcode 中启动一个新的游戏项目。
- 删除样板:
-
let scene = SCNScene()而不是SCNScene(named: "art.scnassets/ship.dae")! - 注释掉
let ship = ...和ship.runAction(...)
-
- 将
scnView.backgroundColor = UIColor.blackColor()更改为scnView.backgroundColor = UIColor.grayColor()(我原本以为线条可能只是黑色背景上的黑色,所以相应地更改了背景颜色) -
添加以下代码:
var positions: [SCNVector3] = [SCNVector3Make(0.0,0.0,0.0),SCNVector3Make(10.0,10.0,10.0)] // using Swift's Int gives an error with unsupported "SceneKit: error, C3DRendererContextBindMeshElement unsupported byte per index (8)" // when constructing element: SCNGeometryElement below, so switched to CInt var indicies: [CInt] = [0,1] // for whatever reason, the following doesn't work on iOS: // let vertexSource = SCNGeometrySource(vertices: &positions, count: positions.count) // see https://stackoverflow.com/questions/26890739/how-to-solve-scenekit-double-not-supported-error // so using more complex SCNGeometrySource() initializer instead let vertexData = NSData(bytes: &positions, length: positions.count) let vertexSource = SCNGeometrySource(data: vertexData, semantic: SCNGeometrySourceSemanticVertex, vectorCount: positions.count, floatComponents: true, componentsPerVector: 3, bytesPerComponent: sizeof(Float), dataOffset: 0, dataStride: sizeof(SCNVector3)) let indexData = NSData(bytes: &indicies, length: indicies.count) let element = SCNGeometryElement(data: indexData, primitiveType: SCNGeometryPrimitiveType.Line, primitiveCount: indicies.count, bytesPerIndex: sizeof(CInt)) let lines = SCNGeometry(sources: [vertexSource], elements: [element]) let cellNode = SCNNode(geometry: lines) scene.rootNode.addChildNode(cellNode)
如前所述,SceneKit 统计数据表明正在绘制线 ,但它似乎没有出现,并且没有编译或运行时错误,这使得追踪起来有点棘手.
编辑:在调试导航器中使用 GPU“分析”工具出现以下错误:
Draw call exceeded element array buffer bounds
Draw call exceeded array buffer bounds
在glDrawElements(GL_LINES, 4, GL_UNSIGNED_INT, NULL) 中,这表明我的 SCNGeometryElement 构造有问题?
【问题讨论】: