Search code examples
scenekithittestscnnode

SceneKit, 'hit' testing between camera orientation and geometry


I'm currently following David Roonqvist's 3D Graphics with SceneKit book.

Chapter 5 deals with hit testing, no issues implementing it, but only deals with his testing from mouse events.

Now, I know my camera's orientation will ALWAYS intersect a certain geometry.

I need to obtain the texture coordinate of the intersecting point between the camera orientation and the geometry.

Any pointers as how to go about it? I've been looking all over without much success.

Any help is appreciated.


Solution

  • I haven't tried this but...

    SCNHitTestResult returns, among other things, textureCoordinatesWithMappingChannel(_:). Documentation cites the example of adding scorch marks to a game character's texture after it's been hit by a laser.

    It appears that SCNNode's

    hitTestWithSegmentFromPoint(_ pointA: SCNVector3,
                         toPoint pointB: SCNVector3,
                         options options: [String : AnyObject]?) -> [SCNHitTestResult]
    

    will give you the hits you need, between the camera and the geometry, regardless of the orientation of the camera. If the camera and the target are using different local node coordinate systems, you'll have to do some conversion using convertPosition(_:from:Node:) or similar functions.