# Parallax Camera Effect

**URL:** <https://forum.babylonjs.com/t/parallax-camera-effect/34376>\
**Category:** Demos and projects\
**Created:** [September 30, 2022, 1:22pm UTC](https://forum.babylonjs.com/t/parallax-camera-effect/34376 "2022-09-30T13:22:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cedric](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cedric/32/2742_2.png) [@Cedric](https://forum.babylonjs.com/u/Cedric)\
**Post date:** [September 30, 2022, 1:22pm UTC](https://forum.babylonjs.com/t/parallax-camera-effect/34376/1 "2022-09-30T13:22:40Z")

</div>

For quite some time, I’ve wanted to see how parallax camera works.  
Basically, it allows to deform perspective just like your screen becomes a window.  
Then, you move your point of view and perspective changes accordingly.

A famous video showing the effect:

[![](https://img.youtube.com/vi/Jd3-eiid-Uw/hqdefault.jpg "Head Tracking for Desktop VR Displays using the WiiRemote") ](https://www.youtube.com/watch?v=Jd3-eiid-Uw)

Now the PG:

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

The important function is `projViewFromRectangle`.  
From a world eye position, a projection and view destination matrix, a view ratio (rendering width / rendering height) and a fov, it computes the projection and view so the border of the screen corresponds to a world size quad of 1 at Z = 0;

This is also usable for eye position for VR. One picture is better than a thousand words:

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/a/f/aff481e867ee551ecc4c17b67fcab5fd95039dbd.png)  
from [https://www.researchgate.net/figure/Camera-parallax-and-orientation-methods-toe-in-on-the-left-and-off-axis-on-the\_fig5\_323459560](https://www.researchgate.net/figure/Camera-parallax-and-orientation-methods-toe-in-on-the-left-and-off-axis-on-the_fig5_323459560)

The most important part of the code is to compute the distance from the eye to the plane at Z = 0 depending on a fixed fov.

```auto
const factor = Math.cos(halfFov) / Math.sin(halfFov);

```

Then, the eye Z, knowing the plan is easy to compute.  
The main trick is to add a translation to the projection matrix. This is the main effect that deforms the projection.

Hope this helps doing nice effects with Babylon.js!

---

<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:** [September 30, 2022, 1:40pm UTC](https://forum.babylonjs.com/t/parallax-camera-effect/34376/2 "2022-09-30T13:40:48Z")

</div>

This is awesome !!!

---

<div class="post-metadata">

**Author:** ![brianzinn](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/brianzinn/32/28013_2.png) [@brianzinn](https://forum.babylonjs.com/u/brianzinn)\
**Post date:** [September 30, 2022, 7:26pm UTC](https://forum.babylonjs.com/t/parallax-camera-effect/34376/3 "2022-09-30T19:26:20Z")

</div>

Nice! Would be fun to try wired to tensorflow.js (face landmark?)!

---

<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:** [October 3, 2022, 12:27pm UTC](https://forum.babylonjs.com/t/parallax-camera-effect/34376/4 "2022-10-03T12:27:46Z")

</div>

@Cedric you just need to find a tiny spot in the doc to reference it 🙂
