ScriptsApr 11, 2026·2 min read

Three.js — JavaScript 3D Library for the Web

Three.js is the most popular 3D library for JavaScript — WebGL/WebGPU rendering made accessible. Build 3D scenes, models, shaders, physics, VR/AR, and interactive experiences without touching low-level graphics APIs.

TL;DR
Three.js is the standard JavaScript library for building 3D scenes, models, and interactive experiences in the browser via WebGL/WebGPU.
§01

What it is

Three.js is a JavaScript library that makes WebGL and WebGPU rendering accessible. Instead of writing low-level shader code and managing GPU buffers, you work with high-level abstractions: scenes, cameras, lights, geometries, and materials. It is the most widely used 3D library for the web.

Three.js serves frontend developers who need 3D visualization (product configurators, data visualization, games), creative developers building interactive experiences, and teams adding 3D elements to existing web applications.

§02

How it saves time or tokens

Raw WebGL requires hundreds of lines of boilerplate for a spinning cube: shader compilation, buffer binding, matrix math, and render loop management. Three.js reduces this to a dozen lines. The built-in loader system handles glTF, OBJ, FBX, and other 3D file formats without custom parsing code.

The ecosystem of examples, addons, and community extensions means most common 3D patterns (orbit controls, post-processing, physics) are already solved.

§03

How to use

  1. Install Three.js:
npm install three
  1. Create a basic scene:
import * as THREE from 'three';

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);
  1. Add a mesh and render:
const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshStandardMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);
camera.position.z = 5;

function animate() {
  cube.rotation.x += 0.01;
  cube.rotation.y += 0.01;
  renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
§04

Example

Loading a glTF model with orbit controls:

import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';

const controls = new OrbitControls(camera, renderer.domElement);
const loader = new GLTFLoader();

loader.load('model.glb', (gltf) => {
  scene.add(gltf.scene);
});

This gives you a draggable, zoomable 3D model viewer in under 20 lines of code.

§05

Related on TokRepo

§06

Common pitfalls

  • Not disposing of geometries and materials when removing objects. Three.js does not garbage-collect GPU resources automatically. Call .dispose() on geometries, materials, and textures to prevent memory leaks.
  • Using uncompressed textures. Large PNG/JPG textures consume GPU memory. Use KTX2/Basis compressed textures for production applications.
  • Ignoring the render loop frame budget. At 60fps you have 16ms per frame. Complex scenes with many draw calls need optimization (instancing, LOD, frustum culling).

Frequently Asked Questions

Does Three.js support WebGPU?+

Yes. Three.js added experimental WebGPU support via the WebGPURenderer. As browsers ship WebGPU by default, Three.js provides a migration path from WebGL. The API remains largely the same; you swap the renderer.

How does Three.js compare to Babylon.js?+

Both are full-featured 3D libraries for the web. Three.js has a larger community, more examples, and a thinner API surface. Babylon.js includes a built-in physics engine, GUI system, and scene editor. Three.js is more modular; Babylon.js is more batteries-included.

Can I use Three.js with React?+

Yes. React Three Fiber (R3F) is a React renderer for Three.js that lets you write 3D scenes as JSX components. It handles the render loop, cleanup, and React integration. It is the standard way to use Three.js in React applications.

What 3D file formats does Three.js support?+

Three.js supports glTF/GLB (recommended), OBJ, FBX, STL, PLY, USDZ, and more via loader addons. glTF is the recommended format because it is compact, well-specified, and supports PBR materials, animations, and scene hierarchy.

Is Three.js suitable for production web applications?+

Yes. Three.js powers 3D experiences on major websites including product configurators, real estate tours, and data visualizations. Performance depends on scene complexity and optimization. Use techniques like instanced rendering, texture compression, and LOD for large scenes.

Citations (3)

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets