Bläddra i källkod

Merge pull request #3984 from RaananW/small-feature-viewer

(viewer) add model on init, remove from scene when disposed.
Raanan Weber 7 år sedan
förälder
incheckning
6bf3fcd686

+ 1 - 1
Viewer/src/model/modelLoader.ts

@@ -31,7 +31,7 @@ export class ModelLoader {
      */
     public load(modelConfiguration: IModelConfiguration): ViewerModel {
 
-        const model = new ViewerModel(this._viewer.scene, modelConfiguration);
+        const model = new ViewerModel(this._viewer, modelConfiguration);
 
         if (!modelConfiguration.url) {
             model.state = ModelState.ERROR;

+ 6 - 2
Viewer/src/model/viewerModel.ts

@@ -3,6 +3,7 @@ import { IModelConfiguration } from "../configuration/configuration";
 import { IModelAnimation, GroupModelAnimation, AnimationPlayMode } from "./modelAnimation";
 
 import * as deepmerge from '../../assets/deepmerge.min.js';
+import { AbstractViewer } from "..";
 
 
 export enum ModelState {
@@ -78,7 +79,7 @@ export class ViewerModel implements IDisposable {
     private _loadedUrl: string;
     private _modelConfiguration: IModelConfiguration;
 
-    constructor(private _scene: Scene, modelConfiguration: IModelConfiguration) {
+    constructor(private _viewer: AbstractViewer, modelConfiguration: IModelConfiguration) {
         this.onLoadedObservable = new Observable();
         this.onLoadErrorObservable = new Observable();
         this.onLoadProgressObservable = new Observable();
@@ -89,6 +90,8 @@ export class ViewerModel implements IDisposable {
         this._animations = [];
         //create a copy of the configuration to make sure it doesn't change even after it is changed in the viewer
         this._modelConfiguration = deepmerge({}, modelConfiguration);
+
+        this._viewer.models.push(this);
     }
 
     /**
@@ -128,7 +131,7 @@ export class ViewerModel implements IDisposable {
         // check if this is not a gltf loader and init the animations
         if (this.loader.name !== 'gltf') {
             this.skeletons.forEach((skeleton, idx) => {
-                let ag = new AnimationGroup("animation-" + idx, this._scene);
+                let ag = new AnimationGroup("animation-" + idx, this._viewer.scene);
                 skeleton.getAnimatables().forEach(a => {
                     if (a.animations[0]) {
                         ag.addTargetedAnimation(a.animations[0], a);
@@ -327,5 +330,6 @@ export class ViewerModel implements IDisposable {
         this._animations.length = 0;
         this.meshes.forEach(m => m.dispose());
         this.meshes.length = 0;
+        this._viewer.models.splice(this._viewer.models.indexOf(this), 1);
     }
 }

+ 0 - 1
Viewer/src/viewer/viewer.ts

@@ -845,7 +845,6 @@ export abstract class AbstractViewer {
             this.models.length = 0;
         }
 
-        this.models.push(model);
         this.lastUsedLoader = model.loader;
         model.onLoadErrorObservable.add((errorObject) => {
             this.onModelLoadErrorObservable.notifyObserversWithPromise(errorObject);

+ 1 - 1
dist/preview release/viewer/babylon.viewer.d.ts

@@ -447,7 +447,7 @@ declare module BabylonViewer {
     }
 
     export class ViewerModel {
-        constructor(scene: BABYLON.Scene, modelConfiguration: IModelConfiguration);
+        constructor(viewer: AbstractViewer, modelConfiguration: IModelConfiguration);
         loader: BABYLON.ISceneLoaderPlugin | BABYLON.ISceneLoaderPluginAsync;
         meshes: Array<BABYLON.AbstractMesh>;
         rootMesh: BABYLON.AbstractMesh;