微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – ARkit – 从SCNScene中的Web服务器URL加载.scn文件

我正在为我的应用程序使用ARKit,我尝试从我的web服务器(URL)动态加载.scn文件

这是我的代码的一部分

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文件下载与上面提到的(新位置)文件路径..但​​当我尝试在SCNScene中使用此下载文件

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

总是场景值为零.
错误

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

如何解决这个问题.谢谢

解决方法

关于init?(named:String),文档说:

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

因为您在主包中没有这样的文件(来自下载),您可以尝试使用以下构造函数

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

所以你的代码可能是:

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

原文地址:https://www.jb51.cc/iOS/331699.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