I am using SceneKit and SwiftUI to save worldMap.
While loading the worldMap it took a long time to load worldMap anchor. while troubleshooting I found that first it loads the planes list and after that anchor list. below is the code
// WorldMapData
//
import UIKit
import ARKit
import Compression
class RouteViewController: UIViewController {
@IBOutlet weak var sceneView: ARSCNView!
private var session: ARSession?
var mapData = [String: Any]()
var viewModel = ViewModel()
var base64Url: String?
override func viewDidLoad() {
super.viewDidLoad()
sceneView.delegate = self
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
guard let myMapData = dict(toWorldMap: base64Url ?? "") else {return}
self.resetTrackingConfiguration(with: myMapData)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
sceneView.session.pause()
}
func resetTrackingConfiguration(with worldMap: ARWorldMap? = nil) {
let configuration = ARWorldTrackingConfiguration()
configuration.planeDetection = [.horizontal, .vertical]
let options: ARSession.RunOptions = [.resetTracking, .removeExistingAnchors]
if let worldMap = worldMap {
configuration.initialWorldMap = worldMap
for item in worldMap.anchors {
print("Anchor identifier: \(item.identifier)")
}
}
sceneView.debugOptions = [.showFeaturePoints]
sceneView.session.run(configuration, options: options)
}
}
extension RouteViewController: ARSCNViewDelegate {
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
DispatchQueue.main.async {
guard !(anchor is ARPlaneAnchor) else {
print("STEP 7: Error")
return
}
let sphereNode = generateSphereNode()
node.addChildNode(sphereNode)
}
}
func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor)
{
DispatchQueue.main.async {
guard !(anchor is ARPlaneAnchor) else {
print("STEP 8: Error")
return
}
print("STEP 8 Indetifier: \(anchor.identifier)")
let sphereNode = generateSphereNode()
node.addChildNode(sphereNode)
}
}
}
Please let me help how to sort out the load worldMap anchor issue. It would be good to have any worldMap example.
I would appreciate your help and ideas.