简体   繁体   中英

Extract faces information from SCNGeometry in SceneKit

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?

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.

eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM