Detecting whether an object is in view or hovered over
I have quite a few ideas in mind with many questions for each, and figured I'd start with an essential question which I'm going to need for many things I'll be doing. I'd like to know how I can probe the visibility of an object, as well detect whether the player is hovering over certain objects. Overall I'm curious about 3 different detection types which I'd prefer doing individually:
- How do I know if an object is within the player's field of view? Can you verify if a mesh is intersecting the active camera's FOV? Ideally Godot can cheaply check whether any vertice of the model intersects the camera and thus part of the mesh is visible, if not an estimated bounding box would work just as nicely.
- How can I tell whether an object is fully hidden behind an opaque surface? Can a Godot script currently assert when a model is poking behind a solid surface, and when it's fully masked by another model from the camera's perspective?
- What is the easiest way to check whether the mouse cursor or crosshair (virtual dot in the center of the screen) is positioned over an object and the player is hovering over them? I imagine a ray trace would be the easiest way, but am wondering if such detection can also be done in the 2D screen space which may be a cheaper solution.
Note that I'm not necessarily looking for this as a means of optimizing performance, as I'm aware Godot does (or will) support occlusion culling natively and that shouldn't be coded by projects. This is mainly to allow certain mechanics to work properly, the most common being spawners: In many cases you wish to have an invisible object spawn a character or item, however you only want that to happen when the player isn't looking so they don't notice someone / something popping into existence in front of them... the spawner must activate either when it's outside the camera FOV or hidden by an opaque wall. This can also be used so characters know when the player is looking at them and other fun stuff. Hover detection is itself essential so you can highlight and click on objects to interact with them.
Also note that I aim to work primarily with visualscript. An answer for achieving those detections in gdscript will definitely help most developers, but if possible I'd like to know what vscript functions could be used as well. Existing demos in this regard would also be helpful if anyone has made any as of yet.