Help with lag on first presenting SceneKit view

I know about preloading SKTextures to speed up the Scene initialisation

e.g.

SKTexture.preloadWithCompletionHandler { 
    // Now everything you put into the texture atlas has been loaded in memory
}

However, what I’m struggling with is that my app mainly uses UIKit View Controllers, however, one of them has a SceneKit view with skyboxes and textures that I’ve been using SKTexture objects for.

When the use tabs to this particular view controller, for the first time, there is a massive 4 second pause.

Once everything’s loaded, the user can tab around with no lag to any view controllers in the app, including returning to the SceneKit one.

Because the SceneKit ViewController is NEVER the first view controller to be displayed when the app is first launched, it hasn’t even been instantiated from the storyboard or loaded into view in order to execute a

SKTexture.preloadWithCompletionHandler {}

So, can anyone please advise the best way to do this? I wondered about static methods and static variables so that in the App Delegate I could call those, but that felt dirty and broke ‘Encapsulation’.

Is there a better way?

NOT WORKING EXAMPLE CODE:

In AppDelegate didLaunch

sceneKitViewController.preloadTextures()

in sceneKitViewController (the SceneKit View Controller that is not the first VC being presented:

    static var daySkyboxPX:             SKTexture?
    static var daySkyboxNX:             SKTexture?
    static var daySkyboxPY:             SKTexture?
    static var daySkyboxNY:             SKTexture?
    static var daySkyboxPZ:             SKTexture?
    static var daySkyboxNZ:             SKTexture?

    static var overcastSkyboxPX:        SKTexture?
    static var overcastSkyboxNX:        SKTexture?
    static var overcastSkyboxPY:        SKTexture?
    static var overcastSkyboxNY:        SKTexture?
    static var overcastSkyboxPZ:        SKTexture?
    static var overcastSkyboxNZ:        SKTexture?

    static var nightSkyboxPX:           SKTexture?
    static var nightSkyboxNX:           SKTexture?
    static var nightSkyboxPY:           SKTexture?
    static var nightSkyboxNY:           SKTexture?
    static var nightSkyboxPZ:           SKTexture?
    static var nightSkyboxNZ:           SKTexture?


    static func preloadTextures()
    {
        sceneKitViewController.daySkyboxPX = SKTexture(imageNamed: "dpx")
        sceneKitViewController.daySkyboxNX = SKTexture(imageNamed: "dnx")
        sceneKitViewController.daySkyboxPY = SKTexture(imageNamed: "dpy")
        sceneKitViewController.daySkyboxNY = SKTexture(imageNamed: "dny")
        sceneKitViewController.daySkyboxPZ = SKTexture(imageNamed: "dpz")
        sceneKitViewController.daySkyboxNZ = SKTexture(imageNamed: "dnz")

        sceneKitViewController.overcastSkyboxPX = SKTexture(imageNamed: "opx")
        sceneKitViewController.overcastSkyboxNX = SKTexture(imageNamed: "onx")
        sceneKitViewController.overcastSkyboxPY = SKTexture(imageNamed: "opy")
        sceneKitViewController.overcastSkyboxNY = SKTexture(imageNamed: "ony")
        sceneKitViewController.overcastSkyboxPZ = SKTexture(imageNamed: "opz")
        sceneKitViewController.overcastSkyboxNZ = SKTexture(imageNamed: "onz")

        sceneKitViewController.nightSkyboxPX = SKTexture(imageNamed: "npx")
        sceneKitViewController.nightSkyboxNX = SKTexture(imageNamed: "nnx")
        sceneKitViewController.nightSkyboxPY = SKTexture(imageNamed: "npy")
        sceneKitViewController.nightSkyboxNY = SKTexture(imageNamed: "nny")
        sceneKitViewController.nightSkyboxPZ = SKTexture(imageNamed: "npz")
        sceneKitViewController.nightSkyboxNZ = SKTexture(imageNamed: "nnz")

        SKTexture.preload([sceneKitViewController.daySkyboxPX!,
                           sceneKitViewController.daySkyboxNX!,
                           sceneKitViewController.daySkyboxPY!,
                           sceneKitViewController.daySkyboxNY!,
                           sceneKitViewController.daySkyboxPZ!,
                           sceneKitViewController.daySkyboxNZ!,
                           sceneKitViewController.overcastSkyboxPX!,
                           sceneKitViewController.overcastSkyboxNX!,
                           sceneKitViewController.overcastSkyboxPY!,
                           sceneKitViewController.overcastSkyboxNY!,
                           sceneKitViewController.overcastSkyboxPZ!,
                           sceneKitViewController.overcastSkyboxNZ!,
                           sceneKitViewController.nightSkyboxPX!,
                           sceneKitViewController.nightSkyboxNX!,
                           sceneKitViewController.nightSkyboxPY!,
                           sceneKitViewController.nightSkyboxNY!,
                           sceneKitViewController.nightSkyboxPZ!,
                           sceneKitViewController.nightSkyboxNZ!,
                           sceneKitViewController.dayWater!,
                           sceneKitViewController.overcastWater!,
                           sceneKitViewController.nightWater!], withCompletionHandler: {} )

        sceneKitViewController.daySkybox? = [sceneKitViewController.daySkyboxPX!,
                                        sceneKitViewController.daySkyboxNX!,
                                        sceneKitViewController.daySkyboxPY!,
                                        sceneKitViewController.daySkyboxNY!,
                                        sceneKitViewController.daySkyboxPZ!,
                                        sceneKitViewController.daySkyboxNZ!]

        sceneKitViewController.overcastSkybox? = [sceneKitViewController.overcastSkyboxPX!,
                                             sceneKitViewController.overcastSkyboxNX!,
                                             sceneKitViewController.overcastSkyboxPY!,
                                             sceneKitViewController.overcastSkyboxNY!,
                                             sceneKitViewController.overcastSkyboxPZ!,
                                             sceneKitViewController.overcastSkyboxNZ!]

        sceneKitViewController.nightSkybox? = [sceneKitViewController.nightSkyboxPX!,
                                       sceneKitViewController.nightSkyboxNX!,
                                       sceneKitViewController.nightSkyboxPY!,
                                       sceneKitViewController.nightSkyboxNY!,
                                       sceneKitViewController.nightSkyboxPZ!,
                                       sceneKitViewController.nightSkyboxNZ!]
    }

@annemarie1185 Does the original solution with static variables and methods work for you?

No. It didn’t.

I never found a solution, and just have to live with the lag.

This topic was automatically closed after 166 days. New replies are no longer allowed.