Check if OnscreenText object is above the off-screen

First of all, in general, is it possible to check anything such as a 3D object that is not within the screen view? If so, then how is it possible?

My reasoning is to make a credit screen where once the entire text is above the screen’s viewing point (by having the text have a task assigned to decrement the y coordinates), it would initiate a function to destroy the OnscreenText object and anything else that relates to the Credits class.

I have thought of using a counter that increments by one for each text position going up by one and checks if the counter’s nth number has reached, but this would require precise timing, which would be cumbersome to maintain should the text be changed later.

To simply put, what is a better way to check if the OnscreenText object is above the screen without resorting to using a counter?

While I’m not aware of a method that accurately checks whether an entire object is in view, there is a method that will check whether a point is in view: The “isInView” method of LensNode.

Combined with some additional logic, I could see this providing a reasonably accurate means of determining whether an object were in view.

Alternatively, one could potentially perform an additional rendering of the scene, with the target object displayed in a solid colour and all else in black, and then check for the presence of that colour.

And finally, this forum-post describes an approach that compares the bounds of the camera-frustum against the bounds of the object, as I understand it.

This, however, is easier I believe:

OnscreenText is a wrapper around TextNode, and TextNode has a method that returns the bottom-extent of the object. Thus you should be able to just compare the object’s bottom-extent against the screen’s top: it should be off-screen when its bottom plus its vertical position is higher than the z-coordinate of the top of the screen plus the TextNode’s height.

(OnscreenText is a NodePath, and its TextNode is the node to which it points. Thus you should be able to access an OnscreenText’s TextNode by calling its “node” method, like so: myTextNode = myOnscreenText.node(). The vertical position of the OnscreenText might be retrieved via its “getTextPos” method, or via the normal “getPos”, depending on how you place it, I think.)

Is the text in the 3D scene or 2D scene? For a 2D scene it is a lot easier, you just have to check the Z value, within some margin.

I think implementing a generic solution for checking whether an OnscreenText is entirely out side the camera frustum is overengineering and that just implementing a timeout or a rough Z position check after some generous margin would be just fine for most purposes. An OnscreenText that is out of view already takes up pretty much no time to render.

If you do want to do it the “right” way, then you could take the bounding volume of the text node and check it for intersection against the camera’s lens’ frustum. You do have to transform the bounding volume by the relative transformation matrix to the camera first.