如何在sceneView中的节点的子节点上执行hitTest?

时间:2018-04-30 12:14:50

标签: ios swift augmented-reality arkit hittest

我有一个ARSceneView,我使用detectionImages找到一个平面,这样我就可以根据我检测到的图像添加节点。

我的问题是,视图一直在我在“我的图像的节点”中添加为子节点的节点上方添加一个不可见的平面节点,当我向sceneView添加TapGestureRecognizer时,我无法检测到节点,因为它检测到飞机。有时它可以工作,但是当它添加飞机时它不起作用。

当飞机在场景上时,我的节点就是这些

我希望我的节点像这样......

▿3个元素   - 0:|没有孩子>   - 1:SCNNode:0x1c41f4100 |光=   - 2:SCNNode:0x1c43e1000'ExcactableChildrenParent'post(-0.009494 -0.081575 -0.360098)rot(0.997592 -0.060896 0.033189 1.335512)| 2个孩子>

我的2个孩子只是可检测的节点..

但总是这样

▿4个元素   - 0:SCNNode:0x1c01fcc00 pos(-0.093724 0.119850 -0.060124)rot(-0.981372 0.172364 -0.084866 0.457864)scale(1.000000 1.000000 1.000000)|相机= |没有孩子>   - 1:SCNNode:0x1c01fc200 |光= |没有孩子>   - 2:SCNNode:0x1c01fd100'ExcactableChildrenParent'post(-0.149971 0.050361 -0.365586)rot(0.996908 0.061535 -0.048872 1.379775)scale(1.000000 1.000000 1.000000)| 2个孩子>   - 3:SCNNode:0x1c01f9f00 | geometry = |没有孩子>

并始终检测最后一个节点。

我要检测的代码是

@objc func addNodeToScene(withGestureRecognizer recognizer: UIGestureRecognizer) {
            let tapLocation = recognizer.location(in: self.sceneView)
            let hitTestResults = sceneView.hitTest(tapLocation)
            if let node = hitTestResults.first?.node {
                    print(node.name ?? "")
            }
    }

如何运行命中测试以仅检查DetectableChildrenParent的子项?

1 个答案:

答案 0 :(得分:1)

假设我已正确解释您的问题,您所寻找的是childNodes的{​​{1}}属性,该属性指的是:

  

场景图层次结构中节点子节点的数组。

因此我相信你可以尝试这样的事情:

SCNHitTestResult Node

或者,不是寻找override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { //1. Get The Current Touch Location guard let currentTouchLocation = touches.first?.location(in: self.augmentedRealityView), //2. Get The Tapped Node From An SCNHitTest let hitTestResultNode = self.augmentedRealityView.hitTest(currentTouchLocation, options: nil).first?.node else { return } //3. Loop Through The ChildNodes for node in hitTestResultNode.childNodes{ //4. If The Node Has A Name Then Print It Out if let validName = node.name{ print("Node\(validName) Is A Child Node Of \(hitTestResultNode)") } } } 的第一个结果,而是寻找最后一个,例如:

SCNHitTest

如果您正在寻找特定override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { //1. Get The Current Touch Location guard let currentTouchLocation = touches.first?.location(in: self.augmentedRealityView), //2. Get The Tapped Node From An SCNHitTest let hitTestResultNode = self.augmentedRealityView.hitTest(currentTouchLocation, options: nil).last?.node else { return } //3. Loop Through The ChildNodes for node in hitTestResultNode.childNodes{ //4. If The Node Has A Name Then Print It Out if let validName = node.name{ print("Node\(validName) Is A Child Node Of \(hitTestResultNode)") } } } 的孩子,请使用以下内容:

SCNNode

或者您可以简单地循环遍历override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { //1. Get The Current Touch Location guard let currentTouchLocation = touches.first?.location(in: self.augmentedRealityView), //2. Get The Tapped Node From An SCNHitTest let hitTestResultNode = self.augmentedRealityView.hitTest(currentTouchLocation, options: nil).first?.node else { return } //3. If The Name Of The Nodes Is Equal To DetectableChildrenParent Then Get The Children if hitTestResultNode.name == "DetectableChildrenParent"{ let childNodes = hitTestResultNode.childNodes print(childNodes) } } ,如此:

SCNHitTestResults

希望它有所帮助...