I am developing an iOS app with SwiftUI and SceneKit. My scene has only 2 visible nodes.
I want to tap the node that is in front of the other, and get the tap position.
I thus implemented the following View:
SceneKitView(viewModel: viewModel)
.gesture(
SpatialTapGesture(count: 1)
.onEnded() { event in
guard let renderer = viewModel.lastRenderer else { return }
let hits = renderer.hitTest(event.location, options: nil)
print("Nr hits: \(hits.count)")
}
)
When I set a breakpoint at the print statement and tap the visible node on the screen, renderer is defined, and it has the correct scene (verified by lldb). Moreover, event.location is a valid point in the middle of the screen.
But nr of hits is always 0.
I don't understand why hitTest does not hit the tapped object, since I don't have any category mask set.
Any help is welcome!
Problem solved, although I don't understand why:
I used a SCNCamera with
camera.zNear = 0.001
camera.zFar = 40000
With this setting, the node to be hit was visually displayed, i.e. the setting was correct, but hitTest did not hit the node.
When I replaced both lines by
camera.automaticallyAdjustsZRange = true
nothing changed visually, but hitTest finds the node.