4

I am trying to display a reality file created using Reality Composer. The below code works for usdz but not for reality. Here is my code

struct ARViewContainer: UIViewRepresentable {
    
    var modelConfirmedForPlacement: AnchorEntity?
    
    func makeUIView(context: Context) -> ARView {
        
        let arView = ARView(frame: .zero,
                            cameraMode: .ar,automaticallyConfigureSession: true)
        
        let config = ARFaceTrackingConfiguration()
            
        arView.session.run(config)
        return arView            
    }
    
    func updateUIView(_ uiView: ARView, context: Context) {
        if let modelEntity = modelConfirmedForPlacement {
                      
           // Add the box anchor to the scene
            uiView.scene.anchors.append(modelEntity)   
        }
    }
}

//ViewModel

final class ContentViewModel: ObservableObject {
    
    @Published var modelConfirmedForPlacement: AnchorEntity?
    
    private var cancellable: AnyCancellable? = nil
    
    func updateModelEntity(path: String){
        let url = URL(fileURLWithPath: path)
        self.cancellable = AnchorEntity.loadAnchorAsync(contentsOf: url)
            .sink(receiveCompletion: { loadCompletion in
                if case let .failure(error) = loadCompletion {
                    print("Unable to load a model due to error \(error)")
                }
                self.cancellable?.cancel()
            }, receiveValue: { modelEntity in
                debugPrint("hereeeeeeeee")
                self.modelConfirmedForPlacement = modelEntity
            })
    }

func loadFileSync(url: URL, completion: @escaping (String?, Error?) -> Void)
    { 
//load from network and save locally }

The file does gets saved locally correctly but for reality files it is not displaying Here are the Xcode logs

File already exists [/var/mobile/Containers/Data/Application/880ED2EE-CCE7-4EE5-B854-0D1398650705/Documents/Experience.reality]
2021-07-25 16:29:09.060267+0530 RealityKitLoadModelFromNetwork[1352:474463] Metal GPU Frame Capture Enabled
2021-07-25 16:29:09.060478+0530 RealityKitLoadModelFromNetwork[1352:474463] Metal API Validation Enabled

1 Answer 1

2

Uploading a .reality model from web works fine. You can easily check this in Xcode Simulator:

import UIKit
import RealityKit

class ViewController: UIViewController {
    
    @IBOutlet var arView: ARView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let url = URL(string: "https://developer.apple.com/augmented-reality/quick-look/models/cosmonaut/CosmonautSuit_en.reality")
        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.loadAnchor(contentsOf: destination)
                    self.arView.scene.addAnchor(model)
                } catch {
                    print("Fail loading entity.")
                }
            }
        })
        downloadTask.resume()
    }
}
4
  • 1
    Thanks It works. But when I try to use combine it does not work. My combine code is as follows self.cancellable = AnchorEntity.loadAnchorAsync(contentsOf: url) .sink(receiveCompletion: { loadCompletion in..... but when I directy use do { let model = try Entity.loadAnchor(contentsOf: destination) self.arView.scene.addAnchor(model) } catch { print("Fail loading entity.") } It works then do you know what could be the reason? Jul 27, 2021 at 3:43
  • Yep, this results in an error when implementing combine...
    – Andy Jazz
    Jul 27, 2021 at 10:22
  • 1
    Do you know what could be the issue. Is this just because it is a reality file and not an usdz file. Is this bug from Apple's side? Jul 27, 2021 at 15:01
  • More than a year past, Dec2022 and it seems I have just run into the same bug... unable to load a reality file using combine. Dec 10, 2022 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.