为什么在 ARKit 中将节点添加到检测到的平面下方

Why Node is being added below the detected plane in ARKit

我正在开发一个 ARKit 应用程序,我正在其中检测一架飞机,现在我想在飞机顶部放置一个物体。该对象确实被添加到平面上,但它在平面下方一点点。我可以添加框的 height/2 ,它会解决问题,但想知道是否有更简单的方法。

private func addBox(at position: SCNVector3) {

    let box = SCNBox(width: 0.2, height: 0.2, length: 0.2, chamferRadius: 0)
    let material = SCNMaterial()
    material.diffuse.contents = UIColor.red

    let boxNode = SCNNode(geometry: box)
    boxNode.position = position
    self.sceneView.scene.rootNode.addChildNode(boxNode)
}

@objc func tapped(recognizer :UITapGestureRecognizer) {

    let touch = recognizer.location(in: self.sceneView)

    if let nodeHitTest = self.sceneView.hitTest(touch, options: nil).first, nodeHitTest.node.name != "plane" {
        print("node")

        print(nodeHitTest.node)

    } else if let planeHitTest = self.sceneView.hitTest(touch, types: .estimatedHorizontalPlane).first {

        let position = SCNVector3(planeHitTest.worldTransform.columns.3.x, planeHitTest.worldTransform.columns.3.y, planeHitTest.worldTransform.columns.3.z)

        addBox(at: position)

        print("plane found")
    }

}

当您设置 SCNNode 的位置时,您正在定义节点的 中心 点的位置,这就是您的节点垂直于飞机的中间。

将节点高度的一半添加到 y 轴可能是将节点放置在平面上的一种非常简单的方法,正如您已经提到的那样:

let position = SCNVector3(planeHitTest.worldTransform.columns.3.x,
                          planeHitTest.worldTransform.columns.3.y + Float(box.height/2), 
                          planeHitTest.worldTransform.columns.3.z)