Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #14992 #14994

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 15 additions & 3 deletions packages/dev/core/src/Animations/runtimeAnimation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ export class RuntimeAnimation {
*/
private _previousElapsedTime: number = 0;

private _yoyoDirection: number = 1;

/**
* The previous absolute frame of the runtime animation (meaning, without taking into account the from/to values, only the elapsed time and the fps)
*/
Expand Down Expand Up @@ -512,14 +514,24 @@ export class RuntimeAnimation {
let highLimitValue = 0;

// Apply the yoyo function if required
if (loop && this._animationState.loopMode === Animation.ANIMATIONLOOPMODE_YOYO) {
let yoyoLoop = false;
const yoyoMode = loop && this._animationState.loopMode === Animation.ANIMATIONLOOPMODE_YOYO;
if (yoyoMode) {
const position = (absoluteFrame - from) / frameRange;

// Apply the yoyo curve
const yoyoPosition = Math.abs(Math.sin(position * Math.PI));
const sin = Math.sin(position * Math.PI);
const yoyoPosition = Math.abs(sin);

// Map the yoyo position back to the range
absoluteFrame = yoyoPosition * frameRange + from;

const direction = sin >= 0 ? 1 : -1;
if (this._yoyoDirection !== direction) {
yoyoLoop = true;
}

this._yoyoDirection = direction;
}

this._previousElapsedTime = elapsedTimeSinceAnimationStart;
Expand Down Expand Up @@ -628,7 +640,7 @@ export class RuntimeAnimation {
const events = this._events;

// Reset event/state if looping
if ((speedRatio > 0 && this.currentFrame > currentFrame) || (speedRatio < 0 && this.currentFrame < currentFrame)) {
if ((!yoyoMode && ((speedRatio > 0 && this.currentFrame > currentFrame) || (speedRatio < 0 && this.currentFrame < currentFrame))) || (yoyoMode && yoyoLoop)) {
this._onLoop();

// Need to reset animation events
Expand Down