【问题标题】:detect which node clicked on 3D model检测哪个节点点击了 3D 模型
【发布时间】:2019-03-28 20:09:27
【问题描述】:

我有 3D 模型,想点击这个模型上的特定节点来创建一些动作。我的模型是一组节点;每个节点都没有冷冻节点,我想单击其中一个节点来原谅某些动作,然后单击另一个节点来原谅不同的动作;如何检测哪个节点点击了我的模型?

【问题讨论】:

标签: ios swift arkit


【解决方案1】:

您应该阅读一些有关 SceneKit 命中测试方法的信息并关注 hit-test method

func registerGestureRecognizer() {
    let tap = UITapGestureRecognizer(target: self, action: #selector(detectNode))
    self.sceneView.addGestureRecognizer(tap)
}

@objc func detectNode(_ sender: UITapGestureRecognizer) {
    let sceneView = sender.view as! ARSCNView
    let location = sender.location(in: sceneView)
    let results = sceneView.hitTest(location, options: [SCNHitTestOption.searchMode : 1])

    for result in results.filter( { $0.node.name != nil }) {
        if result.node.name == "Your node name" {
            // excuse some actions
        }
    }
}

希望对你有帮助!

【讨论】:

    【解决方案2】:

    您可以使用SCNSceneRenderer 中的hitTest(_:options:) 来检测从 2D 点触摸的 3D 对象。

    它将返回一个包含node 属性的SCNHitTestResult 数组。

    【讨论】:

    • 使用此代码guard let currentTouchLocation = touches.first?.location(in: self.sceneView), let hitTestResultNode = self.sceneView.hitTest(currentTouchLocation, options: nil).first else {return} // print(hitTestResultNode.node.name) if hitTestResultNode.node.name == "Hair"{ print("hair node is clicked ") }else if hitTestResultNode.node.name == "_"{ print("eye is clicked ") }
    猜你喜欢
    • 1970-01-01
    • 2014-09-01
    • 2016-08-18
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 2016-02-08
    • 2014-12-09
    相关资源
    最近更新 更多