ARkit - Loading .scn file from Web-Server URL in SCNScene

ε祈祈猫儿з 提交于 2019-12-03 07:35:51

问题


I am using ARKit for my application and I try to dynamically load .scn files from my web-server(URL)

Here is a part of my code

 let urlString = "https://da5645f1.ngrok.io/mug.scn"
        let url = URL.init(string: urlString)
        let request = URLRequest(url: url!)
        let session = URLSession.shared
        let downloadTask = session.downloadTask(with: request,
                completionHandler: { (location:URL?, response:URLResponse?, error:Error?)
                -> Void in
                print("location:\(String(describing: location))")
                let locationPath = location!.path
                let documents:String = NSHomeDirectory() + "/Documents/mug.scn"
                ls = NSHomeDirectory() + "/Documents"
                let fileManager = FileManager.default
                if (fileManager.fileExists(atPath: documents)){
                     try! fileManager.removeItem(atPath: documents)
                }
                try! fileManager.moveItem(atPath: locationPath, toPath: documents)
                print("new location:\(documents)")
                let node = SCNNode()
                let scene =  SCNScene(named:"mug.scn", inDirectory: ls)
                let nodess = scene?.rootNode.childNode(withName: "Mug", recursively: true)
                node.addChildNode(nodess!)
                let nodeArray = scene!.rootNode.childNodes
                for childNode in nodeArray {
                    node.addChildNode(childNode as SCNNode)
                }
                 self.addChildNode(node)
                 self.modelLoaded = true

        })
        downloadTask.resume()

Nslog:

location:Optional(file:///private/var/mobile/Containers/Data/Application/A1B996D7-ABE9-4000-91DB-2370076198D5/tmp/CFNetworkDownload_duDlwf.tmp)

new location:/var/mobile/Containers/Data/Application/A1B996D7-ABE9-4000-91DB-2370076198D5/Documents/mug.scn

.scn file downloading with the above mentioned(new location) file path.. but when i try to use this downloaded file in SCNScene

let scene =  SCNScene(named:"mug.scn", inDirectory: ls)

always scene value is nil. error

Thread 4: Fatal error: Unexpectedly found nil while unwrapping an Optional value

how to resolve this issues. Thank you


回答1:


About init?(named: String), the documentation says:

Loads a scene from a file with the specified name in the app’s main bundle

since you don't have such file inside the main bundle (is coming from a download), you may try with the following constructor:

init(url: URL, options: [SCNSceneSource.LoadingOption : Any]? = nil)

so your code might be:

do {
   let documents = "yourValidPath"
   let scene = try SCNScene(url: URL(fileURLWithPath: documents), options: nil)
} catch {}



回答2:


    let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    let pathToObject = documentDirectory + "ship.scn"
    let fileUrl = URL(fileURLWithPath: pathToObject)

    guard let cshipScene = try? SCNScene(url: fileUrl, options: nil) else { return }
    let shipNode = SCNNode()
    let shipSceneChildNodes = shipScene.rootNode.childNodes
    for childNode in shipSceneChildNodes {
        shipNode.addChildNode(childNode)
    }

    node.addChildNode(shipNode)


来源:https://stackoverflow.com/questions/48395068/arkit-loading-scn-file-from-web-server-url-in-scnscene

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!