在ARKit中将世界原点更改为场景中的现有节点

时间:2018-05-23 17:28:29

标签: scenekit arkit

我的应用程序有一个ARSCNView并识别带图像检测的图像 目标是寻找一个图像,一旦识别出来,就将世界位置改为其中心

有些东西不起作用。任何帮助将不胜感激!

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")
        }
    }
}

2 个答案:

答案 0 :(得分:0)

我是这样做的:

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

答案 1 :(得分:0)

不知道它是否仍然与您相关,但是在您更改了世界原点之后,使用self.sceneView.session.setWorldOrigin,您可以使用.resetTracking选项恢复会话,该选项会将您的世界原点设置为相机。

摆脱该选项应该会有所帮助。