1

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.

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

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
Steve
  • 31
  • 2

1 Answers1

0

In your case, there's no need to run ARWorldTrackingConfig or ARImageTrackingConfig.

Unlike ARKit, RealityKit automatically tracks all your AnchorEntities based on Target's cases (.object, .image, .plane, etc). Reality Composer has already created AnchorEntity(.image) for you, so ARImageTrackingConfig was automatically assigned to the session.

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.

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) { }
}
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220