전체코드
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Three.js with GLTF Model</title>
</head>
<body>
<canvas id="canvas" width="800" height="600"></canvas>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/three@0.141.0/build/three.module.js",
"GLTFLoader" : "https://unpkg.com/three@0.141.0/examples/jsm/loaders/GLTFLoader.js",
"OrbitControls": "https://unpkg.com/three@0.141.0/examples/jsm/controls/OrbitControls.js"
}
}
</script>
<script type="module">
import { GLTFLoader } from 'GLTFLoader';
import * as THREE from 'three';
import { OrbitControls } from 'OrbitControls';
let scene = new THREE.Scene();
let renderer = new THREE.WebGLRenderer({
canvas: document.querySelector('#canvas'),
antialias: true
});
renderer.outputEncoding = THREE.sRGBEncoding;
let camera = new THREE.PerspectiveCamera(30, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.set(0, 10, 0);
camera.lookAt(new THREE.Vector3(0, 0, 0));
scene.background = new THREE.Color('white');
let light = new THREE.DirectionalLight(0xffffff, 1);
scene.add(light);
let loader = new GLTFLoader();
loader.load('scene.gltf', function(gltf){
scene.add(gltf.scene);
});
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.25;
controls.screenSpacePanning = false;
controls.maxPolarAngle = Math.PI / 2;
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
animate();
window.addEventListener('resize', () => {
const newWidth = window.innerWidth;
const newHeight = window.innerHeight;
camera.aspect = newWidth / newHeight;
camera.updateProjectionMatrix();
renderer.setSize(newWidth, newHeight);
});
</script>
</body>
</html>
orbitContrls
// OrbitControls를 사용하여 마우스 및 터치 입력으로 3D 공간을 회전 및 이동하는 컨트롤을 설정합니다.
// enableDamping: 부드러운 감속 효과를 활성화합니다. 마우스 드래그 또는 터치 이동이 자연스럽게 감속됩니다.
controls.enableDamping = true;
// dampingFactor: enableDamping이 활성화된 경우, 이 값이 높을수록 감속 효과가 더 강력해집니다.
controls.dampingFactor = 0.25;
// screenSpacePanning: true로 설정하면 화면 고정 평면에서의 패닝이 가능해집니다. (기본값은 false)
controls.screenSpacePanning = false;
// maxPolarAngle: 카메라의 최대 위도 각도를 설정합니다. 이 값 이상으로는 카메라가 올라가지 않습니다.
// Math.PI / 2로 설정하면 상하로 90도까지만 이동이 가능합니다. (수평 기준)
controls.maxPolarAngle = Math.PI / 2;