Browse Source

api and configuration changes

Raanan Weber 7 years ago
parent
commit
467b635a84
3 changed files with 34 additions and 10 deletions
  1. 20 6
      Viewer/src/model/modelAnimation.ts
  2. 12 1
      Viewer/src/model/viewerModel.ts
  3. 2 3
      Viewer/src/viewer/viewer.ts

+ 20 - 6
Viewer/src/model/modelAnimation.ts

@@ -7,8 +7,10 @@ export enum AnimationPlayMode {
 
 
 export enum AnimationState {
 export enum AnimationState {
     INIT,
     INIT,
-    STARTED,
-    STOPPED
+    PLAYING,
+    PAUSED,
+    STOPPED,
+    ENDED
 }
 }
 
 
 export interface IModelAnimation extends IDisposable {
 export interface IModelAnimation extends IDisposable {
@@ -33,6 +35,11 @@ export class GroupModelAnimation implements IModelAnimation {
     constructor(private _animationGroup: AnimationGroup) {
     constructor(private _animationGroup: AnimationGroup) {
         this._state = AnimationState.INIT;
         this._state = AnimationState.INIT;
         this._playMode = AnimationPlayMode.LOOP;
         this._playMode = AnimationPlayMode.LOOP;
+
+        this._animationGroup.onAnimationEndObservable.add(() => {
+            this.stop();
+            this._state = AnimationState.ENDED;
+        })
     }
     }
 
 
     public get name() {
     public get name() {
@@ -76,8 +83,11 @@ export class GroupModelAnimation implements IModelAnimation {
 
 
         this._playMode = value;
         this._playMode = value;
 
 
-        if (this.state === AnimationState.STARTED) {
-            this.start();
+        if (this.state === AnimationState.PLAYING) {
+            this._animationGroup.play(this._playMode === AnimationPlayMode.LOOP);
+        } else {
+            this._animationGroup.reset();
+            this._state = AnimationState.INIT;
         }
         }
     }
     }
 
 
@@ -90,18 +100,22 @@ export class GroupModelAnimation implements IModelAnimation {
     }
     }
 
 
     goToFrame(frameNumber: number) {
     goToFrame(frameNumber: number) {
-        this._animationGroup.goToFrame(frameNumber);
+        // this._animationGroup.goToFrame(frameNumber);
+        this._animationGroup['_animatables'].forEach(a => {
+            a.goToFrame(frameNumber);
+        })
     }
     }
 
 
     public start() {
     public start() {
         this._animationGroup.start(this.playMode === AnimationPlayMode.LOOP, this.speedRatio);
         this._animationGroup.start(this.playMode === AnimationPlayMode.LOOP, this.speedRatio);
         if (this._animationGroup.isStarted) {
         if (this._animationGroup.isStarted) {
-            this._state = AnimationState.STARTED;
+            this._state = AnimationState.PLAYING;
         }
         }
     }
     }
 
 
     pause() {
     pause() {
         this._animationGroup.pause();
         this._animationGroup.pause();
+        this._state = AnimationState.PAUSED;
     }
     }
 
 
     public stop() {
     public stop() {

+ 12 - 1
Viewer/src/model/viewerModel.ts

@@ -64,6 +64,9 @@ export class ViewerModel implements IDisposable {
 
 
         let plugin = this._modelConfiguration.loader;
         let plugin = this._modelConfiguration.loader;
 
 
+        //temp solution for animation group handling
+        let animationsArray = this._scene.animationGroups.slice();
+
         this.loader = SceneLoader.ImportMesh(undefined, base, filename, this._scene, (meshes, particleSystems, skeletons) => {
         this.loader = SceneLoader.ImportMesh(undefined, base, filename, this._scene, (meshes, particleSystems, skeletons) => {
             meshes.forEach(mesh => {
             meshes.forEach(mesh => {
                 Tags.AddTagsTo(mesh, "viewerMesh");
                 Tags.AddTagsTo(mesh, "viewerMesh");
@@ -73,10 +76,18 @@ export class ViewerModel implements IDisposable {
             this.skeletons = skeletons;
             this.skeletons = skeletons;
 
 
             // check if this is a gltf loader and load the animations
             // check if this is a gltf loader and load the animations
-            if (this.loader['_loader'] && this.loader['_loader']['_gltf'] && this.loader['_loader']['_gltf'].animations) {
+            /*if (this.loader['_loader'] && this.loader['_loader']['_gltf'] && this.loader['_loader']['_gltf'].animations) {
                 this.loader['_loader']['_gltf'].animations.forEach(animation => {
                 this.loader['_loader']['_gltf'].animations.forEach(animation => {
                     this._animations.push(new GroupModelAnimation(animation._babylonAnimationGroup));
                     this._animations.push(new GroupModelAnimation(animation._babylonAnimationGroup));
                 });
                 });
+            }*/
+            if (this.loader.name === 'gltf') {
+                this._scene.animationGroups.forEach(ag => {
+                    // add animations that didn't exist before
+                    if (animationsArray.indexOf(ag) === -1) {
+                        this._animations.push(new GroupModelAnimation(ag));
+                    }
+                })
             } else {
             } else {
                 skeletons.forEach((skeleton, idx) => {
                 skeletons.forEach((skeleton, idx) => {
                     let ag = new BABYLON.AnimationGroup("animation-" + idx, this._scene);
                     let ag = new BABYLON.AnimationGroup("animation-" + idx, this._scene);

+ 2 - 3
Viewer/src/viewer/viewer.ts

@@ -21,9 +21,6 @@ export abstract class AbstractViewer {
 
 
     /**
     /**
      * The last loader used to load a model. 
      * The last loader used to load a model. 
-     * 
-     * @type {(ISceneLoaderPlugin | ISceneLoaderPluginAsync)}
-     * @memberof AbstractViewer
      */
      */
     public lastUsedLoader: ISceneLoaderPlugin | ISceneLoaderPluginAsync;
     public lastUsedLoader: ISceneLoaderPlugin | ISceneLoaderPluginAsync;
 
 
@@ -71,6 +68,7 @@ export abstract class AbstractViewer {
         this.onLoaderInitObservable = new Observable();
         this.onLoaderInitObservable = new Observable();
 
 
         this.registeredOnBeforerenderFunctions = [];
         this.registeredOnBeforerenderFunctions = [];
+        this.models = [];
 
 
         // add this viewer to the viewer manager
         // add this viewer to the viewer manager
         viewerManager.addViewer(this);
         viewerManager.addViewer(this);
@@ -795,6 +793,7 @@ export abstract class AbstractViewer {
             return new Promise<ViewerModel>((resolve, reject) => {
             return new Promise<ViewerModel>((resolve, reject) => {
                 // at this point, configuration.model is an object, not a string
                 // at this point, configuration.model is an object, not a string
                 let model = new ViewerModel(<IModelConfiguration>this.configuration.model, this.scene);
                 let model = new ViewerModel(<IModelConfiguration>this.configuration.model, this.scene);
+                this.models.push(model);
                 this.lastUsedLoader = model.loader;
                 this.lastUsedLoader = model.loader;
                 model.onLoadedObservable.add((model) => {
                 model.onLoadedObservable.add((model) => {
                     resolve(model);
                     resolve(model);