장면·카메라·빛, 고칠 때 쓰는 말
앞 절에서는 '카메라'라는 낱말 하나로 잘린 공이 고쳐졌습니다. 3D 화면에는 이렇게 이름이 붙은 부품이 몇 개 있고, 증상을 말할 때 그 이름을 넣으면 AI가 어디를 고칠지 알아듣기 쉽습니다.
1. 3D 화면은 촬영 스튜디오

화면이 이상하면 이 그림에서 어느 상자의 일인지 먼저 고릅니다. 물체가 잘리거나 너무 작으면 카메라, 까맣거나 어두우면 빛, 색이나 반짝임이 이상하면 재질, 생김새가 이상하면 모양입니다. 고른 상자의 이름이 요청에 넣을 낱말입니다. 아래 표가 그 목록입니다. 표 아래쪽 세 줄(모델, 애니메이션, 충돌과 거리)은 그림의 상자는 아니지만 증상을 말할 때 함께 씁니다.
| 말 | 화면에서 하는 일 | 이럴 때 이 말을 씁니다 |
|---|---|---|
| 장면 Scene | 물체와 빛을 모아 두는 무대 | 배경색을 바꾸거나 물체를 더하고 뺄 때 |
| 카메라 Camera | 어디서 어느 쪽을 보는지 | 물체가 잘리거나 너무 작게, 너무 크게 보일 때 |
| 빛 Light | 물체를 비추는 조명 | 물체가 까맣거나 어둡거나 색이 이상할 때 |
| 물체 Mesh | 무대 위의 것 하나. 모양과 재질을 합친 것 | 무엇을 고칠지 가리킬 때 ("왼쪽 물체만") |
| 모양 Geometry | 물체의 생김새 | 공을 상자로, 각진 면을 매끈하게 바꿀 때 |
| 재질 Material | 물체의 색, 반짝임, 투명도 | 모양은 두고 겉모습만 바꿀 때 |
| 렌더러 Renderer | 장면과 카메라로 그림을 그리는 장치 | 휴대폰에서 끊겨서 그리는 해상도를 낮출 때 (2장 3절) |
| 모델 GLB | 3D 프로그램으로 만든 물체 파일 하나 | 한라봉, 라이캣처럼 공·상자로 만들 수 없는 것을 쓸 때 (3장) |
| 애니메이션 | 조금 움직이고 다시 그리기를 되풀이하는 것 | 도는 속도, 걷는 동작을 말할 때 |
| 충돌과 거리 | 두 물체가 얼마나 가까운지 재는 것 | 귤에 닿았는데 안 먹어질 때 (4장) |
2. 숫자 하나씩 바꿔 보기
아래 블록은 이 교재 화면에서 바로 실행됩니다. 코드는 읽지 않습니다. [실행]을 누르면 아래에 3D 화면이 나옵니다. 맨 위에 대문자로 쓴 이름의 값 하나만 바꾸고 다시 [실행]합니다. "sphere"처럼 따옴표가 있는 값은 따옴표를 남기고, 숫자와 true·false에는 따옴표를 붙이지 않습니다.
값을 바꾸면 [처음 코드로]가 나타납니다. 누르고 다시 [실행]하면 처음 상태로 돌아옵니다. 첫 실행은 Three.js를 받느라 몇 초 걸립니다.
블록마다 끝에 "AI에게는 이렇게 말합니다"를 붙였습니다. 지금 보낼 말이 아니라, 나중에 이런 증상을 볼 때 쓸 말의 꼴입니다. 지금 프로젝트는 귤 하나짜리라 "모양만 도넛으로"를 보내면 귤이 도넛이 됩니다. 보내 보고 싶다면 하나만 보내 보고 [되돌리기]로 돌아옵니다.
2.1 가장 작은 장면
장면, 카메라, 빛, 물체 하나, 렌더러가 다 들어 있는 가장 작은 3D 화면입니다. 파란 상자가 도는지 봅니다. ROTATION_SPEED를 0.6에서 0으로 바꾸면 멈춥니다.

