-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsequence.html
More file actions
268 lines (228 loc) · 9.46 KB
/
sequence.html
File metadata and controls
268 lines (228 loc) · 9.46 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
<!DOCTYPE html>
<html>
<head>
<title>VRMA Animation Sequence Player</title>
<meta name="description" content="Plays a sequence of VRMA animations with buffering and blending">
<script src="js/aframe-v1.7.1.js"></script>
<script src="js/aframe-environment-component.js"></script>
<script src="js/three-vrm.js"></script>
<script src="js/three-vrm-animation.js"></script>
<script src="js/aframe-vrm-bundle.js"></script>
</head>
<body>
<style>
#info {
position: fixed;
top: 10px;
left: 10px;
color: lime;
font-family: monospace;
background: rgba(0,0,0,0.7);
padding: 10px;
pointer-events: none;
}
</style>
<div id="info">
<div>Sequence Player</div>
<div id="status">Initializing...</div>
<div id="current-file"></div>
<div id="current-description"></div>
</div>
<script>
AFRAME.registerComponent('vrm-anim-sequence', {
schema: {
src: { type: 'string', default: 'animations.json' },
vrm: { type: 'string', default: 'models/avatar.vrm' },
blendWindow: { type: 'number', default: 1.0 } // Seconds before end to start blending
},
init: function () {
this.playlist = [];
this.currentIndex = 0;
this.clipCache = new Map(); // url -> clip
this.vrm = null;
this.mixer = null;
this.currentAction = null;
this.nextAction = null; // For crossfading
this.isLoading = false;
// Bindings
this.onModelLoaded = this.onModelLoaded.bind(this);
// Load playlist
this.loadPlaylist();
// Listen for VRM
this.el.addEventListener('model-loaded', this.onModelLoaded);
// Set VRM src
this.el.setAttribute('vrm', 'src', this.data.vrm);
},
loadPlaylist: async function() {
try {
const response = await fetch(this.data.src);
this.playlist = await response.json();
// Sort naturally just in case
this.playlist.sort((a, b) => new Intl.Collator('en', {numeric: true, sensitivity: 'base'}).compare(a.url, b.url));
console.log(`Loaded playlist with ${this.playlist.length} animations.`);
this.updateStatus(`Playlist loaded: ${this.playlist.length} items`);
// If VRM is already ready, start buffering/playing
if (this.vrm && this.playlist.length > 0) {
this.startSequence();
}
} catch (e) {
console.error("Failed to load playlist:", e);
this.updateStatus("Error loading playlist");
}
},
onModelLoaded: function (evt) {
const vrm = evt.detail.vrm;
if (!vrm) return;
this.vrm = vrm;
console.log("VRM loaded");
// Setup Mixer
if (this.el.components.vrm && this.el.components.vrm.mixer) {
this.mixer = this.el.components.vrm.mixer;
} else {
this.mixer = new THREE.AnimationMixer(this.vrm.scene);
}
if (this.playlist.length > 0) {
this.startSequence();
}
},
getClip: async function(url) {
if (this.clipCache.has(url)) {
return this.clipCache.get(url);
}
// Load
return new Promise((resolve, reject) => {
const loader = new THREE.GLTFLoader();
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;
});
loader.load(url, (gltf) => {
const vrmAnimations = gltf.userData.vrmAnimations;
if (vrmAnimations && vrmAnimations.length > 0) {
const clip = THREE.createVRMAnimationClip(vrmAnimations[0], this.vrm);
this.clipCache.set(url, clip);
resolve(clip);
} else {
resolve(null); // No animation found
}
}, undefined, (err) => {
console.error("Error loading " + url, err);
resolve(null); // Resolve null on error to keep sequence moving
});
});
},
startSequence: async function() {
if (this.isLoading) return;
this.isLoading = true;
// Load first animation
const animation = this.playlist[this.currentIndex];
this.updateStatus("Loading first animation...");
const clip = await this.getClip(animation.url);
if (clip) {
this.playClip(clip, animation);
// Buffer next
this.bufferNext();
} else {
// Skip if failed
this.currentIndex++;
this.isLoading = false;
this.startSequence();
}
},
bufferNext: function() {
const nextIdx = (this.currentIndex + 1) % this.playlist.length;
const nextUrl = this.playlist[nextIdx].url;
if (!this.clipCache.has(nextUrl)) {
console.log("Buffering:", nextUrl);
this.getClip(nextUrl).then(() => console.log("Buffered:", nextUrl));
}
// Clean up old cache (optional, keep last 5?)
// For now simple infinite cache or manual cleanup if memory issues.
// Let's keep it simple.
},
playClip: function(clip, animation) {
this.isLoading = false; // Done loading current
// Setup action
const action = this.mixer.clipAction(clip);
action.loop = THREE.LoopOnce;
action.clampWhenFinished = true;
action.reset();
action.play();
if (this.currentAction) {
// Crossfade
// Synchronize? No, just fade.
action.crossFadeFrom(this.currentAction, this.data.blendWindow, true);
}
this.currentAction = action;
this.currentUrl = animation.url;
this.updateStatus("Playing");
document.getElementById('current-file').textContent = animation.url;
document.getElementById('current-description').textContent = animation.description;
// We need to track when to transition
this.transitionScheduled = false;
},
updateStatus: function(msg) {
document.getElementById('status').textContent = msg;
},
tick: function(t, dt) {
// Check animation progress
if (this.currentAction && !this.transitionScheduled && !this.isLoading) {
const clip = this.currentAction.getClip();
const duration = clip.duration;
const time = this.currentAction.time;
// Time to transition?
if (duration - time <= this.data.blendWindow) {
this.transitionScheduled = true;
this.playNext();
}
}
},
playNext: async function() {
this.currentIndex = (this.currentIndex + 1) % this.playlist.length;
const animation = this.playlist[this.currentIndex];
// Try to get from cache immediately
let clip = this.clipCache.get(animation.url);
if (!clip) {
// Not buffered yet? Load now.
console.warn("Animation not buffered, loading now:", animation.url);
this.updateStatus("Loading next (buffering lag)...");
clip = await this.getClip(animation.url);
}
if (clip) {
this.playClip(clip, animation);
this.bufferNext();
} else {
// Failed to load, skip
console.error("Skipping " + animation.url);
this.playNext(); // Recurse
}
}
});
</script>
<a-scene>
<a-sky color="#111"></a-sky>
<a-plane position="0 0 0" rotation="-90 0 0" width="50" height="50" color="#222" shadow="receive: true"></a-plane>
<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>
<a-entity position="0 1.6 3">
<a-camera></a-camera>
</a-entity>
<!-- The sequence player -->
<a-entity
id="avatar"
vrm-anim-sequence="vrm: models/Asian/Asian_F_1_Casual.vrm; src: animations.json; blendWindow: 1.0"
position="0 0 -4"
rotation="0 180 0"
shadow="cast: true">
</a-entity>
</a-scene>
</body>
</html>