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.
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.