const ROTATION_SPEED = 0.6; // 회전 속도, 0이면 멈춥니다.
const HEIGHT = 320; // 결과 화면의 높이입니다.
import * as THREE from "https://cdn.jsdelivr.net/npm/three@0.186.1/build/three.module.js";
// Scene, 스튜디오 무대입니다.
const scene = new THREE.Scene();
scene.background = new THREE.Color("#eef4ff");
// Camera, 촬영 카메라입니다. 원점의 도형을 바라봅니다.
const camera = new THREE.PerspectiveCamera(45, 1, 0.1, 100);
camera.position.z = 5;
// Renderer, 장면과 카메라로 화면을 그리는 장비입니다.
const renderer = new THREE.WebGLRenderer({ antialias: true });
// 화면 크기 설정과 ResizeObserver는 결과 칸에 맞추는 준비 코드입니다. 그대로 두세요.
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.5));
document.body.style.padding = "0";
document.body.append(renderer.domElement);
renderer.domElement.style.cssText = `display:block;width:100%;height:${HEIGHT}px`;
const geometry = new THREE.BoxGeometry(1.5, 1.5, 1.5);
const material = new THREE.MeshStandardMaterial({ color: "#356fda" });
const mesh = new THREE.Mesh(geometry, material);
mesh.rotation.x = 0.4;
scene.add(mesh);
// Light, 스튜디오 조명입니다.
const light = new THREE.DirectionalLight("#ffffff", 3);
light.position.set(3, 4, 5);
scene.add(light, new THREE.AmbientLight("#ffffff", 0.6));
// 결과 칸의 너비가 바뀌어도 도형이 찌그러지지 않게 합니다.
new ResizeObserver(() => {
const width = Math.max(1, document.body.clientWidth);
renderer.setSize(width, HEIGHT, false);
camera.aspect = width / HEIGHT;
camera.updateProjectionMatrix();
}).observe(document.body);
// 애니메이션 루프, 조금씩 돌리고 다시 촬영합니다.
const started = performance.now();
function animate(now) {
mesh.rotation.y = ((now - started) / 1000) * ROTATION_SPEED;
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
앞 절 자기소개의 귤이라면 AI에게는 이렇게 말합니다.
귤이 도는 게 조금 빨라. 지금 속도의 절반으로 천천히 돌게 해 줘.
2.2 카메라를 뒤로 빼기
CAMERA_Z를 5에서 15로 바꾸면 상자가 작게 보입니다. 상자의 크기는 그대로이고 카메라만 멀어졌습니다. 앞 절에서 AI가 한 일이 이것입니다. [처음 코드로]로 되돌린 뒤 CAMERA_Y만 5로 바꾸면 위에서 내려다봅니다.

const CAMERA_Z = 5; // 15로 바꾸면 카메라가 3배 뒤로 갑니다.
const CAMERA_Y = 0; // 5로 바꾸면 위에서 내려다봅니다.
const HEIGHT = 320;
import * as THREE from "https://cdn.jsdelivr.net/npm/three@0.186.1/build/three.module.js";
const scene = new THREE.Scene();
scene.background = new THREE.Color("#eef4ff");
const camera = new THREE.PerspectiveCamera(45, 1, 0.1, 100);
camera.position.set(0, CAMERA_Y, CAMERA_Z);
camera.lookAt(0, 0, 0); // 카메라가 바라보는 목표입니다.
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.5));
document.body.style.padding = "0";
document.body.append(renderer.domElement);
renderer.domElement.style.cssText = `display:block;width:100%;height:${HEIGHT}px`;
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(1.5, 1.5, 1.5),
new THREE.MeshStandardMaterial({ color: "#356fda" })
);
mesh.rotation.set(0.2, 0.5, 0);
scene.add(mesh);
const floor = new THREE.GridHelper(8, 8, "#96aac3", "#ccd7e8");
floor.position.y = -1.1;
scene.add(floor);
const light = new THREE.DirectionalLight("#ffffff", 3);
light.position.set(3, 4, 5);
scene.add(light, new THREE.AmbientLight("#ffffff", 0.6));
// 멈춘 장면도 화면의 너비가 바뀌면 다시 그립니다.
new ResizeObserver(() => {
const width = Math.max(1, document.body.clientWidth);
renderer.setSize(width, HEIGHT, false);
camera.aspect = width / HEIGHT;
camera.updateProjectionMatrix();
renderer.render(scene, camera);
}).observe(document.body);

