Swift 如何使用ARKit从远程服务器加载usdz模型和纹理?
如何使用ARKit从远程服务器加载Swift 如何使用ARKit从远程服务器加载usdz模型和纹理?,swift,augmented-reality,arkit,usdz,Swift,Augmented Reality,Arkit,Usdz,如何使用ARKit从远程服务器加载usdz模型和纹理 如下例所示: let myURL = NSURL(string: "https://mywebsite.com/vase.scn") guard let scene = try? SCNScene(url: myURL! as URL, options: nil) else { return } let node = scene.rootNode.childNode(withName: "vase&q
usdz
模型和纹理
如下例所示:
let myURL = NSURL(string: "https://mywebsite.com/vase.scn")
guard let scene = try? SCNScene(url: myURL! as URL, options: nil) else {
return
}
let node = scene.rootNode.childNode(withName: "vase", recursively: true)
let transform = queryResult.worldTransform
let thirdColumn = transform.columns.3
node!.position = SCNVector3(thirdColumn.x, thirdColumn.y, thirdColumn.z)
self.sceneView.scene.rootNode.addChildNode(node!)
我找到了如下解决方案:
func downloadModel() {
guard let url = URL(url: originalURL) else { return }
let urlSession = URLSession(configuration: .default, delegate: self, delegateQueue: OperationQueue())
let downloadTask = urlSession.downloadTask(with: url)
downloadTask.resume()
}
extension ARViewController: URLSessionDownloadDelegate {
public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) { print("locationUrl:", location.path)
// create destination URL with the original file name
guard let url = downloadTask.originalRequest?.url else { return }
let documentsPath = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let destinationURL = documentsPath.appendingPathComponent(url.lastPathComponent)
// delete original copy
try? FileManager.default.removeItem(at: destinationURL)
// copy from temp to Document
do {
try FileManager.default.copyItem(at: location, to: destinationURL)
self.originalURL = destinationURL
print("originalURL:", originalURL!.path)
} catch let error {
print("Copy Error: \(error.localizedDescription)")
}
}
}
func addItem(queryResult: ARRaycastResult) {
let url = URL(string: originalURL!.path)
let mdlAsset = MDLAsset(url: url!)
mdlAsset.loadTextures()
let scene = SCNScene(mdlAsset: mdlAsset)
let node = scene.rootNode.childNode(withName: "model", recursively: true)
let transform = queryResult.worldTransform
let thirdColumn = transform.columns.3
node!.position = SCNVector3(thirdColumn.x, thirdColumn.y, thirdColumn.z)
self.sceneView.scene.rootNode.addChildNode(node!)
}
您是否尝试在SCNScene init上捕获错误?它可能会给你一个错误的线索