12/7/2024
Share this post:
Export:

Last night while at Pinthouse Pizza I started to look into both Three.js and Babylon.js to create a simple D20 die renderer. Both frameworks offer impressive capabilities, but they take different approaches to 3D web development.
Three.js provides granular control over every aspect of 3D rendering. This power comes with a learning curve:
Babylon.js takes a more opinionated approach, offering:
After implementing the same D20 renderer in both frameworks:
Setup Time:
TypeScript Support:
Material Handling:
Learning Curve:
Here's a quick look at creating a basic scene in both frameworks:
typescript
// Scene setup
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x222222);
// Camera setup
const camera = new THREE.PerspectiveCamera(75, width/height, 0.1, 1000);
camera.position.set(2, 2, 4);
camera.lookAt(0, 0, 0);
// Renderer setup
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(width, height);
containerRef.current.appendChild(renderer.domElement);
// Basic D20
const d20Geometry = new THREE.IcosahedronGeometry(1);
const material = new THREE.MeshBasicMaterial({
color: 0xff0000,
wireframe: true,
side: THREE.DoubleSide
});
const d20 = new THREE.Mesh(d20Geometry, material);
And Babylon.js provides a more structured approach:
const engine = new Engine(containerRef.current, true);
const scene = new Scene(engine);
scene.clearColor = new Color3(0.1, 0.1, 0.1);
// Camera setup - much simpler!
const camera = new ArcRotateCamera(
"camera",
0,
Math.PI / 3,
8,
Vector3.Zero(),
scene
);
camera.attachControl(containerRef.current, true);
// Basic D20 - one line!
const d20 = MeshBuilder.CreatePolyhedron("d20", {
type: 3, // type 3 is icosahedron
size: 1.5
}, scene);
Three.js requires manual animation frame handling:
typescript
const animate = () => {
requestAnimationFrame(animate);
d20.rotation.x += 0.002;
d20.rotation.y += 0.003;
controls.update();
renderer.render(scene, camera);
};
animate();
typescript
scene.registerBeforeRender(() => {
d20.rotation.y += 0.002;
d20.rotation.x += 0.003;
});
engine.runRenderLoop(() => {
scene.render();
});
Both frameworks are powerful and capable, but they serve different needs:
Choose Three.js if you need:
Choose Babylon.js if you want:
This is just the beginning of my exploration into WebGL frameworks. Stay tuned for more detailed comparisons and advanced features!
Zero to Hero: Building with Claude Code
A complete beginner's guide to setting up your development environment and building your first project with Claude Code. Covers Mac and Windows, ...
Adding Comments to My Blog with Giscus!
How I added GitHub Discussions-powered comments to my blog using Giscus, with help from Cursor.AI
Get notified when I publish new blog posts about game development, AI, entrepreneurship, and technology. No spam, unsubscribe anytime.