# Create a preloader

**URL:** <https://forum.babylonjs.com/t/create-a-preloader/12950>\
**Category:** Questions\
**Created:** [July 28, 2020, 3:42pm UTC](https://forum.babylonjs.com/t/create-a-preloader/12950 "2020-07-28T15:42:31Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![saifshk17](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/saifshk17/32/1418_2.png) [@saifshk17](https://forum.babylonjs.com/u/saifshk17)\
**Post date:** [July 28, 2020, 3:42pm UTC](https://forum.babylonjs.com/t/create-a-preloader/12950/1 "2020-07-28T15:42:31Z")

</div>

Hi people,

I have 2 heavy things in my scene, a 3D model and an image. First the smaller images load and then the heavier models load. Is there a way to assign a preloader so that the app only loads when everything else has been loaded?

```
<!DOCTYPE html>

```

```
<title>Babylon.js</title>

<!-- Babylon.js -->
<script src="https://code.jquery.com/pep/0.4.2/pep.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.6.2/dat.gui.min.js"></script>
<script src="https://preview.babylonjs.com/ammo.js"></script>
<script src="https://preview.babylonjs.com/cannon.js"></script>
<script src="https://preview.babylonjs.com/Oimo.js"></script>
<script src="https://preview.babylonjs.com/libktx.js"></script>
<script src="https://preview.babylonjs.com/earcut.min.js"></script>
<script src="https://preview.babylonjs.com/babylon.js"></script>
<script src="https://preview.babylonjs.com/inspector/babylon.inspector.bundle.js"></script>
<script src="https://preview.babylonjs.com/materialsLibrary/babylonjs.materials.min.js"></script>
<script src="https://preview.babylonjs.com/proceduralTexturesLibrary/babylonjs.proceduralTextures.min.js"></script>
<script src="https://preview.babylonjs.com/postProcessesLibrary/babylonjs.postProcess.min.js"></script>
<script src="https://preview.babylonjs.com/loaders/babylonjs.loaders.js"></script>
<script src="https://preview.babylonjs.com/serializers/babylonjs.serializers.min.js"></script>
<script src="https://preview.babylonjs.com/gui/babylon.gui.min.js"></script>
<script src="https://cdn.babylonjs.com/loaders/"></script>
<style>
    html,
    body {
        overflow: hidden;
        width: 100%;
        height: 100%;
        margin: 0;
        padding: 0;
    }

#renderCanvas {
    width: 100%;
    height: 100%;
    touch-action: none;
}
</style>
</head>
    <body>
        <canvas id="renderCanvas"></canvas>
        <script>
        var canvas = document.getElementById("renderCanvas");

    var engine = null;
        var scene = null;
        var sceneToRender = null;
        var createDefaultEngine = function () { return new BABYLON.Engine(canvas, true, { preserveDrawingBuffer: true, stencil: true }); };
    class Playground
    {
        static CreateScene(engine, canvas)
        {
        BABYLON.SceneLoader.ImportMesh("", "Models/", "scene.gltf", scene, function (vpMesh) {  

         }, undefined, undefined, '.gltf');
        var Lamp = new BABYLON.SpriteManager("Lamp", "RoomTexture/Lampe.png", 51, 98, scene);

            var spriteLamp = new BABYLON.Sprite("spriteLamp", Lamp);
            spriteLamp.position.x = 900;
            spriteLamp.position.y = -60;
            spriteLamp.position.z = -1200;

            spriteLamp.size = 220;

            spriteLamp.renderingGroupId = 3;
            Lamp.renderingGroupId = 3;
            return scene;
        }
    }

 createScene = function () { return Playground.CreateScene(engine, engine.getRenderingCanvas()); }

    var engine;

    try

    {

        engine = createDefaultEngine();

    } catch (e)

    {

        console.log("the available createEngine function failed. Creating the default engine instead");

        engine = createDefaultEngine();

    }

    if (!engine) throw 'engine should not be null.';

    scene = createScene();

    sceneToRender = scene

    engine.runRenderLoop(function ()

    {

        if (sceneToRender)

        {

            sceneToRender.render();

        }

    });

    // Resize

    window.addEventListener("resize", function ()

    {

        engine.resize();

    });

</script>

```

`Preformatted text`

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [July 28, 2020, 4:07pm UTC](https://forum.babylonjs.com/t/create-a-preloader/12950/2 "2020-07-28T16:07:05Z")

</div>

Hello, I’m not sure to understand

What do you want to delay load? The js files or your scene?

---

<div class="post-metadata">

**Author:** ![saifshk17](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/saifshk17/32/1418_2.png) [@saifshk17](https://forum.babylonjs.com/u/saifshk17)\
**Post date:** [July 28, 2020, 4:18pm UTC](https://forum.babylonjs.com/t/create-a-preloader/12950/3 "2020-07-28T16:18:43Z")

</div>

Sorry, what I meant was, I want to delay it. As in, I only want the user to see the scene once everything in the scene has been loaded. The includes images, meshes etc. So due to this I want to implement a preloader.

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [July 28, 2020, 4:21pm UTC](https://forum.babylonjs.com/t/create-a-preloader/12950/4 "2020-07-28T16:21:51Z")

</div>

oh so you can probably rely on:

```auto
scene.executeWhenReady(() => {
.. start rendering here
});
```

---

<div class="post-metadata">

**Author:** ![saifshk17](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/saifshk17/32/1418_2.png) [@saifshk17](https://forum.babylonjs.com/u/saifshk17)\
**Post date:** [July 28, 2020, 4:31pm UTC](https://forum.babylonjs.com/t/create-a-preloader/12950/5 "2020-07-28T16:31:36Z")

</div>

Thanks, where should I paste this? Could you please show me in a sample background player?  
Also can you suggest a preloader example that I use along with this?

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [July 28, 2020, 4:40pm UTC](https://forum.babylonjs.com/t/create-a-preloader/12950/6 "2020-07-28T16:40:17Z")

</div>

Can you share your current code somewhere?

---

<div class="post-metadata">

**Author:** ![saifshk17](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/saifshk17/32/1418_2.png) [@saifshk17](https://forum.babylonjs.com/u/saifshk17)\
**Post date:** [July 28, 2020, 5:23pm UTC](https://forum.babylonjs.com/t/create-a-preloader/12950/7 "2020-07-28T17:23:45Z")

</div>

I will try to add it to Playground, it will take some time. In the meanwhile I have added the whole code here, where should I implement your solution?

---

<div class="post-metadata">

**Author:** ![saifshk17](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/saifshk17/32/1418_2.png) [@saifshk17](https://forum.babylonjs.com/u/saifshk17)\
**Post date:** [July 28, 2020, 5:27pm UTC](https://forum.babylonjs.com/t/create-a-preloader/12950/8 "2020-07-28T17:27:16Z")

</div>

> [@Deltakosh](#):
>
> `scene.executeWhenReady(() => {`

I got it, i added your solution as:

```
scene.executeWhenReady(() => {
    engine.runRenderLoop(function ()
    {
        if (sceneToRender)
        {
            sceneToRender.render();
        }
    });
});

```

Now I have to create a preloader showing percentage of completion. Is there a way to achieve this?

---

<div class="post-metadata">

**Author:** ![DisownedWheat](https://avatars.discourse-cdn.com/v4/letter/d/3be4f8/32.png) [@DisownedWheat](https://forum.babylonjs.com/u/DisownedWheat)\
**Post date:** [July 30, 2020, 3:11am UTC](https://forum.babylonjs.com/t/create-a-preloader/12950/9 "2020-07-30T03:11:59Z")

</div>

I’d use an [Asset Manager](https://doc.babylonjs.com/how_to/how_to_use_assetsmanager). It has an `onProgress` callback that you can use to update a loading bar on screen [Load Files with Assets Manager - Babylon.js Documentation](https://doc.babylonjs.com/how_to/how_to_use_assetsmanager#manager-callbacks-and-observables)

---

<div class="post-metadata">

**Author:** ![Pieter](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/pieter/32/13656_2.png) [@Pieter](https://forum.babylonjs.com/u/Pieter)\
**Post date:** [February 8, 2021, 11:43am UTC](https://forum.babylonjs.com/t/create-a-preloader/12950/10 "2021-02-08T11:43:27Z")

</div>

This way is interesting if there are a lot of smaller files equal in size.  
What if I have a more complex model with a larger size. Is there a way to present the actual progress based on x kb/ total kb loaded?

Thanks  
Pieter
