Our scene only has about 20 nodes. The code below lets the user pan, conducting a hit test on each pan -- the goal is to highlight blocks as the user pans around the screen.
However, it is noticeably sluggish on a iPhone 5S. It's not deterministic but happens often enough to be irritating (every 5-10 pans).
We considered using hitTestWithSegment
since you could tightly bound the range for testing but believe that should be slower because you must first compute the two points required for the function.
Moreover, the SCNHitTestClipToZRangeKey
option for hitTest
should provide a comparable performance boost by tightening the hit range without requiring the computation of two additional points.
Any suggestions for speeding up the performance of hitTest
?
func sceneViewPannedOneFinger(sender: UIPanGestureRecognizer) {
// Get pan distance & convert to radians
let translation = sender.translationInView(sender.view!)
var xRadians = GLKMathDegreesToRadians(Float(translation.x))
var yRadians = GLKMathDegreesToRadians(Float(translation.y))
// Get x & y radians
xRadians = (xRadians / 4) + curXRadians
yRadians = (yRadians / 4) + curYRadians
// Limit yRadians to prevent rotating 360 degrees vertically
yRadians = max(Float(-M_PI_2), min(Float(M_PI_2), yRadians))
// Set rotation values to avoid Gimbal Lock
cameraNode.rotation = SCNVector4(x: 1, y: 0, z: 0, w: yRadians)
userNode.rotation = SCNVector4(x: 0, y: 1, z: 0, w: xRadians)
// Save value for next rotation
if sender.state == UIGestureRecognizerState.Ended {
curXRadians = xRadians
curYRadians = yRadians
}
// Set preview block
setPreviewBlock(sender)
}
private func setPreviewBlock(recognizer: UIGestureRecognizer) {
let point = recognizer.locationInView(sceneView)
let options = [SCNHitTestRootNodeKey: sceneView.scene!.rootNode, SCNHitTestClipToZRangeKey: 15, SCNHitTestSortResultsKey: true]
let hits = sceneView.hitTest(point, options: options)
print(hits.first?.worldCoordinates)
}