【问题标题】:RealityKit – Loading Reality Composer scenes with SwiftUIRealityKit - 使用 SwiftUI 加载 Reality Composer 场景
【发布时间】:2020-08-17 14:10:11
【问题描述】:

我正在尝试使用 SwiftUI、RealityKit 和 ARKit 在面部加载不同的模型。

struct AugmentedRealityView: UIViewRepresentable {

    @Binding var modelName: String

    func makeUIView(context: Context) -> ARView {
    
        let arView = ARView(frame: .zero)
    
        let configuration = ARFaceTrackingConfiguration()

        arView.session.run(configuration, options: [.removeExistingAnchors, 
                                                    .resetTracking])
    
        loadModel(name: modelName, arView: arView)
    
        return arView
    
    }

    func updateUIView(_ uiView: ARView, context: Context) { }

    private func loadModel(name: String, arView: ARView) {

        var cancellable: AnyCancellable? = nil
    
        cancellable = ModelEntity.loadAsync(named: name).sink(
                 receiveCompletion: { loadCompletion in
            
            if case let .failure(error) = loadCompletion {
                print("Unable to load model: \(error.localizedDescription)")
            }                
            cancellable?.cancel()
        },
        receiveValue: { model in
            
            let faceAnchor = AnchorEntity(.face)
            arView.scene.addAnchor(faceAnchor)
            
            faceAnchor.addChild(model)
            
            model.scale = [1, 1, 1]
        })
    }
}

这是我加载它们的方式,但是当相机视图打开并加载一个模型时,其他模型将不会被加载。有人可以帮帮我吗?

【问题讨论】:

标签: swiftui augmented-reality arkit realitykit reality-composer


【解决方案1】:

当您的 Binding 的值发生变化时,SwiftUI 正在调用您的 updateUIView(_:,context:) 实现,这并没有说明。

此外,您没有存储AnyCancellable。当sink 返回的令牌被释放时,请求将被取消。在尝试加载更大的模型时,这可能会导致意外失败。

要解决这两个问题,请使用Coordinator

import UIKit
import RealityKit
import SwiftUI
import Combine
import ARKit

struct AugmentedRealityView: UIViewRepresentable {
    class Coordinator {
        private var token: AnyCancellable?
        private var currentModelName: String?
        
        fileprivate func loadModel(_ name: String, into arView: ARView) {
            // Only load model if the name is different from the previous one
            guard name != currentModelName else {
                return
            }
            currentModelName = name
            
            // This is optional
            // When the token gets overwritten
            // the request gets cancelled
            // automatically
            token?.cancel()
            
            token = ModelEntity.loadAsync(named: name).sink(
                receiveCompletion: { loadCompletion in
                    
                    if case let .failure(error) = loadCompletion {
                        print("Unable to load model: \(error.localizedDescription)")
                    }
                },
                receiveValue: { model in
                    
                    let faceAnchor = AnchorEntity(.camera)
                    arView.scene.addAnchor(faceAnchor)
                    
                    faceAnchor.addChild(model)
                    
                    model.scale = [1, 1, 1]
                })
            }
        
        fileprivate func cancelRequest() {
            token?.cancel()
        }
    }
    
    @Binding var modelName: String
    
    func makeCoordinator() -> Coordinator {
        Coordinator()
    }
    
    static func dismantleUIView(_ uiView: ARView, coordinator: Coordinator) {
        coordinator.cancelRequest()
    }
    
    func makeUIView(context: Context) -> ARView {
        
        let arView = ARView(frame: .zero)
        
        let configuration = ARFaceTrackingConfiguration()
        
        arView.session.run(configuration, options: [.removeExistingAnchors,
                                                    .resetTracking])
        
        context.coordinator.loadModel(modelName, into: arView)
        
        return arView
        
    }
    
    func updateUIView(_ uiView: ARView, context: Context) {
        context.coordinator.loadModel(modelName, into: uiView)
    }
}

我们创建一个嵌套的Coordinator 类,该类包含AnyCancellable 令牌并将loadModel 函数移动到Coordinator。 除了 SwiftUI ViewCoordinator 是一个 class,它会在您的视图可见时存在(请记住,SwiftUI 可能会随意创建和销毁您的 View,它的生命周期与实际的“视图”,显示在屏幕上)。

loadModel 类中,我们仔细检查Binding 的值是否确实发生了变化,这样当SwiftUI 更新我们的View 时,我们就不会取消对同一模型的持续请求,例如因为环境的变化。

然后我们实现makeCoordinator 函数来构造我们的Coordinator 对象之一。 在makeUIViewupdateUIView 中,我们在Coordinator 上调用loadModel 函数。

dimantleUIView 方法是可选的。当Coordinator 被解构时,我们的token 也被释放,这将触发Combine 取消正在进行的请求。

【讨论】:

    猜你喜欢
    • 2020-10-30
    • 1970-01-01
    • 2020-01-18
    • 2019-10-25
    • 2023-03-26
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多