ARkit - 从 SCNScene 中的 Web 服务器 URL 加载 .scn 文件 [英] ARkit - Loading .scn file from Web-Server URL in SCNScene
问题描述
我正在为我的应用程序使用 ARKit,并尝试从我的网络服务器(URL)动态加载 .scn 文件
I am using ARKit for my application and I try to dynamically load .scn files from my web-server(URL)
这是我的代码的一部分
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()
日志:
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
.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)
场景值总是nil
.错误
线程 4:致命错误:在展开可选值时意外发现 nil
Thread 4: Fatal error: Unexpectedly found nil while unwrapping an Optional value
如何解决这个问题.谢谢
how to resolve this issues. Thank you
推荐答案
关于init?(named: String)
,文档说:
从应用主包中具有指定名称的文件中加载场景
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)
所以你的代码可能是:
do {
let documents = "yourValidPath"
let scene = try SCNScene(url: URL(fileURLWithPath: documents), options: nil)
} catch {}
这篇关于ARkit - 从 SCNScene 中的 Web 服务器 URL 加载 .scn 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!