Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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])
like image 724
PopKernel Avatar asked Nov 22 '25 09:11

PopKernel


1 Answers

When creating custom SCNGeometryElements the type of the indices needs to be Int161. 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.


Edit

1: As @mnuages has pointed out, SceneKit supports only 32bit Integers as indices. So you can use Int8, Int16 and Int32.

like image 106
jlsiewert Avatar answered Nov 23 '25 22:11

jlsiewert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!