0
votes

Is there a native way with SceneKit to find a node at a given position, e.g., (0,0,0), within a SCNView?

Assume a SCNView contains a SCNNode which acts as a layer for other SCNNodes. Is there a native way to find all the nodes at a specific position within the layer?

Reading the class docs, it seems like the only option is to create and maintain a custom data structure mapping positions to nodes.

But hopefully there's something native since this seems like a common problem.

If a custom data structure is required, would a dictionary be the most efficient way to maintain tabs on 10K nodes in Swift?

2

2 Answers

2
votes

a node conforms to the SCNBoundingVolume and so you can have a rough idea of whether a node "contains" the origin. You can also use -[SCNNode - hitTestWithSegmentFromPoint:toPoint:options:] with different options to refine your search.

1
votes

Although I've never tried it, it looks like SCNPhysicsWorld can help you do this. You'd need a physics body on each node you want to track or query.

You could place an invisible body at your target point, and wait for collision delegate callbacks.

There's also convexSweepTestWithShape(_ shape: SCNPhysicsShape, fromTransform from: SCNMatrix4, toTransform to: SCNMatrix4, options options: [String : AnyObject]?) -> [SCNPhysicsContact].

Another approach would be to use SCNSceneRenderer's nodesInsideFrustumWithPointOfView function, with an orthographic SCNCamera so that you can get a rectangular or cubic frustum.

In general with SceneKit, btw, check all of the applicable protocol references. Many of the common calls are on a protocol and not a class.