如何在SwiftUI上异步加载Reality Composer场景?

huangapple go评论145阅读模式
英文:

How to load Reality Composer scenes asynchronously on SwiftUI?

问题

在我的项目中,我像这样同步加载现实文件:

  1. struct DemoView: View {
  2. @State private var arView = ARView(frame: .zero)
  3. @State private var Scene1 = try! Experience1.loadScene1()
  4. var body: some View {
  5. ZStack {
  6. ARViewContainer(arView: $arView, Scene1: $Scene1)
  7. .ignoresSafeArea()
  8. Button("play") {
  9. Scene1.notifications.replay.post()
  10. }
  11. }
  12. }
  13. }
  14. struct ARViewContainer: UIViewRepresentable {
  15. @Binding var arView: ARView
  16. @Binding var Scene1: Experience1.Scene1
  17. func makeUIView(context: Context) -> ARView {
  18. DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
  19. arView.scene.anchors.append(Scene1)
  20. }
  21. return ARView
  22. }
  23. }

由于我的现实场景中的模型具有大量多边形,我可以尝试以异步方式加载现实文件吗?

英文:

On my project, I load reality files synchronously like this:

  1. struct: DemoView: View {
  2. @State private var arView = ARView(frame: .zero)
  3. @State private var Scene1 = try! Experience1.loadScene1()
  4. var body: some View {
  5. ZStack {
  6. ARViewContainer(arView: $arView, Scene1: $Scene1)
  7. .ignoresSafeArea()
  8. Button("play") {
  9. Scene1.notifications.replay.post()
  10. }
  11. }
  12. }
  13. }
  14. struct ARViewContainer: UIViewRepresentable {
  15. @Binding var arView: ARView
  16. @Binding var Scene1: Experience1.Scene1
  17. func makeUIView(context: Context) -> ARView {
  18. DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
  19. arView.scene.anchors.append(Scene1)
  20. }
  21. return ARView
  22. }
  23. }

Since my model in Reality Scene has a lot of polygons, can I turn to load reality files asynchronously?

答案1

得分: 2

以下是代码的中文翻译部分:

用于异步加载 Reality Composer 文件的方法

当您需要异步加载 Reality Composer 场景时,请尝试以下代码:

  1. import SwiftUI
  2. import RealityKit
  3. struct ContentView : View {
  4. var body: some View {
  5. ARViewContainer().ignoresSafeArea()
  6. }
  7. }

  1. struct ARViewContainer : UIViewRepresentable {
  2. let arView = ARView(frame: .zero)
  3. let anchor = AnchorEntity()
  4. func makeUIView(context: Context) -> ARView {
  5. Experience.loadBoxAsync(completion: { result in
  6. do {
  7. let heavyBoxScene = try result.get()
  8. arView.scene.anchors.append(heavyBoxScene)
  9. } catch {
  10. print("错误: \(error.localizedDescription)")
  11. }
  12. })
  13. return arView
  14. }
  15. func updateUIView(_ vue: ARView, context: Context) { }
  16. }

用于异步加载 USDZ 文件的方法

当您需要异步加载 .usdz 模型时,请尝试以下代码:

  1. import SwiftUI
  2. import RealityKit
  3. import Combine
  4. struct ARViewContainer : UIViewRepresentable {
  5. @State var cancellable: AnyCancellable? = nil
  6. let anchor = AnchorEntity()
  7. let arView = ARView(frame: .zero)
  8. func makeUIView(context: Context) -> ARView {
  9. DispatchQueue.main.async {
  10. cancellable = Entity.loadModelAsync(named: "character.usdz").sink(
  11. receiveCompletion: { completion in
  12. if case let .failure(error) = completion {
  13. print("由于 \(error) 无法加载模型")
  14. }
  15. self.cancellable?.cancel()
  16. }, receiveValue: { [self] (model: Entity) in
  17. anchor.addChild(model)
  18. anchor.position.z = -1.0
  19. arView.scene.anchors.append(anchor)
  20. })
  21. }
  22. return arView
  23. }
  24. func updateUIView(_ vue: ARView, context: Context) { }
  25. }

附言

回答您的下一个问题

英文:

Async loading method for Reality Composer files

Try the following code when you need to load a Reality Composer scene asynchronously:

  1. import SwiftUI
  2. import RealityKit
  3. struct ContentView : View {
  4. var body: some View {
  5. ARViewContainer().ignoresSafeArea()
  6. }
  7. }

  1. struct ARViewContainer : UIViewRepresentable {
  2. let arView = ARView(frame: .zero)
  3. let anchor = AnchorEntity()
  4. func makeUIView(context: Context) -> ARView {
  5. Experience.loadBoxAsync(completion: { result in
  6. do {
  7. let heavyBoxScene = try result.get()
  8. arView.scene.anchors.append(heavyBoxScene)
  9. } catch {
  10. print("Error: \(error.localizedDescription)")
  11. }
  12. })
  13. return arView
  14. }
  15. func updateUIView(_ vue: ARView, context: Context) { }
  16. }

Async loading method for USDZ files

Try the following code when you need to load .usdz model asynchronously:

  1. import SwiftUI
  2. import RealityKit
  3. import Combine
  4. struct ARViewContainer : UIViewRepresentable {
  5. @State var cancellable: AnyCancellable? = nil
  6. let anchor = AnchorEntity()
  7. let arView = ARView(frame: .zero)
  8. func makeUIView(context: Context) -> ARView {
  9. DispatchQueue.main.async {
  10. cancellable = Entity.loadModelAsync(named: "character.usdz").sink(
  11. receiveCompletion: { completion in
  12. if case let .failure(error) = completion {
  13. print("Unable to load a model due to \(error)")
  14. }
  15. self.cancellable?.cancel()
  16. }, receiveValue: { [self] (model: Entity) in
  17. anchor.addChild(model)
  18. anchor.position.z = -1.0
  19. arView.scene.anchors.append(anchor)
  20. })
  21. }
  22. return arView
  23. }
  24. func updateUIView(_ vue: ARView, context: Context) { }
  25. }

P. S.

Answer to your next question.

huangapple
  • 本文由 发表于 2023年3月8日 18:18:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75671780.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定