<html>
<head>
<meta charset="utf-8" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/110/three.min.js"></script>
<script src="./ColladaLoader.js"></script>
<title>サンプル</title>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
let model = null;
window.addEventListener('load', init);
function init() {
const width = 500;
const height = 400;
const renderer = new THREE.WebGLRenderer({
canvas: document.querySelector('#canvas')
});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(width, height);
renderer.gammaInput = true;
renderer.gammaOutput = true;
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(45, width / height, 0.1, 10000);
// カメラの初期座標を設定
camera.position.set(0, 2.5, 5);
camera.lookAt(new THREE.Vector3(0, 0, 0));
// 平行光源を作成
const directionalLight = new THREE.DirectionalLight(0xffffff);
directionalLight.position.set(0, 1, 0);
scene.add(directionalLight);
// 環境光を追加
const ambientLight = new THREE.AmbientLight(0x333333);
scene.add(ambientLight);
// 3DS形式のモデルデータを読み込む
const loader = new THREE.ColladaLoader();
loader.load('./clock.dae', collada => {
// 読み込み後に3D空間に追加
model = collada.scene;
// 文字盤がうまく読み込めないのでpngファイルでテクスチャを生成して描画する
for(let i=0; i<model.children.length; i++) {
if(model.children[i].material != null) {
if(model.children[i].material.name == "material_clock_face") {
const loader = new THREE.TextureLoader();
let texture = loader.load('face.png');
// マテリアルを作成
const material = new THREE.MeshStandardMaterial({map: texture });
model.children[i].material = material;
}
}
}
scene.add(model);
});
tick();
let ang = 0;
// 毎フレーム時に実行されるループイベントです
function tick() {
// レンダリング
if(model != null) {
ang += 0.01;
model.rotation.z = ang;
}
renderer.render(scene, camera);
requestAnimationFrame(tick);
}
}
</script>
</body>
</html>