如何为SCNNode创建边框以指示其在iOS 11 ARKit-Scenekit中的选择? [英] How to create a border for SCNNode to indicate its selection in iOS 11 ARKit-Scenekit?

查看:193
本文介绍了如何为SCNNode创建边框以指示其在iOS 11 ARKit-Scenekit中的选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何绘制边框以突出显示SCNNode并向用户指示已选择该节点? 在我的项目中,用户可以放置多个虚拟对象,并且用户可以随时选择任何对象.选择后,我应该向用户显示突出显示的3D对象.有没有一种方法可以直接实现这一目标或在SCNNode上划出边界?

How to draw a border to highlight a SCNNode and indicate to user that the node is selected? In my project user can place multiple virtual objects and user can select any object anytime. Upon selection i should show the user highlighted 3D object. Is there a way to directly achieve this or draw a border over SCNNode?

推荐答案

您需要在sceneView上添加轻击手势识别器.

You need to add a tap gesture recognizer to the sceneView.

// add a tap gesture recognizer
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
scnView.addGestureRecognizer(tapGesture)

然后,处理水龙头并突出显示该节点:

Then, handle the tap and highlight the node:

@objc
func handleTap(_ gestureRecognize: UIGestureRecognizer) {
    // retrieve the SCNView
    let scnView = self.view as! SCNView

    // check what nodes are tapped
    let p = gestureRecognize.location(in: scnView)
    let hitResults = scnView.hitTest(p, options: [:])
    // check that we clicked on at least one object
    if hitResults.count > 0 {
        // retrieved the first clicked object
        let result = hitResults[0]

        // get its material
        let material = result.node.geometry!.firstMaterial!

        // highlight it
        SCNTransaction.begin()
        SCNTransaction.animationDuration = 0.5

        // on completion - unhighlight
        SCNTransaction.completionBlock = {
            SCNTransaction.begin()
            SCNTransaction.animationDuration = 0.5

            material.emission.contents = UIColor.black

            SCNTransaction.commit()
        }

        material.emission.contents = UIColor.red

        SCNTransaction.commit()
    }
}

上面的代码片段突出显示了整个节点.如果您要查找的是,则必须对其进行调整以仅突出显示边框.

The snippet above highlights the whole node. You'd have to adjust it to highlight the borders only, if that's what you're looking for.

免责声明:
此代码直接来自打开新游戏(SceneKit)项目时创建的Xcode模板代码.

Disclaimer:
This code was taken directly from Xcode's template code created when opening a new game (SceneKit) project.

这篇关于如何为SCNNode创建边框以指示其在iOS 11 ARKit-Scenekit中的选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