how much of my obstructed object can I see?

What would be the ways I could calculate how much of my red cub I can see?

I’m interested in both quick/rough approaches and high precision/accuracy.

Any thoughts?


Since your object is a 3d object i guess you mean how much of the 2d projection of the object can be seen or how much is obstructed.

The straignt forward precise solution is to use a special replacement shader which renders the visible faces with one solid color and the hidden (depth fail parts) with another solid color. When you render the result to a rendertexture you can grab the informations with ReadPixels and analyse the ratio between the two colors… Make sure that your object renders after all normal opaque geometry (renderqueue). So the shader has two passes. One with the normal ZTest LEqual for the visible part and another pass with ZTest Greater for the invisible part. Of course make sure you switch off all lighting and blending so you get the actual color.

For approximations there are many different solutions. One of the simplest is to do a bunch of raycasts to a couple of key points on the object and see if you reach the object or not. Of course this will test against the collider and not necessarily the visible mesh (depening on the collider used). Though since this is just a very rough approximation this doesn’t really matter. The more raycasts you use the more precise the result.

A generic variation of the raycast method that doesn’t require any knowledge of the object’s shape is to simple cast a linear grid of raycasts according to the bounding box of the object. Do the raycasts two times. First just use Collider.Raycast of your object (or use a layermask to ensure you only cast against your object). This information tells you how many raycasts can actually hit the object at all, even with no obstacles. The second time you do the normal Physics.Raycast and see which of the previous successful raycasts now are obstructed.