: scene, camera 객체를 받아, 카메라의 절두체(사물을 랜더링하는 영역) 안 3D 씬 일부를 이미지로 랜더링한다.
scene graph
: scene이 최상위 노드로서, 배경색과 안개 등의 요소를 포함한다. 부모 자식 트리구조이다.
: 자동차(부모)의 방향이 변하면 바퀴(자식)의 방향도 같이 움직인다.
Texture : 이미지나 canvas로 생성한 이미지 또는 scene객체에서 랜더링한 결과물이다.
Light : 여러 종류의 광원을 말한다.
Material, Geometry는 재사용이 가능하다.
아래는 주황색 정육면체 2개를 그리는 경우이다.
두 정육면체의 위치가 달라야 하니, 2개의 mesh를 준비한다.
각 mesh는 똑같은, 정점데이터와 색을 사용하므로 같은 Geometry, Material를 참조한다.
type="module"
을 꼭 사용해야 한다.<script type="module">
import * as THREE from './resources/threejs/r122/build/three.module.js';
</script>
<body>
<canvas id="c"></canvas>
</body>
WebGLRenderer
은 입력한 데이터를 실제로 canvas에 그려준다.<script type="module">
import * as THREE from './resources/threejs/r122/build/three.module.js';
function main() {
const canvas = document.querySelector('#c'); //canvas 참조
const renderer = new THREE.WebGLRenderer({canvas}); //canvas에 랜더링 객체를 뿌릴거야
...
</script>
PerspectiveCamera(원근 카메라)
를 준비한다.: 시야각으로 아래 예제는 수직면 75도로 설정했다.
: Three.js의 대부분이 각도 단위로 호도(radians)를 사용한다.
: 하지만, 원근 카메라만 특이하게 도(degrees)를 인자로 받는다.
: canvas의 가로 세로 비율이다.
: 기본 설정으로 canvas의 크기는 300x150이다.
: 는 카메라 앞에 렌더링되는 공간 범위를 지정하는 요소이다.
: 이 공간 바깥에 있는 요소는 화면에서 잘려나가며, 렌더링되지 않는다.
: 절두체는 끝부분이 잘려나간 피라미드처럼 생긴 3차원 모양이다.
: 절두체 안에 있는 요소만 렌더링되며, 바깥에 있는 요소는 렌더링되지 않는다.
const fov = 75;
const aspect = 2; // the canvas default
const near = 0.1;
const far = 5;
const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
camera.position.z = 2;
const scene = new THREE.Scene();
const boxWidth = 1;
const boxHeight = 1;
const boxDepth = 1;
const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
const material = new THREE.MeshBasicMaterial({color: 0x44aa88});
scene.add(cube);
renderer.render(scene, camera);
requestAnimationFrame
를 사용한다.requestAnimationFrame
함수는 브라우저에 애니메이션 프레임을 요청하는 함수이다.requestAnimationFrame
함수를 재귀적으로 호출하여 프레임을 계속 호출해야 움직임을 볼 수 있다.아니 재귀적으로 호출하면 제 컴퓨터가 터지잖아요??
아닙니다! three.js는 사용자가 해당 브라우저를 보고 있을 때만 실행된답니다!(와, 효율적이야)
function render(time) {
time *= 0.001; //time -> seconds로 변환
cube.rotation.x = time; //time마다 cube가 회전
cube.rotation.y = time;
requestAnimationFrame(render);
}
requestAnimationFrame(render);