AI에게는 이렇게 말합니다.
물체 크기는 그대로 두고 카메라를 뒤로 빼서 물체가 3D 영역 안에 다 보이게 해 줘.
카메라를 조금 위로 올려서 물체를 살짝 내려다보게 해 줘.
2.3 빛을 끄면
두 상자는 재질만 다릅니다. 왼쪽은 빛에 반응하는 재질(MeshStandardMaterial), 오른쪽은 빛과 상관없이 색을 그대로 칠하는 재질(MeshBasicMaterial)입니다. LIGHT_ON을 false로 바꾸면 왼쪽만 까맣게 됩니다.

const LIGHT_ON = true; // false로 바꾸면 모든 조명을 끕니다.
const LIGHT_POWER = 3; // 조명이 켜져 있을 때의 밝기입니다.
const HEIGHT = 320;
import * as THREE from "https://cdn.jsdelivr.net/npm/three@0.186.1/build/three.module.js";
const scene = new THREE.Scene();
scene.background = new THREE.Color("#eef4ff");
const camera = new THREE.PerspectiveCamera(45, 1, 0.1, 100);
camera.position.z = 7;
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.5));
document.body.style.padding = "0";
document.body.append(renderer.domElement);
renderer.domElement.style.cssText = `display:block;width:100%;height:${HEIGHT}px`;
const geometry = new THREE.BoxGeometry(1.1, 1.1, 1.1);
const left = new THREE.Mesh(
geometry, new THREE.MeshStandardMaterial({ color: "#356fda" })
);
const right = new THREE.Mesh(
geometry, new THREE.MeshBasicMaterial({ color: "#356fda" })
);
left.position.x = -1;
right.position.x = 1;
left.rotation.set(0.4, 0.5, 0);
right.rotation.copy(left.rotation);
scene.add(left, right);
const light = new THREE.DirectionalLight("#ffffff", LIGHT_POWER);
light.position.set(3, 4, 5);
light.visible = LIGHT_ON;
const ambient = new THREE.AmbientLight("#ffffff", 0.6);
ambient.visible = LIGHT_ON;
scene.add(light, ambient);
new ResizeObserver(() => {
const width = Math.max(1, document.body.clientWidth);
renderer.setSize(width, HEIGHT, false);
camera.aspect = width / HEIGHT;
camera.updateProjectionMatrix();
renderer.render(scene, camera);
}).observe(document.body);
화면에 물체가 까맣게 보이면 물체가 없는 것이 아니라 빛이 없는 것일 수 있습니다. 반대로 오른쪽처럼 입체감 없이 납작해 보이면 빛에 반응하지 않는 재질입니다. AI에게는 이렇게 말합니다.
물체가 까맣게만 보여. 빛이 물체를 비추는지 확인하고, 주변을 고르게 밝히는 빛을 더해 줘.
2.4 모양 바꾸기
SHAPE를 "box"(상자), "sphere"(공), "torus"(도넛), "torusKnot"(매듭), "icosahedron"(면이 20개인 이십면체) 중 하나로 바꿔 봅니다.
3D 물체의 겉면은 작고 평평한 면을 이어 붙여 만듭니다. 면이 적으면 각지고, 많으면 매끈해 보이는 대신 그릴 것이 늘어납니다. "sphere"일 때 FLAT_SHADING을 true로 바꾸면 공을 이루는 작은 면들이 각지게 드러납니다. 비교하기 쉽게 면 수를 적게 만든 공입니다.

