调用setWorldOrigin时子节点不可见?

时间:2018-11-27 16:47:36

标签: ios swift arkit

所以我有一个类似下面的函数,它将世界原点设置为扫描图像后放置节点的位置。我从数据库中加载保存的节点,然后将它们添加到一个单独的函数中。

由于某些原因,运行应用程序时节点不会显示。当setWorldOrigin被注释掉时,它起作用。

我希望节点显示相对于图像的原点。

我想念什么吗? setWorldOrigin会更改会话吗?

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
        guard let imageAnchor = anchor as? ARImageAnchor else { return }
        let referenceImage = imageAnchor.referenceImage

        let nodeGeometry = SCNText(string: "Welcome!", extrusionDepth: 1)
        nodeGeometry.font = UIFont(name: "Helvetica", size: 30)
        nodeGeometry.firstMaterial?.diffuse.contents = UIColor.black

        anchorNode.geometry = nodeGeometry
        anchorNode.scale = SCNVector3(0.1, 0.1, 0.1)
        anchorNode.constraints = [SCNBillboardConstraint()]
        anchorNode.position = SCNVector3(imageAnchor.transform.columns.3.x, imageAnchor.transform.columns.3.y, imageAnchor.transform.columns.3.z)
imageAnchor.transform.columns.3.y, imageAnchor.transform.columns.3.z)

        // Create a plane to visualize the initial position of the detected image.
        let plane = SCNPlane(width: referenceImage.physicalSize.width, height: referenceImage.physicalSize.height)

        let planeNode = SCNNode(geometry: plane)
        planeNode.opacity = 0.25

        /*
         * Plane is rotated to match the picture location
         */
        planeNode.eulerAngles.x = -.pi / 2

        /*
         * Scan runs as an action for a set amount of time
         */
        planeNode.runAction(self.imageHighlightAction)

        // Add the plane visualization to the scene.
        node.addChildNode(planeNode)

        sceneView.session.setWorldOrigin(relativeTransform: imageAnchor.transform)
        sceneView.scene.rootNode.addChildNode(node)

        /*
         * Populates the scene
         */
        handleData()
    } // renderer

1 个答案:

答案 0 :(得分:0)

我知道了。我的图像尺寸不正确。我使用的图像是512x512像素。我可以通过右键单击图像并选择“获取信息”并查看尺寸来实现。

我也以米为单位进行测量。我将其更改为厘米,并使用了pixel to centimeters converter