From a1ed3f52a9d50b8847c0e582f4d80156ca4eae35 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Wed, 9 Jun 2021 11:41:11 +0800 Subject: [PATCH 1/9] refactor: opt code --- packages/core/src/animation/Animator.ts | 49 ++++++++++++------------ packages/core/src/animation/CurveData.ts | 2 +- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/packages/core/src/animation/Animator.ts b/packages/core/src/animation/Animator.ts index c1aaba8d45..d816d457d7 100644 --- a/packages/core/src/animation/Animator.ts +++ b/packages/core/src/animation/Animator.ts @@ -1,22 +1,21 @@ -import { AnimatorStateTransition } from "./AnimatorTransition"; -import { WrapMode } from "./enums/WrapMode"; -import { Transform } from "../Transform"; -import { AnimatorState } from "./AnimatorState"; -import { InterpolableValueType } from "./enums/InterpolableValueType"; -import { InterpolableValue } from "./KeyFrame"; -import { AnimatorControllerLayer } from "./AnimatorControllerLayer"; -import { AnimatorController } from "./AnimatorController"; import { Quaternion, Vector2, Vector3, Vector4 } from "@oasis-engine/math"; +import { ignoreClone } from "../clone/CloneManager"; import { Component } from "../Component"; import { Entity } from "../Entity"; -import { AnimatorUtils } from "./AnimatorUtils"; -import { AnimatorLayerBlendingMode } from "./enums/AnimatorLayerBlendingMode"; -import { PlayType } from "./enums/PlayType"; -import { ignoreClone } from "../clone/CloneManager"; -import { AnimationProperty } from "./enums/AnimationProperty"; +import { Transform } from "../Transform"; +import { AnimatorController } from "./AnimatorController"; +import { AnimatorControllerLayer } from "./AnimatorControllerLayer"; import { AnimatorLayerData } from "./AnimatorLayerData"; import { AnimatorStateData } from "./AnimatorStateData"; +import { AnimatorStateTransition } from "./AnimatorTransition"; +import { AnimatorUtils } from "./AnimatorUtils"; import { CurveData } from "./CurveData"; +import { AnimationProperty } from "./enums/AnimationProperty"; +import { AnimatorLayerBlendingMode } from "./enums/AnimatorLayerBlendingMode"; +import { InterpolableValueType } from "./enums/InterpolableValueType"; +import { PlayType } from "./enums/PlayType"; +import { WrapMode } from "./enums/WrapMode"; +import { InterpolableValue } from "./KeyFrame"; interface MergedCurveIndex { curCurveIndex: number; @@ -132,7 +131,7 @@ export class Animator extends Component { mergedCurveIndexList.length = 0; if (isCrossFading) { - this._setTempPoseValue(playingStateData, destStateData); + this._saveFixedPose(playingStateData, destStateData); } destStateData.state = nextState; @@ -141,7 +140,7 @@ export class Animator extends Component { this._setDefaultValueAndTarget(destStateData); if (crossFromFixedPose) { - this._setTempPoseValue(null, destStateData); + this._saveFixedPose(null, destStateData); } if (crossFromFixedPose || isCrossFading) { @@ -283,21 +282,21 @@ export class Animator extends Component { targetValueCache[property] = targetEntity.transform.position.clone(); } curveData.defaultValue = targetValueCache[property]; - curveData.tempPoseValue = new Vector3(); + curveData.fiexedPoseValue = new Vector3(); break; case AnimationProperty.Rotation: if (!targetValueCache[property]) { targetValueCache[property] = targetEntity.transform.rotationQuaternion.clone(); } curveData.defaultValue = targetValueCache[property]; - curveData.tempPoseValue = new Quaternion(); + curveData.fiexedPoseValue = new Quaternion(); break; case AnimationProperty.Scale: if (!targetValueCache[property]) { targetValueCache[property] = targetEntity.transform.scale.clone(); } curveData.defaultValue = targetValueCache[property]; - curveData.tempPoseValue = new Vector3(); + curveData.fiexedPoseValue = new Vector3(); break; } stateData.curveDatas[i] = curveData; @@ -305,7 +304,7 @@ export class Animator extends Component { } } - private _setTempPoseValue( + private _saveFixedPose( playingStateData: AnimatorStateData, destStateData: AnimatorStateData ): void { @@ -323,16 +322,16 @@ export class Animator extends Component { _curveDataForPose[i] = curveData; const effectProperty = effectTargetProperty[instanceId] || (effectTargetProperty[instanceId] = []); effectProperty[property] = true; - const { tempPoseValue } = _curveDataForPose[i]; + const { fiexedPoseValue } = _curveDataForPose[i]; switch (property) { case AnimationProperty.Position: - targetEntity.transform.position.cloneTo(tempPoseValue); + targetEntity.transform.position.cloneTo(fiexedPoseValue); break; case AnimationProperty.Rotation: - targetEntity.transform.rotationQuaternion.cloneTo(tempPoseValue); + targetEntity.transform.rotationQuaternion.cloneTo(fiexedPoseValue); break; case AnimationProperty.Scale: - targetEntity.transform.scale.cloneTo(tempPoseValue); + targetEntity.transform.scale.cloneTo(fiexedPoseValue); break; } } @@ -682,7 +681,9 @@ export class Animator extends Component { const count = curveDataForPose.length; for (let i = count - 1; i >= 0; i--) { - const { target, curveData: fixedPoseCurveData, defaultValue, tempPoseValue } = curveDataForPose[i]; + const { target, curveData: fixedPoseCurveData, defaultValue, fiexedPoseValue: tempPoseValue } = curveDataForPose[ + i + ]; const destCurveData = destStateData.curveDatas[i]; let calculatedValue: InterpolableValue; const { type, property } = fixedPoseCurveData; diff --git a/packages/core/src/animation/CurveData.ts b/packages/core/src/animation/CurveData.ts index f23d8cb79c..44da0805e4 100644 --- a/packages/core/src/animation/CurveData.ts +++ b/packages/core/src/animation/CurveData.ts @@ -10,5 +10,5 @@ export class CurveData { target: Entity; curveData: AnimationClipCurveData; defaultValue: InterpolableValue; - tempPoseValue: InterpolableValue; + fiexedPoseValue: InterpolableValue; } From 7bfb94438e50139c9591f13f9e3239ba8320bf9e Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Wed, 9 Jun 2021 12:54:58 +0800 Subject: [PATCH 2/9] refactor: opt struct of Animation Node data --- .../core/src/animation/AnimationCureOwner.ts | 11 +++ packages/core/src/animation/Animator.ts | 89 +++++++++---------- packages/core/src/animation/CurveData.ts | 10 +-- packages/loader/src/scene-loader/GLTFModel.ts | 3 +- 4 files changed, 59 insertions(+), 54 deletions(-) create mode 100644 packages/core/src/animation/AnimationCureOwner.ts diff --git a/packages/core/src/animation/AnimationCureOwner.ts b/packages/core/src/animation/AnimationCureOwner.ts new file mode 100644 index 0000000000..b1333f3771 --- /dev/null +++ b/packages/core/src/animation/AnimationCureOwner.ts @@ -0,0 +1,11 @@ +import { Entity } from "../Entity"; +import { InterpolableValue } from "./KeyFrame"; + +/** + * @internal + */ +export class AnimationCureOwner { + target: Entity; + defaultValue: InterpolableValue; + fiexedPoseValue: InterpolableValue; +} diff --git a/packages/core/src/animation/Animator.ts b/packages/core/src/animation/Animator.ts index d816d457d7..b7f57f4ffd 100644 --- a/packages/core/src/animation/Animator.ts +++ b/packages/core/src/animation/Animator.ts @@ -3,6 +3,7 @@ import { ignoreClone } from "../clone/CloneManager"; import { Component } from "../Component"; import { Entity } from "../Entity"; import { Transform } from "../Transform"; +import { AnimationCureOwner } from "./AnimationCureOwner"; import { AnimatorController } from "./AnimatorController"; import { AnimatorControllerLayer } from "./AnimatorControllerLayer"; import { AnimatorLayerData } from "./AnimatorLayerData"; @@ -57,9 +58,9 @@ export class Animator extends Component { @ignoreClone private _transitionForPose: AnimatorStateTransition = new AnimatorStateTransition(); @ignoreClone - private _curveDataForPose: CurveData[] = []; + private _curveDataForPose: CurveData[] = []; //CM: 简化 @ignoreClone - private _defaultValueCache: InterpolableValue[][] = []; + private _animationCureOwners: AnimationCureOwner[][] = []; /** * All layers from the AnimatorController which belongs this Animator . @@ -161,7 +162,7 @@ export class Animator extends Component { const curves = state.clip._curves; const curveDatas = playingStateData.curveDatas; for (let i = curves.length - 1; i >= 0; i--) { - const { instanceId } = curveDatas[i].target; + const { instanceId } = curveDatas[i].owner.target; const { property } = curves[i]; const mergeProperty = mergeTargetProperty[instanceId] || (mergeTargetProperty[instanceId] = []); mergeProperty[property] = mergedCurveIndexList.length; @@ -174,7 +175,7 @@ export class Animator extends Component { const curves = nextState.clip._curves; const curveDatas = destStateData.curveDatas; for (let i = curves.length - 1; i >= 0; i--) { - const { instanceId } = curveDatas[i].target; + const { instanceId } = curveDatas[i].owner.target; const { property } = curves[i]; const mergeProperty = mergeTargetProperty[instanceId] || (mergeTargetProperty[instanceId] = []); if (mergeProperty[property] === undefined) { @@ -263,7 +264,7 @@ export class Animator extends Component { * @internal */ _setDefaultValueAndTarget(stateData: AnimatorStateData): void { - const { _defaultValueCache } = this; + const { _animationCureOwners: animationCureOwners } = this; const { clip } = stateData.state; const curves = clip._curves; for (let i = curves.length - 1; i >= 0; i--) { @@ -271,32 +272,27 @@ export class Animator extends Component { const { relativePath, property } = curve; const targetEntity = this.entity.findByPath(relativePath); const { instanceId } = targetEntity; - const targetValueCache = _defaultValueCache[instanceId] || (_defaultValueCache[instanceId] = []); + const propertyOwners = animationCureOwners[instanceId] || (animationCureOwners[instanceId] = []); if (!stateData.curveDatas[i]) { + //CN: 两种对象初始化时机解耦 + const propertyOwner = propertyOwners[property] || (propertyOwners[property] = new AnimationCureOwner()); const curveData = new CurveData(); - curveData.target = targetEntity; + propertyOwner.target = targetEntity; + curveData.owner = propertyOwner; curveData.curveData = curve; + switch (property) { case AnimationProperty.Position: - if (!targetValueCache[property]) { - targetValueCache[property] = targetEntity.transform.position.clone(); - } - curveData.defaultValue = targetValueCache[property]; - curveData.fiexedPoseValue = new Vector3(); + propertyOwner.defaultValue = targetEntity.transform.position.clone(); + propertyOwner.fiexedPoseValue = new Vector3(); break; case AnimationProperty.Rotation: - if (!targetValueCache[property]) { - targetValueCache[property] = targetEntity.transform.rotationQuaternion.clone(); - } - curveData.defaultValue = targetValueCache[property]; - curveData.fiexedPoseValue = new Quaternion(); + propertyOwner.defaultValue = targetEntity.transform.rotationQuaternion.clone(); + propertyOwner.fiexedPoseValue = new Quaternion(); break; case AnimationProperty.Scale: - if (!targetValueCache[property]) { - targetValueCache[property] = targetEntity.transform.scale.clone(); - } - curveData.defaultValue = targetValueCache[property]; - curveData.fiexedPoseValue = new Vector3(); + propertyOwner.defaultValue = targetEntity.transform.scale.clone(); + propertyOwner.fiexedPoseValue = new Vector3(); break; } stateData.curveDatas[i] = curveData; @@ -311,27 +307,28 @@ export class Animator extends Component { const { _curveDataForPose } = this; _curveDataForPose.length = 0; + //CM: 可否简化 let effectTargetProperty: boolean[][] = []; const nextCurves = destStateData.state.clip._curves; for (let i = nextCurves.length - 1; i >= 0; i--) { const curve = nextCurves[i]; const { property } = curve; const curveData = destStateData.curveDatas[i]; - const targetEntity = curveData.target; - const { instanceId } = targetEntity; + const owner = curveData.owner; + const { target, fiexedPoseValue } = owner; + const { instanceId } = target; _curveDataForPose[i] = curveData; const effectProperty = effectTargetProperty[instanceId] || (effectTargetProperty[instanceId] = []); effectProperty[property] = true; - const { fiexedPoseValue } = _curveDataForPose[i]; switch (property) { case AnimationProperty.Position: - targetEntity.transform.position.cloneTo(fiexedPoseValue); + target.transform.position.cloneTo(fiexedPoseValue); break; case AnimationProperty.Rotation: - targetEntity.transform.rotationQuaternion.cloneTo(fiexedPoseValue); + target.transform.rotationQuaternion.cloneTo(fiexedPoseValue); break; case AnimationProperty.Scale: - targetEntity.transform.scale.cloneTo(fiexedPoseValue); + target.transform.scale.cloneTo(fiexedPoseValue); break; } } @@ -341,8 +338,7 @@ export class Animator extends Component { const curve = curCurves[i]; const { property } = curve; const curveData = playingStateData.curveDatas[i]; - const targetEntity = curveData.target; - const { instanceId } = targetEntity; + const { instanceId } = curveData.owner.target; if (!effectTargetProperty[instanceId][property]) { _curveDataForPose.push(curveData); } @@ -574,7 +570,7 @@ export class Animator extends Component { for (let i = curves.length - 1; i >= 0; i--) { const { curve, type, property } = curves[i]; const value = curve.evaluate(frameTime); - const { target, defaultValue } = playingStateData.curveDatas[i]; + const { target, defaultValue } = playingStateData.curveDatas[i].owner; if (isFirstLayer) { this._applyClipValue(target, type, property, defaultValue, value, 1.0); } else { @@ -627,18 +623,18 @@ export class Animator extends Component { const curFrameTime = playingStateData.state._getTheRealFrameTime(playingStateData.frameTime); const curVal = curCurve.evaluate(curFrameTime); const destVal = nextCurve.evaluate(frameTime); - const { target, defaultValue } = playingStateData.curveDatas[curCurveIndex]; + const { target, defaultValue } = playingStateData.curveDatas[curCurveIndex].owner; const calculatedValue = this._getCrossFadeValue(target, type, property, curVal, destVal, crossWeight); this._applyClipValue(target, type, property, defaultValue, calculatedValue, 1); } else if (curCurveIndex >= 0) { const { curve: curCurve, type, property } = curClip._curves[curCurveIndex]; - const { target, defaultValue } = playingStateData.curveDatas[curCurveIndex]; + const { target, defaultValue } = playingStateData.curveDatas[curCurveIndex].owner; const curFrameTime = playingStateData.state._getTheRealFrameTime(playingStateData.frameTime); const curVal = curCurve.evaluate(curFrameTime); const calculatedValue = this._getCrossFadeValue(target, type, property, defaultValue, curVal, 1 - crossWeight); this._applyClipValue(target, type, property, defaultValue, calculatedValue, weight); } else { - const { target, defaultValue } = destStateData.curveDatas[nextCurveIndex]; + const { target, defaultValue } = destStateData.curveDatas[nextCurveIndex].owner; const { curve, type, property } = nextClip._curves[nextCurveIndex]; const val = curve.evaluate(frameTime); const calculatedValue = this._getCrossFadeValue(target, type, property, defaultValue, val, crossWeight); @@ -681,18 +677,19 @@ export class Animator extends Component { const count = curveDataForPose.length; for (let i = count - 1; i >= 0; i--) { - const { target, curveData: fixedPoseCurveData, defaultValue, fiexedPoseValue: tempPoseValue } = curveDataForPose[ - i - ]; + const curveData = curveDataForPose[i]; + const { curveData: fixedPoseCurveData } = curveData; + const { target, defaultValue, fiexedPoseValue } = curveData.owner; + const destCurveData = destStateData.curveDatas[i]; let calculatedValue: InterpolableValue; const { type, property } = fixedPoseCurveData; const { curve } = nextClip._curves[i]; if (destCurveData) { const val = curve.evaluate(frameTime); - calculatedValue = this._getCrossFadeValue(target, type, property, tempPoseValue, val, crossWeight); + calculatedValue = this._getCrossFadeValue(target, type, property, fiexedPoseValue, val, crossWeight); } else { - calculatedValue = this._getCrossFadeValue(target, type, property, tempPoseValue, defaultValue, crossWeight); + calculatedValue = this._getCrossFadeValue(target, type, property, fiexedPoseValue, defaultValue, crossWeight); } this._applyClipValue(target, type, property, defaultValue, calculatedValue, weight); } @@ -709,19 +706,17 @@ export class Animator extends Component { const curves = clip._curves; for (let i = curves.length - 1; i >= 0; i--) { const curve = curves[i]; - const { property } = curve; - const curveData = playingStateData.curveDatas[i]; - const { defaultValue } = curveData; - const { transform } = curveData.target; - switch (property) { + const { owner } = playingStateData.curveDatas[i]; + const { transform } = owner.target; + switch (curve.property) { case AnimationProperty.Position: - transform.position = defaultValue; + transform.position = owner.defaultValue; break; case AnimationProperty.Rotation: - transform.rotationQuaternion = defaultValue; + transform.rotationQuaternion = owner.defaultValue; break; case AnimationProperty.Scale: - transform.scale = defaultValue; + transform.scale = owner.defaultValue; break; } } diff --git a/packages/core/src/animation/CurveData.ts b/packages/core/src/animation/CurveData.ts index 44da0805e4..253588f534 100644 --- a/packages/core/src/animation/CurveData.ts +++ b/packages/core/src/animation/CurveData.ts @@ -1,14 +1,14 @@ -import { Entity } from "../Entity"; import { Component } from "../Component"; -import { InterpolableValue } from "./KeyFrame"; import { AnimationClipCurveData } from "./AnimationClipCurveData"; +import { AnimationCureOwner } from "./AnimationCureOwner"; /** * @internal */ export class CurveData { - target: Entity; + owner: AnimationCureOwner; curveData: AnimationClipCurveData; - defaultValue: InterpolableValue; - fiexedPoseValue: InterpolableValue; + // target: Entity; + // defaultValue: InterpolableValue; + // fiexedPoseValue: InterpolableValue; } diff --git a/packages/loader/src/scene-loader/GLTFModel.ts b/packages/loader/src/scene-loader/GLTFModel.ts index 7b8710330a..0f5cd7f7c0 100644 --- a/packages/loader/src/scene-loader/GLTFModel.ts +++ b/packages/loader/src/scene-loader/GLTFModel.ts @@ -57,8 +57,7 @@ export class GLTFModel extends Component { set loop(value: WrapMode) { if (this._animator && this.autoPlay) { // Play bone animation - const theState = this._animator.play(this._autoPlay); - theState.wrapMode = value; + this._animator.play(this._autoPlay); } this._loop = value; } From 1a694b7b85d211883dbf97c8670ddf8eb638eb53 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Wed, 9 Jun 2021 14:45:50 +0800 Subject: [PATCH 3/9] refactor: add CM --- packages/core/src/animation/Animator.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/core/src/animation/Animator.ts b/packages/core/src/animation/Animator.ts index b7f57f4ffd..7024a7f9e5 100644 --- a/packages/core/src/animation/Animator.ts +++ b/packages/core/src/animation/Animator.ts @@ -274,7 +274,7 @@ export class Animator extends Component { const { instanceId } = targetEntity; const propertyOwners = animationCureOwners[instanceId] || (animationCureOwners[instanceId] = []); if (!stateData.curveDatas[i]) { - //CN: 两种对象初始化时机解耦 + //CM: 两种对象初始化时机解耦 const propertyOwner = propertyOwners[property] || (propertyOwners[property] = new AnimationCureOwner()); const curveData = new CurveData(); propertyOwner.target = targetEntity; @@ -314,8 +314,7 @@ export class Animator extends Component { const curve = nextCurves[i]; const { property } = curve; const curveData = destStateData.curveDatas[i]; - const owner = curveData.owner; - const { target, fiexedPoseValue } = owner; + const { target, fiexedPoseValue } = curveData.owner; const { instanceId } = target; _curveDataForPose[i] = curveData; const effectProperty = effectTargetProperty[instanceId] || (effectTargetProperty[instanceId] = []); @@ -341,6 +340,7 @@ export class Animator extends Component { const { instanceId } = curveData.owner.target; if (!effectTargetProperty[instanceId][property]) { _curveDataForPose.push(curveData); + // CM: 没记录 fiexed pose } } } From ae87c5839ae0a294b01bb93dbdf93570ca4b1c6b Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Wed, 9 Jun 2021 14:51:04 +0800 Subject: [PATCH 4/9] refactor: opt code --- packages/core/src/animation/Animator.ts | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/packages/core/src/animation/Animator.ts b/packages/core/src/animation/Animator.ts index 088db24da9..9f19b6b6d4 100644 --- a/packages/core/src/animation/Animator.ts +++ b/packages/core/src/animation/Animator.ts @@ -1,13 +1,3 @@ -<<<<<<< HEAD -======= -import { AnimatorStateTransition } from "./AnimatorTransition"; -import { WrapMode } from "./enums/WrapMode"; -import { Transform } from "../Transform"; -import { InterpolableValueType } from "./enums/InterpolableValueType"; -import { InterpolableValue } from "./KeyFrame"; -import { AnimatorControllerLayer } from "./AnimatorControllerLayer"; -import { AnimatorController } from "./AnimatorController"; ->>>>>>> 2a72642f64ad97a755e2c29b494665831b277490 import { Quaternion, Vector2, Vector3, Vector4 } from "@oasis-engine/math"; import { ignoreClone } from "../clone/CloneManager"; import { Component } from "../Component"; From da4e3da089dc99827e83cc89765e47df71d8df37 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Wed, 9 Jun 2021 16:16:53 +0800 Subject: [PATCH 5/9] refactor: opt crossFade code --- .../core/src/animation/AnimationCureOwner.ts | 2 + packages/core/src/animation/Animator.ts | 40 +++++++++---------- .../core/src/animation/AnimatorLayerData.ts | 1 + 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/packages/core/src/animation/AnimationCureOwner.ts b/packages/core/src/animation/AnimationCureOwner.ts index b1333f3771..2169d50569 100644 --- a/packages/core/src/animation/AnimationCureOwner.ts +++ b/packages/core/src/animation/AnimationCureOwner.ts @@ -8,4 +8,6 @@ export class AnimationCureOwner { target: Entity; defaultValue: InterpolableValue; fiexedPoseValue: InterpolableValue; + crossCurveMark: number = 0; + crossCurveIndex: number; } diff --git a/packages/core/src/animation/Animator.ts b/packages/core/src/animation/Animator.ts index 9f19b6b6d4..aee018da47 100644 --- a/packages/core/src/animation/Animator.ts +++ b/packages/core/src/animation/Animator.ts @@ -54,7 +54,7 @@ export class Animator extends Component { @ignoreClone private _animatorLayersData: AnimatorLayerData[] = []; @ignoreClone - private _mergedCurveIndexList: MergedCurveIndex[] = []; + private _crossCurveIndies: MergedCurveIndex[] = []; @ignoreClone private _transitionForPose: AnimatorStateTransition = new AnimatorStateTransition(); @ignoreClone @@ -121,15 +121,17 @@ export class Animator extends Component { const nextState = animatorController.layers[layerIndex].stateMachine.findStateByName(stateName); if (nextState) { - const { playingStateData, destStateData } = this._getAnimatorLayerData(layerIndex); + const animatorLayerData = this._getAnimatorLayerData(layerIndex); + const crossCurveMark = ++animatorLayerData.crossCurveMark; + const { playingStateData, destStateData } = animatorLayerData; const { state } = playingStateData; const crossFromFixedPose = !state || !this._playing; const isCrossFading = playingStateData.playType === PlayType.IsFading; let transition: AnimatorStateTransition; - let mergeTargetProperty: number[][] = []; + const animationCureOwners = this._animationCureOwners; - const mergedCurveIndexList = this._mergedCurveIndexList; - mergedCurveIndexList.length = 0; + const crossCurveIndices = this._crossCurveIndies; + crossCurveIndices.length = 0; if (isCrossFading) { this._saveFixedPose(playingStateData, destStateData); @@ -163,10 +165,11 @@ export class Animator extends Component { const curveDatas = playingStateData.curveDatas; for (let i = curves.length - 1; i >= 0; i--) { const { instanceId } = curveDatas[i].owner.target; - const { property } = curves[i]; - const mergeProperty = mergeTargetProperty[instanceId] || (mergeTargetProperty[instanceId] = []); - mergeProperty[property] = mergedCurveIndexList.length; - mergedCurveIndexList.push({ + const propertyOwners = animationCureOwners[instanceId] || (animationCureOwners[instanceId] = []); + const propertyOwner = propertyOwners[curves[i].property]; + propertyOwner.crossCurveMark = crossCurveMark; + propertyOwner.crossCurveIndex = crossCurveIndices.length; + crossCurveIndices.push({ curCurveIndex: i, nextCurveIndex: null }); @@ -176,21 +179,18 @@ export class Animator extends Component { const curveDatas = destStateData.curveDatas; for (let i = curves.length - 1; i >= 0; i--) { const { instanceId } = curveDatas[i].owner.target; - const { property } = curves[i]; - const mergeProperty = mergeTargetProperty[instanceId] || (mergeTargetProperty[instanceId] = []); - if (mergeProperty[property] === undefined) { - mergeProperty[property] = mergedCurveIndexList.length; - mergedCurveIndexList.push({ + const propertyOwners = animationCureOwners[instanceId] || (animationCureOwners[instanceId] = []); + const propertyOwner = propertyOwners[curves[i].property]; + if (propertyOwner.crossCurveMark === crossCurveMark) { + crossCurveIndices[propertyOwner.crossCurveIndex].nextCurveIndex = i; + } else { + propertyOwner.crossCurveMark = crossCurveMark; + crossCurveIndices.push({ curCurveIndex: null, nextCurveIndex: i }); - } else { - const index = mergeProperty[property]; - mergedCurveIndexList[index].nextCurveIndex = i; } } - - mergeTargetProperty = null; } this._playing = true; } @@ -612,7 +612,7 @@ export class Animator extends Component { playingStateData.playType = PlayType.IsFinish; } - const mergedCurveIndexList = this._mergedCurveIndexList; + const mergedCurveIndexList = this._crossCurveIndies; const count = mergedCurveIndexList.length; for (let i = count - 1; i >= 0; i--) { const { curCurveIndex, nextCurveIndex } = mergedCurveIndexList[i]; diff --git a/packages/core/src/animation/AnimatorLayerData.ts b/packages/core/src/animation/AnimatorLayerData.ts index f75df4d221..a290ee07fd 100644 --- a/packages/core/src/animation/AnimatorLayerData.ts +++ b/packages/core/src/animation/AnimatorLayerData.ts @@ -7,4 +7,5 @@ import { AnimatorStateData } from "./AnimatorStateData"; export class AnimatorLayerData { playingStateData: AnimatorStateData = new AnimatorStateData(); destStateData: AnimatorStateData = new AnimatorStateData(); + crossCurveMark: number = 0; } From 3b0976e933aba22601e4305759350517e82c60c2 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Wed, 9 Jun 2021 16:20:55 +0800 Subject: [PATCH 6/9] refactor: opt code --- packages/core/src/animation/CurveData.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/core/src/animation/CurveData.ts b/packages/core/src/animation/CurveData.ts index 253588f534..6126db128f 100644 --- a/packages/core/src/animation/CurveData.ts +++ b/packages/core/src/animation/CurveData.ts @@ -8,7 +8,4 @@ import { AnimationCureOwner } from "./AnimationCureOwner"; export class CurveData { owner: AnimationCureOwner; curveData: AnimationClipCurveData; - // target: Entity; - // defaultValue: InterpolableValue; - // fiexedPoseValue: InterpolableValue; } From 57160ed9648b9810d6d46603113df0189af2ace5 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Wed, 9 Jun 2021 19:05:50 +0800 Subject: [PATCH 7/9] refactor: opt code --- .../core/src/animation/AnimationCureOwner.ts | 19 ++ packages/core/src/animation/Animator.ts | 193 +++++++++--------- 2 files changed, 111 insertions(+), 101 deletions(-) diff --git a/packages/core/src/animation/AnimationCureOwner.ts b/packages/core/src/animation/AnimationCureOwner.ts index 2169d50569..a3488bd749 100644 --- a/packages/core/src/animation/AnimationCureOwner.ts +++ b/packages/core/src/animation/AnimationCureOwner.ts @@ -1,13 +1,32 @@ +import { Quaternion, Vector3 } from "@oasis-engine/math"; +import { Component } from "../Component"; import { Entity } from "../Entity"; +import { AnimationClipCurveData } from "./AnimationClipCurveData"; +import { AnimationProperty } from "./enums/AnimationProperty"; import { InterpolableValue } from "./KeyFrame"; /** * @internal */ export class AnimationCureOwner { + animationClopCurveData: AnimationClipCurveData; target: Entity; defaultValue: InterpolableValue; fiexedPoseValue: InterpolableValue; crossCurveMark: number = 0; crossCurveIndex: number; + + saveFixedPoseValue(): void { + switch (this.animationClopCurveData.property) { + case AnimationProperty.Position: + this.target.transform.position.cloneTo(this.fiexedPoseValue); + break; + case AnimationProperty.Rotation: + this.target.transform.rotationQuaternion.cloneTo(this.fiexedPoseValue); + break; + case AnimationProperty.Scale: + this.target.transform.scale.cloneTo(this.fiexedPoseValue); + break; + } + } } diff --git a/packages/core/src/animation/Animator.ts b/packages/core/src/animation/Animator.ts index aee018da47..335356b583 100644 --- a/packages/core/src/animation/Animator.ts +++ b/packages/core/src/animation/Animator.ts @@ -7,6 +7,7 @@ import { AnimationCureOwner } from "./AnimationCureOwner"; import { AnimatorController } from "./AnimatorController"; import { AnimatorControllerLayer } from "./AnimatorControllerLayer"; import { AnimatorLayerData } from "./AnimatorLayerData"; +import { AnimatorState } from "./AnimatorState"; import { AnimatorStateData } from "./AnimatorStateData"; import { AnimatorStateTransition } from "./AnimatorTransition"; import { AnimatorUtils } from "./AnimatorUtils"; @@ -19,6 +20,7 @@ import { WrapMode } from "./enums/WrapMode"; import { InterpolableValue } from "./KeyFrame"; interface MergedCurveIndex { + owner: AnimationCureOwner; curCurveIndex: number; nextCurveIndex: number; } @@ -58,8 +60,6 @@ export class Animator extends Component { @ignoreClone private _transitionForPose: AnimatorStateTransition = new AnimatorStateTransition(); @ignoreClone - private _curveDataForPose: CurveData[] = []; //CM: 简化 - @ignoreClone private _animationCureOwners: AnimationCureOwner[][] = []; /** @@ -122,29 +122,20 @@ export class Animator extends Component { const nextState = animatorController.layers[layerIndex].stateMachine.findStateByName(stateName); if (nextState) { const animatorLayerData = this._getAnimatorLayerData(layerIndex); - const crossCurveMark = ++animatorLayerData.crossCurveMark; const { playingStateData, destStateData } = animatorLayerData; const { state } = playingStateData; const crossFromFixedPose = !state || !this._playing; const isCrossFading = playingStateData.playType === PlayType.IsFading; let transition: AnimatorStateTransition; - const animationCureOwners = this._animationCureOwners; const crossCurveIndices = this._crossCurveIndies; crossCurveIndices.length = 0; - if (isCrossFading) { - this._saveFixedPose(playingStateData, destStateData); - } - destStateData.state = nextState; destStateData.frameTime = 0; destStateData.playType = PlayType.IsCrossing; this._setDefaultValueAndTarget(destStateData); - if (crossFromFixedPose) { - this._saveFixedPose(null, destStateData); - } if (crossFromFixedPose || isCrossFading) { this._animatorLayersData[layerIndex].playingStateData = new AnimatorStateData(); @@ -160,41 +151,91 @@ export class Animator extends Component { transition.duration = nextState.clipEndTime - transition.offset; } - if (!crossFromFixedPose) { - const curves = state.clip._curves; - const curveDatas = playingStateData.curveDatas; - for (let i = curves.length - 1; i >= 0; i--) { - const { instanceId } = curveDatas[i].owner.target; - const propertyOwners = animationCureOwners[instanceId] || (animationCureOwners[instanceId] = []); - const propertyOwner = propertyOwners[curves[i].property]; - propertyOwner.crossCurveMark = crossCurveMark; - propertyOwner.crossCurveIndex = crossCurveIndices.length; - crossCurveIndices.push({ - curCurveIndex: i, - nextCurveIndex: null - }); - } - } - const curves = nextState.clip._curves; - const curveDatas = destStateData.curveDatas; - for (let i = curves.length - 1; i >= 0; i--) { - const { instanceId } = curveDatas[i].owner.target; - const propertyOwners = animationCureOwners[instanceId] || (animationCureOwners[instanceId] = []); - const propertyOwner = propertyOwners[curves[i].property]; - if (propertyOwner.crossCurveMark === crossCurveMark) { - crossCurveIndices[propertyOwner.crossCurveIndex].nextCurveIndex = i; - } else { - propertyOwner.crossCurveMark = crossCurveMark; - crossCurveIndices.push({ - curCurveIndex: null, - nextCurveIndex: i - }); - } + if (isCrossFading) { + this._prepareFiexdPoseCrossFading(animatorLayerData); + } else { + this._prepareCrossFading(animatorLayerData); } } this._playing = true; } + private _prepareCrossFading(animatorLayerData: AnimatorLayerData): void { + const crossCurveIndices = this._crossCurveIndies; + crossCurveIndices.length = 0; + const { playingStateData, destStateData } = animatorLayerData; + const { state } = playingStateData; + const crossCurveMark = ++animatorLayerData.crossCurveMark; + let curves = state.clip._curves; + let curveDatas = playingStateData.curveDatas; + for (let i = curves.length - 1; i >= 0; i--) { + const owner = curveDatas[i].owner; + owner.crossCurveMark = crossCurveMark; + owner.crossCurveIndex = crossCurveIndices.length; + crossCurveIndices.push({ + owner: owner, + curCurveIndex: i, + nextCurveIndex: null + }); + } + + curveDatas = destStateData.curveDatas; + for (let i = curveDatas.length - 1; i >= 0; i--) { + const owner = curveDatas[i].owner; + if (owner.crossCurveMark === crossCurveMark) { + crossCurveIndices[owner.crossCurveIndex].nextCurveIndex = i; + } else { + owner.crossCurveMark = crossCurveMark; + crossCurveIndices.push({ + owner: owner, + curCurveIndex: null, + nextCurveIndex: i + }); + } + } + } + + private _prepareFiexdPoseCrossFading(animatorLayerData: AnimatorLayerData): void { + const { playingStateData, destStateData } = animatorLayerData; + + const crossCurveIndices = this._crossCurveIndies; + crossCurveIndices.length = 0; + + const crossCurveMark = animatorLayerData.crossCurveMark; + + // Save current crossFade curve owner fixed pose. + for (let i = crossCurveIndices.length - 1; i >= 0; i--) { + const crossCurveIndex = crossCurveIndices[i]; + const curCurveIndex = crossCurveIndex.curCurveIndex; + let owner: AnimationCureOwner; + if (curCurveIndex) { + owner = playingStateData.curveDatas[curCurveIndex].owner; + } else { + owner = playingStateData.curveDatas[crossCurveIndex.nextCurveIndex].owner; + } + owner.saveFixedPoseValue(); + crossCurveIndices[owner.crossCurveIndex].nextCurveIndex = null; + } + + // Save dest curve owner fixed pose. + const curveDatas = destStateData.curveDatas; + for (let i = destStateData.curveDatas.length - 1; i >= 0; i--) { + const owner = curveDatas[i].owner; + // Not inclue in last cross fade. + if (owner.crossCurveMark === crossCurveMark) { + crossCurveIndices[owner.crossCurveIndex].nextCurveIndex = i; + } else { + crossCurveIndices.push({ + owner: owner, + curCurveIndex: null, + nextCurveIndex: i + }); + owner.saveFixedPoseValue(); + owner.crossCurveMark = crossCurveMark; + } + } + } + /** * Evaluates the animator component based on deltaTime. * @param deltaTime - The deltaTime when the animation update @@ -299,53 +340,6 @@ export class Animator extends Component { } } - private _saveFixedPose( - playingStateData: AnimatorStateData, - destStateData: AnimatorStateData - ): void { - const { _curveDataForPose } = this; - _curveDataForPose.length = 0; - - //CM: 可否简化 - let effectTargetProperty: boolean[][] = []; - const nextCurves = destStateData.state.clip._curves; - for (let i = nextCurves.length - 1; i >= 0; i--) { - const curve = nextCurves[i]; - const { property } = curve; - const curveData = destStateData.curveDatas[i]; - const { target, fiexedPoseValue } = curveData.owner; - const { instanceId } = target; - _curveDataForPose[i] = curveData; - const effectProperty = effectTargetProperty[instanceId] || (effectTargetProperty[instanceId] = []); - effectProperty[property] = true; - switch (property) { - case AnimationProperty.Position: - target.transform.position.cloneTo(fiexedPoseValue); - break; - case AnimationProperty.Rotation: - target.transform.rotationQuaternion.cloneTo(fiexedPoseValue); - break; - case AnimationProperty.Scale: - target.transform.scale.cloneTo(fiexedPoseValue); - break; - } - } - if (playingStateData) { - const curCurves = playingStateData.state.clip._curves; - for (let i = curCurves.length - 1; i >= 0; i--) { - const curve = curCurves[i]; - const { property } = curve; - const curveData = playingStateData.curveDatas[i]; - const { instanceId } = curveData.owner.target; - if (!effectTargetProperty[instanceId][property]) { - _curveDataForPose.push(curveData); - // CM: 没记录 fiexed pose - } - } - } - effectTargetProperty = null; - } - private _calculateDiff( valueType: InterpolableValueType, property: AnimationProperty, @@ -672,21 +666,18 @@ export class Animator extends Component { destStateData.playType = PlayType.IsPlaying; } - const curveDataForPose = this._curveDataForPose; - const count = curveDataForPose.length; - - for (let i = count - 1; i >= 0; i--) { - const curveData = curveDataForPose[i]; - const { curveData: fixedPoseCurveData } = curveData; - const { target, defaultValue, fiexedPoseValue } = curveData.owner; + const crossCurveIndies = this._crossCurveIndies; - const destCurveData = destStateData.curveDatas[i]; + const curves = nextClip._curves; + for (let i = crossCurveIndies.length - 1; i >= 0; i--) { let calculatedValue: InterpolableValue; - const { type, property } = fixedPoseCurveData; - const { curve } = nextClip._curves[i]; - if (destCurveData) { - const val = curve.evaluate(frameTime); - calculatedValue = this._getCrossFadeValue(target, type, property, fiexedPoseValue, val, crossWeight); + + const crossCurveIndex = crossCurveIndies[i]; + const { target, fiexedPoseValue, defaultValue, animationClopCurveData } = crossCurveIndex.owner; + const { type, property } = animationClopCurveData; + if (crossCurveIndex.nextCurveIndex) { + const value = curves[i].curve.evaluate(frameTime); + calculatedValue = this._getCrossFadeValue(target, type, property, fiexedPoseValue, value, crossWeight); } else { calculatedValue = this._getCrossFadeValue(target, type, property, fiexedPoseValue, defaultValue, crossWeight); } From 2492fd466933f2ba5f26cb782aa5ea1e991d5fa7 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Wed, 9 Jun 2021 19:37:41 +0800 Subject: [PATCH 8/9] refactor: opt Animator code --- packages/core/src/animation/Animator.ts | 89 +++++++++---------- .../core/src/animation/AnimatorStateData.ts | 2 +- packages/core/src/animation/CurveData.ts | 2 +- 3 files changed, 45 insertions(+), 48 deletions(-) diff --git a/packages/core/src/animation/Animator.ts b/packages/core/src/animation/Animator.ts index 335356b583..5fb6204e89 100644 --- a/packages/core/src/animation/Animator.ts +++ b/packages/core/src/animation/Animator.ts @@ -19,7 +19,7 @@ import { PlayType } from "./enums/PlayType"; import { WrapMode } from "./enums/WrapMode"; import { InterpolableValue } from "./KeyFrame"; -interface MergedCurveIndex { +interface CrossCurveData { owner: AnimationCureOwner; curCurveIndex: number; nextCurveIndex: number; @@ -56,7 +56,7 @@ export class Animator extends Component { @ignoreClone private _animatorLayersData: AnimatorLayerData[] = []; @ignoreClone - private _crossCurveIndies: MergedCurveIndex[] = []; + private _crossCurveData: CrossCurveData[] = []; @ignoreClone private _transitionForPose: AnimatorStateTransition = new AnimatorStateTransition(); @ignoreClone @@ -128,8 +128,8 @@ export class Animator extends Component { const isCrossFading = playingStateData.playType === PlayType.IsFading; let transition: AnimatorStateTransition; - const crossCurveIndices = this._crossCurveIndies; - crossCurveIndices.length = 0; + const crossCurveData = this._crossCurveData; + crossCurveData.length = 0; destStateData.state = nextState; destStateData.frameTime = 0; @@ -161,32 +161,33 @@ export class Animator extends Component { } private _prepareCrossFading(animatorLayerData: AnimatorLayerData): void { - const crossCurveIndices = this._crossCurveIndies; - crossCurveIndices.length = 0; - const { playingStateData, destStateData } = animatorLayerData; - const { state } = playingStateData; + // Reset cross fading data. + const crossCurveData = this._crossCurveData; + crossCurveData.length = 0; const crossCurveMark = ++animatorLayerData.crossCurveMark; - let curves = state.clip._curves; - let curveDatas = playingStateData.curveDatas; - for (let i = curves.length - 1; i >= 0; i--) { - const owner = curveDatas[i].owner; + + // Add playing cross curve data. + const playingDataCollection = animatorLayerData.playingStateData.curveDataCollection; + for (let i = playingDataCollection.length - 1; i >= 0; i--) { + const owner = playingDataCollection[i].owner; owner.crossCurveMark = crossCurveMark; - owner.crossCurveIndex = crossCurveIndices.length; - crossCurveIndices.push({ + owner.crossCurveIndex = crossCurveData.length; + crossCurveData.push({ owner: owner, curCurveIndex: i, nextCurveIndex: null }); } - curveDatas = destStateData.curveDatas; - for (let i = curveDatas.length - 1; i >= 0; i--) { - const owner = curveDatas[i].owner; + // Add dest cross curve data. + const destDataCollection = animatorLayerData.destStateData.curveDataCollection; + for (let i = destDataCollection.length - 1; i >= 0; i--) { + const owner = destDataCollection[i].owner; if (owner.crossCurveMark === crossCurveMark) { - crossCurveIndices[owner.crossCurveIndex].nextCurveIndex = i; + crossCurveData[owner.crossCurveIndex].nextCurveIndex = i; } else { owner.crossCurveMark = crossCurveMark; - crossCurveIndices.push({ + crossCurveData.push({ owner: owner, curCurveIndex: null, nextCurveIndex: i @@ -196,36 +197,32 @@ export class Animator extends Component { } private _prepareFiexdPoseCrossFading(animatorLayerData: AnimatorLayerData): void { - const { playingStateData, destStateData } = animatorLayerData; - - const crossCurveIndices = this._crossCurveIndies; - crossCurveIndices.length = 0; - + const crossCurveData = this._crossCurveData; const crossCurveMark = animatorLayerData.crossCurveMark; - // Save current crossFade curve owner fixed pose. - for (let i = crossCurveIndices.length - 1; i >= 0; i--) { - const crossCurveIndex = crossCurveIndices[i]; - const curCurveIndex = crossCurveIndex.curCurveIndex; + // Save current cross curve data owner fixed pose. + for (let i = crossCurveData.length - 1; i >= 0; i--) { + const dataItem = crossCurveData[i]; + const curCurveIndex = dataItem.curCurveIndex; let owner: AnimationCureOwner; if (curCurveIndex) { - owner = playingStateData.curveDatas[curCurveIndex].owner; + owner = animatorLayerData.playingStateData.curveDataCollection[curCurveIndex].owner; } else { - owner = playingStateData.curveDatas[crossCurveIndex.nextCurveIndex].owner; + owner = animatorLayerData.playingStateData.curveDataCollection[dataItem.nextCurveIndex].owner; } owner.saveFixedPoseValue(); - crossCurveIndices[owner.crossCurveIndex].nextCurveIndex = null; + crossCurveData[owner.crossCurveIndex].nextCurveIndex = null; } // Save dest curve owner fixed pose. - const curveDatas = destStateData.curveDatas; - for (let i = destStateData.curveDatas.length - 1; i >= 0; i--) { - const owner = curveDatas[i].owner; + const curveDataCollection = animatorLayerData.destStateData.curveDataCollection; + for (let i = curveDataCollection.length - 1; i >= 0; i--) { + const owner = curveDataCollection[i].owner; // Not inclue in last cross fade. if (owner.crossCurveMark === crossCurveMark) { - crossCurveIndices[owner.crossCurveIndex].nextCurveIndex = i; + crossCurveData[owner.crossCurveIndex].nextCurveIndex = i; } else { - crossCurveIndices.push({ + crossCurveData.push({ owner: owner, curCurveIndex: null, nextCurveIndex: i @@ -313,13 +310,13 @@ export class Animator extends Component { const targetEntity = this.entity.findByPath(relativePath); const { instanceId } = targetEntity; const propertyOwners = animationCureOwners[instanceId] || (animationCureOwners[instanceId] = []); - if (!stateData.curveDatas[i]) { + if (!stateData.curveDataCollection[i]) { //CM: 两种对象初始化时机解耦 const propertyOwner = propertyOwners[property] || (propertyOwners[property] = new AnimationCureOwner()); const curveData = new CurveData(); propertyOwner.target = targetEntity; curveData.owner = propertyOwner; - curveData.curveData = curve; + curveData.clipCurveData = curve; switch (property) { case AnimationProperty.Position: @@ -335,7 +332,7 @@ export class Animator extends Component { propertyOwner.fiexedPoseValue = new Vector3(); break; } - stateData.curveDatas[i] = curveData; + stateData.curveDataCollection[i] = curveData; } } } @@ -563,7 +560,7 @@ export class Animator extends Component { for (let i = curves.length - 1; i >= 0; i--) { const { curve, type, property } = curves[i]; const value = curve.evaluate(frameTime); - const { target, defaultValue } = playingStateData.curveDatas[i].owner; + const { target, defaultValue } = playingStateData.curveDataCollection[i].owner; if (isFirstLayer) { this._applyClipValue(target, type, property, defaultValue, value, 1.0); } else { @@ -606,7 +603,7 @@ export class Animator extends Component { playingStateData.playType = PlayType.IsFinish; } - const mergedCurveIndexList = this._crossCurveIndies; + const mergedCurveIndexList = this._crossCurveData; const count = mergedCurveIndexList.length; for (let i = count - 1; i >= 0; i--) { const { curCurveIndex, nextCurveIndex } = mergedCurveIndexList[i]; @@ -616,18 +613,18 @@ export class Animator extends Component { const curFrameTime = playingStateData.state._getTheRealFrameTime(playingStateData.frameTime); const curVal = curCurve.evaluate(curFrameTime); const destVal = nextCurve.evaluate(frameTime); - const { target, defaultValue } = playingStateData.curveDatas[curCurveIndex].owner; + const { target, defaultValue } = playingStateData.curveDataCollection[curCurveIndex].owner; const calculatedValue = this._getCrossFadeValue(target, type, property, curVal, destVal, crossWeight); this._applyClipValue(target, type, property, defaultValue, calculatedValue, 1); } else if (curCurveIndex >= 0) { const { curve: curCurve, type, property } = curClip._curves[curCurveIndex]; - const { target, defaultValue } = playingStateData.curveDatas[curCurveIndex].owner; + const { target, defaultValue } = playingStateData.curveDataCollection[curCurveIndex].owner; const curFrameTime = playingStateData.state._getTheRealFrameTime(playingStateData.frameTime); const curVal = curCurve.evaluate(curFrameTime); const calculatedValue = this._getCrossFadeValue(target, type, property, defaultValue, curVal, 1 - crossWeight); this._applyClipValue(target, type, property, defaultValue, calculatedValue, weight); } else { - const { target, defaultValue } = destStateData.curveDatas[nextCurveIndex].owner; + const { target, defaultValue } = destStateData.curveDataCollection[nextCurveIndex].owner; const { curve, type, property } = nextClip._curves[nextCurveIndex]; const val = curve.evaluate(frameTime); const calculatedValue = this._getCrossFadeValue(target, type, property, defaultValue, val, crossWeight); @@ -666,7 +663,7 @@ export class Animator extends Component { destStateData.playType = PlayType.IsPlaying; } - const crossCurveIndies = this._crossCurveIndies; + const crossCurveIndies = this._crossCurveData; const curves = nextClip._curves; for (let i = crossCurveIndies.length - 1; i >= 0; i--) { @@ -696,7 +693,7 @@ export class Animator extends Component { const curves = clip._curves; for (let i = curves.length - 1; i >= 0; i--) { const curve = curves[i]; - const { owner } = playingStateData.curveDatas[i]; + const { owner } = playingStateData.curveDataCollection[i]; const { transform } = owner.target; switch (curve.property) { case AnimationProperty.Position: diff --git a/packages/core/src/animation/AnimatorStateData.ts b/packages/core/src/animation/AnimatorStateData.ts index 0103a8ffdd..64d44d556e 100644 --- a/packages/core/src/animation/AnimatorStateData.ts +++ b/packages/core/src/animation/AnimatorStateData.ts @@ -10,5 +10,5 @@ export class AnimatorStateData { state: AnimatorState; frameTime: number; playType: PlayType; - curveDatas: CurveData[] = []; + curveDataCollection: CurveData[] = []; } diff --git a/packages/core/src/animation/CurveData.ts b/packages/core/src/animation/CurveData.ts index 6126db128f..518d8eec92 100644 --- a/packages/core/src/animation/CurveData.ts +++ b/packages/core/src/animation/CurveData.ts @@ -7,5 +7,5 @@ import { AnimationCureOwner } from "./AnimationCureOwner"; */ export class CurveData { owner: AnimationCureOwner; - curveData: AnimationClipCurveData; + clipCurveData: AnimationClipCurveData; } From cf35b577611fd2d13e70ec96b89e9e9c5676d081 Mon Sep 17 00:00:00 2001 From: GuoLei1990 Date: Wed, 9 Jun 2021 19:47:59 +0800 Subject: [PATCH 9/9] refactor: opt code --- packages/core/src/animation/Animator.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/core/src/animation/Animator.ts b/packages/core/src/animation/Animator.ts index 5fb6204e89..8c3462ba31 100644 --- a/packages/core/src/animation/Animator.ts +++ b/packages/core/src/animation/Animator.ts @@ -7,7 +7,6 @@ import { AnimationCureOwner } from "./AnimationCureOwner"; import { AnimatorController } from "./AnimatorController"; import { AnimatorControllerLayer } from "./AnimatorControllerLayer"; import { AnimatorLayerData } from "./AnimatorLayerData"; -import { AnimatorState } from "./AnimatorState"; import { AnimatorStateData } from "./AnimatorStateData"; import { AnimatorStateTransition } from "./AnimatorTransition"; import { AnimatorUtils } from "./AnimatorUtils"; @@ -145,8 +144,9 @@ export class Animator extends Component { transition = state.addTransition(nextState); } - transition.duration = nextState.clip.length * normalizedTransitionDuration; - transition.offset = nextState.clip.length * normalizedTimeOffset; + const clipLength = nextState.clip.length; + transition.duration = clipLength * normalizedTransitionDuration; + transition.offset = clipLength * normalizedTimeOffset; if (transition.duration > nextState.clipEndTime - transition.offset) { transition.duration = nextState.clipEndTime - transition.offset; }