RealityKit - 从网络 URL 资源加载 ModelEntity

5
我想知道是否有可能从网址加载AR对象(例如.usdz),并将其放置在AR视图中。我尝试了以下代码:
let fileUrl = NSURL(string: "https://developer.apple.com/augmented-reality/quick-look/models/drummertoy/toy_drummer.usdz")
cancellable = Entity.loadModelAsync(contentsOf: fileUrl! as URL)
                .sink(receiveCompletion: { completion in
                    self.cancellable?.cancel()
                }, receiveValue: { [self] (model: Entity) in
                    if let model = model as? ModelEntity {
                        let anchorEntity = AnchorEntity(anchor: anchor)
                        anchorEntity.addChild(model)

                        arView.scene.addAnchor(anchorEntity)
                        loadingView.isHidden = true
                    }
                })

但是它没有起作用并且抛出错误 无法打开场景'https://developer.apple.com/augmented-reality/quick-look/models/drummertoy/toy_drummer.usdz'。

你知道这是否可能吗?


代码在那里,你能看到吗? - Martin Kostadinov
loadModelAsync 是一个苹果方法。 - Martin Kostadinov
哦,好的,明白了,抱歉。我已经为你的问题添加了正确的标签。 - Eric Aya
抱歉,是我的错。 - Martin Kostadinov
您可以在此类似问题中找到用处:这里 - Zach
1个回答

5

试试这个:

import UIKit
import RealityKit

class ViewController: UIViewController {
    
    @IBOutlet var arView: ARView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        arView.environment.background = .color(.black)

        let url = URL(string: "https://developer.apple.com/augmented-reality/quick-look/models/drummertoy/toy_drummer.usdz")
        let documents = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
        let destination = documents.appendingPathComponent(url!.lastPathComponent)
        let session = URLSession(configuration: .default,
                                      delegate: nil,
                                 delegateQueue: nil)
        
        var request = URLRequest(url: url!)
        request.httpMethod = "GET"
        
        let downloadTask = session.downloadTask(with: request, completionHandler: { (location: URL?,
                                  response: URLResponse?,
                                     error: Error?) -> Void in
            
            let fileManager = FileManager.default
                
            if fileManager.fileExists(atPath: destination.path) {
                try! fileManager.removeItem(atPath: destination.path)
            }
            try! fileManager.moveItem(atPath: location!.path,
                                      toPath: destination.path)
                
            DispatchQueue.main.async {
                do {
                    let model = try Entity.load(contentsOf: destination)
                    let anchor = AnchorEntity(world: [0,-0.2,0])
                        anchor.addChild(model)
                    anchor.scale = [5,5,5]                        
                    self.arView.scene.addAnchor(anchor)
                    
                    model.playAnimation(model.availableAnimations.first!.repeat())
                } catch {
                    print("Fail loading entity.")
                }
            }
        })
        downloadTask.resume()
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接