<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8" />
<title>Load a 3D model</title>
<style>
body {
margin: 0;
overflow: hidden;
}
</style>
</head>
<body>
<script
async
src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"
></script>
<script type="importmap">
{
"imports": {
"three": "./js/three.module.js",
"MTLLoader": "./js/MTLLoader.js",
"OBJLoader": "./js/OBJLoader.js",
"OrbitControls": "./js/OrbitControls.js"
"FirstPersonControls": "https://threejs.org/examples/jsm/controls/FirstPersonControls.js"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { MTLLoader } from 'MTLLoader';
import { OBJLoader } from 'OBJLoader';
import { FirstPersonControls } from 'FirstPersonControls';
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 1.6, 3);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const ambientLight = new THREE.AmbientLight(0xcccccc, 4);
scene.add(ambientLight);
const controls = new FirstPersonControls(camera, renderer.domElement);
controls.lookSpeed = 0.1;
controls.movementSpeed = 1;
controls.noFly = true;
controls.lookVertical = true;
controls.constrainVertical = false;
new MTLLoader()
.setPath('models/rouka/')
.load('rouka.mtl', function (materials) {
materials.preload();
new OBJLoader()
.setPath('models/rouka/')
.setMaterials(materials)
.load('rouka.obj', function (object) {
object.position.y = 0;
scene.add(object);
}, onProgress, onError);
});
function onProgress(xhr) {
if (xhr.lengthComputable) {
const percentComplete = (xhr.loaded / xhr.total) * 100;
console.log('モデル読み込み ' + Math.round(percentComplete, 2) + '% 終了');
}
}
function onError(error) {
console.error('モデル読み込みエラー: ', error);
}
const animate = function () {
requestAnimationFrame(animate);
controls.update(0.1);
renderer.render(scene, camera);
};
animate();
window.addEventListener('resize', onWindowResize, false);
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
</script>
</body>
</html>