How can I add GARAnchor to RealityKit’s scene?

I'm using the newest Geospatial API from ARCore, and trying to built it with SwiftUI and RealityKit. I have all SDK and api key set up properly, all coordinates and accuracy info are updated every frame properly. But Whenever I use GARSession.creatAnchor method, it returns a GARAnchor. I used GARAnchor's transform property to create an ARAnchor(transform: GARAnchor.transform), then I created AnchorEntity with this ARAnchor, and add AnchorEntity to ARView.scene. However, the model never showed up. I have checked coordinates, altitude, still no luck at all. So is there anyone could help me out? Thank you so much.

do {
      let garAnchor =  try parent.garSession.createAnchor(coordinate: CLLocationCoordinate2D(latitude: xx.xxxxxxxxx, longitude: xx.xxxxxxx), altitude: xx, eastUpSouthQAnchor: simd_quatf(ix: 0, iy: 0, iz: 0, r: 0))
      if garAnchor.hasValidTransform && garAnchor.trackingState == .tracking {
                    let arAnchor = ARAnchor(transform: garAnchor.transform)
                    let anchorEntity = AnchorEntity(anchor: arAnchor)
                    let mesh = MeshResource.generateSphere(radius: 2)
                    let material = SimpleMaterial(color: .red, isMetallic: true)
                    let sephere = ModelEntity(mesh: mesh, materials: [material])
                    anchorEntity.addChild(sephere)
                    parent.arView.scene.addAnchor(anchorEntity)
                    print("Anchor has valid transform, and anchor is tracking")
     } else {
                    print("Anchor has invalid transform")
                }
            } catch {
                print("Add garAnchor failed: \(error.localizedDescription)")
            }
}

Hello,

I'd recommend that you add some additional logging to debug this.

Log the transform of the anchor itself to make sure it is roughly where you'd expect it to be.

Log the visual bounds of an entity in your scene with the visualBounds(recursive:relativeTo:excludeInactive:) method to get an approximation of an entity’s size, which can help you debug situations where that size is either very small or very large. ARKit's units are in meters, so if an entity has a bounding box which is very small it will be difficult to tell if the entity was actually placed in your scene or not. The same applies if the entity is very large, because it may be so large that, from your current viewpoint, you are actually inside the model and so you cannot see it.

Additionally, if you are on a LiDAR device, I'd recommend temporarily disabling scene occlusion for debugging, just to make sure that your object isn't hiding out behind a wall :)

Hi Franklan010,

Any progress on this matter? I'm stuck on the very same exact point. Well, my problem is slightly different: I create the garAnchor with (the newly added) createAnchorOnTerrain; the anchor is created, but it seems to have an invalid transform.

Id be very greatful if you can help me on this!

Thanks in advance!

Well I think I found the piece we were missing. You have to make the session aware of the newly created anchor, like this:

arView.session.add(anchor: arAnchor)

Well I think I found the piece we were missing. You have to make the session aware of the newly created anchor, like this:

 arView.session.add(anchor: arAnchor)

I hope this solves your problem

So the full code:

func place(_ modelEntity: ModelEntity, in arView: CustomARView, for anchor: GARAnchor) {

        let clonedEntity = modelEntity.clone(recursive: true)

        clonedEntity.generateCollisionShapes(recursive: true)

        print("Size:  \(clonedEntity.visualBounds(relativeTo: .none))")

        if(anchor.hasValidTransform) {

            let arAnchor = ARAnchor(transform: anchor.transform)

            arView.session.add(anchor: arAnchor)

            let anchorEntity = AnchorEntity(anchor: arAnchor)

            anchorEntity.addChild(clonedEntity)

            arView.scene.addAnchor(anchorEntity)

            

            print("Anchor place for: \(anchor.transform)")

        } else {

            print("Not valid transform")

        }

       

    }
How can I add GARAnchor to RealityKit’s scene?
 
 
Q