-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvrm_threejs.html
More file actions
133 lines (117 loc) · 5.4 KB
/
vrm_threejs.html
File metadata and controls
133 lines (117 loc) · 5.4 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<!DOCTYPE html>
<html>
<head>
<title>Three.js VRM Test</title>
<meta charset="utf-8">
<style>
body { margin: 0; }
canvas { display: block; }
</style>
</head>
<body>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/examples/js/loaders/GLTFLoader.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/examples/js/controls/OrbitControls.js"></script>
<!-- Scripts from the user's project -->
<script src="js/mmdparser.js"></script>
<script src="js/CCDIKSolver.js"></script>
<script src="js/BVHLoader.js"></script>
<script src="js/MMDLoader.js"></script>
<!-- Our new standalone script -->
<script src="js/aframe-vrm-standalone.js"></script>
<script>
// 1. Setup scene, camera, renderer
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xbbbbbb);
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
camera.position.set( 0, 1.2, 1.5 );
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setPixelRatio(window.devicePixelRatio);
document.body.appendChild( renderer.domElement );
const controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.target.set( 0, 1.0, 0 );
controls.update();
const light = new THREE.DirectionalLight(0xffffff);
light.position.set(1, 1, 1).normalize();
scene.add(light);
const ambientLight = new THREE.AmbientLight(0xcccccc);
scene.add(ambientLight);
scene.add(new THREE.GridHelper());
// 2. Load VRM model
const vrmUrl = 'models/Asian/Asian_F_1_Busi.vrm';
const vrmaUrl = 'VRMA/Clapping.vrma';
let currentVrm = null;
// Use the globally exposed VRMLoader from aframe-vrm-standalone.js
const loader = new VRMLoader();
console.log('Loading VRM model...');
loader.load(vrmUrl).then((avatar) => {
console.log('VRM model loaded:', avatar);
currentVrm = avatar;
scene.add(currentVrm.model);
currentVrm.model.rotation.y = Math.PI;
// 3. Load and play animation
console.log('Loading VRMA animation...');
// Use the globally exposed VRMALoaderWrapper
const vrmaLoader = new VRMALoaderWrapper();
vrmaLoader.load(vrmaUrl, currentVrm, {}).then((clip) => {
console.log('VRMA animation loaded:', clip);
if (clip && currentVrm && currentVrm.mixer) {
// Apply the same quaternion fix as before
clip.tracks.forEach((track) => {
if (track.name.endsWith('.quaternion')) {
const newValues = [];
for (let i = 0; i < track.values.length; i += 4) {
const qx = track.values[i];
const qy = track.values[i + 1];
const qz = track.values[i + 2];
const qw = track.values[i + 3];
newValues.push(-qx, qy, -qz, qw);
}
// Post-process to ensure shortest path for interpolation
for (let i = 4; i < newValues.length; i += 4) {
const x1 = newValues[i - 4];
const y1 = newValues[i - 3];
const z1 = newValues[i - 2];
const w1 = newValues[i - 1];
const x2 = newValues[i];
const y2 = newValues[i + 1];
const z2 = newValues[i + 2];
const w2 = newValues[i + 3];
if (x1 * x2 + y1 * y2 + z1 * z2 + w1 * w2 < 0) {
newValues[i] = -x2;
newValues[i + 1] = -y2;
newValues[i + 2] = -z2;
newValues[i + 3] = -w2;
}
}
track.values = newValues;
}
});
console.log('Playing animation clip:', clip.name);
currentVrm.mixer.clipAction(clip).play();
} else {
console.error('Animation clip, VRM object, or VRM mixer not found.');
}
}).catch(e => console.error('Error loading animation', e));
}).catch(e => console.error('Error loading VRM', e));
// 4. Animation loop
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame( animate );
const delta = clock.getDelta();
if (currentVrm) {
currentVrm.update(delta);
}
renderer.render( scene, camera );
}
animate();
// Handle window resize
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}, false);
</script>
</body>
</html>