首页 文章

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

提问于
浏览
0

我的应用程序具有 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

    我这样做是这样的:

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

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

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

相关问题