Babylon.js: How to load a .babylon file produced with Blender
Join the DZone community and get the full member experience.
Join For FreeIn 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.
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: http://www.babylonjs.com/Blender2Babylon.zip.
To install it in Blender, please follow this small guide:
- Unzip the file to your Blender’s plugins folder (Should be C:\Program Files\Blender Foundation\Blender\2.67\scripts\addons 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.
Loading your .babylon Inside your page/app
First of all, you should create a simple html web page:
<!DOCTYPE html> <html xmlns="http://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: 0; margin: 0; 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
Playing with your scene
The result is as expected: a orange plane lighted by a point light with a floating sphere using an RGBA texture for its diffuse color. You can use the mouse and the cursors keys to move:
For IE11 preview, you can also directly try the result just here: http://www.babylonjs.com/tutorials/blogs/loadScene/loadscene.html
The full source code is also available there:
http://www.babylonjs.com/tutorials/blogs/loadScene/loadScene.zip
Enjoy!
Others chapters
If you want to go more deeply into babylon.js, here are some useful links:
- Introducing Babylon.js: http://blogs.msdn.com/b/eternalcoding/archive/2013/06/27/babylon-js-a-complete-javascript-framework-for-building-3d-games-with-html-5-and-webgl.aspx
- How to load a scene exported from Blender: http://blogs.msdn.com/b/eternalcoding/archive/2013/06/28/babylon-js-how-to-load-a-babylon-file-produced-with-blender.aspx
Published at DZone with permission of David Catuhe, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments