Top Related Projects
🇨🇭 A React renderer for Three.js
Babylon.js is a powerful, beautiful, simple, and open game and rendering engine packed into a friendly JavaScript framework.
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
Flat, round, designer-friendly pseudo-3D engine for canvas & SVG
A lightweight 3D physics engine written in JavaScript.
Quick Overview
Three.js is a popular JavaScript 3D library that makes it easier to create and display animated 3D computer graphics in a web browser. It provides a high-level API for creating and manipulating 3D scenes using WebGL, without requiring developers to write WebGL code directly.
Pros
- Easy to use and learn, with a gentle learning curve for 3D graphics
- Extensive documentation and large community support
- Cross-browser compatibility and good performance
- Wide range of built-in geometries, materials, and effects
Cons
- Can be overkill for simple 2D or basic 3D projects
- Performance may suffer with very complex scenes or large numbers of objects
- Limited built-in physics capabilities compared to dedicated physics engines
- Learning 3D concepts is still necessary for advanced usage
Code Examples
Creating a basic scene with a rotating cube:
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);
const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);
camera.position.z = 5;
function animate() {
requestAnimationFrame(animate);
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render(scene, camera);
}
animate();
Adding lights and a more complex material:
const light = new THREE.PointLight(0xffffff, 1, 100);
light.position.set(0, 0, 10);
scene.add(light);
const material = new THREE.MeshPhongMaterial({ color: 0x00ff00 });
Loading a 3D model:
const loader = new THREE.GLTFLoader();
loader.load('path/to/model.gltf', (gltf) => {
scene.add(gltf.scene);
}, undefined, (error) => {
console.error('An error happened', error);
});
Getting Started
-
Include Three.js in your project:
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
-
Create a basic HTML structure:
<!DOCTYPE html> <html> <head> <title>My Three.js Scene</title> <style>body { margin: 0; }</style> </head> <body> <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script> <script> // Your Three.js code here </script> </body> </html>
-
Add the basic scene creation code from the first example above in the script tag.
-
Open the HTML file in a web browser to see your 3D scene.
Competitor Comparisons
🇨🇭 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, aligning with React paradigms
- Simplified API for common Three.js operations
Cons of react-three-fiber
- Additional abstraction layer may impact performance for complex scenes
- Learning curve for developers unfamiliar with React concepts
- Limited access to some low-level Three.js 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);
react-three-fiber:
import React from 'react'
import { Canvas } from '@react-three/fiber'
function App() {
return (
<Canvas>
<perspectiveCamera position={[0, 0, 5]} />
<mesh>
<boxGeometry />
<meshStandardMaterial />
</mesh>
</Canvas>
)
}
Babylon.js is a powerful, beautiful, simple, and open game and rendering engine packed into a friendly JavaScript framework.
Pros of Babylon.js
- More comprehensive built-in features, including physics engines and GUI tools
- Better documentation and learning resources
- Stronger TypeScript support and integration
Cons of Babylon.js
- Steeper learning curve due to more complex API
- Larger file size, which may impact load times for simpler projects
- Less flexibility for low-level customization compared to Three.js
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);
Three.js:
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const light = new THREE.HemisphereLight(0xffffff, 0x444444);
const geometry = new THREE.SphereGeometry(1, 32, 32);
const material = new THREE.MeshBasicMaterial({color: 0xffffff});
const sphere = new THREE.Mesh(geometry, material);
Both libraries offer powerful 3D rendering capabilities, but Babylon.js provides more out-of-the-box features and better documentation, while Three.js offers greater flexibility and a lighter footprint. 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
- Optimized for 2D rendering, resulting in better performance for 2D graphics
- Easier learning curve for developers familiar with 2D graphics concepts
- Smaller file size, leading to faster load times for web applications
Cons of PixiJS
- Limited 3D capabilities compared to Three.js
- Smaller community and ecosystem, resulting in fewer plugins and extensions
- Less suitable for complex 3D scenes or applications requiring advanced 3D features
Code Comparison
Three.js (3D cube):
const geometry = new THREE.BoxGeometry(1, 1, 1);
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);
PixiJS (2D rectangle):
const rectangle = new PIXI.Graphics();
rectangle.beginFill(0x00ff00);
rectangle.drawRect(0, 0, 100, 100);
app.stage.addChild(rectangle);
Both libraries offer straightforward ways to create basic shapes, but Three.js focuses on 3D objects while PixiJS excels in 2D rendering. Three.js provides more complex 3D capabilities, while PixiJS offers simpler and more performant 2D graphics creation.
JavaScript game engine built on WebGL, WebGPU, WebXR and glTF
Pros of PlayCanvas Engine
- Built-in editor and asset pipeline for easier game development
- Designed for web-first, with optimized performance for browsers
- Comprehensive entity-component system for flexible game object management
Cons of PlayCanvas Engine
- Smaller community and ecosystem compared to Three.js
- Less flexibility for low-level rendering customization
- Steeper learning curve for developers new to game engines
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);
PlayCanvas Engine:
const canvas = document.createElement('canvas');
const app = new pc.Application(canvas);
app.start();
const camera = new pc.Entity('camera');
camera.addComponent('camera', { clearColor: new pc.Color(0.1, 0.1, 0.1) });
app.root.addChild(camera);
Both engines provide powerful 3D rendering capabilities, but PlayCanvas Engine offers a more integrated game development environment, while Three.js provides greater flexibility and a larger community. The choice between them depends on the specific project requirements and developer preferences.
Flat, round, designer-friendly pseudo-3D engine for canvas & SVG
Pros of Zdog
- Lightweight and simple, ideal for small 3D projects
- Easy to learn and use, with a flat learning curve
- Pseudo-3D rendering, which can be more performant for certain use cases
Cons of Zdog
- Limited features compared to Three.js
- Less suitable for complex 3D scenes or realistic rendering
- Smaller community and ecosystem
Code Comparison
Zdog:
let illo = new Zdog.Illustration({
element: '.zdog-canvas',
zoom: 4,
});
new Zdog.Box({
addTo: illo,
width: 100,
height: 100,
depth: 100,
stroke: false,
color: '#C25',
});
illo.updateRenderGraph();
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();
const geometry = new THREE.BoxGeometry(1, 1, 1);
const material = new THREE.MeshBasicMaterial({ color: 0xcc2255 });
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);
renderer.render(scene, camera);
Three.js offers a more comprehensive 3D engine with advanced features, while Zdog provides a simpler approach for pseudo-3D graphics. Three.js is better suited for complex 3D applications, while Zdog excels in creating lightweight, stylized 3D illustrations.
A lightweight 3D physics engine written in JavaScript.
Pros of Cannon.js
- Specialized physics engine for 3D simulations
- Lightweight and focused on performance
- Easier to implement complex physics interactions
Cons of Cannon.js
- Smaller community and fewer resources compared to Three.js
- Less frequent updates and maintenance
- Limited to physics simulations, not a full 3D graphics library
Code Comparison
Three.js (3D graphics):
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
const geometry = new THREE.BoxGeometry(1, 1, 1);
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
Cannon.js (Physics simulation):
const world = new CANNON.World();
world.gravity.set(0, -9.82, 0);
const shape = new CANNON.Box(new CANNON.Vec3(1, 1, 1));
const body = new CANNON.Body({ mass: 1 });
body.addShape(shape);
Three.js is a comprehensive 3D graphics library for creating and rendering 3D scenes in the browser. It offers a wide range of features for 3D modeling, animation, and rendering. Cannon.js, on the other hand, is a physics engine specifically designed for 3D simulations. While Three.js excels in visual representation, Cannon.js focuses on accurate physics calculations and interactions between objects.
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
three.js
JavaScript 3D library
The aim of the project is to create an easy-to-use, lightweight, cross-browser, general-purpose 3D library. The current builds only include a WebGL renderer but WebGPU (experimental), SVG and CSS3D renderers are also available as addons.
Examples — Docs — Manual — Wiki — Migrating — Questions — Forum — Discord
Usage
This code creates a scene, a camera, and a geometric cube, and it adds the cube to the scene. It then creates a WebGL
renderer for the scene and camera, and it adds that viewport to the document.body
element. Finally, it animates the cube within the scene for the camera.
import * as THREE from 'three';
const width = window.innerWidth, height = window.innerHeight;
// init
const camera = new THREE.PerspectiveCamera( 70, width / height, 0.01, 10 );
camera.position.z = 1;
const scene = new THREE.Scene();
const geometry = new THREE.BoxGeometry( 0.2, 0.2, 0.2 );
const material = new THREE.MeshNormalMaterial();
const mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
const renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setSize( width, height );
renderer.setAnimationLoop( animate );
document.body.appendChild( renderer.domElement );
// animation
function animate( time ) {
mesh.rotation.x = time / 2000;
mesh.rotation.y = time / 1000;
renderer.render( scene, camera );
}
If everything goes well, you should see this.
Cloning this repository
Cloning the repo with all its history results in a ~2 GB download. If you don't need the whole history you can use the depth
parameter to significantly reduce download size.
git clone --depth=1 https://github.com/mrdoob/three.js.git
Change log
Top Related Projects
🇨🇭 A React renderer for Three.js
Babylon.js is a powerful, beautiful, simple, and open game and rendering engine packed into a friendly JavaScript framework.
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
Flat, round, designer-friendly pseudo-3D engine for canvas & SVG
A lightweight 3D physics engine written in JavaScript.
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