Search code examples
swiftaugmented-realityscenekitarkitrealitykit

ARKit – Getting "Unexpectedly found nil" when using SCN file > 300 MB


I am loading a 300mb 3d model of extension .dae, converted into .scn, with 4.4 million vertices, 1.5 million polygons, which is a 3d model of a building, created in 3DS MAX by an artist, like so:

let sceneToLoad = SCNScene(named: "art.scnassets/building1.scn")!

(It is loaded in a SCNView default viewer in the app so that the user can view, rotate it etc., by SCNView.allowsCameraControl = true)

Xcode will immediately crash when it reads that line, with only compiler info "unexpectedly found nil while unwrapping an optional value".

The memory does not go up at all when it runs to that line - suggesting it refuses to read it , and crashes instead. The 3d model is perfectly loaded and vieweable, rotateable etc in the XCODE Scenekit editor graphical viewer. When I replace it to point to another file name of a smaller 3d model it works fine, and even when I remove the model SCNNode in the same file (in the same "building1.scn" file) and replace with a smaller SCNNode of another random object, then miraculously it also works and loads fine.

I have not found anything similar on SO - in other similar answers iOS tries to load the model even if it's huge, but in none it crashes immediately finding a nil value.

Have tried all workarounds, remove/delete file and add again, load it as .dae in its original form, load the scene without unwrapping and unwrap later when searching for a node - nothing works, always crashes in the same way. The same thing happens when I try to load it in an ARKIT scene - it crashes at the above line that tries to just load the file.

Has anyone come across this, or knows of any workaround?

Many thanks


Solution

  • When load a 3D model with 1.5M polygons into SceneKit/ARKit, into RealityKit, or into AR Quick Look you'll always fail. That's because a robust number of polygons per 3D model must be not greater that 10K (with UV-texture having max resolution 2Kx2K, or with a regular texture rez 1Kx1K), and a maximum number of polygons per 3D scene must be not greater that 100K. You have exceeded the "unspoken" AR limit in 15 times.

    Game engines and AR frameworks, like SceneKit, RealityKit and AR Quick Look, are incapable of rendering such a huge number of polygons using 60 fps framerate on iOS device (even most desktop computers fail to do this). The best solution for an ARKit/RealityKit applications is to use an optimized low-poly models. The most preferred format for working with AR on mobile platform is Pixar USDZ. A USDZ file is a no compression, unencrypted zip archive of USD file.

    Look at this low-poly model from Turbosquid. It has just 5K polygons and it looks fine, doesn't it?

    enter image description here

    P.S.

    You can convert obj, fbx or abc in usdz using command line tools. Read about it HERE.