Продолжение моего предыдущего вопроса в Objective-C здесь (в частности, я пытаюсь перевести ответ, который я дал с помощью Objective-C, в Swift):
Рисование линии между двумя точками с помощью SceneKit
Проблема: Статистика SceneKit показывает, что линия рисуется, но не появляется на экране.
Минимальный пример:
- Запустите новый игровой проект в Xcode, используя Swift и SceneKit.
- Remove boilerplate:
let scene = SCNScene()
instead ofSCNScene(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 предполагает, что линия прорисовывается, но она не появляется, и нет ошибок компиляции или времени выполнения, что немного усложняет отслеживание.
Изменить: использование инструмента «Анализ» графического процессора в навигаторе отладки дало следующие ошибки:
Draw call exceeded element array buffer bounds
Draw call exceeded array buffer bounds
в glDrawElements(GL_LINES, 4, GL_UNSIGNED_INT, NULL)
что предполагает, что что-то не так с моей конструкцией SCNGeometryElement?