Method to check if mesh is visible in viewport

Hi!

I have some camera logic that does raycasting and tries to find an optimal place for the camera facing a building. This all works well, but my customers think the camera moves around to much.
so my though is that if I can check if the hitbox is not visible in the viewport, I’ll only do the animation then.

so if I click an apartment which is on the far side of the building the camera will animate.
but if the apartment is already in view, nothing will happen.

Try pressing on any of the units in either the list or 3d scene.

I found this example which seems do do what I want.

but it runs on every frame.

Is there a way I can do a one time operation and check if a mesh is visible in the viewport?

Regarding your PG example.
If you change registerBeforeRender to the observable like onBeforeRenderObservable it will do the same - https://playground.babylonjs.com/#QDAZ80#556
When you don’t need this observer anymore, just remove it according to your conditions. Here is the example with simple timeout - https://playground.babylonjs.com/#QDAZ80#557
More docs here - Observables | Babylon.js Documentation

camera.isInFrustum(mesh) should do it ?

1 Like