Nuno Cordeiro
Nuno Cordeiro

Reputation: 1

Change world origin to existing node in scene in ARKit

My app has a ARSCNView and recognises an image with image detection The goal is to look for an image and once it is recognised change the world position to its center

Something is not working. Any help would be greatly appreciated!

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {

    guard let imageAnchor = anchor as? ARImageAnchor else {return}

    if let imageName = imageAnchor.referenceImage.name  {
        switch imageName {
            case "myImage":

            //OK, the image gets recognized

            let refSphere = SCNSphere(radius: 0.02)
            let refNode = SCNNode(geometry: refSphere)
            refSphere.firstMaterial?.diffuse.contents = UIColor.red.withAlphaComponent(0.8)
            node.addChildNode(refNode) // OK, there's a sphere representation on top of the recognized image

            sceneView.session.pause()
            self.sceneView.session.setWorldOrigin(relativeTransform: node.simdTransform) // Trying to move the world Origin to the center of recognized image
            sceneView.session.run(configuration, options: [.resetTracking])

            let box = SCNBox(width: 0.05, height: 0.05, length: 0.05, chamferRadius: 0.01)
            let boxNode = SCNNode(geometry: box)

            box.firstMaterial?.diffuse.contents = UIColor.green.withAlphaComponent(0.9)
            boxNode.position = SCNVector3(0,0,0)

            sceneView.scene.rootNode.addChildNode(boxNode)  // Not OK - I was expecting to have world Origin in the center of the recognized image and a green box on top of it

        default:
            print("other")
        }
    }
}

Upvotes: 0

Views: 1758

Answers (2)

Don't know if it's still relevant to you, but after you change the world origin, with self.sceneView.session.setWorldOrigin, you resume session with .resetTracking option, which sets your world origin to the location of the camera.

Getting rid of that option should help.

Upvotes: 0

MoD
MoD

Reputation: 634

I did it this way:

self.sceneView.session.setWorldOrigin(relativeTransform: imageAnchor.transform)

Upvotes: 2

Related Questions