Skip to main content 首页 创作者 beko2210 firstbrain threejs-fundamentals
threejs-fundamentals Three.js scene setup, cameras, renderer, Object3D hierarchy, coordinate systems. Use when setting up 3D scenes, creating cameras, configuring renderers, managing object hierarchies, or working with transforms.
跳到安装 Skills Marketplace 发现并探索由社区构建的 Agent Skills
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/BEKO2210/Firstbrain --skill threejs-fundamentals命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
下载 Zip 下载中... name threejs-fundamentals description Three.js scene setup, cameras, renderer, Object3D hierarchy, coordinate systems. Use when setting up 3D scenes, creating cameras, configuring renderers, managing object hierarchies, or working with transforms. type skill created 2026-02-27T00:00:00.000Z domain design-creative category 3d-animation risk unknown source community tags ["skill","design-creative","3d-animation","threejs","fundamentals"]
Three.js Fundamentals
When to Use
You need to set up the core structure of a Three.js scene.
The task involves scenes, cameras, renderers, transforms, resize handling, or object hierarchy basics.
You want foundational Three.js guidance before working on specialized topics like shaders or post-processing.
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 ));
document .body .appendChild (renderer.domElement );
const geometry = . ( , , );
material = . ({ : });
cube = . (geometry, material);
scene. (cube);
scene. ( . ( , ));
dirLight = . ( , );
dirLight. . ( , , );
scene. (dirLight);
camera. . = ;
( ) {
(animate);
cube. . += ;
cube. . += ;
renderer. (scene, camera);
}
();
. ( , {
camera. = . / . ;
camera. ();
renderer. ( . , . );
});
new
THREE
BoxGeometry
1
1
1
const
new
THREE
MeshStandardMaterial
color
0x00ff00
const
new
THREE
Mesh
add
add
new
THREE
AmbientLight
0xffffff
0.5
const
new
THREE
DirectionalLight
0xffffff
1
position
set
5
5
5
add
position
z
5
function
animate
requestAnimationFrame
rotation
x
0.01
rotation
y
0.01
render
animate
window
addEventListener
"resize"
() =>
aspect
window
innerWidth
window
innerHeight
updateProjectionMatrix
setSize
window
innerWidth
window
innerHeight
Core Classes
Scene Container for all 3D objects, lights, and cameras.
const scene = new THREE .Scene ();
scene.background = new THREE .Color (0x000000 );
scene.background = texture;
scene.background = cubeTexture;
scene.environment = envMap;
scene.fog = new THREE .Fog (0xffffff , 1 , 100 );
scene.fog = new THREE .FogExp2 (0xffffff , 0.02 );
Cameras PerspectiveCamera - Most common, simulates human eye.
const camera = new THREE .PerspectiveCamera (
75 ,
window .innerWidth / window .innerHeight ,
0.1 ,
1000 ,
);
camera.position .set (0 , 5 , 10 );
camera.lookAt (0 , 0 , 0 );
camera.updateProjectionMatrix ();
OrthographicCamera - No perspective distortion, good for 2D/isometric.
const aspect = window .innerWidth / window .innerHeight ;
const frustumSize = 10 ;
const camera = new THREE .OrthographicCamera (
(frustumSize * aspect) / -2 ,
(frustumSize * aspect) / 2 ,
frustumSize / 2 ,
frustumSize / -2 ,
0.1 ,
1000 ,
);
ArrayCamera - Multiple viewports with sub-cameras.
const cameras = [];
for (let i = 0 ; i < 4 ; i++) {
const subcamera = new THREE .PerspectiveCamera (40 , 1 , 0.1 , 100 );
subcamera.viewport = new THREE .Vector4 (
Math .floor (i % 2 ) * 0.5 ,
Math .floor (i / 2 ) * 0.5 ,
0.5 ,
0.5 ,
);
cameras.push (subcamera);
}
const arrayCamera = new THREE .ArrayCamera (cameras);
CubeCamera - Renders environment maps for reflections.
const cubeRenderTarget = new THREE .WebGLCubeRenderTarget (256 );
const cubeCamera = new THREE .CubeCamera (0.1 , 1000 , cubeRenderTarget);
scene.add (cubeCamera);
material.envMap = cubeRenderTarget.texture ;
cubeCamera.position .copy (reflectiveMesh.position );
cubeCamera.update (renderer, scene);
WebGLRenderer const renderer = new THREE .WebGLRenderer ({
canvas : document .querySelector ("#canvas" ),
antialias : true ,
alpha : true ,
powerPreference : "high-performance" ,
preserveDrawingBuffer : true ,
});
renderer.setSize (width, height);
renderer.setPixelRatio (Math .min (window .devicePixelRatio , 2 ));
renderer.toneMapping = THREE .ACESFilmicToneMapping ;
renderer.toneMappingExposure = 1.0 ;
renderer.outputColorSpace = THREE .SRGBColorSpace ;
renderer.shadowMap .enabled = true ;
renderer.shadowMap .type = THREE .PCFSoftShadowMap ;
renderer.setClearColor (0x000000 , 1 );
renderer.render (scene, camera);
Object3D Base class for all 3D objects. Mesh, Group, Light, Camera all extend Object3D.
const obj = new THREE .Object3D ();
obj.position .set (x, y, z);
obj.rotation .set (x, y, z);
obj.quaternion .set (x, y, z, w);
obj.scale .set (x, y, z);
obj.getWorldPosition (targetVector);
obj.getWorldQuaternion (targetQuaternion);
obj.getWorldDirection (targetVector);
obj.add (child);
obj.remove (child);
obj.parent ;
obj.children ;
obj.visible = false ;
obj.layers .set (1 );
obj.layers .enable (2 );
obj.layers .disable (0 );
obj.traverse ((child ) => {
if (child.isMesh ) child.material .color .set (0xff0000 );
});
obj.matrixAutoUpdate = true ;
obj.updateMatrix ();
obj.updateMatrixWorld (true );
Group Empty container for organizing objects.
const group = new THREE .Group ();
group.add (mesh1);
group.add (mesh2);
scene.add (group);
group.position .x = 5 ;
group.rotation .y = Math .PI / 4 ;
Mesh Combines geometry and material.
const mesh = new THREE .Mesh (geometry, material);
const mesh = new THREE .Mesh (geometry, [material1, material2]);
mesh.geometry ;
mesh.material ;
mesh.castShadow = true ;
mesh.receiveShadow = true ;
mesh.frustumCulled = true ;
mesh.renderOrder = 10 ;
Coordinate System Three.js uses a right-handed coordinate system :
+X points right
+Y points up
+Z points toward viewer (out of screen)
const axesHelper = new THREE .AxesHelper (5 );
scene.add (axesHelper);
Math Utilities
Vector3 const v = new THREE .Vector3 (x, y, z);
v.set (x, y, z);
v.copy (otherVector);
v.clone ();
v.add (v2);
v.sub (v2);
v.multiply (v2);
v.multiplyScalar (2 );
v.divideScalar (2 );
v.normalize ();
v.negate ();
v.clamp (min, max);
v.lerp (target, alpha);
v.length ();
v.lengthSq ();
v.distanceTo (v2);
v.dot (v2);
v.cross (v2);
v.angleTo (v2);
v.applyMatrix4 (matrix);
v.applyQuaternion (q);
v.project (camera);
v.unproject (camera);
Matrix4 const m = new THREE .Matrix4 ();
m.identity ();
m.copy (other);
m.clone ();
m.makeTranslation (x, y, z);
m.makeRotationX (theta);
m.makeRotationY (theta);
m.makeRotationZ (theta);
m.makeRotationFromQuaternion (q);
m.makeScale (x, y, z);
m.compose (position, quaternion, scale);
m.decompose (position, quaternion, scale);
m.multiply (m2);
m.premultiply (m2);
m.invert ();
m.transpose ();
m.makePerspective (left, right, top, bottom, near, far);
m.makeOrthographic (left, right, top, bottom, near, far);
m.lookAt (eye, target, up);
Quaternion const q = new THREE .Quaternion ();
q.setFromEuler (euler);
q.setFromAxisAngle (axis, angle);
q.setFromRotationMatrix (matrix);
q.multiply (q2);
q.slerp (target, t);
q.normalize ();
q.invert ();
Euler const euler = new THREE .Euler (x, y, z, "XYZ" );
euler.setFromQuaternion (q);
euler.setFromRotationMatrix (m);
Color const color = new THREE .Color (0xff0000 );
const color = new THREE .Color ("red" );
const color = new THREE .Color ("rgb(255, 0, 0)" );
const color = new THREE .Color ("#ff0000" );
color.setHex (0x00ff00 );
color.setRGB (r, g, b);
color.setHSL (h, s, l);
color.lerp (otherColor, alpha);
color.multiply (otherColor);
color.multiplyScalar (2 );
MathUtils THREE .MathUtils .clamp (value, min, max);
THREE .MathUtils .lerp (start, end, alpha);
THREE .MathUtils .mapLinear (value, inMin, inMax, outMin, outMax);
THREE .MathUtils .degToRad (degrees);
THREE .MathUtils .radToDeg (radians);
THREE .MathUtils .randFloat (min, max);
THREE .MathUtils .randInt (min, max);
THREE .MathUtils .smoothstep (x, min, max);
THREE .MathUtils .smootherstep (x, min, max);
Common Patterns
Proper Cleanup function dispose ( ) {
mesh.geometry .dispose ();
if (Array .isArray (mesh.material )) {
mesh.material .forEach ((m ) => m.dispose ());
} else {
mesh.material .dispose ();
}
texture.dispose ();
scene.remove (mesh);
renderer.dispose ();
}
Timer and Clock for Animation Timer (recommended in r183) - pauses when tab is hidden, cleaner API:
const timer = new THREE .Timer ();
renderer.setAnimationLoop (() => {
timer.update ();
const delta = timer.getDelta ();
const elapsed = timer.getElapsed ();
mesh.rotation .y += delta * 0.5 ;
renderer.render (scene, camera);
});
Clock (legacy, still works):
const clock = new THREE .Clock ();
function animate ( ) {
const delta = clock.getDelta ();
const elapsed = clock.getElapsedTime ();
mesh.rotation .y += delta * 0.5 ;
requestAnimationFrame (animate);
renderer.render (scene, camera);
}
Animation Loop Prefer renderer.setAnimationLoop() over manual requestAnimationFrame. It handles WebXR compatibility and is the standard Three.js pattern:
renderer.setAnimationLoop (() => {
controls.update ();
renderer.render (scene, camera);
});
Responsive Canvas function onWindowResize ( ) {
const width = window .innerWidth ;
const height = window .innerHeight ;
camera.aspect = width / height;
camera.updateProjectionMatrix ();
renderer.setSize (width, height);
renderer.setPixelRatio (Math .min (window .devicePixelRatio , 2 ));
}
window .addEventListener ("resize" , onWindowResize);
Loading Manager const manager = new THREE .LoadingManager ();
manager.onStart = (url, loaded, total ) => console .log ("Started loading" );
manager.onLoad = () => console .log ("All loaded" );
manager.onProgress = (url, loaded, total ) => console .log (`${loaded} /${total} ` );
manager.onError = (url ) => console .error (`Error loading ${url} ` );
const textureLoader = new THREE .TextureLoader (manager);
const gltfLoader = new GLTFLoader (manager);
Performance Tips
Limit draw calls : Merge geometries, use instancing, atlas textures
Frustum culling : Enabled by default, ensure bounding boxes are correct
LOD (Level of Detail) : Use THREE.LOD for distance-based mesh switching
Object pooling : Reuse objects instead of creating/destroying
Avoid getWorldPosition in loops : Cache results
import { mergeGeometries } from "three/examples/jsm/utils/BufferGeometryUtils.js" ;
const merged = mergeGeometries ([geo1, geo2, geo3]);
const lod = new THREE .LOD ();
lod.addLevel (highDetailMesh, 0 );
lod.addLevel (medDetailMesh, 50 );
lod.addLevel (lowDetailMesh, 100 );
scene.add (lod);
WebGPU Renderer (r183) Three.js includes an experimental WebGPU renderer as an alternative to WebGL:
import { WebGPURenderer } from "three/addons/renderers/webgpu/WebGPURenderer.js" ;
const renderer = new WebGPURenderer ({ antialias : true });
await renderer.init ();
renderer.setSize (window .innerWidth , window .innerHeight );
document .body .appendChild (renderer.domElement );
WebGPU uses TSL (Three.js Shading Language) instead of GLSL. The WebGL renderer remains the default and is fully supported.
See Also
threejs-geometry - Geometry creation and manipulation
threejs-materials - Material types and properties
threejs-lighting - Light types and shadows
Connections
Domain: [[Design & Kreativitaet]]
Kategorie: [[3D & Animation]]
Navigation: [[Skills Uebersicht]], [[Home]]