How to Save and Load Arworldmap in Swiftui App

Persist object orientations in ARKit ARWorldMap

Apple Documentation for ARWorldMap shows that the properties for an ARWorldMap class are:
<code>anchors: [ARAnchor]</code>, <code>center: simd_float3</code>, and <code>extent: simd_float3</code>

When you archive a world map, these are the only information that get saved. Any information about the nodes added to the anchors during the session (e.g. changing node scale and orientation) are not saved along with the world map during the archiving.

I remember watching a WWDC session where they demoed a multiplayer AR game called SwiftShot where players hit different objects with balls. They provided the source code and I noticed they used a custom ARAnchor subclass called BoardAnchor which they used to store additional information in the anchor class such as the size of the game board.
See: SwiftShot: Creating a Game for Augmented Reality.

You can use the same approach to store, for example, the scale and orientation of a node, so that when you unarchive the world map and it get's relocalized, you can use ARSCNViewDelegate's renderer(_:didAdd:for:) to resize and scale the node based on the information stored in your custom ARAnchor.

Persistance with an ARWorldMap created with ARKit through Unity

Saving ARWorldMap is not a rocket science. If this feature is supported in ARKit extension for Unity, ARWorldMap will be saved in any AR app the same way as expected. The main difference is that Unity builds for iOS are written in slow Objective-C, not in faster Swift for UIKit, and not in the fastest Swift for SwiftUI. In iOS for storing ARWorldMap you must use NSKeyedArchiver, and for retrieving ARWorldMap data you must use NSKeyedUnarchiver.

func writeWorldMap(_ worldMap: ARWorldMap, to url: URL) throws {

let data = try NSKeyedArchiver.archivedData(withRootObject: worldMap,
requiringSecureCoding: true)
try data.write(to: url)
}

ARWorldMap stores world coordinate grid, position and orientation of your device, reference snapshots of a surrounding environment and ARAnchors. It's hard to say what you mean saying ...as effective as Azure... but ARWorldMap data is the most effective offline way to store and retrieve persistence data in iOS AR apps.



Related Topics



Leave a reply



Submit