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 animation time error because of speed and animatorParameter rename bug #2273

Merged
merged 9 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 14 additions & 22 deletions packages/core/src/animation/Animator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { AnimatorLayerData } from "./internal/AnimatorLayerData";
import { AnimatorStateData } from "./internal/AnimatorStateData";
import { AnimatorStatePlayData } from "./internal/AnimatorStatePlayData";
import { AnimationCurveOwner } from "./internal/animationCurveOwner/AnimationCurveOwner";
import { MathUtil } from "@galacean/engine-math";

/**
* The controller of the animation system.
Expand Down Expand Up @@ -602,14 +603,12 @@ export class Animator extends Component {
layerData.layerState = LayerState.Finished;
}

costTime = Math.abs(costTime);

this._evaluatePlayingState(srcPlayData, weight, additive, aniUpdate);
this._fireAnimationEventsAndCallScripts(layerIndex, srcPlayData, state, lastClipTime, lastPlayState, costTime);

if (needSwitchLayerState) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opt code

const remainDeltaTime = deltaTime - costTime;
this._updateState(layerIndex, layerData, layer, remainDeltaTime, aniUpdate);
const remainDeltaTime = deltaTime - costTime / actualSpeed;
remainDeltaTime >= 0 && this._updateState(layerIndex, layerData, layer, remainDeltaTime, aniUpdate);
}
}

Expand Down Expand Up @@ -704,7 +703,7 @@ export class Animator extends Component {
this._preparePlayOwner(layerData, destState);
this._evaluatePlayingState(destPlayData, weight, additive, aniUpdate);
} else {
this._evaluateCrossFadeState(layerData, srcPlayData, destPlayData, weight, additive, aniUpdate);
this._evaluateCrossFadeState(layerData, srcPlayData, destPlayData, weight, crossWeight, additive, aniUpdate);
}

this._fireAnimationEventsAndCallScripts(
Expand All @@ -713,7 +712,7 @@ export class Animator extends Component {
srcState,
lastSrcClipTime,
lastSrcPlayState,
Math.abs(srcCostTime)
srcCostTime
);

this._fireAnimationEventsAndCallScripts(
Expand All @@ -722,13 +721,13 @@ export class Animator extends Component {
destState,
lastDestClipTime,
lastDstPlayState,
Math.abs(destCostTime)
destCostTime
);

if (crossFadeFinished) {
this._updateCrossFadeData(layerData);
const remainDeltaTime = deltaTime - costTime;
remainDeltaTime > 0 && this._updateState(layerIndex, layerData, layer, remainDeltaTime, aniUpdate);
remainDeltaTime >= 0 && this._updateState(layerIndex, layerData, layer, remainDeltaTime, aniUpdate);
}
}

Expand All @@ -737,6 +736,7 @@ export class Animator extends Component {
srcPlayData: AnimatorStatePlayData,
destPlayData: AnimatorStatePlayData,
weight: number,
crossWeight: number,
additive: boolean,
aniUpdate: boolean
) {
Expand All @@ -745,10 +745,6 @@ export class Animator extends Component {
const { state: destState } = destPlayData;
const { _curveBindings: destCurves } = destState.clip;

const transitionDuration = destState._getDuration() * layerData.crossFadeTransition.duration;
let crossWeight = Math.abs(destPlayData.frameTime) / transitionDuration;
(crossWeight >= 1.0 || transitionDuration === 0) && (crossWeight = 1.0);

const finished = destPlayData.playState === AnimatorStatePlayState.Finished;

if (aniUpdate || finished) {
Expand Down Expand Up @@ -831,7 +827,7 @@ export class Animator extends Component {
this._preparePlayOwner(layerData, state);
this._evaluatePlayingState(destPlayData, weight, additive, aniUpdate);
} else {
this._evaluateCrossFadeFromPoseState(layerData, destPlayData, weight, additive, aniUpdate);
this._evaluateCrossFadeFromPoseState(layerData, destPlayData, weight, crossWeight, additive, aniUpdate);
}

this._fireAnimationEventsAndCallScripts(
Expand All @@ -840,31 +836,28 @@ export class Animator extends Component {
state,
lastDestClipTime,
lastPlayState,
Math.abs(destCostTime)
destCostTime
);

if (crossFadeFinished) {
this._updateCrossFadeData(layerData);
const remainDeltaTime = deltaTime - costTime;
remainDeltaTime > 0 && this._updateState(layerIndex, layerData, layer, remainDeltaTime, aniUpdate);
remainDeltaTime >= 0 && this._updateState(layerIndex, layerData, layer, remainDeltaTime, aniUpdate);
}
}

private _evaluateCrossFadeFromPoseState(
layerData: AnimatorLayerData,
destPlayData: AnimatorStatePlayData,
weight: number,
crossWeight: number,
additive: boolean,
aniUpdate: boolean
) {
const { crossLayerOwnerCollection } = layerData;
const { state } = destPlayData;
const { _curveBindings: curveBindings } = state.clip;

const duration = state._getDuration() * layerData.crossFadeTransition.duration;
let crossWeight = Math.abs(destPlayData.frameTime) / duration;
(crossWeight >= 1.0 || duration === 0) && (crossWeight = 1.0);

const { clipTime: destClipTime, playState } = destPlayData;
const finished = playState === AnimatorStatePlayState.Finished;

Expand Down Expand Up @@ -1321,18 +1314,17 @@ export class Animator extends Component {
const clipDuration = state.clip.length;
const startTime = state.clipStartTime * clipDuration;
const endTime = state.clipEndTime * clipDuration;
const actualDeltaTime = this.speed * state.speed * deltaTime;

if (isForwards) {
if (lastClipTime + actualDeltaTime >= endTime) {
if (lastClipTime + deltaTime >= endTime) {
this._fireSubAnimationEvents(playData, eventHandlers, lastClipTime, state.clipEndTime * clipDuration);
playData.currentEventIndex = 0;
this._fireSubAnimationEvents(playData, eventHandlers, state.clipStartTime * clipDuration, clipTime);
} else {
this._fireSubAnimationEvents(playData, eventHandlers, lastClipTime, clipTime);
}
} else {
if (lastClipTime + actualDeltaTime <= startTime) {
if (lastClipTime + deltaTime <= startTime) {
this._fireBackwardSubAnimationEvents(playData, eventHandlers, lastClipTime, state.clipStartTime * clipDuration);
playData.currentEventIndex = eventHandlers.length - 1;
this._fireBackwardSubAnimationEvents(playData, eventHandlers, state.clipEndTime * clipDuration, clipTime);
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/animation/AnimatorController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ export class AnimatorController extends ReferResource {
param.name = name;
param.defaultValue = defaultValue;
}
param._onNameChanged = (oldName, newName) => {
delete this._parametersMap[oldName];
this._parametersMap[newName] = param as AnimatorControllerParameter;
};
this._parametersMap[param.name] = param;
this._parameters.push(param);
return param;
Expand Down
25 changes: 23 additions & 2 deletions packages/core/src/animation/AnimatorControllerParameter.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
import { AnimatorController } from "./AnimatorController";

export type AnimatorControllerParameterValue = number | string | boolean;

/**
* Used to communicate between scripting and the controller, parameters can be set in scripting and used by the controller.
*/
export class AnimatorControllerParameter {
/** The name of the parameter. */
name: string;
/** The default value of the parameter. */
defaultValue: AnimatorControllerParameterValue;

/** @internal */
_onNameChanged: (oldName: string, newName: string) => void = null;

private _name: string;

/**
* The name of the parameter.
*/
get name(): string {
return this._name;
}

set name(name: string) {
if (this._name === name) {
return;
}
const oldName = this._name;
this._name = name;
this._onNameChanged?.(oldName, name);
}
}
20 changes: 14 additions & 6 deletions tests/src/core/Animator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ describe("Animator test", function () {
animator.animatorController.addParameter("playerSpeed", 1);
const stateMachine = animator.animatorController.layers[0].stateMachine;
const idleState = animator.findAnimatorState("Survey");
const idleSpeed = 2;
idleState.speed = idleSpeed;
idleState.clearTransitions();
const walkState = animator.findAnimatorState("Walk");
walkState.clearTransitions();
Expand All @@ -344,7 +346,9 @@ describe("Animator test", function () {
idleState.addTransition(toWalkTransition);
idleToWalkTime =
//@ts-ignore
toWalkTransition.exitTime * idleState._getDuration() + toWalkTransition.duration * walkState._getDuration();
(toWalkTransition.exitTime * idleState._getDuration()) / idleSpeed +
//@ts-ignore
toWalkTransition.duration * walkState._getDuration();

const exitTransition = idleState.addExitTransition();
exitTransition.addCondition(AnimatorConditionMode.Equals, "playerSpeed", 0);
Expand All @@ -370,7 +374,7 @@ describe("Animator test", function () {
//@ts-ignore
(toIdleTransition.exitTime - toRunTransition.duration) * walkState._getDuration() +
//@ts-ignore
toIdleTransition.duration * idleState._getDuration();
(toIdleTransition.duration * idleState._getDuration()) / idleSpeed;

// to run state
const runToWalkTransition = new AnimatorStateTransition();
Expand All @@ -395,7 +399,7 @@ describe("Animator test", function () {
// @ts-ignore
(anyTransition.exitTime - toIdleTransition.duration) * walkState._getDuration() +
// @ts-ignore
anyTransition.duration * idleState._getDuration();
(anyTransition.duration * idleState._getDuration()) / idleSpeed;

// @ts-ignore
animator.engine.time._frameCount++;
Expand Down Expand Up @@ -447,6 +451,8 @@ describe("Animator test", function () {
stateMachine._anyStateTransitions.length = 0;

const idleState = animator.findAnimatorState("Survey");
const idleSpeed = 2;
idleState.speed = idleSpeed;
idleState.clearTransitions();
const walkState = animator.findAnimatorState("Walk");
walkState.clearTransitions();
Expand All @@ -466,7 +472,9 @@ describe("Animator test", function () {
idleState.addTransition(toWalkTransition);
idleToWalkTime =
//@ts-ignore
(1 - toWalkTransition.exitTime) * idleState._getDuration() + toWalkTransition.duration * walkState._getDuration();
((1 - toWalkTransition.exitTime) * idleState._getDuration()) / idleSpeed +
//@ts-ignore
toWalkTransition.duration * walkState._getDuration();

const exitTransition = idleState.addExitTransition();
exitTransition.addCondition(AnimatorConditionMode.Equals, "playerSpeed", 0);
Expand All @@ -492,7 +500,7 @@ describe("Animator test", function () {
//@ts-ignore
(1 - toIdleTransition.exitTime - toRunTransition.duration) * walkState._getDuration() +
//@ts-ignore
toIdleTransition.duration * idleState._getDuration();
(toIdleTransition.duration * idleState._getDuration()) / idleSpeed;

// to run state
const runToWalkTransition = new AnimatorStateTransition();
Expand All @@ -517,7 +525,7 @@ describe("Animator test", function () {
// @ts-ignore
(1 - anyTransition.exitTime - toIdleTransition.duration) * walkState._getDuration() +
// @ts-ignore
anyTransition.duration * idleState._getDuration();
(anyTransition.duration * idleState._getDuration()) / idleSpeed;

// @ts-ignore
animator.engine.time._frameCount++;
Expand Down
Loading