RealityKit – 从 Web URL 资源加载 ModelEntity

问题描述 投票:0回答:2

我想知道是否有人知道是否可以加载 AR 对象(例如,从 Web url 加载 .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
                    }
                })

但它不起作用并抛出错误

Failed to open scene 'https://developer.apple.com/augmented-reality/quick-look/models/drummertoy/toy_drummer.usdz'.

如果可以的话你会吗?

swift augmented-reality realitykit quicklook usdz
2个回答
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()
    }
}

0
投票

您可以在此处使用帮助程序脚本 - 只需将其导入您的代码库即可:https://haloclinetech.com/learn

然后您可以这样加载实体:

let halocline = HaloclineHelper()
let url = URL(string: "your-halocline-url")!
let modelEntity = try await halocline.loadEntity(from: url)

注意:我隶属于Halocline。

© www.soinside.com 2019 - 2024. All rights reserved.