gpt4 book ai didi

swift - 显示来自网络的现实文件时出错

转载 作者:行者123 更新时间:2023-12-04 17:16:53 24 4
gpt4 key购买 nike

我正在尝试显示 reality使用 Reality Composer 创建的文件.以下代码适用于 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)
}
}
}
// View 模型
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 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()
}
}

关于swift - 显示来自网络的现实文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68517968/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com