Skip to content

Commit

Permalink
feat: opt code
Browse files Browse the repository at this point in the history
  • Loading branch information
luzhuang committed Jul 26, 2024
1 parent 91d8fc3 commit 1b37930
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 34 deletions.
51 changes: 30 additions & 21 deletions packages/core/src/animation/Animator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { assignmentClone, ignoreClone } from "../clone/CloneManager";
import { ClearableObjectPool } from "../utils/ClearableObjectPool";
import { AnimatorController } from "./AnimatorController";
import { AnimatorControllerLayer } from "./AnimatorControllerLayer";
import { AnimatorControllerParameter, AnimatorControllerParameterValueType } from "./AnimatorControllerParameter";
import { AnimatorControllerParameter, AnimatorControllerParameterValue } from "./AnimatorControllerParameter";
import { AnimatorState } from "./AnimatorState";
import { AnimatorStateTransition } from "./AnimatorStateTransition";
import { KeyframeValueType } from "./Keyframe";
Expand Down Expand Up @@ -54,7 +54,7 @@ export class Animator extends Component {
@ignoreClone
private _animationEventHandlerPool = new ClearableObjectPool(AnimationEventHandler);
@ignoreClone
private _parametersValueMap: Record<string, AnimatorControllerParameterValueType> = Object.create(null);
private _parametersValueMap = <Record<string, AnimatorControllerParameterValue>>Object.create(null);

@ignoreClone
private _tempAnimatorStateInfo: IAnimatorStateInfo = { layerIndex: -1, state: null };
Expand Down Expand Up @@ -229,10 +229,10 @@ export class Animator extends Component {
* @param name - The name of the parameter
* @param value - The value of the parameter
*/
getParameterValue(name: string): AnimatorControllerParameterValueType {
getParameterValue(name: string): AnimatorControllerParameterValue {
const parameter = this._animatorController?._parametersMap[name];
if (parameter) {
return this._parametersValueMap[name] ?? parameter.value;
return this._parametersValueMap[name] ?? parameter.defaultValue;
}
return undefined;
}
Expand All @@ -242,9 +242,9 @@ export class Animator extends Component {
* @param name - The name of the parameter
* @param value - The value of the parameter
*/
setParameterValue(name: string, value: AnimatorControllerParameterValueType) {
setParameterValue(name: string, value: AnimatorControllerParameterValue) {
const parameter = this._animatorController?._parametersMap[name];
if (parameter && parameter.value !== value) {
if (parameter && parameter.defaultValue !== value) {
this._parametersValueMap[name] = value;
}
}
Expand Down Expand Up @@ -599,7 +599,7 @@ export class Animator extends Component {
aniUpdate
);

let costTime = actualDeltaTime;
let costTime: number;
if (transition) {
const clipDuration = state.clip.length;
const clipEndTime = state.clipEndTime * clipDuration;
Expand All @@ -622,18 +622,27 @@ export class Animator extends Component {
}
// Revert actualDeltaTime and update costTime
srcPlayData.update(costTime - actualDeltaTime);
} else {
costTime = actualDeltaTime;
}

let willSwitchState = !!transition;
let needSwitchLayerState = !!transition;

const { playState } = srcPlayData;
if (playState === AnimatorStatePlayState.Finished) {

let layerFinished: boolean;
if (!needSwitchLayerState) {
layerFinished = playState === AnimatorStatePlayState.Finished;
} else {
layerFinished = false;
}

if (layerFinished) {
layerData.layerState = LayerState.Finished;
willSwitchState = true;
}

// need newest value when crossFading with the next state
if (willSwitchState) {
if (needSwitchLayerState || layerFinished) {
this._evaluatePlayingState(
layerIndex,
srcPlayData,
Expand All @@ -652,7 +661,7 @@ export class Animator extends Component {
this._callAnimatorScriptOnEnter(state, layerIndex);
}

if (willSwitchState) {
if (needSwitchLayerState) {
const remainDeltaTime = deltaTime - costTime;
this._updateState(layerIndex, layerData, layer, remainDeltaTime, aniUpdate);
}
Expand Down Expand Up @@ -711,7 +720,7 @@ export class Animator extends Component {
const { eventHandlers: srcEventHandlers } = srcStateData;
const { state: destState, stateData: destStateData } = destPlayData;
const { eventHandlers: destEventHandlers } = destStateData;

if (srcPlayData.state.name === "squat") console.log(111);
const destStateDuration = destState._getDuration();
const transitionDuration = destStateDuration * layerData.crossFadeTransition.duration;

Expand All @@ -725,7 +734,7 @@ export class Animator extends Component {
const { clipTime: lastSrcClipTime, playState: lastSrcPlayState } = srcPlayData;
const { clipTime: lastDestClipTime, playState: lastDstPlayState } = destPlayData;

let destCostTime = 0;
let destCostTime: number;
if (destPlayData.isForwards) {
destCostTime =
lastDestClipTime + actualDestDeltaTime > transitionDuration
Expand Down Expand Up @@ -754,8 +763,8 @@ export class Animator extends Component {
// For precision problem, loose judgment, expect to crossFade
(crossWeight >= 1.0 - MathUtil.zeroTolerance || transitionDuration === 0) && (crossWeight = 1.0);

const willSwitchState = crossWeight === 1.0;
if (willSwitchState) {
const needSwitchLayerState = crossWeight === 1.0;
if (needSwitchLayerState) {
this._preparePlayOwner(layerData, destState);
this._callAnimatorScriptOnExit(srcState, layerIndex);
destPlayData.playState === AnimatorStatePlayState.Finished &&
Expand Down Expand Up @@ -797,7 +806,7 @@ export class Animator extends Component {

const remainDeltaTime = deltaTime - costTime;
// For precision problem, strict judgment, expect not to update
willSwitchState &&
needSwitchLayerState &&
remainDeltaTime > MathUtil.zeroTolerance &&
this._updateState(layerIndex, layerData, layer, remainDeltaTime, aniUpdate);
}
Expand Down Expand Up @@ -882,7 +891,7 @@ export class Animator extends Component {

const { clipTime: lastDestClipTime, playState: lastPlayState } = destPlayData;

let destCostTime = 0;
let destCostTime: number;
if (destPlayData.isForwards) {
destCostTime =
lastDestClipTime + actualDeltaTime > transitionDuration
Expand All @@ -909,8 +918,8 @@ export class Animator extends Component {
// For precision problem, loose judgment, expect to crossFade
(crossWeight >= 1.0 - MathUtil.zeroTolerance || transitionDuration === 0) && (crossWeight = 1.0);

const willSwitchState = crossWeight === 1.0;
if (willSwitchState) {
const needSwitchLayerState = crossWeight === 1.0;
if (needSwitchLayerState) {
this._preparePlayOwner(layerData, state);
this._callAnimatorScriptOnExit(state, layerIndex);
destPlayData.playState === AnimatorStatePlayState.Finished &&
Expand Down Expand Up @@ -940,7 +949,7 @@ export class Animator extends Component {
crossWeight === 1.0 && this._updateCrossFadeData(layerData);
// For precision problem, strict judgment, expect not to update
const remainDeltaTime = deltaTime - costTime;
willSwitchState &&
needSwitchLayerState &&
remainDeltaTime > MathUtil.zeroTolerance &&
this._updateState(layerIndex, layerData, layer, remainDeltaTime, aniUpdate);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/animation/AnimatorCondition.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AnimatorControllerParameterValueType } from "./AnimatorControllerParameter";
import { AnimatorControllerParameterValue } from "./AnimatorControllerParameter";
import { AnimatorConditionMode } from "./enums/AnimatorConditionMode";

/**
Expand All @@ -10,5 +10,5 @@ export class AnimatorCondition {
/** The name of the parameter used in the condition. */
parameterName: string;
/** The AnimatorParameter's threshold value for the condition to be true. */
threshold?: AnimatorControllerParameterValueType;
threshold?: AnimatorControllerParameterValue;
}
21 changes: 16 additions & 5 deletions packages/core/src/animation/AnimatorController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BoolUpdateFlag } from "../BoolUpdateFlag";
import { AnimatorControllerParameter, AnimatorControllerParameterValueType } from "./AnimatorControllerParameter";
import { AnimatorControllerParameter, AnimatorControllerParameterValue } from "./AnimatorControllerParameter";
import { UpdateFlagManager } from "../UpdateFlagManager";
import { AnimatorControllerLayer } from "./AnimatorControllerLayer";
import { ReferResource } from "../asset/ReferResource";
Expand Down Expand Up @@ -34,29 +34,40 @@ export class AnimatorController extends ReferResource {
return this._parameters;
}

/**
* Create an AnimatorController.
* @param engine - Engine to which the animatorController belongs
*/
constructor(engine: Engine);

/**
* @deprecated
*/
constructor();

constructor(engine?: Engine) {
engine && super(engine);
}

/**
* Add a parameter to the controller.
* @param name - The name of the parameter
* @param initialValue - The initial value of the parameter
* @param defaultValue - The default value of the parameter
*/
addParameter(name: string, initialValue?: AnimatorControllerParameterValueType): AnimatorControllerParameter;
addParameter(name: string, defaultValue?: AnimatorControllerParameterValue): AnimatorControllerParameter;

/**
* Add a parameter to the controller.
* @param parameter - The parameter
*/
addParameter(parameter: AnimatorControllerParameter): AnimatorControllerParameter;

addParameter(param: AnimatorControllerParameter | string, value?: AnimatorControllerParameterValueType) {
addParameter(param: AnimatorControllerParameter | string, value?: AnimatorControllerParameterValue) {
if (typeof param === "string") {
const name = param;
param = new AnimatorControllerParameter();
param.name = name;
param.value = value;
param.defaultValue = value;
}
this._parametersMap[param.name] = param;
this._parameters.push(param);
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/animation/AnimatorControllerParameter.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export type AnimatorControllerParameterValueType = number | string | boolean;
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 initial value of the parameter. */
value: AnimatorControllerParameterValueType;
/** The default value of the parameter. */
defaultValue: AnimatorControllerParameterValue;
}
6 changes: 3 additions & 3 deletions packages/core/src/animation/AnimatorStateTransition.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AnimatorControllerParameterValueType } from "./AnimatorControllerParameter";
import { AnimatorControllerParameterValue } from "./AnimatorControllerParameter";
import { AnimatorConditionMode } from "./enums/AnimatorConditionMode";
import { AnimatorCondition } from "./AnimatorCondition";
import { AnimatorState } from "./AnimatorState";
Expand Down Expand Up @@ -61,7 +61,7 @@ export class AnimatorStateTransition {
addCondition(
mode: AnimatorConditionMode,
parameterName: string,
threshold?: AnimatorControllerParameterValueType
threshold?: AnimatorControllerParameterValue
): AnimatorCondition;

/**
Expand All @@ -73,7 +73,7 @@ export class AnimatorStateTransition {
addCondition(
param: AnimatorConditionMode | AnimatorCondition,
parameterName?: string,
threshold?: AnimatorControllerParameterValueType
threshold?: AnimatorControllerParameterValue
): AnimatorCondition {
if (typeof param === "object") {
this._conditions.push(param);
Expand Down

0 comments on commit 1b37930

Please sign in to comment.