three-js
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseThree.js Complete Reference (Vanilla)
Three.js 完整参考文档(原生版)
React Three Fiber users: This reference is for vanilla Three.js. For R3F/Drei patterns, use theskill. However, understanding Three.js concepts here helps when working with R3F since R3F is a React renderer for Three.js.r3f-best-practices
React Three Fiber 用户注意:本参考文档适用于原生Three.js。如需R3F/Drei相关实践,请使用Skill。不过,理解此处的Three.js概念对使用R3F也有帮助,因为R3F是Three.js的React渲染器。r3f-best-practices
Quick Start
快速开始
javascript
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));
document.body.appendChild(renderer.domElement);
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);
scene.add(new THREE.AmbientLight(0xffffff, 0.5));
const light = new THREE.DirectionalLight(0xffffff, 1);
light.position.set(5, 5, 5);
scene.add(light);
camera.position.z = 5;
function animate() {
requestAnimationFrame(animate);
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render(scene, camera);
}
animate();javascript
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));
document.body.appendChild(renderer.domElement);
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);
scene.add(new THREE.AmbientLight(0xffffff, 0.5));
const light = new THREE.DirectionalLight(0xffffff, 1);
light.position.set(5, 5, 5);
scene.add(light);
camera.position.z = 5;
function animate() {
requestAnimationFrame(animate);
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render(scene, camera);
}
animate();Reference Index
参考索引
Load the appropriate reference file based on task:
根据任务加载对应的参考文档:
Core Foundation
核心基础
| Reference | Use When |
|---|---|
| Scene setup, renderer config, Object3D hierarchy, coordinate systems |
| Creating shapes, BufferGeometry, instancing, points, lines |
| Camera types, frustum, viewport, projection |
| Vector3, Matrix4, Quaternion, Euler, Color, MathUtils |
| 参考文档 | 使用场景 |
|---|---|
| 场景设置、渲染器配置、Object3D层级、坐标系 |
| 创建形状、BufferGeometry、实例化、点、线 |
| 相机类型、视锥体、视口、投影 |
| Vector3、Matrix4、Quaternion、Euler、Color、MathUtils |
Visual Appearance
视觉外观
| Reference | Use When |
|---|---|
| PBR materials, shader materials, all material types |
| Texture loading, UV mapping, render targets, environment maps |
| Light types, shadows, IBL, light probes |
| Custom GLSL shaders, uniforms, varyings, shader patterns |
| 参考文档 | 使用场景 |
|---|---|
| PBR材质、着色器材质、所有材质类型 |
| 纹理加载、UV映射、渲染目标、环境贴图 |
| 光源类型、阴影、IBL、光探针 |
| 自定义GLSL着色器、uniforms、varyings、着色器模式 |
Motion & Interaction
运动与交互
| Reference | Use When |
|---|---|
| Keyframe animation, skeletal, morph targets, procedural motion |
| Raycasting, selection, drag, coordinate conversion |
| OrbitControls, FlyControls, PointerLockControls, etc. |
| 参考文档 | 使用场景 |
|---|---|
| 关键帧动画、骨骼动画、形态目标、程序化运动 |
| 射线检测、选择、拖拽、坐标转换 |
| OrbitControls、FlyControls、PointerLockControls等 |
Assets
资源
| Reference | Use When |
|---|---|
| GLTF, FBX, textures, HDR, Draco compression, async patterns |
| 参考文档 | 使用场景 |
|---|---|
| GLTF、FBX、纹理、HDR、Draco压缩、异步模式 |
Effects
特效
| Reference | Use When |
|---|---|
| Bloom, DOF, SSAO, custom effects, EffectComposer |
| 参考文档 | 使用场景 |
|---|---|
| Bloom、DOF、SSAO、自定义特效、EffectComposer |
Advanced
进阶内容
| Reference | Use When |
|---|---|
| Optimization, profiling, LOD, culling, batching |
| TSL (Three Shading Language), node-based materials |
| Physics engines, WebXR, VR/AR integration |
| WebGPU renderer, compute shaders, WGSL |
| Architecture patterns, asset management, cleanup, state |
| 参考文档 | 使用场景 |
|---|---|
| 优化、性能分析、LOD、剔除、批处理 |
| TSL(Three Shading Language)、基于节点的材质 |
| 物理引擎、WebXR、VR/AR集成 |
| WebGPU渲染器、计算着色器、WGSL |
| 架构模式、资源管理、清理、状态管理 |
Common Patterns Quick Reference
常见模式快速参考
Resize Handler
窗口大小调整处理器
javascript
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});javascript
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});Cleanup/Dispose
清理/释放资源
javascript
function dispose(obj) {
obj.traverse(child => {
if (child.geometry) child.geometry.dispose();
if (child.material) {
if (Array.isArray(child.material)) {
child.material.forEach(m => m.dispose());
} else {
child.material.dispose();
}
}
});
}javascript
function dispose(obj) {
obj.traverse(child => {
if (child.geometry) child.geometry.dispose();
if (child.material) {
if (Array.isArray(child.material)) {
child.material.forEach(m => m.dispose());
} else {
child.material.dispose();
}
}
});
}Animation Loop with Clock
使用Clock的动画循环
javascript
const clock = new THREE.Clock();
function animate() {
const delta = clock.getDelta();
const elapsed = clock.getElapsedTime();
// Use delta for frame-rate independent animation
mixer?.update(delta);
requestAnimationFrame(animate);
renderer.render(scene, camera);
}javascript
const clock = new THREE.Clock();
function animate() {
const delta = clock.getDelta();
const elapsed = clock.getElapsedTime();
// 使用delta实现帧率无关的动画
mixer?.update(delta);
requestAnimationFrame(animate);
renderer.render(scene, camera);
}Import Patterns
导入模式
javascript
// Core
import * as THREE from 'three';
// Addons (controls, loaders, effects)
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
// Compression support
import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js';javascript
// 核心库
import * as THREE from 'three';
// 附加组件(控制器、加载器、特效)
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
// 压缩支持
import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js';Version Notes
版本说明
This reference targets Three.js r150+ with notes for:
- WebGPU support (r150+)
- Node materials / TSL
- Modern ES module imports
For version-specific APIs, check the Three.js migration guide.
本参考文档针对Three.js r150+版本,包含以下内容的说明:
- WebGPU支持(r150+)
- 节点材质 / TSL
- 现代ES模块导入
如需查看特定版本的API,请查阅Three.js迁移指南。