Skip to content

Commit

Permalink
Merge pull request #1315 from actnwit/fix/animation-blending
Browse files Browse the repository at this point in the history
fix: animation blending ratio
  • Loading branch information
emadurandal authored Jan 3, 2024
2 parents 1edc009 + 8c3d3c5 commit 7010119
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
24 changes: 7 additions & 17 deletions src/foundation/components/Animation/AnimationComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export class AnimationComponent extends Component {
// The name of the current Active Track
private __firstActiveAnimationTrackName?: AnimationTrackName;
private __secondActiveAnimationTrackName?: AnimationTrackName; // for animation blending
private __interpolationRatioBtwFirstAndSecond = 0; // the value range is [0,1]
public animationBlendingRatio = 0; // the value range is [0,1]

// Animation Data of each AnimationComponent
private __animationTracks: Map<AnimationTrackName, AnimationTrack> = new Map();
Expand Down Expand Up @@ -136,10 +136,7 @@ export class AnimationComponent extends Component {
let time = this.time;

// process the first active animation track
if (
Is.exist(this.__firstActiveAnimationTrackName) &&
this.__interpolationRatioBtwFirstAndSecond < 1
) {
if (Is.exist(this.__firstActiveAnimationTrackName) && this.animationBlendingRatio < 1) {
if (this.useGlobalTime) {
if (this.isLoop) {
const duration = this.getEndInputValueOfAnimation(this.__firstActiveAnimationTrackName!);
Expand Down Expand Up @@ -186,10 +183,7 @@ export class AnimationComponent extends Component {
}

// process the second active animation track, and blending with the first's one
if (
Is.exist(this.__secondActiveAnimationTrackName) &&
this.__interpolationRatioBtwFirstAndSecond > 0
) {
if (Is.exist(this.__secondActiveAnimationTrackName) && this.animationBlendingRatio > 0) {
if (this.useGlobalTime) {
if (this.isLoop) {
const duration = this.getEndInputValueOfAnimation(this.__secondActiveAnimationTrackName!);
Expand All @@ -211,29 +205,29 @@ export class AnimationComponent extends Component {
this.__transformComponent!.localRotation = Quaternion.qlerp(
this.__transformComponent!.localRotationInner,
quatOf2nd,
this.__interpolationRatioBtwFirstAndSecond
this.animationBlendingRatio
);
} else if (i === AnimationAttribute.Translate.index) {
const vec3Of2nd = Vector3.fromCopyArray3(value as Array3<number>);
this.__transformComponent!.localPosition = Vector3.lerp(
this.__transformComponent!.localPositionInner,
vec3Of2nd,
this.__interpolationRatioBtwFirstAndSecond
this.animationBlendingRatio
);
} else if (i === AnimationAttribute.Scale.index) {
const vec3of2nd = Vector3.fromCopyArray3(value as Array3<number>);
this.__transformComponent!.localScale = Vector3.lerp(
this.__transformComponent!.localScaleInner,
vec3of2nd,
this.__interpolationRatioBtwFirstAndSecond
this.animationBlendingRatio
);
} else if (i === AnimationAttribute.Weights.index) {
const weightsOf2nd = value;
for (let i = 0; i < weightsOf2nd.length; i++) {
this.__blendShapeComponent!.weights[i] = MathUtil.lerp(
this.__blendShapeComponent!.weights[i],
weightsOf2nd[i],
this.__interpolationRatioBtwFirstAndSecond
this.animationBlendingRatio
);
}
} else if (i === AnimationAttribute.Effekseer.index) {
Expand Down Expand Up @@ -279,10 +273,6 @@ export class AnimationComponent extends Component {
}
}

set interpolationRatioBtwFirstAndSecond(ratio: number) {
this.__interpolationRatioBtwFirstAndSecond = ratio;
}

getActiveAnimationTrack() {
return this.__firstActiveAnimationTrackName;
}
Expand Down
11 changes: 11 additions & 0 deletions src/foundation/components/SceneGraph/SceneGraphComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,17 @@ export class SceneGraphComponent extends Component {
}
}

setAnimationBlendingRatio(ratio: number) {
const animationComponent = this.entity.tryToGetAnimation();
if (animationComponent != null) {
animationComponent.animationBlendingRatio = ratio;
}
const children = this.children;
for (let i = 0; i < children.length; i++) {
children[i].setAnimationBlendingRatio(ratio);
}
}

_destroy() {
this.__aabbGizmo?._destroy();
this.__locatorGizmo?._destroy();
Expand Down

0 comments on commit 7010119

Please sign in to comment.