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.