hitTestWithSegment

SCNNode有一个hitTestWithSegment方法用以检测两点连线通过的Node,使用方法如下:

let scene = SCNScene()
sceneView.delegate = self
let boxGeometry = SCNBox(width: 1.0, height: 1.0, length: 1.0, chamferRadius: 0.0)
let boxNode = SCNNode(geometry: boxGeometry)
boxNode.position=SCNVector3(x: 0, y: 0, z: 0)
scene.rootNode.addChildNode(boxNode)
sceneView.scene = scene
let hitList = sceneView.scene.rootNode.hitTestWithSegment(from: SCNVector3(x:-10,y:0,z:0), to: SCNVector3(x:10,y:0,z:0), options:[SCNHitTestOption.backFaceCulling.rawValue:false, SCNHitTestOption.sortResults.rawValue:true, SCNHitTestOption.ignoreHiddenNodes.rawValue:false])
if hitList.count > 0 {
    print("Hit found: \n\n\( hitList[0] )")
} else {
    print("No hit")
}

但是如果我们调整一下代码顺序:

let scene = SCNScene()
sceneView.delegate = self
sceneView.scene = scene
let boxGeometry = SCNBox(width: 1.0, height: 1.0, length: 1.0, chamferRadius: 0.0)
let boxNode = SCNNode(geometry: boxGeometry)
boxNode.position=SCNVector3(x: 0, y: 0, z: 0)
sceneView.scene.rootNode.addChildNode(boxNode)
let hitList = sceneView.scene.rootNode.hitTestWithSegment(from: SCNVector3(x:-10,y:0,z:0), to: SCNVector3(x:10,y:0,z:0), options:[SCNHitTestOption.backFaceCulling.rawValue:false, SCNHitTestOption.sortResults.rawValue:true, SCNHitTestOption.ignoreHiddenNodes.rawValue:false])
if hitList.count > 0 {
    print("Hit found: \n\n\( hitList[0] )")
} else {
    print("No hit")
}

将得不到预期的结果,不知道这是bug还是有其他我没理解的地方。
实际使用中多是用第二种写法,这时我们不能在直接使用

sceneView.scene.rootNode.hitTestWithSegment

我们需要使用SCNSceneRenderer

extension UIViewController: ARSCNViewDelegate{
    public func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
        let hitList = renderer.scene?.rootNode.hitTestWithSegment(from: SCNVector3(x:-10,y:0,z:0), to: SCNVector3(x:10,y:0,z:0), options:[SCNHitTestOption.backFaceCulling.rawValue:false, SCNHitTestOption.sortResults.rawValue:true, SCNHitTestOption.ignoreHiddenNodes.rawValue:false])
        if (hitList?.count)! > 0 {
            print("Hit found: \n\n\( hitList![0] )")
        } else {
            print("No hit")
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容