const SHAPE = "sphere"; // box, sphere, torus, torusKnot, icosahedron
const FLAT_SHADING = false; // true이면 면마다 각진 느낌이 납니다.
const HEIGHT = 320;
import * as THREE from "https://cdn.jsdelivr.net/npm/three@0.186.1/build/three.module.js";
const scene = new THREE.Scene();
scene.background = new THREE.Color("#eef4ff");
const camera = new THREE.PerspectiveCamera(45, 1, 0.1, 100);
camera.position.z = 5;
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.5));
document.body.style.padding = "0";
document.body.append(renderer.domElement);
renderer.domElement.style.cssText = `display:block;width:100%;height:${HEIGHT}px`;
// 이름에 해당하는 도형만 만듭니다.
const shapes = {
box: () => new THREE.BoxGeometry(1.5, 1.5, 1.5),
sphere: () => new THREE.SphereGeometry(1, 12, 8),
torus: () => new THREE.TorusGeometry(0.8, 0.3, 16, 48),
torusKnot: () => new THREE.TorusKnotGeometry(0.7, 0.24, 80, 12),
icosahedron: () => new THREE.IcosahedronGeometry(1.1, 0),
};
const material = new THREE.MeshStandardMaterial({
color: "#356fda", roughness: 0.45, flatShading: FLAT_SHADING,
});
const mesh = new THREE.Mesh(shapes[SHAPE](), material);
mesh.rotation.set(0.4, 0.5, 0);
scene.add(mesh);
const light = new THREE.DirectionalLight("#ffffff", 3);
light.position.set(3, 4, 5);
scene.add(light, new THREE.AmbientLight("#ffffff", 0.6));
new ResizeObserver(() => {
const width = Math.max(1, document.body.clientWidth);
renderer.setSize(width, HEIGHT, false);
camera.aspect = width / HEIGHT;
camera.updateProjectionMatrix();
renderer.render(scene, camera);
}).observe(document.body);
AI에게는 이렇게 말합니다.
재질은 그대로 두고 모양만 도넛으로 바꿔 줘.
공 표면이 각져 보여. 모양은 그대로 두고 더 매끈하게 보이게 해 줘.
2.5 재질 바꾸기
아래 값을 하나씩 바꿔 봅니다. 하나를 본 뒤에는 [처음 코드로]로 되돌리고 다음 값을 바꿉니다.
COLOR를"#9b5de5"로: 공이 보라가 됩니다.#으로 시작하는 여섯 글자는 색 번호이고,#356fda는 지금의 파랑,#9b5de5는 보라입니다.ROUGHNESS를0.1로: 표면이 매끈해져 공 위의 밝은 점이 작고 또렷해집니다.WIREFRAME을true로: 겉면이 사라지고 면의 테두리 선만 남아, 뒤의 주황 상자가 선 사이로 보입니다. 위 2.4에서 말한 작은 면이 이 선으로 나뉜 칸들입니다.OPACITY를0.35로: 공이 반투명해져 뒤에 있는 주황 상자가 비칩니다.METALNESS를1로: 공이 어두워지고 밝은 점 하나만 남습니다. 금속은 주변을 비추는데 이 장면에는 비칠 풍경이 없어서입니다.

