'Spawning model on image tracker with RealityKit

I'm trying to spawn a large 3D model on an image anchor with RealityKit

I want my model to spawn on an image (not track it) and then track the position using world tracking.

This is my code so far, but I'm stuck after watching all youtube tutorials I could find.

import UIKit
import RealityKit

class ViewController: UIViewController {
    
    @IBOutlet var arView: ARView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let box = try! Experience.loadBox()
        let anchor = AnchorEntity(.image(group: "AR Resources", name: "TrackerImage"))
        anchor.addChild(box)
        
        box.position = anchor.position
        arView.scene.anchors.append(anchor)
    }
}


Solution 1:[1]

Finally figured out a suitable solution for my situation. For those looking for the same solution, here is my code:

import UIKit
import RealityKit
import ARKit

class ViewController: UIViewController, ARSessionDelegate {
    @IBOutlet var arView: ARView!
    let boxAnchor = try! Experience.loadBox()
    var imageAnchorToEntity: [ARImageAnchor: AnchorEntity] = [:]
    
           
    override func viewDidLoad() {
        super.viewDidLoad()
        boxAnchor.generateCollisionShapes(recursive: true)
        let box = boxAnchor.steelBox as? Entity & HasCollision
        arView.installGestures(for: box!)
        arView.scene.addAnchor(boxAnchor)
        arView.session.delegate = self
    }
           
    func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
        anchors.compactMap { $0 as? ARImageAnchor }.forEach {
            let anchorEntity = AnchorEntity()
            let modelEntity = boxAnchor.steelBox!
                anchorEntity.addChild(modelEntity)
                arView.scene.addAnchor(anchorEntity)
                anchorEntity.transform.matrix = $0.transform
                imageAnchorToEntity[$0] = anchorEntity
            }
        }

        func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
            anchors.compactMap { $0 as? ARImageAnchor }.forEach {
                 let anchorEntity = imageAnchorToEntity[$0]
                     anchorEntity?.transform.matrix = $0.transform
            }
        }
        func installGestures(on object:ModelEntity){
            object.generateCollisionShapes(recursive: true)
            arView.installGestures([.rotation,.scale], for: object)
        }
    }
}

The function;

func installGestures(on object:ModelEntity) {
    object.generateCollisionShapes(recursive: true)
    arView.installGestures([.rotation,.scale], for: object)
}

and this section in the superViewDidLoad()

func installGestures(on object:ModelEntity) {
    object.generateCollisionShapes(recursive: true)
    arView.installGestures([.rotation,.scale], for: object)
}

Is for scaling, rotating and moving the entity, and can be removed if not needed.

The code above was modified from this source: https://developer.apple.com/forums/thread/126845

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tyler2P