Home > front end >  How to change the reference anchor Image dynamically in RealityKit
How to change the reference anchor Image dynamically in RealityKit

Time:11-03

I want to use an image load from the network as an anchor to attach my object in the real world.

However, I found that reality kit only provides using the image in the asset folder as an anchor, which seems can't be added by code.

AnchorEntity(.image(group: "", name: ""))

So is there a possible way for ARKit to do such things?

CodePudding user response:

I found a way to load ARreferenceImage in the code:

//create a CIImage
let image = UIImage(named: "refimage.jpg",
                                        in: Bundle(for: type(of:self)),
                                        compatibleWith: nil)
        let ciimage = CIImage(image: image!)
        let cgimage = convertCIImageToCGImage(inputImage: ciimage!)!
        let arReference = ARReferenceImage(cgimage, orientation: .up, physicalWidth: 0.05)
//add this image into ARReferenceImage<Set>
        refImage.insert(arReference)
//add this set to the ARView Tracking Config
        let config = ARImageTrackingConfiguration()
        config.trackingImages = refImage
//run the config
        arView.session.run(config)

Then you can get notified when the system detects the image in your ARSessionDelegate

 func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
        for i in anchors{
            if let a = i as? ARImageAnchor{
                DispatchQueue.main.async {
                    print("find anchor")
                    let imageAnchor = AnchorEntity(anchor: a)
                    self.boxEntity = ModelEntity(mesh: MeshResource.generateBox(size: 0.005),materials: [SimpleMaterial(color: .green, isMetallic: true)])
                    imageAnchor.addChild(self.boxEntity)
                    self.arView.scene.addAnchor(imageAnchor)
                }
            }
        }
    }
  • Related