如何对场景视图中节点的子节点进行hitTest?
How to do a hitTest on subnodes of node in sceneView?
我有一个 ARSceneView,我使用 detectionImages 找到一个平面,这样我就可以根据我检测到的图像添加节点。
我的问题是,视图一直在我在 "node of my Image" 中作为子节点添加的节点上方添加一个不可见的平面节点,当我将 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 children 只是可检测节点..
但总是这样
▿ 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 1.000000) | 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 ?? "")
}
}
我如何 运行 命中测试只检查 DetectableChildrenParent 的 children?
假设我已经正确解释了你的问题,你正在寻找的是 SCNHitTestResult Node
的 childNodes
属性,它指的是:
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
的 children,请使用以下内容:
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)
}
}
希望对您有所帮助...
我有一个 ARSceneView,我使用 detectionImages 找到一个平面,这样我就可以根据我检测到的图像添加节点。
我的问题是,视图一直在我在 "node of my Image" 中作为子节点添加的节点上方添加一个不可见的平面节点,当我将 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 children 只是可检测节点..
但总是这样
▿ 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 1.000000) | 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 ?? "")
}
}
我如何 运行 命中测试只检查 DetectableChildrenParent 的 children?
假设我已经正确解释了你的问题,你正在寻找的是 SCNHitTestResult Node
的 childNodes
属性,它指的是:
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
的 children,请使用以下内容:
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)
}
}
希望对您有所帮助...