How do you convert Wavefront OBJ file to an SCNNod

2019-01-23 20:43发布

问题:

I've imported a Wavefront OBJ file from a URL and now I'd like to insert it into my scene (SceneKit) on my iOS 9 app (in Swift). What I've done so far is:

let asset = MDLAsset(URL: localFileUrl)
print("count = \(asset.count)")  // 1

Any help converting this to a SCNNode would be appreciated. According to to Apple's docs:

Model I/O can share data buffers with the MetalKit, GLKit, and SceneKit frameworks to help you load, process, and render 3D assets efficiently.

But I'm not sure how to get buffer from an MDLAsset into a SCNNode.

回答1:

Turns out this quite easy as many of the ModelIO classes already bridge. I was doing import ModelIO which gave me access to all the ModelIO classes and likewise import SceneKit which gave me the SceneKit classes, but, I was missing import SceneKit.ModelIO to bring in the SceneKit support for ModelIO.

let url = NSURL(string: "url-to-your-obj-here")
let asset = MDLAsset(URL: url!)
let object = asset.objectAtIndex(0)
let node = SCNNode(MDLObject: object)

Easy as that...