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 blending ratio #1315

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading