无论如何,有没有在ARKit中标识被触摸的节点?

时间:2019-05-31 10:29:16

标签: ios swift arkit scnnode

我正在使用ARKit投影3D文件。在该3D中,有多个子节点。当用户触摸任何节点时,我们必须显示有关该触摸节点的一些信息。

有什么办法可以确定用户在哪个节点上触摸过?

1 个答案:

答案 0 :(得分:2)

You can perform a hit test to identify which node user has touched. Assuming you have two nodes in your scene, for example:

override func viewDidLoad() {
    ...

    let scene = SCNScene()

    let node1 = SCNNode()
    node1.name = "node1"
    let node2 = SCNNode()
    node2.name = "node2"

    scene.rootNode.addChildNode(node1)
    scene.rootNode.addChildNode(node2)

    sceneView.scene = scene

    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapped))
    sceneView.addGestureRecognizer(tapGestureRecognizer)
}

In your tap handler, you can detect the touched node and perform any logic you need, like displaying some information about the node.

@objc func tapped(recognizer: UIGestureRecognizer) {
    guard let sceneView = recognizer.view as? SCNView else { return }
    let touchLocation = recognizer.location(in: sceneView)

    let results = sceneView.hitTest(touchLocation, options: [:])

    if results.count == 1 {
        let node = results[0].node
        print(node.name) // prints "node1" or "node2" if user touched either of them
        if node.name == "node1" {
            // display node1 information
        } else if node.name == "node2" {
            // display node2 information
        }
    }
}