3

I'm building an ARKit + SceneKit app which requires displaying models on to the real world. Every time a user selects a button, the model changes.

I tried loading a new model apon button press into a node and then adding it to the scene's root node, but this causes the camera to freeze for a few seconds until it is added to the scene.

I then read that it might be better to load all the nodes on start (viewDidLoad or something), call the scene's prepare method, and then add the nodes to the scene, but this causes the app to freeze a few times for a few seconds until the process is done. This is what I'm currently doing, and it isn't working well! Any ideas?

Keep in mind the model files are quite heavy (~15MB each)

func addNodesToScene() {
    for menuItem in self.menuItems {
                let tempScene = SCNScene(named: menuItem.modelPath)!
                let node = tempScene.rootNode
                node.scale = SCNVector3Make(0.1, 0.1, 0.1)
                node.isHidden = true
                self.foodNodes[menuItem] = node
                print("\(menuItem.title) node loaded")
            }

            print("done loading nodes - adding to scene")
            self.sceneView.prepare(Array(self.foodNodes.values), completionHandler: { (success) in
                for nNode in self.foodNodes.values {
                    self.sceneView.scene.rootNode.addChildNode(nNode)
                }
            })
}
royherma
  • 4,095
  • 1
  • 31
  • 42

1 Answers1

1

The prepare method prepares the models (and textures) to be loaded fast onto the GPU. Your function essentially combines two functions into one, loading the nodes from the .SCN files and preparing them for the GPU. The prepare method can prevent stutter in high frame rate games for example, it’s not going to solve loading 15 mb models.

If you are going to load 15MB models you are bound to get a delay somewhere. A model of 15MB should provide so much detail that a delay (while starting the app) would be acceptable. If you need multiple of that, the real problem here is likely the size of the models. Can you post an example?

Xartec
  • 2,369
  • 11
  • 22
  • It actually mitigated the issue, works pretty well. A few delays/freezes, but i can put some static "loading" screen in the meantime while it does that – royherma Sep 24 '17 at 22:42
  • i dont mind a delay, but since the main thread is blocked, i can't provide any animated loader or anything like that, which makes for a bad UX – royherma Oct 02 '17 at 17:28
  • The prepare method executes on a secondary thread already but maybe try to run the first for loop on a background thread manually? How many menu items are there? Also, did you check which part of the code actually causes the delay? (Loading the scenefiles or adding the nodes). You may be able to gain some performance by adding all the models to a single scene file. – Xartec Oct 02 '17 at 17:41
  • And on a different note, you should check the success value in the prepare block before actually adding to the scene. – Xartec Oct 02 '17 at 17:42
  • Loading the nodes works pretty fast, and i've managed to do it on a BG thread so can show some progress loader. The second for loop, which is the `prepare` method and adding to scene is what is causing the blocking. Doing some debugging on it now in order to get more info @Xartec – royherma Oct 03 '17 at 09:27
  • Also, are `.scn` files faster to load then `.dae` files or is there no difference, and what are the advantages with working with one vs the other – royherma Oct 03 '17 at 10:06
  • Pretty sure SCN files offer better performance, the DAE file is basically an XML file optimized for exchanging models between different 3D tools. If anything, the files should become a bit smaller (some quick test show about 20-25 decrease in size) if you convert them to SCN (see Editor Menu in SceneKit editor). The main difference is that you can still read and modify DAE files in another package. With such large models you are in a good position to give a conclusive answer to a performance difference yourself. – Xartec Oct 03 '17 at 15:26