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