显示来自网络的现实文件时出错
Error in displaying reality file from network
我正在尝试显示使用 Reality Composer
创建的 reality
文件。以下代码适用于 usdz
但不适用于 reality
。这是我的代码
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)
}
}
}
//视图模型
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 }
文件确实在本地正确保存,但对于现实文件,它没有显示
这是 Xcode 日志
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
从网络上传 .reality
模型工作正常。您可以在 Xcode 模拟器中轻松检查:
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()
}
}
我正在尝试显示使用 Reality Composer
创建的 reality
文件。以下代码适用于 usdz
但不适用于 reality
。这是我的代码
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)
}
}
}
//视图模型
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 }
文件确实在本地正确保存,但对于现实文件,它没有显示 这是 Xcode 日志
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
从网络上传 .reality
模型工作正常。您可以在 Xcode 模拟器中轻松检查:
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()
}
}