Extract faces information from SCNGeometry in SceneKit

430 views Asked by At

I want to extract faces information from a SCNNode geometry just like we can extract vertices information from geometry sources. Any idea on how that can be achieved?

1

There are 1 answers

0
Andrew Chinery On BEST ANSWER

If you have a SCNGeometry object (which you can get from a SCNNode with node.geometry) then you can look at the elements property which will contain the face information as an array of SCNGeometryElement objects.

e.g. assuming you just want the first element

let element = geometry.elements[0]
let faces = element.data.withUnsafeBytes {(ptr: UnsafeRawBufferPointer) -> [Int32] in
    guard let boundPtr = ptr.baseAddress?.assumingMemoryBound(to: Int32.self) else {return []}
    let buffer = UnsafeBufferPointer(start: boundPtr, count: element.data.count / 4)
    return Array<Int32>(buffer)
}
print(faces)

Depending on element.primitiveType you will need to interpret the indices differently. See the documentation for SCNGeometryPrimitiveType.