First Scene
Three.js needs three things to show anything: a Scene (what exists), a Camera (where you’re looking from), and a Renderer (what draws pixels on screen). Everything else builds on this triangle.
Core Setup
import * as THREE from 'three';
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, innerWidth / innerHeight, 0.1, 100);
camera.position.z = 3;
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
The Animation Loop
function animate() {
requestAnimationFrame(animate);
mesh.rotation.x += 0.01;
mesh.rotation.y += 0.01;
renderer.render(scene, camera);
}
animate();
Resize Handling
Always handle resize — otherwise the canvas distorts on window resize:
window.addEventListener('resize', () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
});
What to Experiment With
- Change
PerspectiveCameraFOV (first arg) from 30–120 to see distortion - Try
BoxGeometry(1,1,1)vsSphereGeometry(0.8, 32, 32)vsTorusGeometry(0.7, 0.3, 16, 100) MeshBasicMaterialignores light — swap toMeshNormalMaterialfor instant color
Three.js가 무언가를 표시하려면 세 가지가 필요합니다: Scene(존재하는 것), Camera(보는 위치), Renderer(화면에 픽셀을 그리는 것). 나머지 모든 것은 이 삼각형 위에 구축됩니다.
핵심 설정
import * as THREE from 'three';
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, innerWidth / innerHeight, 0.1, 100);
camera.position.z = 3;
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
애니메이션 루프
function animate() {
requestAnimationFrame(animate);
mesh.rotation.x += 0.01;
mesh.rotation.y += 0.01;
renderer.render(scene, camera);
}
animate();
리사이즈 처리
리사이즈를 처리하지 않으면 창 크기 변경 시 캔버스가 왜곡됩니다:
window.addEventListener('resize', () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
});
실험해보기
PerspectiveCameraFOV(첫 번째 인자)를 30–120으로 바꿔 왜곡 확인BoxGeometry(1,1,1)vsSphereGeometry(0.8, 32, 32)vsTorusGeometry(0.7, 0.3, 16, 100)비교MeshBasicMaterial은 빛을 무시합니다 —MeshNormalMaterial로 바꿔 즉각적인 색상 확인