This repository has been archived by the owner on Nov 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
webgl_animation_skinning_additive_blending.html
430 lines (273 loc) · 10.8 KB
/
webgl_animation_skinning_additive_blending.html
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<!DOCTYPE html>
<html lang="en">
<head>
<title>Verge3D webgl - additive animation - skinning</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
<style>
a {
color: blue;
}
.control-inactive button {
color: #888;
}
</style>
</head>
<body>
<div id="container"></div>
<div id="info">
<a href="https://www.soft8soft.com/verge3d" target="_blank" rel="noopener">Verge3D</a> - Skeletal Additive Animation Blending
(model from <a href="https://www.mixamo.com/" target="_blank" rel="noopener">mixamo.com</a>)<br/>
</div>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"v3d": "../build/v3d.module.js",
"v3d/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as v3d from 'v3d';
import Stats from 'v3d/addons/libs/stats.module.js';
import { GUI } from 'v3d/addons/libs/lil-gui.module.min.js';
import { OrbitControls } from 'v3d/addons/controls/OrbitControls.js';
import { GLTFLoader } from 'v3d/addons/loaders/GLTFLoader.js';
let scene, renderer, camera, stats;
let model, skeleton, mixer, clock;
const crossFadeControls = [];
let currentBaseAction = 'idle';
const allActions = [];
const baseActions = {
idle: { weight: 1 },
walk: { weight: 0 },
run: { weight: 0 }
};
const additiveActions = {
sneak_pose: { weight: 0 },
sad_pose: { weight: 0 },
agree: { weight: 0 },
headShake: { weight: 0 }
};
let panelSettings, numAnimations;
init();
function init() {
const container = document.getElementById('container');
clock = new v3d.Clock();
scene = new v3d.Scene();
scene.background = new v3d.Color(0xa0a0a0);
scene.fog = new v3d.Fog(0xa0a0a0, 10, 50);
const hemiLight = new v3d.HemisphereLight(0xffffff, 0x444444);
hemiLight.position.set(0, 20, 0);
scene.add(hemiLight);
const dirLight = new v3d.DirectionalLight(0xffffff);
dirLight.position.set(3, 10, 10);
dirLight.castShadow = true;
dirLight.shadow.camera.top = 2;
dirLight.shadow.camera.bottom = -2;
dirLight.shadow.camera.left = -2;
dirLight.shadow.camera.right = 2;
dirLight.shadow.camera.near = 0.1;
dirLight.shadow.camera.far = 40;
scene.add(dirLight);
// ground
const mesh = new v3d.Mesh(new v3d.PlaneGeometry(100, 100), new v3d.MeshPhongMaterial({ color: 0x999999, depthWrite: false }));
mesh.rotation.x = - Math.PI / 2;
mesh.receiveShadow = true;
scene.add(mesh);
const loader = new GLTFLoader();
loader.load('models/gltf/Xbot.glb', function(gltf) {
model = gltf.scene;
scene.add(model);
model.traverse(function(object) {
if (object.isMesh) object.castShadow = true;
});
skeleton = new v3d.SkeletonHelper(model);
skeleton.visible = false;
scene.add(skeleton);
const animations = gltf.animations;
mixer = new v3d.AnimationMixer(model);
numAnimations = animations.length;
for (let i = 0; i !== numAnimations; ++ i) {
let clip = animations[i];
const name = clip.name;
if (baseActions[name]) {
const action = mixer.clipAction(clip);
activateAction(action);
baseActions[name].action = action;
allActions.push(action);
} else if (additiveActions[name]) {
// Make the clip additive and remove the reference frame
v3d.AnimationUtils.makeClipAdditive(clip);
if (clip.name.endsWith('_pose')) {
clip = v3d.AnimationUtils.subclip(clip, clip.name, 2, 3, 30);
}
const action = mixer.clipAction(clip);
activateAction(action);
additiveActions[name].action = action;
allActions.push(action);
}
}
createPanel();
animate();
});
renderer = new v3d.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputEncoding = v3d.sRGBEncoding;
renderer.shadowMap.enabled = true;
container.appendChild(renderer.domElement);
// camera
camera = new v3d.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 100);
camera.position.set(- 1, 2, 3);
const controls = new OrbitControls(camera, renderer.domElement);
controls.enablePan = false;
controls.enableZoom = false;
controls.target.set(0, 1, 0);
controls.update();
stats = new Stats();
container.appendChild(stats.dom);
window.addEventListener('resize', onWindowResize);
}
function createPanel() {
const panel = new GUI({ width: 310 });
const folder1 = panel.addFolder('Base Actions');
const folder2 = panel.addFolder('Additive Action Weights');
const folder3 = panel.addFolder('General Speed');
panelSettings = {
'modify time scale': 1.0
};
const baseNames = ['None', ...Object.keys(baseActions)];
for (let i = 0, l = baseNames.length; i !== l; ++ i) {
const name = baseNames[i];
const settings = baseActions[name];
panelSettings[name] = function() {
const currentSettings = baseActions[currentBaseAction];
const currentAction = currentSettings ? currentSettings.action : null;
const action = settings ? settings.action : null;
if (currentAction !== action) {
prepareCrossFade(currentAction, action, 0.35);
}
};
crossFadeControls.push(folder1.add(panelSettings, name));
}
for (const name of Object.keys(additiveActions)) {
const settings = additiveActions[name];
panelSettings[name] = settings.weight;
folder2.add(panelSettings, name, 0.0, 1.0, 0.01).listen().onChange(function(weight) {
setWeight(settings.action, weight);
settings.weight = weight;
});
}
folder3.add(panelSettings, 'modify time scale', 0.0, 1.5, 0.01).onChange(modifyTimeScale);
folder1.open();
folder2.open();
folder3.open();
crossFadeControls.forEach(function(control) {
control.setInactive = function() {
control.domElement.classList.add('control-inactive');
};
control.setActive = function() {
control.domElement.classList.remove('control-inactive');
};
const settings = baseActions[control.property];
if (!settings || ! settings.weight) {
control.setInactive();
}
});
}
function activateAction(action) {
const clip = action.getClip();
const settings = baseActions[clip.name] || additiveActions[clip.name];
setWeight(action, settings.weight);
action.play();
}
function modifyTimeScale(speed) {
mixer.timeScale = speed;
}
function prepareCrossFade(startAction, endAction, duration) {
// If the current action is 'idle', execute the crossfade immediately;
// else wait until the current action has finished its current loop
if (currentBaseAction === 'idle' || ! startAction || ! endAction) {
executeCrossFade(startAction, endAction, duration);
} else {
synchronizeCrossFade(startAction, endAction, duration);
}
// Update control colors
if (endAction) {
const clip = endAction.getClip();
currentBaseAction = clip.name;
} else {
currentBaseAction = 'None';
}
crossFadeControls.forEach(function(control) {
const name = control.property;
if (name === currentBaseAction) {
control.setActive();
} else {
control.setInactive();
}
});
}
function synchronizeCrossFade(startAction, endAction, duration) {
mixer.addEventListener('loop', onLoopFinished);
function onLoopFinished(event) {
if (event.action === startAction) {
mixer.removeEventListener('loop', onLoopFinished);
executeCrossFade(startAction, endAction, duration);
}
}
}
function executeCrossFade(startAction, endAction, duration) {
// Not only the start action, but also the end action must get a weight of 1 before fading
// (concerning the start action this is already guaranteed in this place)
if (endAction) {
setWeight(endAction, 1);
endAction.time = 0;
if (startAction) {
// Crossfade with warping
startAction.crossFadeTo(endAction, duration, true);
} else {
// Fade in
endAction.fadeIn(duration);
}
} else {
// Fade out
startAction.fadeOut(duration);
}
}
// This function is needed, since animationAction.crossFadeTo() disables its start action and sets
// the start action's timeScale to ((start animation's duration) / (end animation's duration))
function setWeight(action, weight) {
action.enabled = true;
action.setEffectiveTimeScale(1);
action.setEffectiveWeight(weight);
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
// Render loop
requestAnimationFrame(animate);
for (let i = 0; i !== numAnimations; ++ i) {
const action = allActions[i];
const clip = action.getClip();
const settings = baseActions[clip.name] || additiveActions[clip.name];
settings.weight = action.getEffectiveWeight();
}
// Get the time elapsed since the last frame, used for mixer update
const mixerUpdateDelta = clock.getDelta();
// Update the animation mixer, the stats panel, and render this frame
mixer.update(mixerUpdateDelta);
stats.update();
renderer.render(scene, camera);
}
</script>
</body>
</html>