Babylon.js: How to load a .babylon file produced with Blender

In a previous post, I described Babylon.js, a brand new 3D engine for WebGL and JavaScript. Among others features, Babylon.js is capable of loading a JSON file through the .babylon file format.

During this post, I will show you how to use Babylon.js API to load a scene created with Blender.

One important thing to remember with Blender is to use the Blender Render if you want to export your scene to Babylon.js

Creating a scene and exporting a .babylon file with Blender

In my previous post, I already described how to install the .babylon exporter in Blender, but for the sake of comprehension, I copy/paste the process here:

First of all, please download the exporter script right here: https://github.com/BabylonJS/Babylon.js/tree/master/Exporters/Blender

To install it in Blender, please follow this small guide:

  • Unzip the file to your Blender’s plugins folder (Should be C:Program FilesBlender FoundationBlender2.67scriptsaddons for Blender 2.67 x64).
  • Launch Blender and go to File/User Préférences/Addon and select Import-Export category. You will be able to activate Babylon.js exporter.

  • Create your scene
  • Go to File/Export and select Babylon.js format. Choose a filename and you are done !

Once the exporter is installed, you can unleash your artist side and create the most beautiful scene your imagination can produce. In my case, it will be fairly simple:

  • A camera
  • A point light
  • A plane for the ground
  • A sphere

Just to be something a bit less austere, I will add some colors for the ground and the sphere:

I will also add a texture for the sphere. This texture will be used for the diffuse channel of the material:

Please pay attention to:

  • Use Alpha checkbox to indicate to Babylon.js to use alpha values from the texture
  • Color checkbox to indicate that this texture must be use for diffuse color

Once you are satisfied (You can obviously create a more complex scene), just go to File/Export/Babylon.js to create your .babylon file.

Using the Babylon.js Sandbox

There is a great and easy way to test your scene: the Babylon.js Sandbox. With a single drag’n’drop into a web page, you will be able to test your scene without any more setup:

https://blogs.msdn.com/b/davrous/archive/2013/12/17/designers-test-amp-create-your-webgl-3d-worlds-inside-the-babylon-js-sandbox-amp-editor.aspx

Loading your .babylon inside your page/app

First of all, you should create a simple html web page:

<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
    <title>Using babylon.js - How to load a scene</title>
    <script src="babylon.js"></script>
    <style>
        html, body {
            width: 100%;
            height: 100%;
            padding: ;
            margin: ;
            overflow: hidden;
        }

        #renderCanvas {
            width: 100%;
            height: 100%;
        }
    </style>
</head>
<body>
    <canvas id="renderCanvas"></canvas>
 </body>
</html>

This page is pretty simple because all you need is just a canvas and a reference to babylon.js.

Then you will have to use BABYLON.SceneLoader object to load your scene. To do so, just add this script block right after the canvas:

<script>
    if (BABYLON.Engine.isSupported()) {
        var canvas = document.getElementById("renderCanvas");
        var engine = new BABYLON.Engine(canvas, true);

        BABYLON.SceneLoader.Load("", "scene.babylon", engine, function (newScene) {
            // Wait for textures and shaders to be ready
            newScene.executeWhenReady(function () {
                // Attach camera to canvas inputs
                newScene.activeCamera.attachControl(canvas);

                // Once the scene is loaded, just register a render loop to render it
                engine.runRenderLoop(function() {
                    newScene.render();
                });
            });
        }, function (progress) {
            // To do: give progress feedback to user
        });
    }
</script>

the Load function takes the following parameters:

  • scene folder (can be empty to use the same folder as your page)
  • scene file name
  • a reference to the engine
  • a callback to give you the loaded scene (in my case, I use this callback to attach the camera to the canvas and to launch my render loop)
  • a callback for progress report

Once the scene is loaded, just wait for the textures and shaders to be ready, connect the camera to the canvas and let’s go!

Fairly simple, isn’t it?

Please note that the textures and the .babylon file must be side by side

Another function is also available to interact with .babylon files: BABYLON.SceneLoader.importMesh:

BABYLON.SceneLoader.ImportMesh("spaceship", "Scenes/SpaceDek/", "SpaceDek.babylon", scene, function (newMeshes, particleSystems) {
});

This function is intended to import meshes (with their materials and particle systems) from a scene to another. It takes the following parameters:

  • object name (if you omit this parameter, all the objects are imported)
  • scene folder (can be empty to use the same folder as your page)
  • scene file name
  • a reference to the target scene
  • a callback to give you the list of imported meshes and particle systems

Adding MIME types to your web server

You will need to authorize .babylon MIME type on your web server (and furthermore you will also need to authorize .babylonmeshdata MIME type if you want to use incremental loading)

For IIS, you will need to update your web.config with this section: