# Mesh with shadow returns different Vector3.Project result in onBeforeDrawObservable

**URL:** <https://forum.babylonjs.com/t/mesh-with-shadow-returns-different-vector3-project-result-in-onbeforedrawobservable/35891>\
**Category:** Questions\
**Tags:** lighting, mesh, shadows\
**Created:** [November 22, 2022, 3:27am UTC](https://forum.babylonjs.com/t/mesh-with-shadow-returns-different-vector3-project-result-in-onbeforedrawobservable/35891 "2022-11-22T03:27:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rainbow\_Doge](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/rainbow_doge/32/20433_2.png) [@Rainbow\_Doge](https://forum.babylonjs.com/u/Rainbow_Doge)\
**Post date:** [November 22, 2022, 3:27am UTC](https://forum.babylonjs.com/t/mesh-with-shadow-returns-different-vector3-project-result-in-onbeforedrawobservable/35891/1 "2022-11-22T03:27:09Z")

</div>

Hi everyone,

I have a piece of work that is using `onBeforeDrawObservable` of the mesh to check if mesh is inside/outside viewport. It’s been working great until I start to add shadow to mesh.

In this playground [Mesh with no shadow](https://playground.babylonjs.com/#IFYDRS#431) when I log the Vector3.Project result in observable, the result is stable no matter where mesh is located (inside or outside of viewport). For example:

```auto
...
position e {_isDirty: true, _x: 798.9455910326021, _y: 576.1805893373077, _z: 0.9860939636044204}
position e {_isDirty: true, _x: 798.9455910326021, _y: 576.1805893373077, _z: 0.9860939636044204}
position e {_isDirty: true, _x: 798.9455910326021, _y: 576.1805893373077, _z: 0.9860939636044204}
position e {_isDirty: true, _x: 798.9455910326021, _y: 576.1805893373077, _z: 0.9860939636044204}
...

```

However when I add shadow to the mesh in this playground [Mesh with shadow on](https://playground.babylonjs.com/#IFYDRS#430), when mesh or/and shadow are inside viewport, Vector3.Project value starts to alternate between 2 different values. For example:

```auto
...
position e {_isDirty: true, _x: 375.00002564673196, _y: 776.0707440406622, _z: 0.002290388183970621}
position e {_isDirty: true, _x: 798.9455910326021, _y: 576.1805893373077, _z: 0.9860939636044204}
position e {_isDirty: true, _x: 375.00002564673196, _y: 776.0707440406622, _z: 0.002290388183970621}
position e {_isDirty: true, _x: 798.9455910326021, _y: 576.1805893373077, _z: 0.9860939636044204}
...

```

It seems that it’s alternating between position of Mesh and shadow? Once mesh and shadow are both outside of viewport, Vector3.Project value starts to be stable value again.

I wonder how I can get correct Vector3.Project value that won’t be affected by shadow postition inside onBeforeDrawObservable?

* * *

**Updates**

Both methods mentioned in the marked solution worked. I chose `onBeforeRenderObservable` methoed in the project. Thanks!

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [November 22, 2022, 9:41am UTC](https://forum.babylonjs.com/t/mesh-with-shadow-returns-different-vector3-project-result-in-onbeforedrawobservable/35891/2 "2022-11-22T09:41:02Z")

</div>

`onBeforeDrawObservable` is called each time a mesh (submesh in fact) is drawn. When shadows are visible, the mesh is drawn twice, once in the shadow map and once in the regular rendering pass.

You can check that the current render pass is not the shadow render pass by checking:

```javascript
if (engine.currentRenderPassId !== shadowGenerator.getShadowMap().renderPassId)
    console.log('position', position)

```

You can also use `onBeforeRenderObservable` instead of `onBeforeDrawObservable` as the former is only called during the regular render pass.