const COLOR = "#356fda"; // 색입니다.
const METALNESS = 0.2; // 0은 비금속, 1은 금속입니다.
const ROUGHNESS = 0.4; // 작을수록 표면이 매끈합니다.
const WIREFRAME = false; // true이면 삼각형의 선을 표시합니다.
const OPACITY = 1; // 0은 투명, 1은 불투명입니다.
const HEIGHT = 320;
import * as THREE from "https://cdn.jsdelivr.net/npm/three@0.186.1/build/three.module.js";
const scene = new THREE.Scene();
scene.background = new THREE.Color("#eef4ff");
const camera = new THREE.PerspectiveCamera(45, 1, 0.1, 100);
camera.position.z = 5;
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.5));
document.body.style.padding = "0";
document.body.append(renderer.domElement);
renderer.domElement.style.cssText = `display:block;width:100%;height:${HEIGHT}px`;
const material = new THREE.MeshStandardMaterial({
color: COLOR, metalness: METALNESS, roughness: ROUGHNESS,
wireframe: WIREFRAME, transparent: OPACITY < 1, opacity: OPACITY,
});
const mesh = new THREE.Mesh(new THREE.SphereGeometry(1, 32, 24), material);
scene.add(mesh);
// 투명도를 비교하도록 뒤에 다른 물체를 놓습니다.
const behind = new THREE.Mesh(
new THREE.BoxGeometry(1.3, 1.3, 1.3),
new THREE.MeshStandardMaterial({ color: "#ef8d32" })
);
behind.position.set(0.45, 0, -1.6);
scene.add(behind);
const light = new THREE.DirectionalLight("#ffffff", 4);
light.position.set(3, 4, 5);
scene.add(light, new THREE.AmbientLight("#ffffff", 0.7));
new ResizeObserver(() => {
const width = Math.max(1, document.body.clientWidth);
renderer.setSize(width, HEIGHT, false);
camera.aspect = width / HEIGHT;
camera.updateProjectionMatrix();
renderer.render(scene, camera);
}).observe(document.body);
AI에게는 이렇게 말합니다.
모양은 그대로 두고 재질만 뒤가 살짝 비치는 반투명으로 바꿔 줘.
금속처럼 반짝이게 해 줘. 지금은 금속으로 바꾸면 어둡게만 보이니 비칠 주변 환경도 함께 넣어 줘.
2.6 두 물체를 다른 속도로
애니메이션은 조금 움직이고 다시 그리기를 되풀이하는 것입니다. 물체마다 움직임을 따로 줄 수 있습니다. LEFT_SPEED를 0으로 바꾸면 왼쪽 상자만, PLAY를 false로 바꾸면 둘 다 멈춥니다.

