AR Persistence with ARKit & RealityKit: AR with iOS (Part-VII)

Shiru99
4 min readJun 22, 2023

AR persistence is a powerful feature that allows us to save and share augmented reality experiences in iOS applications. By leveraging ARKit and RealityKit, developers can create immersive AR content that persists across multiple sessions, enabling users to revisit their augmented experiences and share them with others. In this article, we will explore the concept of AR persistence, its benefits, and how to implement it in iOS apps. Discover the potential of AR persistence and unlock new possibilities for interactive and collaborative augmented reality.

https://iosexample.com/ar-persistence-with-arkit-and-realitykit/

AR Persistence

AR Persistence refers to the ability of an augmented reality (AR) application to save and retrieve virtual content in real-world locations across multiple sessions. It allows users to place virtual objects, annotations, or effects in the physical environment and have them remain in their designated positions when they return to the app later. This means that the virtual content becomes persistent and retains its spatial context, enabling users to interact with and share their augmented reality experiences over time. AR Persistence leverages technologies such as ARKit and RealityKit to accurately track and anchor virtual content in the real world, providing a seamless and continuous AR experience.

ARWorldMap

The ARWorldMap in ARKit represents a serialized representation of the AR session’s spatial mapping and anchor data. It captures the geometric and semantic information of the real-world environment, including detected surfaces, images, and the positions and orientations of ARAnchors.

The WorldMap is used for persistence, allowing you to save the state of an AR session and later load it to restore the previously mapped environment and placed ARAnchors. By saving and loading WorldMaps, you can preserve the spatial understanding of the scene and the positions of virtual objects, enabling a consistent AR experience across different sessions or devices.

While the WorldMap itself does not directly store the specifics of model entities or their properties, it provides the foundation for reconstructing the AR scene and can serve as a basis for loading and placing virtual content based on the stored anchor data.

In summary, the WorldMap is a crucial component in ARKit and RealityKit that allows for persistence by capturing the spatial mapping and anchor data of an AR session, facilitating the restoration of the environment and virtual content placement.

ARPersistence Package

import SwiftUI
import RealityKit
import ARKit
import ARPersistence


struct ARViewContainer: UIViewRepresentable {
@Binding var modelName: String?
@Binding var isResetOn: Bool
@Binding var isSaveOn: Bool
@Binding var loadSaved: Bool

func makeUIView(context: Context) -> ARView {
let arView = ARView(frame: UIScreen.main.bounds)
arView.debugOptions.insert([.showWorldOrigin])
return arView
}


func setPropertiesToModelEntity(_ uiView: ARView, _ modelEntity: ModelEntity) {
modelEntity.transform.translation.x = 0

modelEntity.generateCollisionShapes(recursive: false)
uiView.installGestures([.rotation, .translation, .scale], for: modelEntity)
}


func place3DModel(_ uiView: ARView) {

guard let modelName = modelName else {
return
}

// AR Anchor
let arAnchor = ARAnchor(name: modelName, transform: matrix_identity_float4x4)
uiView.session.add(anchor: arAnchor)


// Anchor Entitty
let anchorEntity = AnchorEntity(anchor: arAnchor)
anchorEntity.name = arAnchor.identifier.uuidString // MUST
uiView.scene.addAnchor(anchorEntity)


// Model Entity
let modelEntity: ModelEntity = try! ModelEntity.loadModel(named: modelName + ".usdz")
setPropertiesToModelEntity(uiView, modelEntity)
anchorEntity.addChild(modelEntity)
}


func updateUIView(_ uiView: ARView, context: Context) {

if(loadSaved){
uiView.scene.anchors.removeAll()
ARPersistence.shared.load(uiView)
loadSaved.toggle()
return
}

if(isSaveOn){
ARPersistence.shared.save(uiView)
isSaveOn.toggle()
return
}

if(isResetOn){
uiView.scene.anchors.removeAll()
isResetOn.toggle()
return
}

place3DModel(uiView)
}
}

In summary, the code allows users to store AR anchors and model entity transformations in .map and .json files using ARPersistence package, enabling them to retrieve and load the saved state in future sessions. This provides persistence and continuity to the AR experience, allowing users to resume their augmented reality environment with the previously placed 3D models.

Explanation

Code demonstrates an AR experience using SwiftUI, RealityKit, ARKit, and ARPersistence. It allows users to place 3D models in the AR scene, reset the scene, save the current state, and load a previously saved state.

The ARViewContainer struct represents the AR view and conforms to UIViewRepresentable. It sets up the ARView and handles the placement of 3D models, resetting the scene, saving the state, and loading a saved state using the ARPersistence library.

The saveState function saves the current AR anchors and model entity transformations by calling the save method from the ARPersistence library, which stores the data in .map and .json files.

The loadState function retrieves the previously saved AR anchors and model entity transformations by calling the load method from the ARPersistence library, which loads the data from the .map and .json files and applies it to the AR scene.

The updateUIView function is responsible for the main functionality. It checks the state of the isSaveOn, isResetOn, and loadSaved flags to determine whether to save the state, reset the scene, or load a saved state. If none of these flags are active, it calls the place3DModel function to place the selected 3D model in the AR scene.

More Details about packageARPersistence

--

--