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