【问题标题】:Find a tapped childNode inside the node in 3D object in SceneKit in ARKIT在 ARKIT 的 SceneKit 中的 3D 对象中的节点内找到一个被点击的 childNode
【发布时间】:2018-04-09 19:00:07
【问题描述】:

我想在 SceneKit 中的 3D 对象中的节点内找到一个点击的 childNode。像苹果的 Cup 节点的 3D 对象示例有子节点,如勺子、盘子。一旦我们触摸勺子,我们怎么能找到勺子被触摸或盘子被触摸???

【问题讨论】:

    标签: ios swift scenekit arkit


    【解决方案1】:

    我在 ARViewController 中保留了一个数组,其中包含我想要对点击做出反应的 SCNNode。然后我执行 hitTest 并检查结果是否包含该节点。像这样(如果点击距离对象足够近,hitTest 无法提高可用性,我实际上也在围绕点的半径为 R 的圆中执行命中测试)

    @objc func onTap(_ sender: UITapGestureRecognizer) {
        let position = sender.location(in: scnView)
        let hitTestOptions: [SCNHitTestOption : Any] = [.boundingBoxOnly: false, .searchMode: SCNHitTestSearchMode.all.rawValue]
        var hitTestResults: [SCNHitTestResult] = scnView.hitTest(point, options: hitTestOptions)
        let filteredResults = hitTestResults.filter { (hitTestResult) -> Bool in
            return nodeBelongsToVirtualObject(hitTestResult.node)
        }
        let nearestNode = filteredResults.first?.node // This was the tapped node
    }
    
    private func nodeBelongsToVirtualObject(_ node: SCNNode?) -> Bool {
        if let _ = node {
            return self.nodes.contains(node!) || nodeBelongsToVirtualObject(node!.parent)
        }
    
        return false
    }
    

    因此,您需要在 nodes 数组中跟踪您想要检测点击的那些子节点

    【讨论】:

    • 其实我现在已经得到了答案.. 很简单。让 lastNode = sceneHitTestResult.last?.node
    【解决方案2】:

    为添加到场景视图的节点命名 yournode.name = "某个名字"

    将点击手势识别器添加到场景视图中,并将以下代码添加到点击操作函数中

    @objc func tapFunction(withGestureRecognizer recognizer: UIGestureRecognizer){
    
        guard recognizer.state == .ended else{return}
         let taplocation = recognizer.location(in: sceneView)
        let hittestreults = sceneView.hitTest(swipelocation,options: [:])
        guard let hitTestResult = hittestreults.first else { return }
        let finalnode = hitTestResult.node
         print(finalnode.name)} 
    

    检查 finalnode.name 以了解哪个节点被点击。

    【讨论】:

      猜你喜欢
      • 2016-05-03
      • 2021-08-17
      • 1970-01-01
      • 1970-01-01
      • 2017-05-15
      • 2018-12-24
      • 2014-12-14
      • 1970-01-01
      • 2019-01-10
      相关资源
      最近更新 更多