问题描述
我正在学习 ARKit 并尝试在检测到的平面上放置一个对象.但它无法正常工作,并且飞机和 3D 对象之间存在空间.
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)
}
然后 3d 模型得到相同的位置:
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 ?
推荐答案
因为anchor transform是和世界坐标相关的.func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor)
中的节点已经定位在世界坐标中.所以你所需要的 - 只是将你自己的节点添加为渲染节点的子节点:
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)
}
这篇关于ARKIT:将对象放置在平面上无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!