# Method to check if mesh is visible in viewport

**URL:** <https://forum.babylonjs.com/t/method-to-check-if-mesh-is-visible-in-viewport/45606>\
**Category:** Questions\
**Tags:** occlusion\
**Created:** [November 10, 2023, 2:32pm UTC](https://forum.babylonjs.com/t/method-to-check-if-mesh-is-visible-in-viewport/45606 "2023-11-10T14:32:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leon](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/leon/32/30387_2.png) [@Leon](https://forum.babylonjs.com/u/Leon)\
**Post date:** [November 10, 2023, 2:32pm UTC](https://forum.babylonjs.com/t/method-to-check-if-mesh-is-visible-in-viewport/45606/1 "2023-11-10T14:32:09Z")

</div>

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.

> **[Locka](https://view.wec360.com/demo/webgl-package/webgl-exterior-with-unit-panel?menu=false)**
>
> Locka project

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.

> **[Babylon.js Playground](https://playground.babylonjs.com/#QDAZ80%235)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

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?

---

<div class="post-metadata">

**Author:** ![labris](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/labris/32/12576_2.png) [@labris](https://forum.babylonjs.com/u/labris)\
**Post date:** [November 10, 2023, 3:34pm UTC](https://forum.babylonjs.com/t/method-to-check-if-mesh-is-visible-in-viewport/45606/2 "2023-11-10T15:34:53Z")

</div>

Regarding your PG example.  
If you change `registerBeforeRender` to the observable like `onBeforeRenderObservable` it will do the same - [https://playground.babylonjs.com/#QDAZ80#556](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](https://playground.babylonjs.com/#QDAZ80#557)  
More docs here - [Observables | Babylon.js Documentation](https://doc.babylonjs.com/features/featuresDeepDive/events/observables)

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [November 10, 2023, 7:05pm UTC](https://forum.babylonjs.com/t/method-to-check-if-mesh-is-visible-in-viewport/45606/3 "2023-11-10T19:05:14Z")

</div>

`camera.isInFrustum(mesh)` should do it ?
