Browse Source

create viewermodel from objects

Raanan Weber 7 years ago
parent
commit
49680c1a40

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

@@ -16,7 +16,7 @@ export class ViewerModel implements IDisposable {
     public onLoadProgressObservable: Observable<SceneLoaderProgressEvent>;
     public onLoadErrorObservable: Observable<{ message: string; exception: any }>;
 
-    constructor(private _modelConfiguration: IModelConfiguration, private _scene: Scene, disableAutoLoad = false) {
+    constructor(private _scene: Scene, private _modelConfiguration?: IModelConfiguration, disableAutoLoad = false) {
         this.onLoadedObservable = new Observable();
         this.onLoadErrorObservable = new Observable();
         this.onLoadProgressObservable = new Observable();
@@ -36,9 +36,9 @@ export class ViewerModel implements IDisposable {
         }
     }
 
-    //public getAnimations() {
-    //    return this._animations;
-    //}
+    public getAnimations() {
+        return this._animations;
+    }
 
     public getAnimationNames() {
         return this._animations.map(a => a.name);
@@ -110,6 +110,8 @@ export class ViewerModel implements IDisposable {
                 });
             }
 
+            if (!this._modelConfiguration) return;
+
             if (this._modelConfiguration.animation) {
                 if (this._modelConfiguration.animation.playOnce) {
                     this._animations.forEach(a => {

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

@@ -1,12 +1,13 @@
 import { viewerManager } from './viewerManager';
 import { TemplateManager } from './../templateManager';
 import { ConfigurationLoader } from './../configuration/loader';
-import { CubeTexture, Color3, IEnvironmentHelperOptions, EnvironmentHelper, Effect, SceneOptimizer, SceneOptimizerOptions, Observable, Engine, Scene, ArcRotateCamera, Vector3, SceneLoader, AbstractMesh, Mesh, HemisphericLight, Database, SceneLoaderProgressEvent, ISceneLoaderPlugin, ISceneLoaderPluginAsync, Quaternion, Light, ShadowLight, ShadowGenerator, Tags, AutoRotationBehavior, BouncingBehavior, FramingBehavior, Behavior, Tools } from 'babylonjs';
+import { Skeleton, AnimationGroup, ParticleSystem, CubeTexture, Color3, IEnvironmentHelperOptions, EnvironmentHelper, Effect, SceneOptimizer, SceneOptimizerOptions, Observable, Engine, Scene, ArcRotateCamera, Vector3, SceneLoader, AbstractMesh, Mesh, HemisphericLight, Database, SceneLoaderProgressEvent, ISceneLoaderPlugin, ISceneLoaderPluginAsync, Quaternion, Light, ShadowLight, ShadowGenerator, Tags, AutoRotationBehavior, BouncingBehavior, FramingBehavior, Behavior, Tools } from 'babylonjs';
 import { ViewerConfiguration, ISceneConfiguration, ISceneOptimizerConfiguration, IObserversConfiguration, IModelConfiguration, ISkyboxConfiguration, IGroundConfiguration, ILightConfiguration, ICameraConfiguration } from '../configuration/configuration';
 
 import * as deepmerge from '../../assets/deepmerge.min.js';
 import { CameraBehavior } from 'src/interfaces';
 import { ViewerModel } from '../model/viewerModel';
+import { GroupModelAnimation } from '../model/modelAnimation';
 
 export abstract class AbstractViewer {
 
@@ -821,7 +822,7 @@ export abstract class AbstractViewer {
         }).then(() => {
             return new Promise<ViewerModel>((resolve, reject) => {
                 // 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(this.scene, <IModelConfiguration>this.configuration.model);
                 this.models.push(model);
                 this.lastUsedLoader = model.loader;
                 model.onLoadedObservable.add((model) => {
@@ -858,6 +859,19 @@ export abstract class AbstractViewer {
         });
     }
 
+    public addModel(meshes: Array<AbstractMesh>, skeletons: Array<Skeleton>, particleSystems: Array<ParticleSystem>, animationGroups: Array<AnimationGroup>): ViewerModel {
+        let model = new ViewerModel(this.scene);
+        model.meshes = meshes;
+        model.skeletons = skeletons;
+        model.particleSystems = particleSystems;
+        let animations = model.getAnimations();
+        animationGroups.forEach(ag => {
+            animations.push(new GroupModelAnimation(ag));
+        });
+
+        return model;
+    }
+
     protected initEnvironment(model?: ViewerModel): Promise<Scene> {
         this.configureEnvironment(this.configuration.skybox, this.configuration.ground);
 

+ 2 - 0
dist/preview release/viewer/babylon.viewer.d.ts

@@ -443,6 +443,7 @@ declare module BabylonViewer {
             exception: any;
         }>;
         load(): void;
+        getAnimations(): Array<IModelAnimation>;
         getAnimationNames(): string[];
         playAnimation(name: string): IModelAnimation;
         dispose(): void;
@@ -500,6 +501,7 @@ declare module BabylonViewer {
         protected initEngine(): Promise<BABYLON.Engine>;
         protected initScene(): Promise<BABYLON.Scene>;
         loadModel(modelConfig?: any, clearScene?: boolean): Promise<ViewerModel>;
+        public addModel(meshes: Array<AbstractMesh>, skeletons: Array<Skeleton>, particleSystems: Array<ParticleSystem>, animationGroups: Array<AnimationGroup>): ViewerModel;
         protected initEnvironment(viewerModel?: ViewerModel): Promise<BABYLON.Scene>;
         protected handleHardwareLimitations(): void;
         protected injectCustomShaders(): void;