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.
Instalación lista para agent
Este activo puede instalarse después de elegir el runtime, revisar el plan y ejecutar el comando correspondiente.
npx -y tokrepo@latest install b3e54897-3588-11f1-9bc6-00163e2b0d79 --target codexEjecutar después de confirmar el plan con dry-run.
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.
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.
How to use
- Install Three.js:
npm install three
- 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);
- 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);
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.
Related on TokRepo
- AI tools for design -- Tools for visual and 3D design workflows
- AI tools for coding -- JavaScript development tools and libraries
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).
Preguntas frecuentes
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.
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.
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.
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.
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.
Referencias (3)
- Three.js GitHub— Three.js is the most popular JavaScript 3D library
- Khronos Group glTF Spec— glTF is the recommended 3D format for web applications
- W3C WebGPU Specification— WebGPU API specification for next-generation GPU access
Relacionados en TokRepo
Discusión
Activos relacionados
Babylon.js — Powerful 3D Game and Rendering Engine
Babylon.js is a powerful, beautiful, simple, open 3D game and rendering engine for the web. WebGL + WebGPU, Playground IDE, Node Material Editor, GUI system, physics, and VR/AR support. Microsoft-backed with enterprise polish.
Theatre.js — Visual Animation Toolbox for the Web
A JavaScript animation library with a visual editor that lets you design and fine-tune complex animations for DOM elements, Three.js scenes, and any JavaScript value with a professional timeline UI.
React Three Fiber — A React Renderer for Three.js
Build interactive 3D scenes and visualizations using React's declarative component model with full Three.js power.
p5.js — JavaScript Library for Creative Coding with a Focus on Accessibility
p5.js is the modern JS descendant of Processing, built for learning, art, and creative coding. Sketch interactive graphics in the browser in a handful of lines — no build step, no setup.