threejs
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseThree.js
Three.js
Overview
概述
Guides building high-performance 3D web experiences with Three.js, WebGPU-first rendering, TSL (Three Shader Language), and React Three Fiber. Covers scene architecture, asset compression, draw call budgets, and React 19 / Next.js integration patterns.
When to use: Creating 3D scenes, WebGPU rendering setup, TSL shader authoring, asset optimization (Draco/KTX2), React Three Fiber composition, Next.js streaming for 3D content, loading GLTF models, setting up lighting and shadows, animation playback and blending.
When NOT to use: 2D canvas animations (use Canvas API), simple SVG graphics, server-side rendering without client hydration, projects targeting pre-2022 hardware exclusively.
指导你使用Three.js、WebGPU优先的渲染模式、TSL(Three着色器语言)和React Three Fiber构建高性能3D网页体验,涵盖场景架构、资源压缩、绘制调用预算,以及React 19 / Next.js集成模式。
适用场景: 创建3D场景、WebGPU渲染配置、TSL着色器开发、资源优化(Draco/KTX2)、React Three Fiber组合开发、为3D内容配置Next.js流式传输、加载GLTF模型、设置光照与阴影、动画播放与融合。
不适用场景: 2D画布动画(请使用Canvas API)、简单SVG图形、无客户端水合的服务端渲染、仅面向2022年之前硬件的项目。
Quick Reference
快速参考
| Pattern | API / Approach | Key Points |
|---|---|---|
| WebGPU renderer | | Must |
| R3F canvas | | Wrap in |
| Frame updates | | Mutate refs directly; never use |
| TSL shaders | | Node-based; compiles to WGSL or GLSL |
| Instancing | | Single draw call for repeated geometry |
| Batched mesh | | Different geometries sharing one material |
| Draco compression | | Up to 90% geometry size reduction |
| KTX2 textures | Basis Universal via | Stays compressed in VRAM |
| LOD | | Swap detail levels by camera distance |
| On-demand render | | Only render when scene state changes |
| Cleanup | | Geometries, materials, and textures |
| Compute shaders | | GPU-side physics, particles, flocking |
| Lighting | | Enable |
| GLTF loading | | Draco for geometry compression; traverse for shadows |
| Animation | | Update with |
| Crossfade | | Weight-based blending between walk/run/idle |
| Environment maps | | Set |
| Raycasting | | Mouse/touch picking; use |
| Morph targets | | Facial animation, blend shapes from GLTF |
| 模式 | API / 实现方式 | 核心要点 |
|---|---|---|
| WebGPU渲染器 | | 首次渲染前必须执行 |
| R3F画布 | | 外层包裹 |
| 帧更新 | | 直接修改引用值,禁止使用 |
| TSL着色器 | | 基于节点开发,可编译为WGSL或GLSL |
| 实例化 | | 重复几何图形仅需单次绘制调用 |
| 批量网格 | | 不同几何图形可共用同一份材质 |
| Draco压缩 | | 最高可减少90%几何体积 |
| KTX2纹理 | 基于 | 可在显存中保持压缩状态 |
| 层级细节 | | 根据相机距离切换细节等级 |
| 按需渲染 | | 仅当场景状态变更时渲染 |
| 资源清理 | 组件卸载时调用 | 需清理几何、材质和纹理 |
| 计算着色器 | | 可实现GPU端物理模拟、粒子系统、群体运动 |
| 光照 | | 需同时开启渲染器的 |
| GLTF加载 | | 用Draco做几何压缩,遍历节点开启阴影 |
| 动画 | | 每帧使用 |
| 交叉淡入淡出 | | 基于权重实现行走/奔跑/待机状态的融合 |
| 环境贴图 | | 设置 |
| 射线检测 | | 实现鼠标/触摸点选,GLTF模型需设置 |
| 形变目标 | | 实现面部动画、GLTF导出的混合形状 |
Common Mistakes
常见错误
| Mistake | Correct Pattern |
|---|---|
Allocating | Pre-allocate outside the loop to avoid GC pressure every frame |
Using | Use R3F's |
Not awaiting | Always |
Loading assets without | Wrap |
| Using high-poly models for background or distant elements | Use LOD (Level of Detail) or Impostors to reduce draw calls |
Using | Mutate refs directly via |
Speed tied to frame rate ( | Multiply by |
Not using | Always pass delta time to |
| Forgetting to dispose loaded GLTF models | Traverse and dispose geometries, materials, and textures on removal |
| Shadow map enabled on renderer but not on the light | Set both |
Large | Keep ratio small to avoid z-fighting; set |
| 错误做法 | 正确方案 |
|---|---|
在 | 在循环外提前预分配实例,避免每帧产生GC压力 |
在React项目中手动使用 | 使用R3F的 |
WebGPU渲染时未等待 | 首次渲染前必须 |
加载资源时没有 | 外层包裹 |
| 背景或远处元素使用高多边形模型 | 使用LOD(层级细节)或 impostor 减少绘制调用 |
在渲染循环中使用 | 通过 |
动画速度与帧率绑定(如 | 乘以 |
| 始终向 |
| 忘记销毁加载的GLTF模型 | 移除时遍历并销毁几何、材质和纹理 |
| 仅开启渲染器的阴影映射,未开启光源的阴影投射 | 同时设置 |
相机的 | 保持较小比值避免z-fighting,尽量将 |
Delegation
任务委派
- Asset and scene graph exploration: Use agent
Explore - Multi-file scene refactoring and optimization passes: Use agent
Task - 3D architecture and rendering pipeline planning: Use agent
Plan
- 资源与场景图探索:使用Agent
Explore - 多文件场景重构与优化流程:使用Agent
Task - 3D架构与渲染管线规划:使用Agent
Plan
References
参考资料
- Scene, lighting, and model loading
- Animation system and blending
- Performance and asset optimization
- WebGPU and TSL shader patterns
- React Three Fiber patterns and Next.js integration
- 场景、光照与模型加载
- 动画系统与融合
- 性能与资源优化
- WebGPU和TSL着色器模式
- React Three Fiber模式与Next.js集成