How do you convert Wavefront OBJ file to an SCNNode with Model I/O

主宰稳场 提交于 2019-11-27 16:49:00

问题


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



来源:https://stackoverflow.com/questions/34667969/how-do-you-convert-wavefront-obj-file-to-an-scnnode-with-model-i-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!