iOS 11 ARKit-Scenekit 如何为 SCNNode 创建边框以指示其选择?

How to create a border for SCNNode to indicate its selection in iOS 11 ARKit-Scenekit?

如何绘制边框以突出显示 SCNNode 并向用户指示该节点已 selected? 在我的项目中,用户可以放置多个虚拟对象,用户可以随时 select 任何对象。在 selection 我应该向用户显示突出显示的 3D 对象。有没有办法直接实现这个或者在SCNNode上画一个边框?

您需要向 sceneView 添加点击手势识别器。

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

然后,处理点击并突出显示节点:

@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()
    }
}

上面的代码片段突出显示了整个节点。如果您正在寻找的话,您必须调整它以仅突出显示边框。

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