简体   繁体   English

Reality Composer 未正确导入 RealityKit

[英]Reality Composer not importing correctly into RealityKit

I'm trying to anchor a 3d model onto a real-world image, in my case a QR code... It works fine within Reality Composer, however, I attempted to add it into my RealityKit project and nothing is showing up in my ARView... Nothing relevant is printed in the console.我正在尝试将 3d 模型锚定到真实世界的图像上,在我的例子中是 QR 码......它在 Reality Composer 中运行良好,但是,我试图将它添加到我的 RealityKit 项目中,但我的ARView... 控制台中没有打印任何相关内容。

Here is my code:这是我的代码:

struct MainARView: UIViewRepresentable {

    func makeUIView(context: Context) -> ARView {

        let view = ARView()
        let session = view.session
        let config = ARWorldTrackingConfiguration()      
        session.run(config)

        view.debugOptions = [.showFeaturePoints, 
                             .showAnchorOrigins, 
                             .showAnchorGeometry]
           
        guard let anchor = try? TestObj.loadScene() else {
            print("Error loading TestObj")
            return view     
        }       
        view.scene.addAnchor(anchor)
        return view
    }
    func updateUIView(_ view: ARView, context: Context) { }
}

Any help would be much appreciated任何帮助将非常感激

In your case, there's no need to run ARWorldTrackingConfig or ARImageTrackingConfig .在您的情况下,无需运行ARWorldTrackingConfigARImageTrackingConfig

Unlike ARKit , RealityKit automatically tracks all your AnchorEntities based on Target's cases ( .object , .image , .plane , etc). 与 ARKit 不同,RealityKit 会根据 Target 的案例( .object.image.plane等)自动跟踪您的所有 AnchorEntities。 Reality Composer has already created AnchorEntity(.image) for you, so ARImageTrackingConfig was automatically assigned to the session. Reality Composer 已经为您创建了AnchorEntity(.image) ,因此ARImageTrackingConfig会自动分配给会话。

import SwiftUI
import RealityKit

struct ARViewContainer: UIViewRepresentable {

    func makeUIView(context: Context) -> ARView {

        let arView = ARView(frame: .zero)
        let qrCodeModel = try! Experience.loadModel()
        arView.scene.anchors.append(qrCodeModel)
        return arView
    }
    func updateUIView(_ uiView: ARView, context: Context) { }
}

In addition to the above, here's a way to create the same scenario in RealityKit from scratch.除了上述之外,这里还有一种在 RealityKit 中从头开始创建相同场景的方法。

struct ARViewContainer: UIViewRepresentable {
    
    func makeUIView(context: Context) -> ARView {
        
        let arView = ARView(frame: .zero)
        let entity = ModelEntity(mesh: .generateBox(size: 0.125))
        let anchor = AnchorEntity(.image(group: "AR Resources", 
                                          name: "QRCode.png"))
        entity.setParent(anchor)
        arView.scene.anchors.append(anchor)
        return arView
    }
    func updateUIView(_ uiView: ARView, context: Context) { }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM