Skip to content

Commit 77db144

Browse files
committed
アニメーションサンプルを追加
1 parent 2575c88 commit 77db144

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

samples/loader_glb_animation.html

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<html>
2+
<head>
3+
<meta charset="utf-8" />
4+
<script src="https://unpkg.com/three@0.147.0/build/three.min.js"></script>
5+
<script src="https://unpkg.com/three@0.147.0/examples/js/controls/OrbitControls.js"></script>
6+
<script src="https://unpkg.com/three@0.147.0/examples/js/loaders/GLTFLoader.js"></script>
7+
8+
<script>
9+
// ページの読み込みを待つ
10+
window.addEventListener('DOMContentLoaded', init);
11+
12+
// 非同期処理で待機するのでasync function宣言とする
13+
async function init() {
14+
// サイズを指定
15+
const width = 960;
16+
const height = 540;
17+
18+
// レンダラーを作成
19+
const canvasElement = document.querySelector('#myCanvas');
20+
const renderer = new THREE.WebGLRenderer({
21+
canvas: canvasElement,
22+
});
23+
renderer.setPixelRatio(window.devicePixelRatio);
24+
renderer.setSize(width, height);
25+
26+
// シーンを作成
27+
const scene = new THREE.Scene();
28+
29+
// カメラを作成
30+
const camera = new THREE.PerspectiveCamera(45, width / height, 0.001, 10000);
31+
// カメラの初期座標を設定
32+
camera.position.set(2, 2, 2);
33+
34+
// カメラコントローラーを作成
35+
const controls = new THREE.OrbitControls(camera, canvasElement);
36+
controls.target.set(0, 1, 0);
37+
controls.update();
38+
39+
// 平行光源を作成
40+
// 上から照らす
41+
const directionalLight = new THREE.DirectionalLight(0xffffff);
42+
directionalLight.position.set(1, 1, 1);
43+
scene.add(directionalLight);
44+
45+
// 横から照らす
46+
const directionalLight2 = new THREE.DirectionalLight(0xffffff);
47+
directionalLight2.position.set(1, 0, 1);
48+
scene.add(directionalLight2);
49+
50+
// 正面から照らす
51+
const directionalLight3 = new THREE.DirectionalLight(0xffffff);
52+
directionalLight3.position.set(0, 0, -1);
53+
scene.add(directionalLight3);
54+
55+
// GLTF形式のモデルデータを読み込む
56+
const loader = new THREE.GLTFLoader();
57+
// GLTFファイルのパスを指定
58+
const gltf = await loader.loadAsync('models/gltf/animation/Soldier.glb');
59+
// 読み込み後に3D空間に追加
60+
const model = gltf.scene;
61+
scene.add(model);
62+
63+
// アニメーションミキサー
64+
const mixer = new THREE.AnimationMixer(model);
65+
66+
// 0 : idle, 1: run, 3: walk
67+
mixer.clipAction(gltf.animations[1]).play();
68+
69+
let oldTime = Date.now();
70+
tick();
71+
72+
// 毎フレーム時に実行されるループイベントです
73+
function tick() {
74+
// レンダリング
75+
renderer.render(scene, camera);
76+
77+
// アニメーションを更新
78+
const diffTime = Date.now() - oldTime;
79+
mixer.update(diffTime / 1000);
80+
oldTime = Date.now();
81+
82+
requestAnimationFrame(tick);
83+
}
84+
}
85+
</script>
86+
</head>
87+
<body>
88+
<canvas id="myCanvas"></canvas>
89+
</body>
90+
</html>
2.06 MB
Binary file not shown.

0 commit comments

Comments
 (0)