Three.js는 웹페이지에 3D 객체를 쉽게 랜더링할 수 있도록 도와주는 Javascript 3D 라이브러리이다.
WebGL?
웹상에서 2D 및 3D 그래픽을 렌더링하기 위한 로우 레벨 Javascript API 이다.
본격적으로 Three.js이 뭔지 실습을 통해 알아보자.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My first three.js app</title>
<style>
body { margin: 0; }
</style>
</head>
<body>
<script type="module" src="/main.js"></script>
</body>
</html>
import * as THREE from 'three';
public/ 폴더는 포함된 파일이 변경되지 않고 웹 사이트에 푸시되기 때문에 정적 폴더라고 한다.
# three.js
npm install --save three
# vite
npm install --save-dev vite
npx vite
- index.html에서 head 태그 안에 아래 코드를 삽입한다.
<script async src="https://unpkg.com/es-module-shims@1.8.0/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/three@<version>/build/three.module.js",
"three/addons/": "https://unpkg.com/three@<version>/examples/jsm/"
}
}
</script>
npx serve .
여기까지 순조롭게 진행이 잘 되었다면, 본격적으로 3D 모델링을 만들 준비가 되었다는 것이다!!
기본적으로 Three.js에는 3D 엔진의 기본 사항이 포함되어 있다.
컨트롤, 로더, 후처리 효과 등 기타 three.js 구성 요소는 addons/ 디렉터리의 일부이다.
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
const controls = new OrbitControls( camera, renderer.domElement );
const loader = new GLTFLoader();