Spaces:
Sleeping
Sleeping
File size: 12,271 Bytes
2b7aae2 | 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 | import {
WrapAroundEnding,
ZeroCurvatureEnding,
ZeroSlopeEnding,
LoopPingPong,
LoopOnce,
LoopRepeat,
NormalAnimationBlendMode,
AdditiveAnimationBlendMode,
} from '../constants.js';
class AnimationAction {
constructor(mixer, clip, localRoot = null, blendMode = clip.blendMode) {
this._mixer = mixer;
this._clip = clip;
this._localRoot = localRoot;
this.blendMode = blendMode;
const tracks = clip.tracks,
nTracks = tracks.length,
interpolants = new Array(nTracks);
const interpolantSettings = {
endingStart: ZeroCurvatureEnding,
endingEnd: ZeroCurvatureEnding,
};
for (let i = 0; i !== nTracks; ++i) {
const interpolant = tracks[i].createInterpolant(null);
interpolants[i] = interpolant;
interpolant.settings = interpolantSettings;
}
this._interpolantSettings = interpolantSettings;
this._interpolants = interpolants; // bound by the mixer
// inside: PropertyMixer (managed by the mixer)
this._propertyBindings = new Array(nTracks);
this._cacheIndex = null; // for the memory manager
this._byClipCacheIndex = null; // for the memory manager
this._timeScaleInterpolant = null;
this._weightInterpolant = null;
this.loop = LoopRepeat;
this._loopCount = -1;
// global mixer time when the action is to be started
// it's set back to 'null' upon start of the action
this._startTime = null;
// scaled local time of the action
// gets clamped or wrapped to 0..clip.duration according to loop
this.time = 0;
this.timeScale = 1;
this._effectiveTimeScale = 1;
this.weight = 1;
this._effectiveWeight = 1;
this.repetitions = Infinity; // no. of repetitions when looping
this.paused = false; // true -> zero effective time scale
this.enabled = true; // false -> zero effective weight
this.clampWhenFinished = false; // keep feeding the last frame?
this.zeroSlopeAtStart = true; // for smooth interpolation w/o separate
this.zeroSlopeAtEnd = true; // clips for start, loop and end
}
// State & Scheduling
play() {
this._mixer._activateAction(this);
return this;
}
stop() {
this._mixer._deactivateAction(this);
return this.reset();
}
reset() {
this.paused = false;
this.enabled = true;
this.time = 0; // restart clip
this._loopCount = -1; // forget previous loops
this._startTime = null; // forget scheduling
return this.stopFading().stopWarping();
}
isRunning() {
return this.enabled && !this.paused && this.timeScale !== 0 && this._startTime === null && this._mixer._isActiveAction(this);
}
// return true when play has been called
isScheduled() {
return this._mixer._isActiveAction(this);
}
startAt(time) {
this._startTime = time;
return this;
}
setLoop(mode, repetitions) {
this.loop = mode;
this.repetitions = repetitions;
return this;
}
// Weight
// set the weight stopping any scheduled fading
// although .enabled = false yields an effective weight of zero, this
// method does *not* change .enabled, because it would be confusing
setEffectiveWeight(weight) {
this.weight = weight;
// note: same logic as when updated at runtime
this._effectiveWeight = this.enabled ? weight : 0;
return this.stopFading();
}
// return the weight considering fading and .enabled
getEffectiveWeight() {
return this._effectiveWeight;
}
fadeIn(duration) {
return this._scheduleFading(duration, 0, 1);
}
fadeOut(duration) {
return this._scheduleFading(duration, 1, 0);
}
crossFadeFrom(fadeOutAction, duration, warp) {
fadeOutAction.fadeOut(duration);
this.fadeIn(duration);
if (warp) {
const fadeInDuration = this._clip.duration,
fadeOutDuration = fadeOutAction._clip.duration,
startEndRatio = fadeOutDuration / fadeInDuration,
endStartRatio = fadeInDuration / fadeOutDuration;
fadeOutAction.warp(1.0, startEndRatio, duration);
this.warp(endStartRatio, 1.0, duration);
}
return this;
}
crossFadeTo(fadeInAction, duration, warp) {
return fadeInAction.crossFadeFrom(this, duration, warp);
}
stopFading() {
const weightInterpolant = this._weightInterpolant;
if (weightInterpolant !== null) {
this._weightInterpolant = null;
this._mixer._takeBackControlInterpolant(weightInterpolant);
}
return this;
}
// Time Scale Control
// set the time scale stopping any scheduled warping
// although .paused = true yields an effective time scale of zero, this
// method does *not* change .paused, because it would be confusing
setEffectiveTimeScale(timeScale) {
this.timeScale = timeScale;
this._effectiveTimeScale = this.paused ? 0 : timeScale;
return this.stopWarping();
}
// return the time scale considering warping and .paused
getEffectiveTimeScale() {
return this._effectiveTimeScale;
}
setDuration(duration) {
this.timeScale = this._clip.duration / duration;
return this.stopWarping();
}
syncWith(action) {
this.time = action.time;
this.timeScale = action.timeScale;
return this.stopWarping();
}
halt(duration) {
return this.warp(this._effectiveTimeScale, 0, duration);
}
warp(startTimeScale, endTimeScale, duration) {
const mixer = this._mixer,
now = mixer.time,
timeScale = this.timeScale;
let interpolant = this._timeScaleInterpolant;
if (interpolant === null) {
interpolant = mixer._lendControlInterpolant();
this._timeScaleInterpolant = interpolant;
}
const times = interpolant.parameterPositions,
values = interpolant.sampleValues;
times[0] = now;
times[1] = now + duration;
values[0] = startTimeScale / timeScale;
values[1] = endTimeScale / timeScale;
return this;
}
stopWarping() {
const timeScaleInterpolant = this._timeScaleInterpolant;
if (timeScaleInterpolant !== null) {
this._timeScaleInterpolant = null;
this._mixer._takeBackControlInterpolant(timeScaleInterpolant);
}
return this;
}
// Object Accessors
getMixer() {
return this._mixer;
}
getClip() {
return this._clip;
}
getRoot() {
return this._localRoot || this._mixer._root;
}
// Interna
_update(time, deltaTime, timeDirection, accuIndex) {
// called by the mixer
if (!this.enabled) {
// call ._updateWeight() to update ._effectiveWeight
this._updateWeight(time);
return;
}
const startTime = this._startTime;
if (startTime !== null) {
// check for scheduled start of action
const timeRunning = (time - startTime) * timeDirection;
if (timeRunning < 0 || timeDirection === 0) {
return; // yet to come / don't decide when delta = 0
}
// start
this._startTime = null; // unschedule
deltaTime = timeDirection * timeRunning;
}
// apply time scale and advance time
deltaTime *= this._updateTimeScale(time);
const clipTime = this._updateTime(deltaTime);
// note: _updateTime may disable the action resulting in
// an effective weight of 0
const weight = this._updateWeight(time);
if (weight > 0) {
const interpolants = this._interpolants;
const propertyMixers = this._propertyBindings;
switch (this.blendMode) {
case AdditiveAnimationBlendMode:
for (let j = 0, m = interpolants.length; j !== m; ++j) {
interpolants[j].evaluate(clipTime);
propertyMixers[j].accumulateAdditive(weight);
}
break;
case NormalAnimationBlendMode:
default:
for (let j = 0, m = interpolants.length; j !== m; ++j) {
interpolants[j].evaluate(clipTime);
propertyMixers[j].accumulate(accuIndex, weight);
}
}
}
}
_updateWeight(time) {
let weight = 0;
if (this.enabled) {
weight = this.weight;
const interpolant = this._weightInterpolant;
if (interpolant !== null) {
const interpolantValue = interpolant.evaluate(time)[0];
weight *= interpolantValue;
if (time > interpolant.parameterPositions[1]) {
this.stopFading();
if (interpolantValue === 0) {
// faded out, disable
this.enabled = false;
}
}
}
}
this._effectiveWeight = weight;
return weight;
}
_updateTimeScale(time) {
let timeScale = 0;
if (!this.paused) {
timeScale = this.timeScale;
const interpolant = this._timeScaleInterpolant;
if (interpolant !== null) {
const interpolantValue = interpolant.evaluate(time)[0];
timeScale *= interpolantValue;
if (time > interpolant.parameterPositions[1]) {
this.stopWarping();
if (timeScale === 0) {
// motion has halted, pause
this.paused = true;
} else {
// warp done - apply final time scale
this.timeScale = timeScale;
}
}
}
}
this._effectiveTimeScale = timeScale;
return timeScale;
}
_updateTime(deltaTime) {
const duration = this._clip.duration;
const loop = this.loop;
let time = this.time + deltaTime;
let loopCount = this._loopCount;
const pingPong = loop === LoopPingPong;
if (deltaTime === 0) {
if (loopCount === -1) return time;
return pingPong && (loopCount & 1) === 1 ? duration - time : time;
}
if (loop === LoopOnce) {
if (loopCount === -1) {
// just started
this._loopCount = 0;
this._setEndings(true, true, false);
}
handle_stop: {
if (time >= duration) {
time = duration;
} else if (time < 0) {
time = 0;
} else {
this.time = time;
break handle_stop;
}
if (this.clampWhenFinished) this.paused = true;
else this.enabled = false;
this.time = time;
this._mixer.dispatchEvent({
type: 'finished',
action: this,
direction: deltaTime < 0 ? -1 : 1,
});
}
} else {
// repetitive Repeat or PingPong
if (loopCount === -1) {
// just started
if (deltaTime >= 0) {
loopCount = 0;
this._setEndings(true, this.repetitions === 0, pingPong);
} else {
// when looping in reverse direction, the initial
// transition through zero counts as a repetition,
// so leave loopCount at -1
this._setEndings(this.repetitions === 0, true, pingPong);
}
}
if (time >= duration || time < 0) {
// wrap around
const loopDelta = Math.floor(time / duration); // signed
time -= duration * loopDelta;
loopCount += Math.abs(loopDelta);
const pending = this.repetitions - loopCount;
if (pending <= 0) {
// have to stop (switch state, clamp time, fire event)
if (this.clampWhenFinished) this.paused = true;
else this.enabled = false;
time = deltaTime > 0 ? duration : 0;
this.time = time;
this._mixer.dispatchEvent({
type: 'finished',
action: this,
direction: deltaTime > 0 ? 1 : -1,
});
} else {
// keep running
if (pending === 1) {
// entering the last round
const atStart = deltaTime < 0;
this._setEndings(atStart, !atStart, pingPong);
} else {
this._setEndings(false, false, pingPong);
}
this._loopCount = loopCount;
this.time = time;
this._mixer.dispatchEvent({
type: 'loop',
action: this,
loopDelta: loopDelta,
});
}
} else {
this.time = time;
}
if (pingPong && (loopCount & 1) === 1) {
// invert time for the "pong round"
return duration - time;
}
}
return time;
}
_setEndings(atStart, atEnd, pingPong) {
const settings = this._interpolantSettings;
if (pingPong) {
settings.endingStart = ZeroSlopeEnding;
settings.endingEnd = ZeroSlopeEnding;
} else {
// assuming for LoopOnce atStart == atEnd == true
if (atStart) {
settings.endingStart = this.zeroSlopeAtStart ? ZeroSlopeEnding : ZeroCurvatureEnding;
} else {
settings.endingStart = WrapAroundEnding;
}
if (atEnd) {
settings.endingEnd = this.zeroSlopeAtEnd ? ZeroSlopeEnding : ZeroCurvatureEnding;
} else {
settings.endingEnd = WrapAroundEnding;
}
}
}
_scheduleFading(duration, weightNow, weightThen) {
const mixer = this._mixer,
now = mixer.time;
let interpolant = this._weightInterpolant;
if (interpolant === null) {
interpolant = mixer._lendControlInterpolant();
this._weightInterpolant = interpolant;
}
const times = interpolant.parameterPositions,
values = interpolant.sampleValues;
times[0] = now;
values[0] = weightNow;
times[1] = now + duration;
values[1] = weightThen;
return this;
}
}
export { AnimationAction };
|