The web has evolved from simple text pages to rich, interactive experiences. One of the most exciting developments in recent years is the ability to create 3D graphics directly in the browser. Three.js is a powerful JavaScript library that makes 3D web design accessible to developers.
What is Three.js?
Three.js is a cross-browser JavaScript library and API used to create and display animated 3D computer graphics in a web browser. It acts as a wrapper around WebGL, making it much easier to work with 3D graphics without needing to write complex WebGL code.
Core Concepts of Three.js
To work with Three.js, you need to understand a few fundamental concepts:
1. Scene
The scene is the container that holds all your 3D objects, lights, and cameras. It's like a stage where everything happens.
const scene = new THREE.Scene();
2. Camera
The camera defines what part of the scene is visible. The most common is the PerspectiveCamera, which mimics how the human eye sees.
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 5;
3. Renderer
The renderer draws the scene from the camera's perspective onto a canvas element in your HTML document.
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
4. Geometry
Geometries define the shape of 3D objects. Three.js provides many built-in geometries like Box, Sphere, Cylinder, etc.
const geometry = new THREE.BoxGeometry(1, 1, 1);
5. Material
Materials define the appearance of objects. They can be simple colors, textures, or complex shaders.
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
6. Mesh
A mesh is an object that takes a geometry and applies a material to it, which can then be added to the scene.
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);
Your First Three.js Scene
Let's put it all together to create a simple rotating cube:
// Set up the scene, camera, and renderer
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);
// Create a 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);
camera.position.z = 5;
// Animation loop
function animate() {
requestAnimationFrame(animate);
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render(scene, camera);
}
animate();
Interactive Demo
Try this simple Three.js demo. You can rotate the cube and change its properties:
Adding Lights
To make objects look more realistic, you need to add lights to your scene. Three.js provides several types of lights:
// Ambient light (affects all objects equally)
const ambientLight = new THREE.AmbientLight(0x404040);
scene.add(ambientLight);
// Directional light (like sunlight)
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
directionalLight.position.set(1, 1, 1);
scene.add(directionalLight);
// Point light (like a light bulb)
const pointLight = new THREE.PointLight(0xff0000, 1, 100);
pointLight.position.set(5, 5, 5);
scene.add(pointLight);
Loading 3D Models
Three.js can load 3D models in various formats. Here's how to load a GLTF model:
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
const loader = new GLTFLoader();
loader.load('model.gltf', function (gltf) {
scene.add(gltf.scene);
}, undefined, function (error) {
console.error(error);
});
Performance Considerations
3D graphics can be resource-intensive. Here are some tips for better performance:
- Use low-poly models when possible
- Reuse materials and geometries
- Implement level of detail (LOD) for complex models
- Use texture compression
- Limit the number of lights in your scene
Resources for Learning Three.js
To dive deeper into Three.js, check out these resources:
- Official Documentation - threejs.org
- Three.js Journey - A popular paid course by Bruno Simon
- Discover Three.js - Free online book by Lewy Blue
- Codecademy - Learn Three.js course
- YouTube Tutorials - Many free tutorials available
Conclusion
Three.js opens up a world of possibilities for creating immersive 3D experiences on the web. While there's a learning curve, the library abstracts away much of the complexity of WebGL, making 3D graphics accessible to web developers.
Start with simple projects and gradually incorporate more advanced features like lights, textures, and animations. With practice, you'll be able to create stunning 3D websites that engage and impress your users.
Back to All Articles