Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
604 views
in Technique[技术] by (71.8m points)

swift - RealityKit – Load ModelEntity from web URL resource

I was wondering if someone know is it possible to load an AR object (.usdz for example from web url and place it in the AR view). I tried with this:

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

but it is not working and throwing error Failed to open scene 'https://developer.apple.com/augmented-reality/quick-look/models/drummertoy/toy_drummer.usdz'.

Do you if it is possible?

question from:https://stackoverflow.com/questions/65936194/realitykit-load-modelentity-from-web-url-resource

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...