[SOLVED] What's the difference between ARSession delegate methods?

Issue

I am in the process of making a Swift app that currently places some simple shapes onto a reference image using an ARImageAnchor and an ARReferenceImage. Right now, all of the augmented content is being placed within the session(_:didAdd:) function, however I know there is also a session(_:didUpdate:) function. The didAdd version makes sense, since it just runs when an anchor is added. But the Apple documentation is a bit vague about the exact purpose of this second version:

func session(ARSession, didUpdate: [ARAnchor])

// Tells the delegate that the session has adjusted 
// the properties of one or more anchors.

Does this mean any time the anchors move within the scene this function will run? Or is "properties" referring to something other than just the position of the anchors? I am just a bit confused what this second function would be used for.

Solution

Foreword

I agree with you that Apple’s documentation is often unclear.

ARKit and/or RealityKit apps are built on a running ARSession object that is a key AR element based on a specific configuration. Each configuration type in ARKit allows you to generate specific ARAnchors (ARPlaneAnchor, ARImageAnchor, ARFaceAnchor, etc). In ARKit, we must use an explicit (manual) configuration, while in RealityKit, a configuration is automatic, due to the fact it’s set according to the AnchorEntity type (.plane, .image, .face, etc).

ARKit anchors can be tracked by implementing the session(…) or renderer(…) delegate methods. ARKit anchors may be accessed through each ARFrame (60 frames per second).

arView.session.currentFrame?.anchors

RealityKit anchors are automatically (implicitly) tracked by the application. RealityKit anchors’ collection can be accessed through the Scene object.

arView.scene.anchors

The ARKit and RealityKit frameworks can work together or separately from each other. RealityKit’s AnchorEntity is capable of using ARKit’s ARAnchor transform to place a model into a scene:

self.anchorEntity = AnchorEntity(anchor: arAnchor)

Use case

ARSessionDelegate protocol has 4 optional session(...) methods that you can use with both frameworks. The first pair of session(...) methods are used quite often. session(_:didAdd:) instance method is used to add/extract specific anchors to/from an ARSession under certain conditions. However, session(_:didUpdate:) instance method allows you to update the content, based on a specific ARAnchor’s data. It can be accomplished this way:

func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
    
    guard let faceAnchor = anchors.first as? ARFaceAnchor
    else { return }
                    
    self.geometry.update(from: faceAnchor.geometry)
    self.facialExpression(anchor: faceAnchor)
}

THIS POST shows you how to use both methods together.

And read this post to find out how to use session(_:didUpdate:) method alone.

Answered By – Andy Jazz

Answer Checked By – Jay B. (BugsFixing Admin)

Leave a Reply

Your email address will not be published.