Three.js WebGL/WebGPU Development
Overview
Three.js is the industry-standard JavaScript library for creating 3D graphics in web browsers using WebGL and WebGPU. This skill provides comprehensive guidance for building performant, interactive 3D experiences including scenes, cameras, renderers, geometries, materials, lights, textures, and animations.
Core Concepts
Scene Graph Architecture
Three.js uses a hierarchical scene graph where all 3D objects are organized in a tree structure:
Essential Components
Every Three.js application requires these core elements:
- Scene: Container for all 3D objects
- Camera: Defines the viewing perspective
- Renderer: Draws the scene to canvas (WebGL or WebGPU)
- Geometry: Defines the shape of objects
- Material: Defines the surface appearance
- Mesh: Combines geometry and material
Quick Start Pattern
Basic Scene Setup
WebGPU Setup (Modern Alternative)
Common Patterns
1. Creating Meshes with Materials
2. Lighting Strategies
3. Instanced Geometry (Performance)
4. Loading 3D Models (glTF)
5. Shadow Configuration
6. Raycasting (Interaction)
Integration Patterns
With GSAP for Animation
With React (see react-three-fiber skill)
With Post-Processing
Performance Optimization
1. Geometry Reuse
2. Use InstancedMesh for Repeated Objects
For hundreds/thousands of identical objects, use InstancedMesh (see pattern above).
3. Texture Optimization
4. Level of Detail (LOD)
5. Frustum Culling
Three.js automatically culls objects outside the camera's view. Ensure objects have correct bounding spheres:
6. Dispose Resources
Best Practices
1. Use Animation Clocks for Consistent Timing
2. Camera Setup Guidelines
- FOV: 45-75° for most applications
- Near plane: As far as possible (avoid z-fighting)
- Far plane: As close as possible (precision)
- Aspect ratio: Always match canvas dimensions
3. Material Selection
- MeshBasicMaterial: Unlit, flat colors (debugging, UI)
- MeshLambertMaterial: Cheap diffuse lighting (mobile)
- MeshPhongMaterial: Specular highlights (older standard)
- MeshStandardMaterial: PBR, realistic (recommended)
- MeshPhysicalMaterial: Advanced PBR (clearcoat, transmission)
4. Coordinate System
- Three.js uses right-handed coordinate system
- +Y is up, +Z is toward camera, +X is right
- Rotations use radians (Math.PI = 180°)
5. Scene Organization
Common Pitfalls
1. Not Updating Aspect Ratio on Resize
Always update camera aspect ratio and projection matrix when window resizes.
2. Creating New Objects in Animation Loop
3. Forgetting to Enable Shadows
Remember to enable shadows on renderer, lights, and objects.
4. Z-Fighting (Flickering)
- Increase near plane distance
- Decrease far plane distance
- Avoid overlapping coplanar surfaces
- Use
material.polygonOffset = truewithmaterial.polygonOffsetFactor
5. Color Space Issues
6. Not Disposing Resources
Always call .dispose() on geometries, materials, textures, and renderers when no longer needed.
Resources
This skill includes bundled resources to accelerate Three.js development:
references/
api_reference.md: Quick API reference for core classes (Scene, Camera, Renderer, etc.)materials_guide.md: Comprehensive material types and propertiesoptimization_checklist.md: Performance optimization strategies
scripts/
setup_scene.py: Generate boilerplate Three.js scene setup codetexture_optimizer.py: Batch optimize textures for web (resize, compress)gltf_validator.py: Validate glTF models before use
assets/
starter_scene/: Complete HTML/JS boilerplate projectshaders/: Custom GLSL shader examples (vertex, fragment)hdri/: Environment maps for PBR lightingdraco/: DRACO decoder for compressed models
Advanced Topics
Custom Shaders (GLSL)
Render Targets (Render-to-Texture)
GPU Computation (GPGPU)
Use GPUComputationRenderer for particle simulations, cloth physics, etc.
When to Use This Skill
Use this skill when:
- Building interactive 3D web experiences
- Creating product configurators or visualizers
- Implementing WebGL/WebGPU rendering
- Working with 3D models, scenes, or animations
- Optimizing Three.js performance
- Integrating Three.js with other libraries (GSAP, React, etc.)
- Debugging Three.js rendering issues
For React integration, use the react-three-fiber skill. For animation, combine with the gsap-scrolltrigger skill. For UI animations, use the motion-framer skill.


