
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Three.js Basic - Cube</title>
<!-- 스타일시트 -->
<link rel="stylesheet" href="style.css" />
<!-- Import Maps: 'three' 모듈 경로를 지정 -->
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/three@0.186.1/build/three.module.js",
"three/addons/": "https://unpkg.com/three@0.186.1/examples/jsm/"
}
}
</script>
</head>
<body>
<!-- 메인 스크립트 (ES Module) -->
<script type="module" src="main.js"></script>
</body>
</html>
/* 기본 여백 제거 */
html,
body {
margin: 0;
padding: 0;
}
/* 화면 밖으로 넘치는 스크롤 방지 */
body {
overflow: hidden;
}
/* 캔버스 여백 제거 */
canvas {
margin: 0;
padding: 0;
}
// ============================================================
// 1. Three.js 모듈 불러오기
// index.html의 importmap에 등록된 'three' 경로를 사용합니다.
// ============================================================
import * as THREE from 'three';
// ============================================================
// 2. 씬(Scene) 생성
// 3D 오브젝트, 조명 등을 담아두는 공간입니다.
// ============================================================
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x222831);
// ============================================================
// 3. 카메라(PerspectiveCamera) 설정
// 사람 눈처럼 원근감이 있는 카메라입니다.
// 인자: 시야각(FOV), 화면 비율, 근거리 클리핑, 원거리 클리핑
// ============================================================
const camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000
);
camera.position.z = 3; // 큐브가 보이도록 카메라를 뒤로 이동
// ============================================================
// 4. 렌더러(WebGLRenderer) 설정
// 씬을 카메라 시점으로 그려서 <canvas>에 출력합니다.
// ============================================================
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2)); // 고해상도 화면 대응
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement); // 캔버스를 페이지에 추가
// ============================================================
// 5. 큐브 생성
// 지오메트리(모양) + 머티리얼(재질) = 메시(Mesh)
// MeshStandardMaterial은 빛을 받아 명암이 표현되는 재질입니다.
// ============================================================
const geometry = new THREE.BoxGeometry(1, 1, 1);
const material = new THREE.MeshStandardMaterial({
color: 0x00aaff,
roughness: 0.4,
metalness: 0.1,
});
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);
// ============================================================
// 6. 조명 설정
// 환경광(AmbientLight): 전체를 고르게 밝히는 은은한 빛
// 방향광(DirectionalLight): 한 방향에서 비추는 빛 → 면마다 명암 차이 발생
// ============================================================
const ambientLight = new THREE.AmbientLight(0xffffff, 0.4);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 2);
directionalLight.position.set(3, 4, 5); // 빛의 위치 (오른쪽 위 앞쪽)
scene.add(directionalLight);
// ============================================================
// 7. 창 크기 변경 대응
// 브라우저 창이 바뀌어도 화면 비율이 깨지지 않도록 갱신합니다.
// ============================================================
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener('resize', onWindowResize);
// ============================================================
// 8. 애니메이션 루프
// 매 프레임마다 큐브를 아주 조금씩 회전시키고 화면을 다시 그립니다.
// ============================================================
function animate() {
cube.rotation.x += 0.002;
cube.rotation.y += 0.004;
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);