PopKernel PopKernel -4 years ago 151
Swift Question

How to debug custom geometry in SceneKit with Swift

I'm trying to learn how to create custom geometry in SceneKit. However, I've tried to make a triangle and it's not showing anything. I'm at a loss as to how to debug this. Is there a way to figure out if the triangle is valid? I just don't know where to start.

For reference, the playground code in question is below. Note that it is written against Swift 4, but the changes between Swift 3 and Swift 4 are so minor that getting it to compile in Swift 3 is trivial.

import UIKit
import SceneKit

let points = [
SCNVector3Make(0, 0, 0),
SCNVector3Make(0, 10, 0),
SCNVector3Make(10, 0, 0),
]
let indices = [
0,2,1,
]

let vertexSource = SCNGeometrySource(vertices: points)
let element = SCNGeometryElement(indices: indices, primitiveType: .triangles)
let geo = SCNGeometry(sources: [vertexSource], elements: [element])

Answer Source

When creating custom SCNGeometryElements the type of the indices needs to be Int16. I don't think this documented anywhere. But when you change the declaration of the indices too

let indices: [Int16] = [
    0, 2, 1
]

the triangle should appear.


Of course you need to create a view, a scene and a node with your geometry to actually see the triangle in an Swift Playground.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download