Get attached scene from ARImageAnchor

I've created a scene with RealityComposer (tracking an image, not plane) an placed a cube on top.


Then I've imported the scene into my ARView. (my Anchor is named "MainScene")...


Now I'm loading the scene an append it to ARView's session:



myAnchor = try? AR.loadMainScene()

arView.scene.anchors.append(myAnchor!)


Now everything works fine !


I'm trying to enable / disable the attached scene if tracking is successfull or not:


func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {

for anch in anchors

{

guard let imageAnchor = anch as? ARImageAnchor else { return }


if(imageAnchor.isTracked)

{

//enable scene

}

else

{

//disable scene

}

}

}


How to do it ?

Accepted Reply

Hello,


You pretty much have it all there already, you could do this:


if(imageAnchor.isTracked)
     {
         myAnchor.isEnabled = true           //enable scene
     }
     else
     {
         myAnchor.isEnabled = false           //disable scene
     }



Setting isEnabled to false on an Entity has the effect of also setting isActive to false on the entity, Inactive entities are not renderered or simulated by RealityKit.

Replies

Hello,


You pretty much have it all there already, you could do this:


if(imageAnchor.isTracked)
     {
         myAnchor.isEnabled = true           //enable scene
     }
     else
     {
         myAnchor.isEnabled = false           //disable scene
     }



Setting isEnabled to false on an Entity has the effect of also setting isActive to false on the entity, Inactive entities are not renderered or simulated by RealityKit.

Thank you - that helped me (so far)


BUT:

if I have more than one anchors (appended to ARView.session.anchors), how can I distinguish which updated ARAnchor( ARImageAnchor) belongs to my added anchors ?


In a different question (ARKit) you mentioned using the name of the referenceImage. To get this value from the (updated) ARImageAnchor ist pretty easy ... but how get the correspondig value from the (imported) RealityKit-scene (.reality file)?


Best regards

Entities which conform to the HasAnchoring protocol have a property called anchorIdentifier.


ARAnchors are each assigned a unique (UUID) identifier when they are created.


If an Entity's anchorIdentifier matches an ARAnchor's identifier, then you know that that particular Entity is anchored to that particular ARAnchor.

Thanks ... tried this - but they are always different 😕 (and never matches)


class ViewController: UIViewController, ARSessionDelegate {

    @IBOutlet weak var arView:RealityKit.ARView!
    
    var myAnchor:AR.MainScene!              // from AR.rcproject

    override func viewDidLoad() {
        super.viewDidLoad()
       
        arView.session.delegate = self
        
        myAnchor = try? AR.loadMainScene()  // from AR.rcproject

        arView.scene.anchors.append(myAnchor!)
        
    }


    func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {

        for anch in anchors
        {
            
            guard let imageAnchor = anch as? ARImageAnchor else { return }

            if(imageAnchor.isTracked)
            {

                if(myAnchor.anchorIdentifier == imageAnchor.identifier)
                {
                    debugPrint("GOT IT !")        // <-- THIS NEVER HAPPENS
                }
                
                myAnchor?.isEnabled = true        // works

            }
            else
            {
                myAnchor?.isEnabled = false       // also works
            }
            
            
        }
    }
}

Hey there,


Your myAnchor is an rcproject. Currently, you are checking if the anchorIdentifier of your scene matches, but that is not how rcprojects are structured. Instead, you should be checking if the child entities in your mainAnchor have an anchorIdentifier that matches your ARAnchor.

Thank you - this helped me !


But (so far) I cannot import more than one rcproject or a rcproject with more than one scene ... but this should be possible in one of the next releases (or betas).

Hey Dr,


Does your code actually load something from Reality Composer? I have things made in RC but cannot import them into an AR project. As there is no sample project from the WWDC that I can find, I am experimenting but not having any luck. When I try your code it throws errors. I'd appreciate any advice you can give.


Pete