-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanimate.html
More file actions
323 lines (282 loc) · 10.9 KB
/
animate.html
File metadata and controls
323 lines (282 loc) · 10.9 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<!DOCTYPE html>
<html>
<head>
<title>VRM CMU Animation Viewer</title>
<meta name="description" content="View CMU mocap animation on random VRM model">
<link rel="icon" type="image/png" href="data:image/png;base64,iVBORw0KGgo=">
<script src="js/aframe-v1.7.1.js"></script>
<script src="js/aframe-environment-component.js"></script>
<!-- Official three-vrm libraries -->
<script src="js/three-vrm.js"></script>
<script src="js/three-vrm-animation.js"></script>
<!-- Custom A-Frame VRM component bundle -->
<script src="js/aframe-vrm-bundle.js"></script>
</head>
<body>
<style>
#info-panel {
display: none;
position: fixed;
top: 10px;
left: 10px;
width: 400px;
background: rgba(0,0,0,0.8);
color: #0f0;
font-family: monospace;
font-size: 12px;
padding: 15px;
z-index: 1000;
border: 1px solid #0f0;
}
#info-panel h3 {
margin: 0 0 10px 0;
color: #0ff;
}
.control-group {
margin-top: 10px;
padding-top: 10px;
border-top: 1px solid #0a5;
}
.slider-container {
margin: 5px 0;
}
.slider-container input[type="range"] {
width: 100%;
margin: 5px 0;
}
.slider-label {
display: flex;
justify-content: space-between;
font-size: 11px;
color: #0ff;
}
</style>
<div id="info-panel">
<h3>VRM CMU Animation Viewer</h3>
<div>Model: <span id="current-model">Loading...</span></div>
<div>Animation: <span id="current-anim">None</span></div>
<div class="control-group">
<div class="slider-container">
<div class="slider-label">
<span>Leg Spread</span>
<span id="leg-spread-value">0.00</span>
</div>
<input type="range" id="leg-spread-slider" min="-1" max="1" step="0.05" value="0">
</div>
</div>
<div style="margin-top: 10px;">
<a href="index.html" style="color: #fff;">< Back to List</a>
</div>
</div>
<script>
// Available VRM models (subset for quick loading)
const VRM_MODELS = [
'models/avatar.vrm',
'models/Asian/Asian_F_1_Busi.vrm',
'models/Hispanic/Hispanic_M_1_Casual.vrm',
'models/AIAN/AIAN_F_1_Util.vrm',
'models/NHPI/NHPI_M_1_Busi.vrm'
];
// ?model= query param overrides random pick (used by compare.html / Playwright)
const params = new URLSearchParams(location.search);
const randomModel = params.get('model') || VRM_MODELS[Math.floor(Math.random() * VRM_MODELS.length)];
console.log('Selected model:', randomModel);
document.getElementById('current-model').textContent = randomModel.split('/').pop();
window.animationReady = false;
// Get animation from hash
const hashAnim = window.location.hash.substring(1); // remove #
console.log('Detected animation hash:', hashAnim);
document.getElementById('current-anim').textContent = hashAnim || "No animation specified";
// Single Animation Player component
AFRAME.registerComponent('anim-player', {
init: function () {
this.vrm = null;
this.mixer = null;
this.currentAction = null;
// Wait for VRM to load
this.el.addEventListener('model-loaded', (e) => {
this.vrm = e.detail.vrm;
this.mixer = this.el.components.vrm?.mixer;
console.log('VRM loaded event fired. vrm:', !!this.vrm, 'mixer:', !!this.mixer);
if (this.vrm) {
console.log('vrm keys:', Object.keys(this.vrm));
if (this.vrm.springBoneManager) {
const sm = this.vrm.springBoneManager;
console.log('springBoneManager found. keys:', Object.keys(sm));
const springs = sm.springs || sm.springGroups || [];
console.log('Detected springs count:', springs.length);
}
if (this.vrm.vrmcVRMCSpringBone) {
console.log('vrmcVRMCSpringBone extension data found');
}
}
if (hashAnim) {
console.log('Calling loadAnimation with:', hashAnim);
this.loadAnimation(hashAnim);
}
});
},
loadAnimation: async function (animUrl) {
if (!this.vrm || !this.mixer) return;
console.log('Loading animation:', animUrl);
const loader = new THREE.GLTFLoader();
console.log('GLTFLoader instance created');
loader.register((parser) => {
const plugin = new THREE.VRMAnimationLoaderPlugin(parser);
const originalAfterRoot = plugin.afterRoot.bind(plugin);
plugin.afterRoot = async function(gltf) {
if (gltf.parser.json.extensions && gltf.parser.json.extensions.VRMC_vrm_animation) {
const vrmcAnim = gltf.parser.json.extensions.VRMC_vrm_animation;
if (!vrmcAnim.specVersion) {
vrmcAnim.specVersion = '1.0';
}
}
return originalAfterRoot(gltf);
};
return plugin;
});
try {
const gltf = await loader.loadAsync(animUrl);
if (gltf.userData.vrmAnimations && gltf.userData.vrmAnimations.length > 0) {
const vrmAnimation = gltf.userData.vrmAnimations[0];
const clip = THREE.createVRMAnimationClip(vrmAnimation, this.vrm);
if (clip) {
if (this.currentAction) this.currentAction.stop();
this.currentAction = this.mixer.clipAction(clip);
this.currentAction.loop = THREE.LoopRepeat;
this.currentAction.clampWhenFinished = false;
this.currentAction.enabled = true;
this.currentAction.timeScale = 1.0;
this.currentAction.play();
window.animationReady = true;
window._vrmComp = this;
console.log('Animation playing:', animUrl, 'duration:', clip.duration);
}
}
} catch (error) {
console.error('Error loading animation:', error);
document.getElementById('current-anim').textContent = "Error loading " + animUrl;
}
}
});
// Playwright hook: seek to time t (seconds) and return bone quaternions.
window.getBoneQuats = function(t, extraBones = []) {
const comp = window._vrmComp;
if (!comp?.vrm || !comp?.mixer) return null;
comp.mixer.setTime(t);
// Use a small delta to allow spring bones to calculate their state,
// though for true physics we'd need to simulate multiple frames.
comp.vrm.update(0.016);
const bones = ['hips','spine','chest','upperChest','neck','head',
'leftShoulder','leftUpperArm','leftLowerArm','leftHand',
'rightShoulder','rightUpperArm','rightLowerArm','rightHand',
'leftUpperLeg','leftLowerLeg','leftFoot',
'rightUpperLeg','rightLowerLeg','rightFoot'];
const result = {};
for (const bone of bones) {
const node = comp.vrm.humanoid.getNormalizedBoneNode(bone);
if (node?.quaternion) {
const q = node.quaternion;
result[bone] = { x: +q.x.toFixed(4), y: +q.y.toFixed(4), z: +q.z.toFixed(4), w: +q.w.toFixed(4) };
}
}
// Add extra bones
for (const boneName of extraBones) {
const node = comp.vrm.scene.getObjectByName(boneName);
if (node?.quaternion) {
const q = node.quaternion;
result[boneName] = { x: +q.x.toFixed(4), y: +q.y.toFixed(4), z: +q.z.toFixed(4), w: +q.w.toFixed(4) };
}
}
return result;
};
// Component to adjust leg spread
AFRAME.registerComponent('leg-fixer', {
schema: {
spread: {type: 'number', default: 0.0 }
},
init: function () {
this.vrm = null;
this.leftUpperLeg = null;
this.rightUpperLeg = null;
this.zAxis = new THREE.Vector3(0, 0, 1);
this.leftOffset = new THREE.Quaternion();
this.rightOffset = new THREE.Quaternion();
this.el.addEventListener('model-loaded', (e) => {
this.vrm = e.detail.vrm;
if (!this.vrm.humanoid) {
console.error("VRM humanoid bones not found.");
return;
}
// Using strings for bone names as they are consistent identifiers
this.leftUpperLeg = this.vrm.humanoid.getBoneNode('leftUpperLeg');
this.rightUpperLeg = this.vrm.humanoid.getBoneNode('rightUpperLeg');
if (!this.leftUpperLeg || !this.rightUpperLeg) {
console.error("Could not find upper leg bones!");
}
});
const slider = document.getElementById('leg-spread-slider');
if (slider) {
slider.addEventListener('input', (event) => {
this.el.setAttribute('leg-fixer', 'spread', event.target.value);
const spreadValueSpan = document.getElementById('leg-spread-value');
if(spreadValueSpan) spreadValueSpan.textContent = parseFloat(event.target.value).toFixed(2);
});
}
},
tick: function () {
if (!this.leftUpperLeg || !this.rightUpperLeg) { return; }
// The vrm component's tick updates the animation mixer.
// This component's tick runs after, so we can modify the bone quaternions
// right after animation has been applied and before rendering.
// The rotation is applied in the bone's local space.
const spreadAngle = this.data.spread * Math.PI / 4; // Max 45 degrees spread/cross
this.leftOffset.setFromAxisAngle(this.zAxis, spreadAngle);
this.rightOffset.setFromAxisAngle(this.zAxis, -spreadAngle);
this.leftUpperLeg.quaternion.premultiply(this.leftOffset);
this.rightUpperLeg.quaternion.premultiply(this.rightOffset);
}
});
</script>
<a-scene>
<a-sky color="#001a33"></a-sky>
<!-- Ground plane -->
<a-plane
position="0 0 0"
rotation="-90 0 0"
width="50"
height="50"
color="#003366"
shadow="receive: true">
</a-plane>
<!-- VRM Avatar with animation player and leg fix -->
<a-entity
id="avatar"
vrm="src: ;"
anim-player
leg-fixer
position="0 0 -4"
rotation="0 180 0"
shadow="cast: true; receive: false;">
</a-entity>
<!-- Lighting -->
<a-entity light="type: ambient; intensity: 0.5;"></a-entity>
<a-entity light="type: directional; intensity: 0.8; castShadow: true;" position="2 4 2"></a-entity>
<!-- Camera -->
<a-entity position="0 1.6 1">
<a-camera></a-camera>
</a-entity>
</a-scene>
<script>
// Set the random model after A-Frame is initialized
setTimeout(() => {
document.getElementById('avatar').setAttribute('vrm', 'src', randomModel);
}, 100);
// Cap pixel ratio to 1 (no HiDPI supersampling) to keep GPU load reasonable
document.querySelector('a-scene').addEventListener('renderstart', () => {
const renderer = document.querySelector('a-scene').renderer;
if (renderer) renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1));
}, { once: true });
</script>
</body>
</html>