0
\$\begingroup\$

I have a number of spheres in my scene. I also have another object that I want to scale depending on the amount of a selected sphere that is visible (it can be occluded by multiple other spheres passing in front of it). Can anyone recommend an efficient way to accomplish this? My renderer is written in Metal. The calculation only depends on the visible region of one selected sphere.

\$\endgroup\$
3
  • \$\begingroup\$ Is the view rectangle also taken into account for the visible region? Is the visible region measured in the view coordinates (pixels) or in sphere coordinates (portion of the surface area of the sphere)? Trying to do the calculation analytically will probably be intractable (will blow up exponentially with the number of spheres) - if you are using view coordinates, probably easiest to render off-screen and count pixels. \$\endgroup\$
    – Roy Ward
    Commented Jun 1, 2023 at 8:26
  • 2
    \$\begingroup\$ Do you want to know how what percent is visible of the spherical surface area, or how much of the circle-shaped silhouette of the sphere? Those would result in different numbers and would probably require vastly different approaches. \$\endgroup\$
    – Philipp
    Commented Jun 1, 2023 at 9:20
  • \$\begingroup\$ Ideally the visible rectangle would be taken into account. I only need to know the percentage of the silhouette. \$\endgroup\$
    – BinaryGuy
    Commented Jun 2, 2023 at 9:49

1 Answer 1

2
\$\begingroup\$

Trying to do this analytically will get intractable very very fast - adding each new sphere will increase the complexity substantially (not sure if it is exponential or not, need to think about that), as each pair of spheres needs to be checked for overlap so you don't double count.

Assuming that you are using view coordinates and don't need super high accuracy, one approach is rendering into an off screen buffer with the selected sphere set to one particular color, and the other spheres a different color or black. Use whatever resolution you need to get the desired accuracy and count pixels of the selected color.

It looks like you can then do the pixel counting on the GPU, which is great in this case, because the buffer is already there: https://stackoverflow.com/questions/44346198/counting-coloured-pixels-on-the-gpu-theory

Of course, this approach doesn't work if you want it in terms of sphere surface area.

\$\endgroup\$
2
  • \$\begingroup\$ This sounds like an interesting approach I’ll give it a go. \$\endgroup\$
    – BinaryGuy
    Commented Jun 2, 2023 at 9:59
  • 2
    \$\begingroup\$ I think you can use hardware occlusion queries to check pixel coverage directly based on a depth map, without needing to render a specifically tagged colour. \$\endgroup\$
    – DMGregory
    Commented Jun 2, 2023 at 13:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .