The documentation in XCode clearly states that hitTesting a geometry in SceneKit can be done with SCNRender, SCNView or the SCNNode themselves when one plans to test a 3D line segment. I have a use for SCNScene with its nodes without a renderer or a view, therefore I am planning to use SCNNode hitTesting. I create a SCNScene, put a SCNNode in it and test a simple ray that goes through, but I always get an empty hitList and I don't understand why:
import Swift
import SceneKit
let boxGeometry = SCNBox(width: 1.0, height: 1.0, length: 1.0, chamferRadius: 0)
let boxNode = SCNNode(geometry: boxGeometry)
var scene = SCNScene()
scene.rootNode.addChildNode(boxNode)
let from = SCNVector3(x: 0, y: -2, z: 0)
let to = SCNVector3(x: 0, y: 2 , z: 0)
var hits = scene.rootNode.hitTestWithSegmentFromPoint(from, toPoint: to, options:nil) // this is always empty
if hits != nil {
if hits!.count > 0 {
var hit = (hits!.first as! SCNHitTestResult).node as SCNNode
}
}
I have tried passing various forms of options but nothing changes.
- SCNHitTestFirstFoundOnlyKey: yes or no does not change anything
- SCNHitTestSortResultsKey: yes or no does not change anything
- SCNHitTestClipToZRangeKey: invalid for SCNNode
- SCNHitTestBackFaceCullingKey: yes or no does not change anything
- SCNHitTestBoundingBoxOnlyKey: yes or no does not change anything
- SCNHitTestRootNodeKey: rootNOde of scene or boxNode does not change anything
- SCNHitTestIgnoreHiddenNodesKey: yes or no does not change anything
What am I doing wrong?