WebGL brought hardware-accelerated 3D graphics to the browser. Since 2011, every major browser has supported it, enabling interactive 3D experiences without plugins. Whether you want to display a 3D model, build a game, or create data visualisations, WebGL is the foundation. But you rarely write raw WebGL — you use a library that handles the complexity.
Why Not Raw WebGL?
Raw WebGL is verbose and complex. Rendering a single textured triangle requires hundreds of lines of code: setting up buffers, writing shader programs, managing the rendering pipeline, and handling edge cases across browsers. It's like writing assembly when you could use Python.
That said, understanding the WebGL pipeline helps you use higher-level libraries more effectively. The core concepts:
- Buffers: Store vertex positions, normals, texture coordinates, and other per-vertex data
- Shaders: Small programs (written in GLSL) that run on the GPU — vertex shaders position vertices, fragment shaders colour pixels
- Textures: Images applied to 3D surfaces
- Draw calls: Commands that tell the GPU to render geometry with the current shader and texture settings
Three.js — The Standard Library
Three.js is the most popular JavaScript 3D library. It wraps WebGL with a clean, object-oriented API:
import * as THREE from 'three';
// Scene, camera, renderer
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, w/h, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({antialias:true});
renderer.setSize(w, h);
document.body.appendChild(renderer.domElement);
// A spinning cube
const geo = new THREE.BoxGeometry();
const mat = new THREE.MeshStandardMaterial({color:0x3b82f6});
const cube = new THREE.Mesh(geo, mat);
scene.add(cube);
// Lighting
scene.add(new THREE.AmbientLight(0xffffff, 0.5));
const light = new THREE.DirectionalLight(0xffffff, 1);
light.position.set(5, 5, 5);
scene.add(light);
// Animation loop
function animate() {
requestAnimationFrame(animate);
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render(scene, camera);
}
animate();
That's a complete 3D scene with a spinning cube, lighting, and animation — about 20 lines instead of the hundreds raw WebGL would require.
Loading 3D Models
Three.js's GLTFLoader loads GLB/GLTF models:
import {GLTFLoader} from 'three/examples/jsm/loaders/GLTFLoader.js';
const loader = new GLTFLoader();
loader.load('model.glb', (gltf) => {
scene.add(gltf.scene);
// Access animations
const mixer = new THREE.AnimationMixer(gltf.scene);
gltf.animations.forEach(clip => {
mixer.clipAction(clip).play();
});
});
The 3D viewer on this site uses a similar approach with Three.js to load and display GLB models with orbit controls, lighting, and animation playback.
Performance Considerations
Draw Calls
Each draw call (rendering one mesh) has overhead. Modern GPUs can handle 1,000-2,000 draw calls per frame at 60fps. If you have thousands of objects, merge meshes that share the same material, or use instanced rendering.
Geometry Complexity
More triangles = more GPU work. A single model with 100,000 triangles renders faster than 100 models with 1,000 triangles each (fewer draw calls). But 10 million triangles in one model may be too much for mobile GPUs.
Use Draco or Meshopt compression to reduce file size, and LOD (Level of Detail) to reduce render complexity for distant objects.
Textures
Textures consume GPU memory. A 2048×2048 RGBA texture uses 16MB of GPU memory. Use compressed textures (KTX2 with Basis Universal) to reduce memory by 75% with minimal quality loss.
Shadows
Shadow maps are expensive — each light with shadows doubles the render cost. Limit shadow-casting lights to one or two, and use lower-resolution shadow maps on mobile.
Mobile Considerations
Mobile GPUs are 5-10x slower than desktop GPUs. For mobile-compatible 3D:
- Keep total triangle count under 100,000
- Use compressed textures (KTX2)
- Limit to 1-2 dynamic lights
- Avoid real-time shadows on mobile
- Use device pixel ratio capping (
renderer.setPixelRatio(Math.min(devicePixelRatio, 2))) - Test on actual mobile devices, not just responsive mode in DevTools
Frameworks Built on Three.js
- React Three Fiber: Three.js for React developers. Declarative 3D scenes as JSX components.
- Babylon.js: Full-featured alternative to Three.js, with built-in physics, GUI, and scene management.
- PlayCanvas: Game engine with visual editor, built on WebGL.
- A-Frame: HTML-based 3D and WebXR, great for VR/AR experiences.
The 3D viewer on this site uses Three.js to render GLB and GLTF models directly in your browser. Upload a model, and it loads with orbit camera controls, lighting, and material rendering. All processing is local — your 3D files never leave your device.