فهرست منبع

camera auto-focus and configuration changes

Raanan Weber 7 سال پیش
والد
کامیت
2dd77eeaf2
3فایلهای تغییر یافته به همراه40 افزوده شده و 35 حذف شده
  1. 2 4
      Viewer/src/model/viewerModel.ts
  2. 29 24
      Viewer/src/viewer/sceneManager.ts
  3. 9 7
      Viewer/src/viewer/viewer.ts

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

@@ -98,7 +98,6 @@ export class ViewerModel implements IDisposable {
         this._viewer.sceneManager.models.push(this);
         this._viewer.onModelAddedObservable.notifyObservers(this);
         this.onLoadedObservable.add(() => {
-            this._configureModel();
             this._viewer.onModelLoadedObservable.notifyObservers(this);
             this._initAnimations();
         });
@@ -247,7 +246,7 @@ export class ViewerModel implements IDisposable {
 
     private _configureModel() {
         // this can be changed to the meshes that have rootMesh a parent without breaking anything.
-        let meshesWithNoParent: Array<AbstractMesh> = [this.rootMesh] //this._meshes.filter(m => m === this.rootMesh);
+        let meshesWithNoParent: Array<AbstractMesh> = [this.rootMesh] //this._meshes.filter(m => m.parent === this.rootMesh);
         let updateMeshesWithNoParent = (variable: string, value: any, param?: string) => {
             meshesWithNoParent.forEach(mesh => {
                 if (param) {
@@ -345,7 +344,7 @@ export class ViewerModel implements IDisposable {
                     mesh.position = center.scale(-1);
 
                     // Set on ground.
-                    mesh.position.y += halfSizeVec.y;
+                    //mesh.position.y = 0;
 
                     // Recompute Info.
                     mesh.computeWorldMatrix(true);
@@ -354,7 +353,6 @@ export class ViewerModel implements IDisposable {
         }
 
         let meshes = this.rootMesh.getChildMeshes(false);
-        meshes.push(this.rootMesh);
         meshes.filter(m => m.material).forEach((mesh) => {
             this._applyModelMaterialConfiguration(mesh.material!);
         });

+ 29 - 24
Viewer/src/viewer/sceneManager.ts

@@ -86,15 +86,6 @@ export class SceneManager {
             this._handleHardwareLimitations();
         });
 
-        this._viewer.onModelLoadedObservable.add((model) => {
-            this._configureLights(this._viewer.configuration.lights, model);
-
-            if (this._viewer.configuration.camera || !this.scene.activeCamera) {
-                this._configureCamera(this._viewer.configuration.camera || {}, model);
-            }
-            return this._initEnvironment(model);
-        });
-
         this.labs = new ViewerLabs(this);
     }
 
@@ -184,7 +175,7 @@ export class SceneManager {
      * @param newConfiguration the delta that should be configured. This includes only the changes
      * @param globalConfiguration The global configuration object, after the new configuration was merged into it
      */
-    public updateConfiguration(newConfiguration: Partial<ViewerConfiguration>, globalConfiguration: ViewerConfiguration) {
+    public updateConfiguration(newConfiguration: Partial<ViewerConfiguration>, globalConfiguration: ViewerConfiguration, model?: ViewerModel) {
 
         if (newConfiguration.lab) {
             if (newConfiguration.lab.environmentAssetsRootURL) {
@@ -228,28 +219,26 @@ export class SceneManager {
             }
         }
 
-
-
         // optimizer
         if (newConfiguration.optimizer) {
             this._configureOptimizer(newConfiguration.optimizer);
         }
 
+        // configure model
+        if (newConfiguration.model && typeof newConfiguration.model === 'object') {
+            this._configureModel(newConfiguration.model);
+        }
+
         // lights
-        this._configureLights(newConfiguration.lights);
+        this._configureLights(newConfiguration.lights, model);
 
         // environment
         if (newConfiguration.skybox !== undefined || newConfiguration.ground !== undefined) {
-            this._configureEnvironment(newConfiguration.skybox, newConfiguration.ground);
-        }
-
-        // configure model
-        if (newConfiguration.model && typeof newConfiguration.model === 'object') {
-            this._configureModel(newConfiguration.model);
+            this._configureEnvironment(newConfiguration.skybox, newConfiguration.ground, model);
         }
 
         // camera
-        this._configureCamera(newConfiguration.camera);
+        this._configureCamera(newConfiguration.camera, model);
     }
 
 
@@ -380,6 +369,7 @@ export class SceneManager {
         if (!this.scene.activeCamera) {
             this.scene.createDefaultCamera(true, true, true);
             this.camera = <ArcRotateCamera>this.scene.activeCamera!;
+            this.camera.setTarget(Vector3.Zero());
         }
         if (cameraConfig.position) {
             let newPosition = this.camera.position.clone();
@@ -389,8 +379,14 @@ export class SceneManager {
 
         if (cameraConfig.target) {
             let newTarget = this.camera.target.clone();
-            extendClassWithConfig(newTarget, cameraConfig.position);
+            extendClassWithConfig(newTarget, cameraConfig.target);
             this.camera.setTarget(newTarget);
+        } else if (model) {
+            const boundingInfo = model.rootMesh.getHierarchyBoundingVectors(true);
+            const sizeVec = boundingInfo.max.subtract(boundingInfo.min);
+            const halfSizeVec = sizeVec.scale(0.5);
+            const center = boundingInfo.min.add(halfSizeVec);
+            this.camera.setTarget(center);
         }
 
         if (cameraConfig.rotation) {
@@ -414,7 +410,7 @@ export class SceneManager {
         const sceneDiagonal = sceneExtends.max.subtract(sceneExtends.min);
         const sceneDiagonalLenght = sceneDiagonal.length();
         if (isFinite(sceneDiagonalLenght))
-            this.camera.upperRadiusLimit = sceneDiagonalLenght * 3;
+            this.camera.upperRadiusLimit = sceneDiagonalLenght * 4;
 
         this.onCameraConfiguredObservable.notifyObservers({
             sceneManager: this,
@@ -433,12 +429,21 @@ export class SceneManager {
             return Promise.resolve(this.scene);
         }
 
+
         const options: Partial<IEnvironmentHelperOptions> = {
             createGround: !!groundConfiguration,
             createSkybox: !!skyboxConifguration,
-            setupImageProcessing: false // will be done at the scene level!
+            setupImageProcessing: false, // will be done at the scene level!,
         };
 
+        if (model) {
+            const boundingInfo = model.rootMesh.getHierarchyBoundingVectors(true);
+            const sizeVec = boundingInfo.max.subtract(boundingInfo.min);
+            const halfSizeVec = sizeVec.scale(0.5);
+            const center = boundingInfo.min.add(halfSizeVec);
+            options.groundYBias = -center.y;
+        }
+
         if (groundConfiguration) {
             let groundConfig = (typeof groundConfiguration === 'boolean') ? {} : groundConfiguration;
 
@@ -574,7 +579,7 @@ export class SceneManager {
         if (Object.keys(lightsToConfigure).length !== lightsAvailable.length) {
             lightsAvailable.forEach(lName => {
                 if (lightsToConfigure.indexOf(lName) === -1) {
-                    this.scene.getLightByName(lName)!.dispose()
+                    this.scene.getLightByName(lName)!.dispose();
                 }
             });
         }

+ 9 - 7
Viewer/src/viewer/viewer.ts

@@ -172,11 +172,6 @@ export abstract class AbstractViewer {
             if (this._configuration.observers) {
                 this._configureObservers(this._configuration.observers);
             }
-            this.onSceneInitObservable.add(() => {
-                this.updateConfiguration(configuration);
-            });
-            //
-
             // initialize the templates
             let templateConfiguration = this._configuration.templates || {};
             this.templateManager.initTemplate(templateConfiguration);
@@ -189,6 +184,10 @@ export abstract class AbstractViewer {
                 this._onTemplateLoaded();
             });
         });
+
+        this.onModelLoadedObservable.add((model) => {
+            this.updateConfiguration(this._configuration, model);
+        });
     }
 
     /**
@@ -314,11 +313,11 @@ export abstract class AbstractViewer {
      * and the entire configuration will be updated. 
      * @param newConfiguration 
      */
-    public updateConfiguration(newConfiguration: Partial<ViewerConfiguration> = this._configuration) {
+    public updateConfiguration(newConfiguration: Partial<ViewerConfiguration> = this._configuration, mode?: ViewerModel) {
         // update this.configuration with the new data
         this._configuration = deepmerge(this._configuration || {}, newConfiguration);
 
-        this.sceneManager.updateConfiguration(newConfiguration, this._configuration);
+        this.sceneManager.updateConfiguration(newConfiguration, this._configuration, mode);
 
         // observers in configuration
         if (newConfiguration.observers) {
@@ -430,6 +429,9 @@ export abstract class AbstractViewer {
                     return this.sceneManager.scene || this.sceneManager.initScene(this._configuration.scene);
                 }
             }).then((scene) => {
+                if (!autoLoad) {
+                    this.updateConfiguration();
+                }
                 return this.onSceneInitObservable.notifyObserversWithPromise(scene);
             }).then(() => {
                 this._isInit = true;