const PLAY = true; // false이면 두 도형을 모두 멈춥니다.
const LEFT_SPEED = 0.4; // 왼쪽 상자의 회전 속도입니다.
const RIGHT_SPEED = 1.2; // 오른쪽 도넛의 회전 속도입니다.
const HEIGHT = 320;
import * as THREE from "https://cdn.jsdelivr.net/npm/three@0.186.1/build/three.module.js";
const scene = new THREE.Scene();
scene.background = new THREE.Color("#eef4ff");
const camera = new THREE.PerspectiveCamera(45, 1, 0.1, 100);
camera.position.z = 7;
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.5));
document.body.style.padding = "0";
document.body.append(renderer.domElement);
renderer.domElement.style.cssText = `display:block;width:100%;height:${HEIGHT}px`;
const left = new THREE.Mesh(
new THREE.BoxGeometry(1.05, 1.05, 1.05),
new THREE.MeshStandardMaterial({ color: "#356fda" })
);
const right = new THREE.Mesh(
new THREE.TorusGeometry(0.5, 0.2, 16, 40),
new THREE.MeshStandardMaterial({ color: "#ef8d32" })
);
left.position.x = -1;
right.position.x = 1;
left.rotation.x = 0.4;
right.rotation.x = 0.4;
scene.add(left, right);
const light = new THREE.DirectionalLight("#ffffff", 3);
light.position.set(3, 4, 5);
scene.add(light, new THREE.AmbientLight("#ffffff", 0.6));
new ResizeObserver(() => {
const width = Math.max(1, document.body.clientWidth);
renderer.setSize(width, HEIGHT, false);
camera.aspect = width / HEIGHT;
camera.updateProjectionMatrix();
}).observe(document.body);
const started = performance.now();
function animate(now) {
const seconds = PLAY ? (now - started) / 1000 : 0;
left.rotation.y = seconds * LEFT_SPEED;
right.rotation.y = seconds * RIGHT_SPEED;
renderer.render(scene, camera);
// 멈춘 동안에도 계속 그리므로, 화면 너비가 바뀌어도 모양이 맞습니다.
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
AI에게는 이렇게 말합니다.
왼쪽 물체는 멈추고, 오른쪽 물체만 지금 속도로 계속 돌게 해 줘.
2.7 끌어서 돌려 보기
매듭을 마우스로 끌거나 손가락으로 쓸어 봅니다. 물체를 돌리는 것처럼 보이지만 실제로는 카메라가 물체 둘레를 돕니다. 빛은 제자리에 있고 카메라만 도니, 끌어서 돌리다 보면 빛이 덜 닿는 뒤쪽도 화면에 들어옵니다. ALLOW_ZOOM을 false로 바꾸면 마우스 휠과 두 손가락 확대가 꺼집니다.

const ALLOW_ZOOM = true; // false이면 확대·축소를 끕니다.
const ROTATE_SPEED = 1; // 드래그에 반응하는 회전 속도입니다.
const HEIGHT = 320;
// 이 예제는 본체와 추가 기능을 같은 제공처·버전으로 불러옵니다.
import * as THREE from "https://esm.sh/three@0.186.1";
import { OrbitControls } from "https://esm.sh/three@0.186.1/addons/controls/OrbitControls.js";
const scene = new THREE.Scene();
scene.background = new THREE.Color("#eef4ff");
const camera = new THREE.PerspectiveCamera(45, 1, 0.1, 100);
camera.position.set(0, 1.5, 5);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.5));
document.body.style.padding = "0";
document.body.append(renderer.domElement);
renderer.domElement.style.cssText = `display:block;width:100%;height:${HEIGHT}px`;
const mesh = new THREE.Mesh(
new THREE.TorusKnotGeometry(0.7, 0.24, 80, 12),
new THREE.MeshStandardMaterial({ color: "#356fda", roughness: 0.35 })
);
scene.add(mesh);
const light = new THREE.DirectionalLight("#ffffff", 3);
light.position.set(3, 4, 5);
scene.add(light, new THREE.AmbientLight("#ffffff", 0.6));
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableZoom = ALLOW_ZOOM;
controls.enablePan = false; // 드래그로 도형이 화면 밖에 나가지 않게 합니다.
controls.rotateSpeed = ROTATE_SPEED;
controls.minDistance = 3;
controls.maxDistance = 10;
const draw = () => renderer.render(scene, camera);
controls.addEventListener("change", draw); // 조작할 때 다시 그립니다.
controls.update();
new ResizeObserver(() => {
const width = Math.max(1, document.body.clientWidth);
renderer.setSize(width, HEIGHT, false);
camera.aspect = width / HEIGHT;
camera.updateProjectionMatrix();
draw();
}).observe(document.body);
AI에게는 이렇게 말합니다.
끌어서 돌리는 건 그대로 두고, 휠이나 두 손가락으로 확대·축소되는 건 꺼 줘.
3. 증상을 말하는 꼴
앞 절의 두 번째 요청 앞부분을 다시 봅니다.
넓은 화면에서 공이 너무 커서 3D 영역 밖으로 잘려 보여. 카메라를 뒤로 빼서 공이 영역 안에 다 보이게 해 줘.
세 가지가 들어 있습니다. 어디서 무엇이 어떻게 보이는지(넓은 화면에서 공이 잘려), 어느 부품을 손볼지(카메라), 어떻게 되면 되는지(영역 안에 다 보이게). 부품 이름을 모르면 첫째와 셋째(어디서 무엇이 어떻게 보이는지, 어떻게 되면 되는지)만 말해도 됩니다. 바꾸지 않을 것이 있으면 "나머지는 그대로 둬"를 붙입니다.
마지막 수정