如何绘制边框以突出显示SCNNode并向用户指示该节点已被选中?
在我的项目中,用户可以放置多个虚拟对象,用户可以随时选择任何对象.选择后,我应该向用户显示高亮显示的3D对象.有没有办法直接实现这个或在SCNNode上绘制边框?
在我的项目中,用户可以放置多个虚拟对象,用户可以随时选择任何对象.选择后,我应该向用户显示高亮显示的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()
- }
- }