0

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 Answer 1

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.