我正在学习ARKit并尝试将对象放在检测到的平面上。但它无法正常工作,并且平面和3D对象之间存在空间。
这是我的平面检测代码:
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)
}
然后3D模型获得相同的位置:
object.position = position
但是当我运行应用程序时,对象和飞机之间有一个很大的空间。我没弄明白为什么?
答案 0 :(得分:0)
因为锚变换与世界坐标有关。 func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor)
中的节点已经位于世界坐标中。所以你需要的只是添加自己的节点作为渲染节点的子节点:
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)
}