gpt4 book ai didi

ios - 如何对场景 View 中节点的子节点进行hitTest?

转载 作者:行者123 更新时间:2023-11-28 12:01:03 26 4
gpt4 key购买 nike

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

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

当飞机在现场时,我的节点是这些

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

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

和我的 2 个 child 只是可检测节点..

但总是这样

▿ 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) |相机= |没有 child > - 1:SCNNode:0x1c01fc200 |灯= |没有 child > - 2: SCNNode: 0x1c01fd100 'DetectableChildrenParent' pos(-0.149971 0.050361 -0.365586) rot(0.996908 0.061535 -0.048872 1.379775) scale(1.000000 1.000000 01.00) 2个 child > - 3:SCNNode:0x1c01f9f00 |几何= |没有 child >

它总是检测到最后一个节点。

我要检测的代码是这个

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

如何运行 HitTest 以仅检查 DetectableChildrenParent 的子级?

最佳答案

假设我已经正确解释了你的问题,你正在寻找的是 SCNHitTestResult NodechildNodes 属性,它指的是:

An array of the node’s children in the scene graph hierarchy.

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

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:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

//1. Get The Current Touch Location
guard let currentTouchLocation = touches.first?.location(in: self.augmentedRealityView) else { return }

//2. Get The Results Of The SCNHitTest
let hitTestResults = self.augmentedRealityView.hitTest(currentTouchLocation, options: nil)


//3. Loop Through Them And Handle The Result
for result in hitTestResults{

print(result.node)
print(result.node.childNodes)
}


}

希望对你有帮助

关于ios - 如何对场景 View 中节点的子节点进行hitTest?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50100340/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com