dschu
dschu

Reputation: 5374

Get rootNode of node

In an ARKit project, when the user taps the screen, I want to get the rootNode of the element, that the user want to interact with.

Gesture & Hit test

func screenTapped(_ sender: UITapGestureRecognizer) {
    let hitTestResult = sceneView.hitTest(touchLocation)
    if let result = hitTestResult.first {
        guard let rootNode = getRoot(for: result.node) else {return}
        ...
    }

Recursive function to get the root node

func getRoot(for node: SCNNode) -> SCNNode? {
    if let node = node.parent {
        return getRoot(for: node)
    }
    else {
        return node
    }
}

But it seems odd to me that Swift doesn't offer something by default, while offering recursive methods for child nodes.

Upvotes: 1

Views: 897

Answers (1)

mnuages
mnuages

Reputation: 13462

Isn't it equivalent to sceneView.scene.rootNode?

Upvotes: 1

Related Questions