Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if node is visible in screen or not in SceneKit?

Tags:

ios

scenekit

I have an arrow in pointofview which points towards a node. I want that arrow to appear only if the node is not visible in the screen.

I have found an approach:
projectPoint 8 corners of node's boundingBox and check if:
1) Any point lies inside screen
2) Any edge from the points lie inside screen
3) Any plane lie inside screen

But it seems very big hard and messy.

So is there any simpler approach to it?

like image 388
Alok Subedi Avatar asked Dec 15 '17 08:12

Alok Subedi


1 Answers

You can use isNode(_:insideFrustumOf:) to check if the bounding box of a given node intersects the view frustum of another node's point of view to indicate whether or not that node might be visible.

It's an instance method on SCNSceneRendered which means that it's available on SCNView:

if let pointOfView = sceneView.pointOfView {
    let isMaybeVisible = sceneView.isNode(yourNode, insideFrustumOf: pointOfView)
    // `yourNode` is in the scene's view frustum and might be visible. 
}

Note, as documented, that this does not perform occlusion testing. This means that a node that appears completely behind another node (i.e. is fully occluded) may not be visible but is still inside the view frustum.

like image 130
David Rönnqvist Avatar answered Sep 26 '22 14:09

David Rönnqvist