forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanim-controller.js
More file actions
581 lines (519 loc) · 24.1 KB
/
anim-controller.js
File metadata and controls
581 lines (519 loc) · 24.1 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
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
import { AnimClip } from '../evaluator/anim-clip.js';
import { AnimState } from './anim-state.js';
import { AnimNode } from './anim-node.js';
import { AnimTransition } from './anim-transition.js';
import {
ANIM_GREATER_THAN, ANIM_LESS_THAN, ANIM_GREATER_THAN_EQUAL_TO, ANIM_LESS_THAN_EQUAL_TO, ANIM_EQUAL_TO, ANIM_NOT_EQUAL_TO,
ANIM_INTERRUPTION_NONE, ANIM_INTERRUPTION_PREV, ANIM_INTERRUPTION_NEXT, ANIM_INTERRUPTION_PREV_NEXT, ANIM_INTERRUPTION_NEXT_PREV,
ANIM_PARAMETER_TRIGGER,
ANIM_STATE_START, ANIM_STATE_END, ANIM_STATE_ANY, ANIM_CONTROL_STATES
} from './constants.js';
/**
* @private
* @class
* @name AnimController
* @classdesc The AnimController manages the animations for it's entity, based on the provided state graph and parameters. It's update method determines which state the controller should be in based on the current time, parameters and available states / transitions. It also ensures the AnimEvaluator is supplied with the correct animations, based on the currently active state.
* @description Create a new AnimController.
* @param {AnimEvaluator} animEvaluator - The animation evaluator used to blend all current playing animation keyframes and update the entities properties based on the current animation values.
* @param {object[]} states - The list of states used to form the controller state graph.
* @param {object[]} transitions - The list of transitions used to form the controller state graph.
* @param {object[]} parameters - The anim components parameters.
* @param {boolean} activate - Determines whether the anim controller should automatically play once all {@link AnimNodes} are assigned animations.
* @param {EventHandler} eventHandler - The event handler which should be notified with anim events
* @param {Set} consumedTriggers - Used to set triggers back to their default state after they have been consumed by a transition
*/
class AnimController {
constructor(animEvaluator, states, transitions, parameters, activate, eventHandler, consumedTriggers) {
this._animEvaluator = animEvaluator;
this._states = {};
this._stateNames = [];
this._eventHandler = eventHandler;
this._consumedTriggers = consumedTriggers;
for (let i = 0; i < states.length; i++) {
this._states[states[i].name] = new AnimState(
this,
states[i].name,
states[i].speed,
states[i].loop,
states[i].blendTree
);
this._stateNames.push(states[i].name);
}
this._transitions = transitions.map((transition) => {
return new AnimTransition({
...transition
});
});
this._findTransitionsFromStateCache = {};
this._findTransitionsBetweenStatesCache = {};
this._parameters = parameters;
this._previousStateName = null;
this._activeStateName = ANIM_STATE_START;
this._playing = false;
this._activate = activate;
this._currTransitionTime = 1.0;
this._totalTransitionTime = 1.0;
this._isTransitioning = false;
this._transitionInterruptionSource = ANIM_INTERRUPTION_NONE;
this._transitionPreviousStates = [];
this._timeInState = 0;
this._timeInStateBefore = 0;
}
get animEvaluator() {
return this._animEvaluator;
}
get activeState() {
return this._findState(this._activeStateName);
}
set activeState(stateName) {
this._activeStateName = stateName;
}
get activeStateName() {
return this._activeStateName;
}
get activeStateAnimations() {
return this.activeState.animations;
}
get previousState() {
return this._findState(this._previousStateName);
}
set previousState(stateName) {
this._previousStateName = stateName;
}
get previousStateName() {
return this._previousStateName;
}
get playable() {
let playable = true;
for (let i = 0; i < this._stateNames.length; i++) {
if (!this._states[this._stateNames[i]].playable) {
playable = false;
}
}
return playable;
}
get playing() {
return this._playing;
}
set playing(value) {
this._playing = value;
}
get activeStateProgress() {
return this._getActiveStateProgressForTime(this._timeInState);
}
get activeStateDuration() {
if (this.activeStateName === ANIM_STATE_START || this.activeStateName === ANIM_STATE_END)
return 0.0;
let maxDuration = 0.0;
for (let i = 0; i < this.activeStateAnimations.length; i++) {
const activeClip = this._animEvaluator.findClip(this.activeStateAnimations[i].name);
if (activeClip) {
maxDuration = Math.max(maxDuration, activeClip.track.duration);
}
}
return maxDuration;
}
get activeStateCurrentTime() {
return this._timeInState;
}
set activeStateCurrentTime(time) {
this._timeInStateBefore = time;
this._timeInState = time;
for (let i = 0; i < this.activeStateAnimations.length; i++) {
const clip = this.animEvaluator.findClip(this.activeStateAnimations[i].name);
if (clip) {
clip.time = time;
}
}
}
get transitioning() {
return this._isTransitioning;
}
get transitionProgress() {
return this._currTransitionTime / this._totalTransitionTime;
}
get states() {
return this._stateNames;
}
_findState(stateName) {
return this._states[stateName];
}
_getActiveStateProgressForTime(time) {
if (this.activeStateName === ANIM_STATE_START || this.activeStateName === ANIM_STATE_END || this.activeStateName === ANIM_STATE_ANY)
return 1.0;
const activeClip = this._animEvaluator.findClip(this.activeStateAnimations[0].name);
if (activeClip) {
return time / activeClip.track.duration;
}
return null;
}
// return all the transitions that have the given stateName as their source state
_findTransitionsFromState(stateName) {
let transitions = this._findTransitionsFromStateCache[stateName];
if (!transitions) {
transitions = this._transitions.filter(function (transition) {
return transition.from === stateName;
});
// sort transitions in priority order
transitions.sort(function (a, b) {
return a.priority < b.priority;
});
this._findTransitionsFromStateCache[stateName] = transitions;
}
return transitions;
}
// return all the transitions that contain the given source and destination states
_findTransitionsBetweenStates(sourceStateName, destinationStateName) {
let transitions = this._findTransitionsBetweenStatesCache[sourceStateName + '->' + destinationStateName];
if (!transitions) {
transitions = this._transitions.filter(function (transition) {
return transition.from === sourceStateName && transition.to === destinationStateName;
});
// sort transitions in priority order
transitions.sort(function (a, b) {
return a.priority < b.priority;
});
this._findTransitionsBetweenStatesCache[sourceStateName + '->' + destinationStateName] = transitions;
}
return transitions;
}
_transitionHasConditionsMet(transition) {
let conditionsMet = true;
for (let i = 0; i < transition.conditions.length; i++) {
const condition = transition.conditions[i];
const parameter = this.findParameter(condition.parameterName);
switch (condition.predicate) {
case ANIM_GREATER_THAN:
conditionsMet = conditionsMet && parameter.value > condition.value;
break;
case ANIM_LESS_THAN:
conditionsMet = conditionsMet && parameter.value < condition.value;
break;
case ANIM_GREATER_THAN_EQUAL_TO:
conditionsMet = conditionsMet && parameter.value >= condition.value;
break;
case ANIM_LESS_THAN_EQUAL_TO:
conditionsMet = conditionsMet && parameter.value <= condition.value;
break;
case ANIM_EQUAL_TO:
conditionsMet = conditionsMet && parameter.value === condition.value;
break;
case ANIM_NOT_EQUAL_TO:
conditionsMet = conditionsMet && parameter.value !== condition.value;
break;
}
if (!conditionsMet)
return conditionsMet;
}
return conditionsMet;
}
_findTransition(from, to) {
let transitions = [];
// If from and to are supplied, find transitions that include the required source and destination states
if (from && to) {
transitions.concat(this._findTransitionsBetweenStates(from, to));
} else {
// If no transition is active, look for transitions from the active & any states.
if (!this._isTransitioning) {
transitions = transitions.concat(this._findTransitionsFromState(this._activeStateName));
transitions = transitions.concat(this._findTransitionsFromState(ANIM_STATE_ANY));
} else {
// Otherwise look for transitions from the previous and active states based on the current interruption source.
// Accept transitions from the any state unless the interruption source is set to none
switch (this._transitionInterruptionSource) {
case ANIM_INTERRUPTION_PREV:
transitions = transitions.concat(this._findTransitionsFromState(this._previousStateName));
transitions = transitions.concat(this._findTransitionsFromState(ANIM_STATE_ANY));
break;
case ANIM_INTERRUPTION_NEXT:
transitions = transitions.concat(this._findTransitionsFromState(this._activeStateName));
transitions = transitions.concat(this._findTransitionsFromState(ANIM_STATE_ANY));
break;
case ANIM_INTERRUPTION_PREV_NEXT:
transitions = transitions.concat(this._findTransitionsFromState(this._previousStateName));
transitions = transitions.concat(this._findTransitionsFromState(this._activeStateName));
transitions = transitions.concat(this._findTransitionsFromState(ANIM_STATE_ANY));
break;
case ANIM_INTERRUPTION_NEXT_PREV:
transitions = transitions.concat(this._findTransitionsFromState(this._activeStateName));
transitions = transitions.concat(this._findTransitionsFromState(this._previousStateName));
transitions = transitions.concat(this._findTransitionsFromState(ANIM_STATE_ANY));
break;
case ANIM_INTERRUPTION_NONE:
default:
}
}
}
// filter out transitions that don't have their conditions met
transitions = transitions.filter(function (transition) {
// if the transition is moving to the already active state, ignore it
if (transition.to === this.activeStateName) {
return false;
}
// when an exit time is present, we should only exit if it falls within the current frame delta time
if (transition.hasExitTime) {
let progressBefore = this._getActiveStateProgressForTime(this._timeInStateBefore);
let progress = this._getActiveStateProgressForTime(this._timeInState);
// when the exit time is smaller than 1 and the state is looping, we should check for an exit each loop
if (transition.exitTime < 1.0 && this.activeState.loop) {
progressBefore -= Math.floor(progressBefore);
progress -= Math.floor(progress);
}
// return false if exit time isn't within the frames delta time
if (!(transition.exitTime > progressBefore && transition.exitTime <= progress)) {
return null;
}
}
// if the exitTime condition has been met or is not present, check condition parameters
return this._transitionHasConditionsMet(transition);
}.bind(this));
// return the highest priority transition to use
if (transitions.length > 0) {
const transition = transitions[0];
if (transition.to === ANIM_STATE_END) {
const startTransition = this._findTransitionsFromState(ANIM_STATE_START)[0];
transition.to = startTransition.to;
}
return transition;
}
return null;
}
updateStateFromTransition(transition) {
let state;
let animation;
let clip;
// If transition.from is set, transition from the active state irregardless of the transition.from value (this could be the previous, active or ANY states).
// Otherwise the previousState is cleared.
this.previousState = transition.from ? this.activeStateName : null;
this.activeState = transition.to;
// turn off any triggers which were required to activate this transition
for (let i = 0; i < transition.conditions.length; i++) {
const condition = transition.conditions[i];
const parameter = this.findParameter(condition.parameterName);
if (parameter.type === ANIM_PARAMETER_TRIGGER) {
this._consumedTriggers.add(condition.parameterName);
}
}
if (this.previousState) {
if (!this._isTransitioning) {
this._transitionPreviousStates = [];
}
// record the transition source state in the previous states array
this._transitionPreviousStates.push({
name: this._previousStateName,
weight: 1
});
// if this new transition was activated during another transition, update the previous transition state weights based
// on the progress through the previous transition.
const interpolatedTime = Math.min(this._currTransitionTime / this._totalTransitionTime, 1.0);
for (let i = 0; i < this._transitionPreviousStates.length; i++) {
// interpolate the weights of the most recent previous state and all other previous states based on the progress through the previous transition
if (!this._isTransitioning) {
this._transitionPreviousStates[i].weight = 1.0;
} else if (i !== this._transitionPreviousStates.length - 1) {
this._transitionPreviousStates[i].weight *= (1.0 - interpolatedTime);
} else {
this._transitionPreviousStates[i].weight = interpolatedTime;
}
state = this._findState(this._transitionPreviousStates[i].name);
// update the animations of previous states, set their name to include their position in the previous state array
// to uniquely identify animations from the same state that were added during different transitions
for (let j = 0; j < state.animations.length; j++) {
animation = state.animations[j];
clip = this._animEvaluator.findClip(animation.name + '.previous.' + i);
if (!clip) {
clip = this._animEvaluator.findClip(animation.name);
clip.name = animation.name + '.previous.' + i;
}
// // pause previous animation clips to reduce their impact on performance
if (i !== this._transitionPreviousStates.length - 1) {
clip.pause();
}
}
}
}
this._isTransitioning = true;
this._totalTransitionTime = transition.time;
this._currTransitionTime = 0;
this._transitionInterruptionSource = transition.interruptionSource;
const activeState = this.activeState;
const hasTransitionOffset = transition.transitionOffset && transition.transitionOffset > 0.0 && transition.transitionOffset < 1.0;
// set the time in the new state to 0 or to a value based on transitionOffset if one was given
let timeInState = 0;
let timeInStateBefore = 0;
if (hasTransitionOffset) {
const offsetTime = activeState.timelineDuration * transition.transitionOffset;
timeInState = offsetTime;
timeInStateBefore = offsetTime;
}
this._timeInState = timeInState;
this._timeInStateBefore = timeInStateBefore;
// Add clips to the evaluator for each animation in the new state.
for (let i = 0; i < activeState.animations.length; i++) {
clip = this._animEvaluator.findClip(activeState.animations[i].name);
if (!clip) {
const speed = Number.isFinite(activeState.animations[i].speed) ? activeState.animations[i].speed : activeState.speed;
clip = new AnimClip(activeState.animations[i].animTrack, this._timeInState, speed, true, activeState.loop, this._eventHandler);
clip.name = activeState.animations[i].name;
this._animEvaluator.addClip(clip);
} else {
clip.reset();
}
if (transition.time > 0) {
clip.blendWeight = 0.0;
} else {
clip.blendWeight = activeState.animations[i].normalizedWeight;
}
clip.play();
if (hasTransitionOffset) {
clip.time = activeState.timelineDuration * transition.transitionOffset;
} else {
const startTime = activeState.speed >= 0 ? 0 : this.activeStateDuration;
clip.time = startTime;
}
}
}
_transitionToState(newStateName) {
if (!this._findState(newStateName)) {
return;
}
// move to the given state, if a transition is present in the state graph use it. Otherwise move instantly to it.
let transition = this._findTransition(this._activeStateName, newStateName);
if (!transition) {
this._animEvaluator.removeClips();
transition = new AnimTransition({ from: null, to: newStateName });
}
this.updateStateFromTransition(transition);
}
assignAnimation(pathString, animTrack, speed, loop) {
const path = pathString.split('.');
let state = this._findState(path[0]);
if (!state) {
state = new AnimState(this, path[0], 1.0);
this._states[path[0]] = state;
this._stateNames.push(path[0]);
}
state.addAnimation(path, animTrack);
if (speed !== undefined) {
state.speed = speed;
}
if (loop !== undefined) {
state.loop = loop;
}
if (!this._playing && this._activate && this.playable) {
this.play();
}
}
removeNodeAnimations(nodeName) {
if (ANIM_CONTROL_STATES.indexOf(nodeName) !== -1) {
return;
}
const state = this._findState(nodeName);
if (!state) {
// #if _DEBUG
console.error('Attempting to unassign animation tracks from a state that does not exist.');
// #endif
return;
}
state.animations = [];
return true;
}
play(stateName) {
if (stateName) {
this._transitionToState(stateName);
}
this._playing = true;
}
pause() {
this._playing = false;
}
reset() {
this._previousStateName = null;
this._activeStateName = ANIM_STATE_START;
this._playing = false;
this._currTransitionTime = 1.0;
this._totalTransitionTime = 1.0;
this._isTransitioning = false;
this._timeInState = 0;
this._timeInStateBefore = 0;
this._animEvaluator.removeClips();
}
rebind() {
this._animEvaluator.rebind();
}
update(dt) {
if (!this._playing) {
return;
}
let state;
let animation;
let clip;
this._timeInStateBefore = this._timeInState;
this._timeInState += dt;
// transition between states if a transition is available from the active state
const transition = this._findTransition(this._activeStateName);
if (transition)
this.updateStateFromTransition(transition);
if (this._isTransitioning) {
this._currTransitionTime += dt;
if (this._currTransitionTime <= this._totalTransitionTime) {
const interpolatedTime = this._currTransitionTime / this._totalTransitionTime;
// while transitioning, set all previous state animations to be weighted by (1.0 - interpolationTime).
for (let i = 0; i < this._transitionPreviousStates.length; i++) {
state = this._findState(this._transitionPreviousStates[i].name);
const stateWeight = this._transitionPreviousStates[i].weight;
for (let j = 0; j < state.animations.length; j++) {
animation = state.animations[j];
clip = this._animEvaluator.findClip(animation.name + '.previous.' + i);
if (clip) {
clip.blendWeight = (1.0 - interpolatedTime) * animation.normalizedWeight * stateWeight;
}
}
}
// while transitioning, set active state animations to be weighted by (interpolationTime).
state = this.activeState;
for (let i = 0; i < state.animations.length; i++) {
animation = state.animations[i];
this._animEvaluator.findClip(animation.name).blendWeight = interpolatedTime * animation.normalizedWeight;
}
} else {
this._isTransitioning = false;
// when a transition ends, remove all previous state clips from the evaluator
const activeClips = this.activeStateAnimations.length;
const totalClips = this._animEvaluator.clips.length;
for (let i = 0; i < totalClips - activeClips; i++) {
this._animEvaluator.removeClip(0);
}
this._transitionPreviousStates = [];
// when a transition ends, set the active state clip weights so they sum to 1
state = this.activeState;
for (let i = 0; i < state.animations.length; i++) {
animation = state.animations[i];
clip = this._animEvaluator.findClip(animation.name);
if (clip) {
clip.blendWeight = animation.normalizedWeight;
}
}
}
} else {
if (this.activeState._blendTree.constructor !== AnimNode) {
state = this.activeState;
for (let i = 0; i < state.animations.length; i++) {
animation = state.animations[i];
clip = this._animEvaluator.findClip(animation.name);
if (clip) {
clip.blendWeight = animation.normalizedWeight;
if (animation.parent.syncAnimations) {
clip.speed = animation.speed;
}
}
}
}
}
this._animEvaluator.update(dt);
}
findParameter(name) {
return this._parameters[name];
}
}
export { AnimController };