首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RealityKit -从web资源加载ModelEntity

RealityKit -从web资源加载ModelEntity
EN

Stack Overflow用户
提问于 2021-01-28 19:26:23
回答 1查看 327关注 0票数 2

我想知道是否有人知道可以加载AR对象(例如,从web加载.usdz并将其放在AR视图中)。我试着这样做:

代码语言:javascript
复制
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'.

如果可能的话,你会这样做吗?

EN

回答 1

Stack Overflow用户

发布于 2021-01-29 03:15:47

试试这个:

代码语言:javascript
复制
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()
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65936194

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档