3

Is it possible to know I a mesh renderer is not visible due to the fact its being occluded by another gameObject? Up to what I researched, Rederer.isVisible responds to "isRendered" and scene params needs to be baked. So I wonder if there could be a simple way to know if an scene element is rendered on top of other in a simple manner. Larger explanation below.

Consider a simplified use case, with a camera (with occlusion culling activated), an occluder and an occludee such as this:

enter image description here

With the shadows options disabled (for both occluder and occludee. I would think disabling this is only needed for the ocludee but disabled for both just in case):

enter image description here

And a simple script to check visibility options:

using UnityEngine;

public class RenderCheck : MonoBehaviour
{
    MeshRenderer mr;

    private void Start()
    {
        mr = GetComponent<MeshRenderer>();
    }
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space)) {
            bool isVisible = mr.isVisible;
            Debug.Log($"cube visible: {isVisible}");
        }
    }

    void OnBecameVisible() {
        Debug.LogError($"{this.GetHashCode()} VISIBLE!!");
    }

    void OnWillRenderObject() {
        Debug.LogError($"{this.GetHashCode()} VISIBLE!!");
    }
}

The result I expected is that the ocludee .isVisible would return false, and that OnWillRenderObject() would be executed constantly after OnBecameVisible() is executed on the event of the occludee been moved out the occluders back, so into the sight of the camera. However I get .isVisible = true and the log thrown by the OnWillRenderObject().

I am aware of the fact that the Scene view cameras will also cause this value to be true. The explained outcome considers no scene camera and only the gamePlay camera (with occlusion culling activated). I got this approach to work baking the scene occlusion options following the documentation steps. But my question here more if its rendered or not regarding the oclussion settings, is to know if a gameobject or a mesh is rendered "on top" of other (in the case of culling not being applied). Unity needs to handle that as it is how the render takes place in the scene.

There seem to be a bunch of useful options here for what I am after, however, for .isVisible to work I need to set and bake the occusion scene options (afaik). So .isVisible responds more to "isRendered" fact. According to the documentation shouldn't Renderer.isVisible be giving the result I expected in a direct/simple manner and not be influenced by the fact of being rendered or not by dynamic occlusion?

Edit: As far as I checked, seems that only a raycast can address blocking by geometry. All these other things mean "visible" in the sense that the camera's rendering pipeline has to consider the object in some way. With a raycast I can get the "physical" block, determined by a collision. Isn't there any way to determine the "graphical" block? As I said, that is figured out somewhere soas to be renedered accordingly in the display screen, so is that info available somehow?

rustyBucketBay
  • 4,320
  • 3
  • 17
  • 47
  • The meaning of `isVisible` is often confused. Basically it means "does in any way play a role in the rendering of the scene" .. this includes e.g. shadows or anything else that influences the rendering of the scene maybe indirectly. Afaik there is no built-in standard way of finding (not) occluded models – derHugo May 22 '21 at 17:43
  • Thanks for your comment Mr. DerHugo. Yeah, seems so, even if the shadow options are disabled in the gameobject component... – rustyBucketBay May 22 '21 at 17:49
  • it seems that "visible" things works just that way, raycast is the right way to check whether object is renderer on top of something. – flankechen Jan 17 '23 at 07:36

0 Answers0