Babylon.js
Babylon.js is a powerful, beautiful, simple, and open game and rendering engine packed into a friendly JavaScript framework.
Top Related Projects
JavaScript 3D Library.
The HTML5 Creation Engine: Create beautiful digital content with the fastest, most flexible 2D WebGL renderer.
JavaScript game engine built on WebGL, WebGPU, WebXR and glTF
Cocos simplifies game creation and distribution with Cocos Creator, a free, open-source, cross-platform game engine. Empowering millions of developers to create high-performance, engaging 2D/3D games and instant web entertainment.
🇨🇭 A React renderer for Three.js
👑 Functional WebGL
Quick Overview
Babylon.js is a powerful, beautiful, simple, and open-source 3D engine built on WebGL, WebGPU, and JavaScript. It allows developers to create and display 3D graphics in a web browser without the need for any plug-ins. Babylon.js is designed to be easy to use for beginners while also offering advanced features for experienced developers.
Pros
- Easy to learn and use, with extensive documentation and community support
- Cross-platform compatibility, running on various devices and browsers
- High performance and optimized for web environments
- Rich set of features, including physics engines, particle systems, and VR/AR support
Cons
- Steeper learning curve compared to some 2D game engines
- Limited native mobile support (primarily web-based)
- Can be resource-intensive for complex scenes on low-end devices
- Smaller ecosystem compared to some other popular game engines
Code Examples
- Creating a basic scene:
const canvas = document.getElementById("renderCanvas");
const engine = new BABYLON.Engine(canvas, true);
const createScene = function () {
const scene = new BABYLON.Scene(engine);
const camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(0, 5, -10), scene);
camera.setTarget(BABYLON.Vector3.Zero());
const light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(0, 1, 0), scene);
const sphere = BABYLON.MeshBuilder.CreateSphere("sphere", {diameter: 2, segments: 32}, scene);
return scene;
};
const scene = createScene();
engine.runRenderLoop(function () {
scene.render();
});
- Adding interactivity:
sphere.actionManager = new BABYLON.ActionManager(scene);
sphere.actionManager.registerAction(
new BABYLON.ExecuteCodeAction(
BABYLON.ActionManager.OnPickTrigger,
function () {
sphere.scaling.x += 0.1;
sphere.scaling.y += 0.1;
sphere.scaling.z += 0.1;
}
)
);
- Implementing physics:
scene.enablePhysics(new BABYLON.Vector3(0, -9.81, 0), new BABYLON.CannonJSPlugin());
sphere.physicsImpostor = new BABYLON.PhysicsImpostor(
sphere,
BABYLON.PhysicsImpostor.SphereImpostor,
{ mass: 1, restitution: 0.9 },
scene
);
Getting Started
- Include Babylon.js in your HTML file:
<script src="https://cdn.babylonjs.com/babylon.js"></script>
- Create a canvas element:
<canvas id="renderCanvas"></canvas>
- Initialize the engine and create a scene:
const canvas = document.getElementById("renderCanvas");
const engine = new BABYLON.Engine(canvas, true);
const scene = new BABYLON.Scene(engine);
// Add your 3D objects and logic here
engine.runRenderLoop(function () {
scene.render();
});
- For more advanced features and detailed tutorials, visit the official Babylon.js documentation: https://doc.babylonjs.com/
Competitor Comparisons
JavaScript 3D Library.
Pros of Three.js
- Larger community and ecosystem, with more resources and third-party libraries
- More flexible and customizable, allowing for lower-level control
- Better performance for complex scenes with many objects
Cons of Three.js
- Steeper learning curve, especially for beginners
- Less built-in features and tools compared to Babylon.js
- Requires more manual setup and configuration for advanced features
Code Comparison
Three.js:
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
Babylon.js:
const canvas = document.getElementById("renderCanvas");
const engine = new BABYLON.Engine(canvas, true);
const scene = new BABYLON.Scene(engine);
const camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(0, 5, -10), scene);
camera.setTarget(BABYLON.Vector3.Zero());
Both libraries offer powerful 3D rendering capabilities, but Three.js provides more flexibility at the cost of complexity, while Babylon.js offers a more streamlined development experience with built-in features. The choice between them depends on project requirements and developer preferences.
The HTML5 Creation Engine: Create beautiful digital content with the fastest, most flexible 2D WebGL renderer.
Pros of PixiJS
- Lightweight and focused on 2D rendering, making it more performant for 2D applications
- Easier learning curve for developers familiar with 2D graphics
- Extensive plugin ecosystem for additional functionality
Cons of PixiJS
- Limited 3D capabilities compared to Babylon.js
- Smaller community and fewer learning resources
- Less suitable for complex 3D games or applications
Code Comparison
Babylon.js (3D scene creation):
const scene = new BABYLON.Scene(engine);
const camera = new BABYLON.FreeCamera("camera", new BABYLON.Vector3(0, 5, -10), scene);
const light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(0, 1, 0), scene);
const sphere = BABYLON.MeshBuilder.CreateSphere("sphere", {diameter: 2}, scene);
PixiJS (2D sprite creation):
const app = new PIXI.Application();
const sprite = PIXI.Sprite.from("image.png");
sprite.anchor.set(0.5);
sprite.x = app.screen.width / 2;
sprite.y = app.screen.height / 2;
app.stage.addChild(sprite);
Both libraries are powerful tools for creating interactive graphics, but they serve different purposes. Babylon.js excels in 3D rendering and complex game development, while PixiJS is optimized for 2D graphics and simpler applications. The choice between them depends on the specific requirements of your project.
JavaScript game engine built on WebGL, WebGPU, WebXR and glTF
Pros of PlayCanvas engine
- More lightweight and performance-focused, ideal for mobile and web-based games
- Built-in visual editor and asset pipeline for easier game development
- Strong focus on collaborative, real-time editing features
Cons of PlayCanvas engine
- Smaller community and ecosystem compared to Babylon.js
- Less extensive documentation and learning resources
- More limited features for non-game 3D applications
Code comparison
Babylon.js:
const scene = new BABYLON.Scene(engine);
const camera = new BABYLON.FreeCamera("camera", new BABYLON.Vector3(0, 5, -10), scene);
const light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(0, 1, 0), scene);
const sphere = BABYLON.MeshBuilder.CreateSphere("sphere", {diameter: 2}, scene);
PlayCanvas engine:
const app = new pc.Application(canvas);
const camera = new pc.Entity("camera");
camera.addComponent("camera", { clearColor: new pc.Color(0.1, 0.1, 0.1) });
app.root.addChild(camera);
const light = new pc.Entity("light");
light.addComponent("light", { type: "directional" });
app.root.addChild(light);
Both engines offer powerful 3D rendering capabilities, but Babylon.js provides a more extensive feature set and larger community, while PlayCanvas engine focuses on lightweight performance and collaborative development.
Cocos simplifies game creation and distribution with Cocos Creator, a free, open-source, cross-platform game engine. Empowering millions of developers to create high-performance, engaging 2D/3D games and instant web entertainment.
Pros of Cocos Engine
- More comprehensive game development ecosystem, including 2D and 3D support
- Better suited for mobile game development with optimized performance
- Extensive documentation and learning resources in multiple languages
Cons of Cocos Engine
- Steeper learning curve due to its broader feature set
- Less flexible for non-game applications compared to Babylon.js
- Smaller community and fewer third-party plugins
Code Comparison
Cocos Engine (TypeScript):
import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('HelloWorld')
export class HelloWorld extends Component {
start() {
console.log('Hello, Cocos!');
}
}
Babylon.js (JavaScript):
const canvas = document.getElementById("renderCanvas");
const engine = new BABYLON.Engine(canvas, true);
const scene = new BABYLON.Scene(engine);
const camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(0, 5, -10), scene);
engine.runRenderLoop(() => scene.render());
Both engines offer powerful features for 3D rendering and game development. Cocos Engine provides a more comprehensive game development ecosystem, while Babylon.js offers greater flexibility for various 3D applications beyond gaming. The choice between them depends on the specific project requirements and the developer's familiarity with each framework.
🇨🇭 A React renderer for Three.js
Pros of react-three-fiber
- Seamless integration with React ecosystem and state management
- Declarative approach to 3D scene creation
- Smaller learning curve for developers already familiar with React
Cons of react-three-fiber
- Less comprehensive documentation compared to Babylon.js
- Smaller community and ecosystem
- Limited built-in features, often requiring additional libraries
Code Comparison
Babylon.js:
const scene = new BABYLON.Scene(engine);
const camera = new BABYLON.FreeCamera("camera", new BABYLON.Vector3(0, 5, -10), scene);
const sphere = BABYLON.MeshBuilder.CreateSphere("sphere", { diameter: 2 }, scene);
react-three-fiber:
function Scene() {
return (
<Canvas>
<perspectiveCamera position={[0, 5, -10]} />
<mesh>
<sphereGeometry args={[1, 32, 32]} />
<meshStandardMaterial color="red" />
</mesh>
</Canvas>
);
}
Both libraries offer powerful 3D rendering capabilities, but react-three-fiber focuses on integrating Three.js with React, while Babylon.js provides a more comprehensive 3D engine with additional features out of the box. The choice between them often depends on the project requirements and the developer's familiarity with React and Three.js ecosystems.
👑 Functional WebGL
Pros of regl
- Lightweight and minimalistic, focusing on WebGL performance
- Flexible and low-level, allowing fine-grained control over rendering
- Functional programming approach, promoting composability and reusability
Cons of regl
- Steeper learning curve for beginners due to its low-level nature
- Less comprehensive feature set compared to Babylon.js
- Smaller community and ecosystem
Code Comparison
regl:
const drawTriangle = regl({
frag: `
precision mediump float;
uniform vec4 color;
void main() {
gl_FragColor = color;
}`,
vert: `
attribute vec2 position;
void main() {
gl_Position = vec4(position, 0, 1);
}`,
attributes: {
position: [[-1, -1], [0, 1], [1, -1]]
},
uniforms: {
color: [1, 0, 0, 1]
},
count: 3
})
Babylon.js:
const scene = new BABYLON.Scene(engine);
const camera = new BABYLON.FreeCamera("camera", new BABYLON.Vector3(0, 0, -10), scene);
const light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(0, 1, 0), scene);
const box = BABYLON.MeshBuilder.CreateBox("box", {}, scene);
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Babylon.js
Getting started? Play directly with the Babylon.js API using our playground. It also contains a lot of samples to learn how to use it.
Any questions? Here is our official forum.
CDN
â ï¸ WARNING: The CDN should not be used in production environments. The purpose of our CDN is to serve Babylon packages to users learning how to use the platform or running small experiments. Once you've built an application and are ready to share it with the world at large, you should serve all packages from your own CDN.
For the preview release, use the following URLs:
A list of additional references can be found here.
npm
BabylonJS and its modules are published on npm with full typing support. To install, use:
npm install babylonjs --save
alternatively, you can now rely on our ES6 packages. Using the ES6 version will allow tree shaking among other bundling benefits.
This will allow you to import BabylonJS entirely using:
import * as BABYLON from 'babylonjs';
or individual classes using:
import { Scene, Engine } from 'babylonjs';
If using TypeScript, don't forget to add 'babylonjs' to 'types' in tsconfig.json
:
...
"types": [
"babylonjs",
"anotherAwesomeDependency"
],
...
To add a module, install the respective package. A list of extra packages and their installation instructions can be found on the babylonjs user on npm.
Usage
See Getting Started:
// Get the canvas DOM element
var canvas = document.getElementById('renderCanvas');
// Load the 3D engine
var engine = new BABYLON.Engine(canvas, true, {preserveDrawingBuffer: true, stencil: true});
// CreateScene function that creates and return the scene
var createScene = function(){
// Create a basic BJS Scene object
var scene = new BABYLON.Scene(engine);
// Create a FreeCamera, and set its position to {x: 0, y: 5, z: -10}
var camera = new BABYLON.FreeCamera('camera1', new BABYLON.Vector3(0, 5, -10), scene);
// Target the camera to scene origin
camera.setTarget(BABYLON.Vector3.Zero());
// Attach the camera to the canvas
camera.attachControl(canvas, false);
// Create a basic light, aiming 0, 1, 0 - meaning, to the sky
var light = new BABYLON.HemisphericLight('light1', new BABYLON.Vector3(0, 1, 0), scene);
// Create a built-in "sphere" shape using the SphereBuilder
var sphere = BABYLON.MeshBuilder.CreateSphere('sphere1', {segments: 16, diameter: 2, sideOrientation: BABYLON.Mesh.FRONTSIDE}, scene);
// Move the sphere upward 1/2 of its height
sphere.position.y = 1;
// Create a built-in "ground" shape;
var ground = BABYLON.MeshBuilder.CreateGround("ground1", { width: 6, height: 6, subdivisions: 2, updatable: false }, scene);
// Return the created scene
return scene;
}
// call the createScene function
var scene = createScene();
// run the render loop
engine.runRenderLoop(function(){
scene.render();
});
// the canvas/window resize event handler
window.addEventListener('resize', function(){
engine.resize();
});
Contributing
If you want to contribute, please read our contribution guidelines first.
Documentation
Useful links
- Official web site: www.babylonjs.com
- Online playground to learn by experimentating
- Online sandbox where you can test your .babylon and glTF scenes with a simple drag'n'drop
- Online shader creation tool where you can learn how to create GLSL shaders
- 3DS Max exporter can be used to generate a .babylon file from 3DS Max
- Maya exporter can be used to generate a .babylon file from Maya
- Blender exporter can be used to generate a .babylon file from Blender 3d
- Unity 5 (deprecated) exporter can be used to export your geometries from Unity 5 scene editor(animations are supported)
- glTF Tools by KhronosGroup
Features
To get a complete list of supported features, please visit our website.
Top Related Projects
JavaScript 3D Library.
The HTML5 Creation Engine: Create beautiful digital content with the fastest, most flexible 2D WebGL renderer.
JavaScript game engine built on WebGL, WebGPU, WebXR and glTF
Cocos simplifies game creation and distribution with Cocos Creator, a free, open-source, cross-platform game engine. Empowering millions of developers to create high-performance, engaging 2D/3D games and instant web entertainment.
🇨🇭 A React renderer for Three.js
👑 Functional WebGL
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot