代码之家  ›  专栏  ›  技术社区  ›  john doe

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

  •  1
  • john doe  · 技术社区  · 6 年前

    我正在开发一个arkit应用程序,在那里我检测到一架飞机,现在我想把一个物体放在飞机的顶部。对象确实被添加到平面上,但它在平面下有一点点。我可以增加盒子的高度/2,它可以解决这个问题,但不知道是否有更简单的方法。

    enter image description here

    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")
        }
    
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   M Reza    6 年前

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

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

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