Hittest doesn't work with SCNRenderer

I'm implementing an example app using ARKit + SceneKit. In order to do hittest on scenekit objects(nodes) in the scene, I used ARSCNView.hittest() and it worked well. However when I draw same scene with SCNRenderer(I'm writing also an offscreen renderer) and try to do hittest with SCNRenderer.hittest() but it doesn't return any results.


I assumed the range of input points should be only inside of GPU viewport which is SCNRenderer.currentViewport. But it doesn't work


Any idea how to do properly do hittest with SCNRenderer?

Thanks in advance

Answered by DTS Engineer in 407569022

Hello,


Are you converting your view touch/click location to the renderer's viewport coordinate space?


let xScale = drawableSize.width/view.frame.width

let yScale = drawableSize.height/view.frame.height

let locationInViewport = CGPoint(x: locationInView.x*xScale, y: locationInView.y*yScale)


renderer.hitTest(locationInViewport, options: nil)

Accepted Answer

Hello,


Are you converting your view touch/click location to the renderer's viewport coordinate space?


let xScale = drawableSize.width/view.frame.width

let yScale = drawableSize.height/view.frame.height

let locationInViewport = CGPoint(x: locationInView.x*xScale, y: locationInView.y*yScale)


renderer.hitTest(locationInViewport, options: nil)

I seem to vagely remember this happening to me. I believe the renderer has to actually render the nodes for hit testing to occur. Is it possible the rendering is not happening (you mentioned something about an offscreen renderer)?


In my case I was trying to increase the size of some small nodes so they could be more easily tapped. I think I changed their scale before the hit test and then restored it afterwards. This didn't work because the hit test used the cached bounding box when the object was last rendered.


As an aside, I did solve my problem by changing the boundingBox of the node before rendering . This doesn't afffect the rendering but does affect the hit testing.

Same problem here. Have tried everything and there is never any result from SCNRenderer.hitTest(). I think there's something in the SceneKit render loop that prepares nodes for hit-testing, and that something is missing when there's no SCNView. Thought it might have been SCNTransaction.flush() but that doesn't help.
In my case the problem turned out to be that the projection matrix I was using was not invertible. This causes SCNRenderer's hitTest() and unprojectPoint() functions to fail.
I had same problem. As some other one already said, a projection matrix in camera was wrong.
It was generated by using projectionMatrix(for:viewportSize:zNear:zFar:) but I set that zFar was too short.

After I set zNearz and zFar value properly (e.g. 10.0 meter), I can get some results of hitTest.

Hello, In my case it started to work when I flipped tap location vertically:

location.y = drawableSize.height - location.y

Hittest doesn't work with SCNRenderer
 
 
Q