Browse Source

current frame, fps and current animation
It is now impossible to play two animations at the same time.

Raanan Weber 7 years ago
parent
commit
1c51062c8d
2 changed files with 33 additions and 11 deletions
  1. 10 0
      Viewer/src/model/modelAnimation.ts
  2. 23 11
      Viewer/src/model/viewerModel.ts

+ 10 - 0
Viewer/src/model/modelAnimation.ts

@@ -17,6 +17,8 @@ export interface IModelAnimation extends IDisposable {
     readonly state: AnimationState;
     readonly name: string;
     readonly frames: number;
+    readonly currentFrame: number;
+    readonly fps: number;
     speedRatio: number;
     playMode: AnimationPlayMode;
     start();
@@ -72,6 +74,14 @@ export class GroupModelAnimation implements IModelAnimation {
         return Math.max.apply(null, animationFrames);
     }
 
+    public get currentFrame(): number {
+        return this._animationGroup['_animatables'][0].getAnimations()[0].currentFrame;
+    }
+
+    public get fps(): number {
+        return this._animationGroup['_animatables'][0].getAnimations()[0].animation.framePerSecond;
+    }
+
     public get playMode(): AnimationPlayMode {
         return this._playMode;
     }

+ 23 - 11
Viewer/src/model/viewerModel.ts

@@ -10,6 +10,7 @@ export class ViewerModel implements IDisposable {
     public meshes: Array<AbstractMesh>;
     public particleSystems: Array<ParticleSystem>;
     public skeletons: Array<Skeleton>;
+    public currentAnimation: IModelAnimation;
 
     public onLoadedObservable: Observable<ViewerModel>;
     public onLoadProgressObservable: Observable<SceneLoaderProgressEvent>;
@@ -35,15 +36,15 @@ export class ViewerModel implements IDisposable {
         }
     }
 
-    public getAnimations() {
-        return this._animations;
-    }
+    //public getAnimations() {
+    //    return this._animations;
+    //}
 
     public getAnimationNames() {
         return this._animations.map(a => a.name);
     }
 
-    public getAnimationByName(name: string): Nullable<IModelAnimation> {
+    protected _getAnimationByName(name: string): Nullable<IModelAnimation> {
         // can't use .find, noe available on IE
         let filtered = this._animations.filter(a => a.name === name);
         // what the next line means - if two animations have the same name, they will not be returned!
@@ -54,6 +55,20 @@ export class ViewerModel implements IDisposable {
         }
     }
 
+    public playAnimation(name: string): IModelAnimation {
+        let animation = this._getAnimationByName(name);
+        if (animation) {
+            if (this.currentAnimation) {
+                this.currentAnimation.stop();
+            }
+            this.currentAnimation = animation;
+            animation.start();
+            return animation;
+        } else {
+            throw new Error("aniamtion not found - " + name);
+        }
+    }
+
     private _initLoad() {
         if (!this._modelConfiguration || !this._modelConfiguration.url) {
             return Tools.Error("No model URL to load.");
@@ -106,13 +121,10 @@ export class ViewerModel implements IDisposable {
                         a.playMode = AnimationPlayMode.ONCE;
                     });
                 }
-                if (this._modelConfiguration.animation.autoStart) {
-
-                    let animation = this._modelConfiguration.animation.autoStart === true ?
-                        this._animations[0] : this.getAnimationByName(this._modelConfiguration.animation.autoStart);
-                    if (animation) {
-                        animation.start();
-                    }
+                if (this._modelConfiguration.animation.autoStart && this._animations.length) {
+                    let animationName = this._modelConfiguration.animation.autoStart === true ?
+                        this._animations[0].name : this._modelConfiguration.animation.autoStart;
+                    this.playAnimation(animationName);
                 }
             }