How to select an individual SceneKit SCNNode using the remote on tvOS

671 Views Asked by At

Is there a way to get the focus engine on SceneKit? I am attempting to build a 3d game, in which players must select from many SceneKit Nodes, but I cannot find a way to give the SCNNodes focus.

3

There are 3 best solutions below

2
On BEST ANSWER

An SCNNode is a model object, not a UIView descendant (or more precisely doesn't conform to UIFocusEnvironment), so I wouldn't expect it to be able to take focus.

So how to select a particular SCNNode in an SCNView? The Apple tvOS SceneKit template implements hit testing, but I don't see how the user can choose what point on the screen they're selecting; there's no visual feedback. The template/demo seems poorly thought out. So here's an approach some colleagues and I discussed the other day, but didn't try to implement.

The SCNView can have a SpriteKit overlay scene, overlaySKScene. Use some gesture (maybe a long press, or the Play button) to switch into "I'm about to pick a node" mode. That means a cursor appears on the overlaySKScene, and can be moved around by finger movement on the remote. User moves the cursor over the correct node and then...clicks? Program grabs the cursor's SpriteKit coordinates, translates that back to a SceneKit hit test input, and performs the hit test on the scene graph.

@vigneshv points out that Apple has discouraged the use of cursors. And that's also what I heard from people who attended the Tech Talks this week. So a modified approach could be to use an invisible cursor on the SKScene. With each movement on the remote, perform a hit test. Highlight the node that's under the invisible cursor, but don't take action until you get a trackpad click. When you first switch into node-select mode, you could even put the invisible cursor over the largest or most central node, to provide some preliminary visual feedback.

Depending on your node graph, this could get complex. I could imagine generating a Voronoi diagram based on the SCNNode centers, and using remote gestures to move from polygon to polygon.

One more idea: instead of the hidden, implied Voronoi diagram, use a simple rectangular (or hex?) grid to track the cursor. The hit testing becomes more complex: you could have multiple nodes under each grid cell. But the grid becomes easier to construct, and easier to navigate.

3
On

I know the original question was asked in 2015 but Apple introduced the focus engine to SceneKit and SpriteKit in tvOS 11. See WWDC 2017.

0
On

Now seems to be built-in, at last ?

https://developer.apple.com/documentation/uikit/focus-based_navigation/adding_user-focusable_elements_to_a_tvos_app

"To make a SceneKit or SpriteKit node focusable, set the focusBehavior property of the node to focusable, as shown below.

node.focusBehavior = .focusable

https://developer.apple.com/documentation/spritekit/sknode/controlling_user_interaction_on_nodes