I'm using multiple unique marker anchors in a scene that each get a ModelEntity
displayed on them. I have no problem detecting each marker individually, but once one is tracked and the model appears, the others won't track. If the tracked marker moves out of frame then suddenly another marker will start being tracked.
My suspicion is that there exists a setting for the max number of markers and it's set to 1. (Like the maximumNumberOfTrackedImages
from SceneKit.) Is there a setting I'm missing here, is this a limitation of RealityKit, or am I just messing something up when I add my anchors to the scene?
I'm calling the following function for each item in an array:
class RealityViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let arView = ARView(frame: UIScreen.main.bounds)
view.addSubview(arView)
let targets = ["image1", "image2", "image3"]
for target in targets {
addTarget(target:target,arView:arView)
}
}
func addTarget(target: String, arView: ARView) {
let imageAnchor = AnchorEntity(.image(group: "Markers", name: target))
arView.scene.addAnchor(imageAnchor)
let plane = MeshResource.generatePlane(width: 0.05, height: 0.05, cornerRadius: 0.0)
let material = SimpleMaterial(color: .blue, roughness: 1.0, isMetallic: false)
let model = ModelEntity(mesh: plane, materials: [material])
imageAnchor.addChild(model)
}
}
Update: While @ARGeo's answer did solve the original question during further testing I found with the updated code I was only able to track a maximum of 4 targets at a time. Again I'm not sure this is a hard limit of RealityKit or what but if anyone has any insight please add to the accepted answer.
Below you can see only 4 of 6 unique markers being tracked:
There's no number of markers being tracked
property in ARKit and RealityKit.
So, to correct a situation, you need to use this code for adding anchors in ARView:
arView.scene.anchors.append(imageAnchor)
And you also might try this code for for-in
loop (because Xcode 11 beta might incorrectly run a loop):
for i in 0..<targets.count {
addTarget(target: targets[i], arView: arView)
}
P.S.
Look at this post. Now ARKit 5.0 has an ability to track more than 4 images at a time (at the moment up to 100 images simultaneously).