【发布时间】:2021-05-16 13:49:28
【问题描述】:
在 iOS 14 中,hitTest(_:types:) 已被弃用。看来您现在应该使用raycastQuery(from:allowing:alignment:)。来自documentation:
光线投射是在现实环境中寻找表面位置的首选方法,但为了兼容性,命中测试功能仍然存在。通过跟踪光线投射,ARKit 会继续优化结果,以提高您通过光线投射放置的虚拟内容的位置准确性。
但是,我如何通过光线投射来测试SCNNodes?我只看到了测试飞机的选项。
这是我当前的代码,它使用命中测试来检测立方体节点上的点击并将其变为蓝色。
class ViewController: UIViewController {
@IBOutlet weak var sceneView: ARSCNView!
override func viewDidLoad() {
super.viewDidLoad()
/// Run the configuration
let worldTrackingConfiguration = ARWorldTrackingConfiguration()
sceneView.session.run(worldTrackingConfiguration)
/// Make the red cube
let cube = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)
cube.materials.first?.diffuse.contents = UIColor.red
let cubeNode = SCNNode(geometry: cube)
cubeNode.position = SCNVector3(0, 0, -0.2) /// 20 cm in front of the camera
cubeNode.name = "ColorCube"
/// Add the node to the ARKit scene
sceneView.scene.rootNode.addChildNode(cubeNode)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
guard let location = touches.first?.location(in: sceneView) else { return }
let results = sceneView.hitTest(location, options: [SCNHitTestOption.searchMode : 1])
for result in results.filter( { $0.node.name == "ColorCube" }) { /// See if the beam hit the cube
let cubeNode = result.node
cubeNode.geometry?.firstMaterial?.diffuse.contents = UIColor.blue /// change to blue
}
}
}
如何将let results = sceneView.hitTest(location, options: [SCNHitTestOption.searchMode : 1]) 替换为等效的raycastQuery 代码?
【问题讨论】:
标签: swift augmented-reality scenekit arkit realitykit