-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.js
66 lines (49 loc) · 3.01 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* global THREE, THREE_VRM, loadVRM, loadMixamoAnimation */
const width = window.innerWidth;
const height = window.innerHeight;
// -- renderer -------------------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer();
renderer.setSize( width, height );
renderer.outputEncoding = THREE.sRGBEncoding;
document.body.appendChild( renderer.domElement );
// -- camera ---------------------------------------------------------------------------------------
const camera = new THREE.PerspectiveCamera( 30.0, width / height, 0.01, 20.0 );
camera.position.set( 0.0, 0.0, 5.0 );
// -- scene ----------------------------------------------------------------------------------------
const scene = new THREE.Scene();
// -- light ----------------------------------------------------------------------------------------
const light = new THREE.DirectionalLight( 0xffffff );
light.position.set( 1.0, 1.0, 1.0 ).normalize();
scene.add( light );
// -- vrm ------------------------------------------------------------------------------------------
let currentVRM = undefined; // 現在使用中のvrm、update内で使えるようにするため
let currentMixer = undefined; // 現在使用中のAnimationMixer、update内で使えるようにするため
const modelUrl = 'https://cdn.glitch.me/c4e5cfb3-513e-4d82-a37f-62836378466b%2Fthree-vrm-girl-1.0-beta.vrm?v=1636610288920'; // モデルのURL
const animationUrl = 'https://cdn.glitch.me/16b81be8-1f14-4a44-b78f-c3f6da842ee7%2FGangnam%20Style.fbx?v=1636708670740'; // MixamoのアニメーションのURL
// See: https://threejs.org/docs/#manual/en/introduction/Animation-system
loadVRM( modelUrl ).then( ( vrm ) => { // vrmを読み込む
currentVRM = vrm; // currentGLTFにvrmを代入
scene.add( vrm.scene ); // モデルをsceneに追加し、表示できるようにする
const head = vrm.humanoid.getBoneNode( 'head' ); // vrmの頭を参照する
camera.position.set( 0.0, head.getWorldPosition( new THREE.Vector3() ).y, 6.0 ); // カメラを頭が中心に来るように動かす
currentMixer = new THREE.AnimationMixer( vrm.scene ); // vrmのAnimationMixerを作る
currentMixer.timeScale = 1;
loadMixamoAnimation( animationUrl, vrm ).then( ( clip ) => { // アニメーションを読み込む
currentMixer.clipAction( clip ).play(); // アニメーションをMixerに適用してplay
} );
} );
// -- update ---------------------------------------------------------------------------------------
const clock = new THREE.Clock();
clock.start();
function update() {
requestAnimationFrame( update );
const delta = clock.getDelta(); // 前フレームとの差分時間を取得
if ( currentMixer ) { // アニメーションが読み込まれていれば
currentMixer.update( delta ); // アニメーションをアップデート
}
if ( currentVRM ) { // VRMが読み込まれていれば
currentVRM.update( delta ); // VRMの各コンポーネントを更新
}
renderer.render( scene, camera ); // 描画
};
update();