Calculate how visible an object is?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By scooty

I’m wondering if there’s an easy way to get sort of a percent of how visible an object is in the 2D viewport.

:bust_in_silhouette: Reply From: sash-rc

There are Rect2 functions: Rect2 — Godot Engine (stable) documentation in English

Use intersection (clip()) of object and viewport rectangle. Then use area of this intersection and area of object.

var visibility_rate : float = 0.0 # 0..1, multiply by 100 for "percentage"
if intersection_area > 0.0:
    visibility_rate = intersection_area / object_area