Skip to content
This repository has been archived by the owner on Mar 5, 2021. It is now read-only.

Support for Multiple Animations (modelrender) #76

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
29 changes: 27 additions & 2 deletions plugins/default/model/components/ModelRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ interface Animation {
keyFrames: AnimationKeyFrames;
}

interface AnimationItem {
name: string;
time?: number;
looping?: boolean;
ignoreBoneNameFilter?: RegExp;
}

function getInterpolationData(keyFrames: any[], time: number) {
let prevKeyFrame = keyFrames[keyFrames.length - 1];

Expand Down Expand Up @@ -287,6 +294,24 @@ export default class ModelRenderer extends SupEngine.ActorComponent {
return;
}

setMultipleAnimations(newAnimationItemList: Array<AnimationItem>) {
for (let i = 0; i < newAnimationItemList.length; i++) {
let newAnimationItem = newAnimationItemList[i];
let newAnimation = this.animationsByName[newAnimationItem.name];
if (newAnimation == null) throw new Error(`Animation ${newAnimationItem.name} doesn't exist`);
this.animation = newAnimation;
this.animationLooping = newAnimationItem.looping ? true : false;
let time = newAnimationItem.time;
if (typeof time === "number") {
if (time < 0 || time > this.getAnimationDuration()) throw new Error("Invalid time");
this.animationTimer = time * this.actor.gameInstance.framesPerSecond;
}
this.isAnimationPlaying = true;
this.updatePose(newAnimationItem.ignoreBoneNameFilter);
}
return;
}

getAnimation(): string { return (this.animation != null) ? this.animation.name : null; }

setAnimationTime(time: number) {
Expand Down Expand Up @@ -341,7 +366,7 @@ export default class ModelRenderer extends SupEngine.ActorComponent {
return { position, orientation, scale };
}

updatePose() {
updatePose(ignoreBoneNameFilter?: RegExp) {
this.hasPoseBeenUpdated = true;

// TODO: this.asset.speedMultiplier
Expand All @@ -361,7 +386,7 @@ export default class ModelRenderer extends SupEngine.ActorComponent {
for (let i = 0; i < (<THREE.SkinnedMesh>this.threeMesh).skeleton.bones.length; i++) {
let bone = (<THREE.SkinnedMesh>this.threeMesh).skeleton.bones[i];
let boneKeyFrames = this.animation.keyFrames[bone.name];
if (boneKeyFrames == null) continue;
if (boneKeyFrames == null || (ignoreBoneNameFilter && ignoreBoneNameFilter.test(bone.name))) continue;

if (boneKeyFrames.translation != null) {
let { prevKeyFrame, nextKeyFrame, t } = getInterpolationData(boneKeyFrames.translation, time);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
interface AnimationItem {
name: string;
time?: number;
looping?: boolean;
ignoreBoneNameFilter?: RegExp;
}

declare namespace Sup {
class ModelRenderer extends ActorComponent {
constructor(actor: Actor, pathOrAsset?: string|Model);
Expand All @@ -23,6 +30,7 @@ declare namespace Sup {
setAnimationTime(time: number): ModelRenderer;
getAnimationTime(): number;
getAnimationDuration(): number;
setMultipleAnimations(animationItemList: Array<AnimationItem>): ModelRenderer;

isAnimationPlaying(): boolean;
playAnimation(looping?: boolean): ModelRenderer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ namespace Sup {
}

setAnimation(animationName, looping) { this.__inner.setAnimation(animationName, looping); return this }
setMultipleAnimations(animationItemList) { this.__inner.setMultipleAnimations(animationItemList); return this }
getAnimation() { return this.__inner.getAnimation() }
setAnimationTime(time) { this.__inner.setAnimationTime(time); return this }
getAnimationTime() { return this.__inner.getAnimationTime() }
Expand Down