# What is the correct way to render on demand

**URL:** <https://forum.babylonjs.com/t/what-is-the-correct-way-to-render-on-demand/17256>\
**Category:** Questions\
**Created:** [January 7, 2021, 8:28am UTC](https://forum.babylonjs.com/t/what-is-the-correct-way-to-render-on-demand/17256 "2021-01-07T08:28:32Z")\
**Posts on this page:** 14\
**Page:** 2

<div class="post-metadata">

**Author:** ![samevision](https://avatars.discourse-cdn.com/v4/letter/s/73ab20/32.png) [@samevision](https://forum.babylonjs.com/u/samevision)\
**Post date:** [June 22, 2021, 8:56pm UTC](https://forum.babylonjs.com/t/what-is-the-correct-way-to-render-on-demand/17256/22 "2021-06-22T20:56:55Z")

</div>

Thanks, @Evgeni_Popov!

That‘s better. Do you have an idea why it slows down faster than with normal render loop?

[https://www.babylonjs-playground.com/#UNB24W#8](https://www.babylonjs-playground.com/#UNB24W#8)

EDIT:

Setting the parameters for scene.render() seems to fix that problem: scene.render(false, true): [https://www.babylonjs-playground.com/#UNB24W#10](https://www.babylonjs-playground.com/#UNB24W#10)

---

<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:** [June 22, 2021, 9:28pm UTC](https://forum.babylonjs.com/t/what-is-the-correct-way-to-render-on-demand/17256/23 "2021-06-22T21:28:10Z")

</div>

I think you can remove the manual call to `camera.update()` and pass `true` as the first parameter to `render`:

[https://www.babylonjs-playground.com/#UNB24W#11](https://www.babylonjs-playground.com/#UNB24W#11)

---

<div class="post-metadata">

**Author:** ![dennemark](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/dennemark/32/10745_2.png) [@dennemark](https://forum.babylonjs.com/u/dennemark)\
**Post date:** [July 15, 2021, 12:23pm UTC](https://forum.babylonjs.com/t/what-is-the-correct-way-to-render-on-demand/17256/24 "2021-07-15T12:23:51Z")

</div>

Hi,  
nice thread! The example by @Evgeni_Popov is done with the ArcRotateCamera. Any chance to do it with other cameras such as FreeCamera, too? FreeCamera does not seem to expose an inertia value.

---

<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:** [July 15, 2021, 1:45pm UTC](https://forum.babylonjs.com/t/what-is-the-correct-way-to-render-on-demand/17256/25 "2021-07-15T13:45:46Z")

</div>

It seems you can test `cameraDirection` and `cameraRotation`:  
[https://www.babylonjs-playground.com/#UNB24W#13](https://www.babylonjs-playground.com/#UNB24W#13)

---

<div class="post-metadata">

**Author:** ![dennemark](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/dennemark/32/10745_2.png) [@dennemark](https://forum.babylonjs.com/u/dennemark)\
**Post date:** [July 16, 2021, 8:49am UTC](https://forum.babylonjs.com/t/what-is-the-correct-way-to-render-on-demand/17256/26 "2021-07-16T08:49:12Z")

</div>

nice, thanks a lot! 🙂

---

<div class="post-metadata">

**Author:** ![dennemark](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/dennemark/32/10745_2.png) [@dennemark](https://forum.babylonjs.com/u/dennemark)\
**Post date:** [July 16, 2021, 9:07am UTC](https://forum.babylonjs.com/t/what-is-the-correct-way-to-render-on-demand/17256/27 "2021-07-16T09:07:33Z")

</div>

Small improvement - Hardware Scaling on Movement. Interesting would be PostProcessing like SSAO that appears only on first render, too.  
[https://www.babylonjs-playground.com/#UNB24W#14](https://www.babylonjs-playground.com/#UNB24W#14)

---

<div class="post-metadata">

**Author:** ![vimcaw](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/vimcaw/32/5719_2.png) [@vimcaw](https://forum.babylonjs.com/u/vimcaw)\
**Post date:** [October 21, 2021, 9:30am UTC](https://forum.babylonjs.com/t/what-is-the-correct-way-to-render-on-demand/17256/28 "2021-10-21T09:30:21Z")

</div>

But that will cause some input doesn’t work, such as the moving by the keyboard in FreeCamera, having a look [https://www.babylonjs-playground.com/#UNB24W#13](https://www.babylonjs-playground.com/#UNB24W#13), moving by the keyboard (arrow keys) doesn’t work, after uncommenting the `camera.update()`, anything works well.

After searching the source code, I found keyboard inputs checking (`camera._checkInputs()`) only called by `camera.update()`:

> <https://github.com/BabylonJS/Babylon.js/blob/e7b5cd8ce3a9a1cfdb463cd7a4122c58c7847693/src/Cameras/camera.ts#L608-L613>

So I think `camera.update()` is needed.

---

<div class="post-metadata">

**Author:** ![bghgary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bghgary/32/48532_2.png) [@bghgary](https://forum.babylonjs.com/u/bghgary)\
**Post date:** [October 21, 2021, 5:03pm UTC](https://forum.babylonjs.com/t/what-is-the-correct-way-to-render-on-demand/17256/29 "2021-10-21T17:03:25Z")

</div>

We can use `camera.onViewMatrixChangedObservable` to do this so that you don’t need special logic for different cameras:

[https://www.babylonjs-playground.com/#UNB24W#15](https://www.babylonjs-playground.com/#UNB24W#15)

This is also a bit lighter on the CPU when nothing is happening since it only calls `camera.update()`.

---

<div class="post-metadata">

**Author:** ![vimcaw](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/vimcaw/32/5719_2.png) [@vimcaw](https://forum.babylonjs.com/u/vimcaw)\
**Post date:** [October 22, 2021, 1:45am UTC](https://forum.babylonjs.com/t/what-is-the-correct-way-to-render-on-demand/17256/30 "2021-10-22T01:45:50Z")

</div>

It works well, thanks a lot!

---

<div class="post-metadata">

**Author:** ![7om](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/7om/32/11158_2.png) [@7om](https://forum.babylonjs.com/u/7om)\
**Post date:** [February 17, 2023, 2:52pm UTC](https://forum.babylonjs.com/t/what-is-the-correct-way-to-render-on-demand/17256/31 "2023-02-17T14:52:48Z")

</div>

Most of the solutions shown here do no longer work since version 5.29 (see [On-demand rendering solution broken since 5.29](https://forum.babylonjs.com/t/on-demand-rendering-solution-broken-since-5-29/36379))

---

<div class="post-metadata">

**Author:** ![bghgary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bghgary/32/48532_2.png) [@bghgary](https://forum.babylonjs.com/u/bghgary)\
**Post date:** [February 17, 2023, 6:24pm UTC](https://forum.babylonjs.com/t/what-is-the-correct-way-to-render-on-demand/17256/32 "2023-02-17T18:24:49Z")

</div>

cc @PolygonalSun

I don’t see why `camera.onViewMatrixChangedObservable` solution won’t work. @7om Do you have a repro?

---

<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:** [February 17, 2023, 6:44pm UTC](https://forum.babylonjs.com/t/what-is-the-correct-way-to-render-on-demand/17256/33 "2023-02-17T18:44:02Z")

</div>

Depending on what’s going on in your scene, you may want to tie into other events like animations or pointer events (ie: hover events, pointer click) where a scene changes without the View Matrix notifications.  
[react-babylonjs/RenderOnDemand at master · brianzinn/react-babylonjs (github.com)](https://github.com/brianzinn/react-babylonjs/blob/master/packages/react-babylonjs/src/customComponents/RenderOnDemand#L47)

Also, in the engine component there is an opt-in mechanism to not render if the canvas is not visible (ie: scrolled off screen):  
[react-babylonjs/Engine.tsx at master · brianzinn/react-babylonjs (github.com)](https://github.com/brianzinn/react-babylonjs/blob/master/packages/react-babylonjs/src/Engine.tsx#L32)

maybe those snippets will be of use. cheers.

---

<div class="post-metadata">

**Author:** ![7om](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/7om/32/11158_2.png) [@7om](https://forum.babylonjs.com/u/7om)\
**Post date:** [February 20, 2023, 4:56am UTC](https://forum.babylonjs.com/t/what-is-the-correct-way-to-render-on-demand/17256/34 "2023-02-20T04:56:28Z")

</div>

> [@bghgary](#):
>
> [https://www.babylonjs-playground.com/#UNB24W#15](https://www.babylonjs-playground.com/#UNB24W#15)

Sure, just test your own PG: [https://www.babylonjs-playground.com/#UNB24W#15](https://www.babylonjs-playground.com/#UNB24W#15)

It is broken for me. Sometimes - when I click and drag - the view does not change. When I release the mouse button, the view jumps to the new position. @PolygonalSun gives some explanations on what has changed in the thread I mentioned above.

---

<div class="post-metadata">

**Author:** ![bghgary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bghgary/32/48532_2.png) [@bghgary](https://forum.babylonjs.com/u/bghgary)\
**Post date:** [February 21, 2023, 5:19pm UTC](https://forum.babylonjs.com/t/what-is-the-correct-way-to-render-on-demand/17256/35 "2023-02-21T17:19:28Z")

</div>

Bug filed: [onViewMatrixChangedObservable not firing with non-standard render loops · Issue #13551 · BabylonJS/Babylon.js (github.com)](https://github.com/BabylonJS/Babylon.js/issues/13551)

We will fix it soon. Sorry for the troubles.

[Previous page](https://forum.babylonjs.com/t/what-is-the-correct-way-to-render-on-demand/17256.md?page=1)
