Ios 如何在sceneView中对节点的子节点执行命中测试?

Ios 如何在sceneView中对节点的子节点执行命中测试?,ios,swift,augmented-reality,arkit,hittest,Ios,Swift,Augmented Reality,Arkit,Hittest,我有一个ARSceneView,我使用detectionImages找到一个平面,这样我可以根据我检测到的图像添加节点 我的问题是,视图始终在我作为“图像的节点”中的子节点添加的节点上方添加不可见的平面节点,而当我向sceneView添加TapGestureRecognitor时,我无法检测到节点,因为它检测到平面。有时它可以工作,但当它添加平面时它就不工作了 当飞机在场景中时,我的节点是 我希望我的节点是这样的 ▿ 三要素 -0:|无子女> -1:SCNNode:0x1c41f4100 |灯=

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

我的问题是,视图始终在我作为“图像的节点”中的子节点添加的节点上方添加不可见的平面节点,而当我向sceneView添加TapGestureRecognitor时,我无法检测到节点,因为它检测到平面。有时它可以工作,但当它添加平面时它就不工作了

当飞机在场景中时,我的节点是

我希望我的节点是这样的

▿ 三要素 -0:|无子女> -1:SCNNode:0x1c41f4100 |灯= -2:SCNNode:0x1c43e1000‘可检测的儿童家长’位置(-0.009494-0.081575-0.360098)rot(0.997592-0.060896 0.033189 1.335512)| 2个儿童>

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

但总是这样

▿ 4要素 -0:SCNNode:0x1c01fcc00位置(-0.093724 0.119850-0.060124)旋转(-0.981372 0.172364-0.084866 0.457864)刻度(1.000000 1.000000 1.000000)|相机=|无子> -1:SCNNode:0x1c01fc200 |灯光=|无子项> -2:SCNNode:0x1c01fd100“可检测儿童家长”位置(-0.149971 0.050361-0.365586)旋转(0.996908 0.061535-0.048872 1.379775)刻度(1.000000 1.000000 1.000000)| 2个儿童> -3:SCNNode:0x1c01f9f00 |几何体=|无子项>

它总是检测最后一个节点

我要检测的代码是

@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 ?? "")
            }
    }

如何运行命中测试以仅检查可检测ChildrenParent的子级?

假设我已正确解释了您的问题,您要查找的是
SCNHITTSRESULT节点的
childNodes
属性,该属性指:

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

因此,我相信您可以尝试以下方式:

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)")
        }

    }

}
如果要查找特定
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)
    }

}

希望它有帮助……

假设我正确地解释了您的问题,您要查找的是
SCNHITTSRESULT节点的
childNodes
属性,该属性指:

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

因此,我相信您可以尝试以下方式:

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)")
        }

    }

}
如果要查找特定
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)
    }

}

希望它有帮助……

不,这是一样的,因为不可见节点3 SCNNode:0x1c01f9f00 | geometry=| No child>高于2:SCNNode:0x1c01fd100“DetectableChildrenParent”pos(-0.149971 0.050361-0.365586)rot(0.996908 0.061535-0.048872 1.379775)刻度(1.000000 1.000000)| 2 children>它在hitTestResultNode中检测为该节点的节点,但谢谢节点名称是“DetectableChildrenParent”?否具有我尝试检测的子节点的节点是“DetectableChildrenParent”,它有2个子节点。我正在尝试检测这些节点,并且仅检测这些节点。感谢您的尝试,但它完全相同,因为在行中让hitTestResultNode=self.augmentedRealityView.hitTest(currentTouchLocation,options:nil)。first?.node else{return}它检测节点3 SCNNode:0x1c01f9f00 | geometry=| no child>因此名称并不重要让hitTestResultNode=self.augmentedRealityView.hitTest(currentTouchLocation,options:nil)。lastNo这是一样的,因为不可见节点3 SCNNode:0x1c01f9f00 | geometry=| no child>位于2:SCNNode:0x1c01fd100“DetectableChildrenParent”位置(-0.149971 0.050361-0.365586)rot(0.996908 0.061535-0.048872 1.379775)刻度(1.000000 1.000000)之上| 2 children>它在hitTestResultNode中检测为该节点的节点,但谢谢节点名称是“DetectableChildrenParent”?否具有我尝试检测的子节点的节点是“DetectableChildrenParent”,它有2个子节点。我正在尝试检测这些节点,并且仅检测这些节点。感谢您的尝试,但它完全相同,因为在行中让hitTestResultNode=self.augmentedRealityView.hitTest(currentTouchLocation,options:nil)。first?.node else{return}它检测节点3 SCNNode:0x1c01f9f00 | geometry=| no child>,因此名称并不重要让hitTestResultNode=self.augmentedRealityView.hitTest(currentTouchLocation,选项:nil)。最后