3DViewer is small demo app for downloading 3d models and showing them in SceneKit at runtime.
The supported formats are: 3d, 3ds, ac, b3d, bvh, cob, dae, dxf, ifc, irr, md2, md5mesh, md5anim, m3sd, nff, obj, off, mesh, xml, ply, q3o, q3s, raw, smd, stl, wrl, xgl, zgl, fbx, md3.
For better results please use models created in 3dsMax 2010.
1. Download the 3d model files. You are able to:
-
start to download by a specified
URl -
pause download
-
resume it
-
and cancel it
In this app you're going to use the URLSession download task to download .obj model file remotely to your app and report the progress to the user while bytes are being downloaded. The UIPasteboard provides you with the needed link for 3d model.
2. Open in.. feature.
3. Unziping archives.
4. Browsing local files with the help of the FileManager.
5. Loading a model file directly into an SCNAssimpScene from a device's internal URL. You're going to do it with the help of AssimpKit which is a great framework for loading uncompressed models.
It is also possible to use to use a downloaded .dae and .obj file and load it at runtime without AssimpKit.
To load .obj you need to use MDLAsset from Apple's ModelIO which was introduced at WWDC 2015. The steps are following:
- Extract the mesh from the asset
// Create a MDLAsset from url
let asset = MDLAsset(url:URL(fileURLWithPath: path))
guard let object = asset.object(at: 0) as? MDLMesh else {
fatalError("Failed to get mesh from asset.")
}- Create a
MDLMaterialand add properties to it to describe the material
// Create a material
let scatteringFunction = MDLScatteringFunction()
let material = MDLMaterial(name: "baseMaterial", scatteringFunction: scatteringFunction)
// Apply the texture
for submesh in object.submeshes! {
if let submesh = submesh as? MDLSubmesh {
submesh.material = material
}
}- Create
SCNNode
// Wrap the ModelIO object in a SceneKit object
let node = SCNNode(mdlObject: object)What about Collada's .dae? If you try to use a downloaded .dae file and load it at runtime, you’ll get the following error:
scenekit COLLADA files are not supported on this platform.That is happening because during the build time Apple applies some magic to the scnassets directory to optimize the included Collada files so usually at runtime SceneKit has a deal with compressed .dae files. But your Collada file wasn't there during that time, so if you are going to download .daes from remote URL and use them at runtime you'll need to run scntool script to compress them and then upload to your server.
For example place your .dae file to the ~/Desktop/ and run the following script in the Terminal:
xcrun scntool --convert yourmodelfile.dae --format c3d --output yourmodelfile-optimized.daeThat's it. Upload the compressed file and try to use it.
3DViewer's license is based on the modified, 3-clause BSD-License.

