【问题标题】:Why Node is being added below the detected plane in ARKit为什么节点被添加到 ARKit 中检测到的平面下方
【发布时间】:2019-01-19 04:36:26
【问题描述】:

我正在开发一个 ARKit 应用程序,我正在检测飞机,现在我想在飞机顶部放置一个物体。该对象确实被添加到平面上,但它在平面下方一点点。我可以添加盒子的高度/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")
    }

}

【问题讨论】:

    标签: ios arkit scnnode


    【解决方案1】:

    当您设置SCNNode 的位置时,您正在定义节点的center 点的位置,这就是您的节点垂直位于平面中间的原因。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-25
      • 1970-01-01
      • 2018-10-20
      • 2020-04-09
      • 1970-01-01
      • 2019-06-30
      • 1970-01-01
      • 2021-04-24
      相关资源
      最近更新 更多