ARKIT : place object on a plane doesn't work properly

这一生的挚爱 提交于 2019-12-10 11:07:52

问题


I am learning ARKit and trying to place an object on a detected plane. But it doesn't work properly and there's a space between the plane and the 3D object.

here's my code for the plane detection :

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
        position = SCNVector3Make(anchor.transform.columns.3.x, anchor.transform.columns.3.y, anchor.transform.columns.3.z)

        guard let planeAnchor = anchor as? ARPlaneAnchor else { return }

        let plane = SCNPlane(width: CGFloat(planeAnchor.extent.x), height: CGFloat(planeAnchor.extent.z))

        planeNode = SCNNode(geometry: plane)
        planeNode.position = position
        planeNode.transform = SCNMatrix4MakeRotation(-Float.pi / 2.0, 1.0, 0.0, 0.0)
        node.addChildNode(planeNode)
}

And then the 3d model gets the same position :

object.position = position

But when I run the application there's a big space between the object and the plane. I didn't figure out why ?


回答1:


Because of anchor transform is related to world coordinates. Node in func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) already positioned in world coordinates. So all that you need - is just add you own node as child node for rendered node:

    func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
        guard let planeAnchor = anchor as? ARPlaneAnchor else { return }

        let plane = SCNPlane(width: CGFloat(planeAnchor.extent.x), height: CGFloat(planeAnchor.extent.z))

        planeNode = SCNNode(geometry: plane)
        planeNode.position = SCNVector3Zero // Position of `planeNode`, related to `node`
        planeNode.transform = SCNMatrix4MakeRotation(-Float.pi / 2.0, 1.0, 0.0, 0.0)
        node.addChildNode(planeNode)
    }


来源:https://stackoverflow.com/questions/45544204/arkit-place-object-on-a-plane-doesnt-work-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!