Follow-up to my previous question in Objective-C here (specifically, I'm trying to translate the answer I provided with Objective-C into Swift):
Drawing a line between two points using SceneKit
Problem: SceneKit statistics show that line is being drawn, but it doesn't appear on screen.
Minimal example:
- Start a new Game project in Xcode using Swift and SceneKit.
- Remove boilerplate:
let scene = SCNScene()
instead ofSCNScene(named: "art.scnassets/ship.dae")!
- comment out
let ship = ...
andship.runAction(...)
- Change
scnView.backgroundColor = UIColor.blackColor()
toscnView.backgroundColor = UIColor.grayColor()
(I originally thought maybe the lines were just black on a black background, so changed the background colour accordingly) Add the following code:
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)
As mentioned, the SceneKit statistics suggest the line is being drawn, but it doesn't seem to appear, and there are no compilation or runtime errors, which makes it a little tricky to track down.
Edit: Using the GPU 'Analyse' tool in the debug navigator gave the following errors:
Draw call exceeded element array buffer bounds
Draw call exceeded array buffer bounds
in glDrawElements(GL_LINES, 4, GL_UNSIGNED_INT, NULL)
which suggests there's something wrong with my SCNGeometryElement construction?