Extracting vertices from scenekit

后端 未结 7 446
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-03 01:42

I\'m having a problem with understanding scenekit geometery.

I have the default cube from Blender, and I export as collada (DAE), and can bring it into scenekit....

7条回答
  •  青春惊慌失措
    2020-12-03 02:21

    Here is an extension method if the data isn't contiguous (the vector size isn't equal to the stride) which can be the case when the geometry is loaded from a DAE file. It also doesn't use copyByte function.

    extension  SCNGeometry{
    
    
        /**
         Get the vertices (3d points coordinates) of the geometry.
    
         - returns: An array of SCNVector3 containing the vertices of the geometry.
         */
        func vertices() -> [SCNVector3]? {
    
            let sources = self.sources(for: .vertex)
    
            guard let source  = sources.first else{return nil}
    
            let stride = source.dataStride / source.bytesPerComponent
            let offset = source.dataOffset / source.bytesPerComponent
            let vectorCount = source.vectorCount
    
            return source.data.withUnsafeBytes { (buffer : UnsafePointer) -> [SCNVector3] in
    
                var result = Array()
                for i in 0...vectorCount - 1 {
                    let start = i * stride + offset
                    let x = buffer[start]
                    let y = buffer[start + 1]
                    let z = buffer[start + 2]
                    result.append(SCNVector3(x, y, z))
                }
                return result
            }
        }
    }
    

提交回复
热议问题