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: threejs3description: Guia completo de desenvolvimento Three.js com padroes obrigatorios: disposal de recursos, cap de pixel ratio em 2x, color spaces por tipo de textura e template scaffold aprovado. Cobre fundamentals, geometria, materiais, iluminacao, shaders, interacao e pos-processamento.4license: Apache-2.05---67# Three.js Best Practices89## Quick Start1011```typescript12import * as THREE from "three";1314const scene = new THREE.Scene();15const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);16const renderer = new THREE.WebGLRenderer({ antialias: true });1718renderer.setSize(window.innerWidth, window.innerHeight);19renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));20renderer.outputColorSpace = THREE.SRGBColorSpace;21renderer.toneMapping = THREE.ACESFilmicToneMapping;22document.body.appendChild(renderer.domElement);2324// Mesh25const geometry = new THREE.BoxGeometry(1, 1, 1);26const material = new THREE.MeshStandardMaterial({ color: 0x00ff00 });27const cube = new THREE.Mesh(geometry, material);28scene.add(cube);2930// Light31const light = new THREE.DirectionalLight(0xffffff, 1);32light.position.set(5, 5, 5);33scene.add(light);34scene.add(new THREE.AmbientLight(0xffffff, 0.3));3536camera.position.z = 5;3738// Animation loop39const clock = new THREE.Clock();40function animate() {41 const delta = clock.getDelta();42 cube.rotation.x += delta;43 cube.rotation.y += delta;44 renderer.render(scene, camera);45}46renderer.setAnimationLoop(animate);4748// Resize49window.addEventListener("resize", () => {50 camera.aspect = window.innerWidth / window.innerHeight;51 camera.updateProjectionMatrix();52 renderer.setSize(window.innerWidth, window.innerHeight);53});54```5556## Topic References5758Read the relevant reference file based on the task at hand:5960- [references/fundamentals.md](references/fundamentals.md) - Scene, camera, renderer, Object3D, math utilities, coordinate system, cleanup/disposal patterns61- [references/geometry.md](references/geometry.md) - Built-in shapes, custom BufferGeometry, InstancedMesh, points, lines, edges, geometry utilities62- [references/materials.md](references/materials.md) - All material types, PBR workflow (Standard/Physical), environment maps, material properties, multiple materials63- [references/lighting-and-shadows.md](references/lighting-and-shadows.md) - Light types (Ambient, Hemisphere, Directional, Point, Spot, RectArea), shadow setup, IBL/HDR, lighting setups64- [references/textures.md](references/textures.md) - Texture loading/config, color spaces, HDR, render targets, UV mapping, texture atlas, memory management65- [references/animation.md](references/animation.md) - AnimationMixer/Clip/Action, skeletal animation, morph targets, animation blending, procedural animation66- [references/shaders.md](references/shaders.md) - ShaderMaterial, GLSL uniforms/varyings, common shader patterns, extending built-in materials, instanced shaders67- [references/interaction.md](references/interaction.md) - Raycasting, camera controls (Orbit/Fly/PointerLock), TransformControls, DragControls, selection, coordinate conversion68- [references/postprocessing.md](references/postprocessing.md) - EffectComposer, bloom, DOF, SSAO, FXAA/SMAA, custom ShaderPass, selective bloom, multi-pass rendering69- [references/loaders.md](references/loaders.md) - GLTF/Draco loading, OBJ/FBX/STL formats, LoadingManager, async patterns, caching, error handling7071## Essential Patterns7273**Always dispose resources when done:**74```typescript75geometry.dispose();76material.dispose();77texture.dispose();78renderer.dispose();79```8081**Frame-rate-independent animation:** Always use `clock.getDelta()` or `clock.getElapsedTime()`.8283**Pixel ratio:** Always `renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))` to cap at 2x.8485**Color spaces:** Set `texture.colorSpace = THREE.SRGBColorSpace` for color/albedo maps. Leave data maps (normal, roughness, metalness) as default.