简体   繁体   中英

SceneKit, test if SCNNode completely visible

I have a 2 SCNNode next to each other ( 1 big box and 1 small box ).

I rotate its parent so that the small box sometimes is not visible any more in the camera, is it possible to know when the small box is not visible anymore because its behind the big box?

my current structure

Scene - RootNode 
           - SCNNode (camera)
           - SCNNode (parent)
                - SCNNode (Big box)
                - SCNNode (Small box)

What you're looking for is called occlusion testing , and it's not something SceneKit offers an API to do. In the general case, it's a big hairy problem.

Since you're just working with boxes, though, there are pretty decent ways to fake it. If the small box is hidden behind the large box, the lines from the camera position to each of the small box's eight corners will intersect the large box.

So:

  1. Get the positions of the camera and each of those corners in the coordinate system of a node containing both (the root node). For the box corners, you can either take the position of the box node and do the math to get the corners, or use the SCNBoundingVolume protocol. You might need to use some of the SCNNode coordinate conversion methods to get all of your points in the same space.

  2. For each corner, do a ray test with the hitTestWithSegmentFromPoint:toPoint:options: method.

  3. If all eight of those hit tests returns the big box, then the small box is completely hidden behind the big box.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM