4

I used to render 3d scene with openGL and metal on IOS, and the file format which I used was OBJ and CTM. These days I am trying Scene Kit. It seems that SceneKit only load DAE file. All the demos I can found on the Internet use DAE file , and I can't see the array of vertex and facet in their codes. How can I load OBJ file or CTM file instead of DAE file?

njuyuanrui
  • 133
  • 2
  • 7

5 Answers5

6

Loading an OBJ file

It is as simple as passing MDLAsset a valid URL.

private func nodeForURL(url: NSURL) -> SCNNode
{
    let asset = MDLAsset(URL: url)
    let object = asset.objectAtIndex(0)
    let node = SCNNode(MDLObject: object)

    return node
}

This will not only correctly load the .obj file, but it will load referenced .mtl files.

Cameron Lowell Palmer
  • 21,528
  • 7
  • 125
  • 126
4

you can do that by writing your own importer. Take a look at SCNGeometry, SCNGeometrySource and SCNGeometryElement.

edit: starting iOS 9.0 and OS X 10.11 SceneKit can open OBJ files or any other file format supported by Model I/O. You can use previously existing APIs to do that (such as +sceneNamed:) or the new +sceneWithMDLAsset: method.

mnuages
  • 13,049
  • 2
  • 23
  • 40
2

EDIT: ModelIO can probably load OBJ files now. I haven’t tried this path myself. This answer was written before iOS 9 and OS X 10.11:

SceneKit can't load DAE files on iOS, actually, it actually precompiles the DAE files to an internal format for iOS devices.

If you want to transform your OBJs to DAEs you can write a simple importer/exporter on OS X to do so — on OS X SceneKit will actually read OBJ files (it's not documented but it works) and will write DAEs.

Or you could download the "Assimp" project on github if you'd like to try to read OBJs yourself, but it's going to be a bit of work getting it into SceneKit objects.

Wil Shipley
  • 9,343
  • 35
  • 59
  • 1
    You *can* load OBJ files on iOS now (iOS 9+) with ModelIO, as noted in @halmueller's answer. But as Wil hints, you probably *shouldn't*. For any assets that ship with your app (or even through your app post-AppStore-submission, like add-on content hosted on your own server), it's best to preprocess on the Mac with Xcode or your own ModelIO/SceneKit-based tools, then ship `.scn` files that load faster on there device. – rickster Jan 12 '16 at 05:24
  • This is a good argument for every iOS developer to learn at least a little about Mac development. This sort of utility (on the Mac) is handy. – Hal Mueller Jan 12 '16 at 07:09
  • _“it actually precompiles the DAE files to an internal format for iOS devices”_   —   It appears that the “internal format” is just the `.scn` format.  DAEs can be loaded on iOS with `let scene = SCNScene(named: "some_3d_model.dae")` _(just checked with Xcode 8 against iOS 10.0.1)_. – Slipp D. Thompson Sep 13 '16 at 23:32
  • 2
    Dug into this a bit more.  The iOS-compiled `….app/some_3d_model.dae` is a binary plist with a `zippedData` entry with a bunch of ZIP-compressed data, that if base64-decoded and uncompressed produces a single `contents` file.  That `contents` file is a binary plist that looks like a SceneKit internal scene representation— there are a number of keys that match 1:1 w/ the SceneKit API like `geometryElements`, `geometrySource`, `SCNMaterial` property names, and a `scene` with `attributes`, `nodeTree`, `startTime`, etc. like `SCNScene`/`MDLAsset`.  (A true `.dae` is a ``-rooted XML file.) – Slipp D. Thompson Sep 14 '16 at 01:52
2

As of iOS 9/OS X 10.11, you can use Model I/O's MDLAsset to import OBJ files (and a few other formats). How do you convert Wavefront OBJ file to an SCNNode with Model I/O has sample code.

Community
  • 1
  • 1
Hal Mueller
  • 7,019
  • 2
  • 24
  • 42
2

Further information regarding the supported file formats:

The following 3D file formats are supported by SceneKit and can be imported to an .scn file using the Scene Editor in Xcode:

DAE, OBJ, Alembic, STL and PLY files.

Source: WWDC 2015 Session "Enhancements to SceneKit" at 02:24

Pierre F
  • 1,332
  • 15
  • 33