gpt4 book ai didi

swift - 如何使用 ARKit 从远程服务器加载 usdz 模型和纹理?

转载 作者:行者123 更新时间:2023-12-04 17:24:07 24 4
gpt4 key购买 nike

如何使用 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!)

最佳答案

我找到了如下解决方案:

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

    }

关于swift - 如何使用 ARKit 从远程服务器加载 usdz 模型和纹理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64518888/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com