【问题标题】:How to load a usdz model and textures from a remote server using ARKit?如何使用 ARKit 从远程服务器加载 usdz 模型和纹理?
【发布时间】:2021-02-07 15:12:17
【问题描述】:

如何使用 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", 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 初始化时捕获错误?它可能会让你知道出了什么问题

标签: swift augmented-reality arkit usdz


【解决方案1】:

我找到了如下解决方案:

  1. 下载模型并保存

    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)")
        }
      }
    }
    
  2. 加载 usdz 模型和纹理

    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!)
    
    }
    

【讨论】:

    猜你喜欢
    • 2019-09-26
    • 2018-07-16
    • 2019-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多