Three.js Best Practices
Quick Start
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({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
document.body.appendChild(renderer.domElement);
// Mesh
const geometry = new THREE.BoxGeometry(1, 1, 1);
const material = new THREE.MeshStandardMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);
// Light
const light = new THREE.DirectionalLight(0xffffff, 1);
light.position.set(5, 5, 5);
scene.add(light);
scene.add(new THREE.AmbientLight(0xffffff, 0.3));
camera.position.z = 5;
// Animation loop
const clock = new THREE.Clock();
function animate() {
const delta = clock.getDelta();
cube.rotation.x += delta;
cube.rotation.y += delta;
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
// Resize
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
Topic References
Read the relevant reference file based on the task at hand:
- references/fundamentals.md - Scene, camera, renderer, Object3D, math utilities, coordinate system, cleanup/disposal patterns
- references/geometry.md - Built-in shapes, custom BufferGeometry, InstancedMesh, points, lines, edges, geometry utilities
- references/materials.md - All material types, PBR workflow (Standard/Physical), environment maps, material properties, multiple materials
- references/lighting-and-shadows.md - Light types (Ambient, Hemisphere, Directional, Point, Spot, RectArea), shadow setup, IBL/HDR, lighting setups
- references/textures.md - Texture loading/config, color spaces, HDR, render targets, UV mapping, texture atlas, memory management
- references/animation.md - AnimationMixer/Clip/Action, skeletal animation, morph targets, animation blending, procedural animation
- references/shaders.md - ShaderMaterial, GLSL uniforms/varyings, common shader patterns, extending built-in materials, instanced shaders
- references/interaction.md - Raycasting, camera controls (Orbit/Fly/PointerLock), TransformControls, DragControls, selection, coordinate conversion
- references/postprocessing.md - EffectComposer, bloom, DOF, SSAO, FXAA/SMAA, custom ShaderPass, selective bloom, multi-pass rendering
- references/loaders.md - GLTF/Draco loading, OBJ/FBX/STL formats, LoadingManager, async patterns, caching, error handling
Essential Patterns
Always dispose resources when done:
geometry.dispose();
material.dispose();
texture.dispose();
renderer.dispose();
Frame-rate-independent animation: Always use clock.getDelta() or clock.getElapsedTime().
Pixel ratio: Always renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2)) to cap at 2x.
Color spaces: Set texture.colorSpace = THREE.SRGBColorSpace for color/albedo maps. Leave data maps (normal, roughness, metalness) as default.
1---2name: threejs-23description: Comprehensive Three.js development guide covering scene setup, geometry, materials, lighting, textures, animation, shaders, interaction, post-processing, and asset loading. Use when the user is building or modifying any Three.js project, creating 3D scenes, working with WebGL rendering, implementing 3D animations, writing GLSL shaders, setting up camera controls, loading 3D models (GLTF/OBJ/FBX), or doing any Three.js-related development. Also use when the user mentions 'three.js', 'threejs', 'WebGL', '3D scene', 'shader', 'GLTF', or 'post-processing effects'.4---56# Three.js Best Practices78## Quick Start910```typescript11import * as THREE from "three";1213const scene = new THREE.Scene();14const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);15const renderer = new THREE.WebGLRenderer({ antialias: true });1617renderer.setSize(window.innerWidth, window.innerHeight);18renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));19renderer.outputColorSpace = THREE.SRGBColorSpace;20renderer.toneMapping = THREE.ACESFilmicToneMapping;21document.body.appendChild(renderer.domElement);2223// Mesh24const geometry = new THREE.BoxGeometry(1, 1, 1);25const material = new THREE.MeshStandardMaterial({ color: 0x00ff00 });26const cube = new THREE.Mesh(geometry, material);27scene.add(cube);2829// Light30const light = new THREE.DirectionalLight(0xffffff, 1);31light.position.set(5, 5, 5);32scene.add(light);33scene.add(new THREE.AmbientLight(0xffffff, 0.3));3435camera.position.z = 5;3637// Animation loop38const clock = new THREE.Clock();39function animate() {40 const delta = clock.getDelta();41 cube.rotation.x += delta;42 cube.rotation.y += delta;43 renderer.render(scene, camera);44}45renderer.setAnimationLoop(animate);4647// Resize48window.addEventListener("resize", () => {49 camera.aspect = window.innerWidth / window.innerHeight;50 camera.updateProjectionMatrix();51 renderer.setSize(window.innerWidth, window.innerHeight);52});53```5455## Topic References5657Read the relevant reference file based on the task at hand:5859- [references/fundamentals.md](references/fundamentals.md) - Scene, camera, renderer, Object3D, math utilities, coordinate system, cleanup/disposal patterns60- [references/geometry.md](references/geometry.md) - Built-in shapes, custom BufferGeometry, InstancedMesh, points, lines, edges, geometry utilities61- [references/materials.md](references/materials.md) - All material types, PBR workflow (Standard/Physical), environment maps, material properties, multiple materials62- [references/lighting-and-shadows.md](references/lighting-and-shadows.md) - Light types (Ambient, Hemisphere, Directional, Point, Spot, RectArea), shadow setup, IBL/HDR, lighting setups63- [references/textures.md](references/textures.md) - Texture loading/config, color spaces, HDR, render targets, UV mapping, texture atlas, memory management64- [references/animation.md](references/animation.md) - AnimationMixer/Clip/Action, skeletal animation, morph targets, animation blending, procedural animation65- [references/shaders.md](references/shaders.md) - ShaderMaterial, GLSL uniforms/varyings, common shader patterns, extending built-in materials, instanced shaders66- [references/interaction.md](references/interaction.md) - Raycasting, camera controls (Orbit/Fly/PointerLock), TransformControls, DragControls, selection, coordinate conversion67- [references/postprocessing.md](references/postprocessing.md) - EffectComposer, bloom, DOF, SSAO, FXAA/SMAA, custom ShaderPass, selective bloom, multi-pass rendering68- [references/loaders.md](references/loaders.md) - GLTF/Draco loading, OBJ/FBX/STL formats, LoadingManager, async patterns, caching, error handling6970## Essential Patterns7172**Always dispose resources when done:**73```typescript74geometry.dispose();75material.dispose();76texture.dispose();77renderer.dispose();78```7980**Frame-rate-independent animation:** Always use `clock.getDelta()` or `clock.getElapsedTime()`.8182**Pixel ratio:** Always `renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))` to cap at 2x.8384**Color spaces:** Set `texture.colorSpace = THREE.SRGBColorSpace` for color/albedo maps. Leave data maps (normal, roughness, metalness) as default.