Pārlūkot izejas kodu

Lights configuration and implementation in default viewer

Raanan Weber 7 gadi atpakaļ
vecāks
revīzija
32d61cac82

+ 45 - 3
Viewer/src/configuration/configuration.ts

@@ -45,18 +45,44 @@ export interface ViewerConfiguration {
     camera?: {
     camera?: {
         position?: { x: number, y: number, z: number };
         position?: { x: number, y: number, z: number };
         rotation?: { x: number, y: number, z: number, w: number };
         rotation?: { x: number, y: number, z: number, w: number };
-        fieldOfView?: number;
+        fov?: number;
+        fovMode?: number;
         minZ?: number;
         minZ?: number;
         maxZ?: number;
         maxZ?: number;
+        inertia?: number;
         behaviors?: Array<number | {
         behaviors?: Array<number | {
             type: number;
             type: number;
             [propName: string]: any;
             [propName: string]: any;
-        }>
+        }>;
+
+        [propName: string]: any;
     },
     },
     lights?: Array<{
     lights?: Array<{
         type: number;
         type: number;
         name?: string;
         name?: string;
+        disabled?: boolean;
+        position?: { x: number, y: number, z: number };
+        target?: { x: number, y: number, z: number };
+        direction?: { x: number, y: number, z: number };
+        diffuse?: { r: number, g: number, b: number };
+        specular?: { r: number, g: number, b: number };
+        intensity?: number;
+        radius?: number;
+        shadownEnabled?: boolean; // only on specific lights!
+        shadowConfig?: {
+            useBlurExponentialShadowMap?: boolean;
+            useKernelBlur?: boolean;
+            blurKernel?: number;
+            blurScale?: number;
+            [propName: string]: any;
+        }
         [propName: string]: any;
         [propName: string]: any;
+
+        // no behaviors for light at the moment, but allowing configuration for future reference.
+        behaviors?: Array<number | {
+            type: number;
+            [propName: string]: any;
+        }>
     }>
     }>
     // engine configuration. optional!
     // engine configuration. optional!
     engine?: {
     engine?: {
@@ -96,10 +122,26 @@ export let defaultConfiguration: ViewerConfiguration = {
             0,
             0,
             {
             {
                 type: 2,
                 type: 2,
-                zoomOnBoundingInfo: true
+                zoomOnBoundingInfo: true,
+                zoomStopsAnimation: false
             }
             }
         ]
         ]
     },
     },
+    lights: [
+        {
+            type: 1,
+            enableShadows: true,
+            direction: { x: -0.2, y: -1, z: 0 },
+            position: { x: 0.017, y: 0.5, z: 0 },
+            intensity: 4.5,
+            shadowConfig: {
+                useBlurExponentialShadowMap: true,
+                useKernelBlur: true,
+                blurKernel: 64,
+                blurScale: 4
+            }
+        }
+    ],
     engine: {
     engine: {
         antialiasing: true
         antialiasing: true
     },
     },

+ 34 - 29
Viewer/src/viewer/defaultViewer.ts

@@ -1,6 +1,6 @@
 import { Template } from './../templateManager';
 import { Template } from './../templateManager';
 import { AbstractViewer } from './viewer';
 import { AbstractViewer } from './viewer';
-import { Observable, BouncingBehavior, FramingBehavior, Behavior, Light, Engine, Scene, AutoRotationBehavior, AbstractMesh, Quaternion, StandardMaterial, ShadowOnlyMaterial, ArcRotateCamera, ImageProcessingConfiguration, Color3, Vector3, SceneLoader, Mesh, HemisphericLight } from 'babylonjs';
+import { Observable, ShadowLight, BouncingBehavior, FramingBehavior, Behavior, Light, Engine, Scene, AutoRotationBehavior, AbstractMesh, Quaternion, StandardMaterial, ShadowOnlyMaterial, ArcRotateCamera, ImageProcessingConfiguration, Color3, Vector3, SceneLoader, Mesh, HemisphericLight } from 'babylonjs';
 import { CameraBehavior } from '../interfaces';
 import { CameraBehavior } from '../interfaces';
 
 
 // A small hack for the inspector. to be removed!
 // A small hack for the inspector. to be removed!
@@ -51,16 +51,7 @@ export class DefaultViewer extends AbstractViewer {
         meshes[0].rotation.y += Math.PI;
         meshes[0].rotation.y += Math.PI;
 
 
         this.setupCamera(meshes);
         this.setupCamera(meshes);
-
-        // Get the bounding vectors of the mesh hierarchy (meshes[0] = root node in gltf)
-
-        // Remove default light and create a new one to have a dynamic shadow                            
-        //this.scene.lights[0].dispose();
-        //var light = new BABYLON.DirectionalLight('light', new BABYLON.Vector3(-0.2, -1, 0), this.scene)
-        //light.position = new BABYLON.Vector3(bounding.max.x * 0.2, bounding.max.y * 2, 0)
-        //light.intensity = 4.5;
-
-        // TODO - move it away from here.
+        this.setupLights(meshes);
 
 
         var ground = Mesh.CreatePlane('ground', 100, this.scene)
         var ground = Mesh.CreatePlane('ground', 100, this.scene)
         ground.rotation.x = Math.PI / 2
         ground.rotation.x = Math.PI / 2
@@ -145,24 +136,38 @@ export class DefaultViewer extends AbstractViewer {
 
 
             this.configuration.lights.forEach((lightConfig, idx) => {
             this.configuration.lights.forEach((lightConfig, idx) => {
                 lightConfig.name = lightConfig.name || 'light-' + idx;
                 lightConfig.name = lightConfig.name || 'light-' + idx;
+                let constructor = Light.GetConstructorFromName(lightConfig.type, lightConfig.name, this.scene);
+                let light = constructor();
 
 
-                //let light = Light.Parse(lightConfig, this.scene);
-
-                /*
-                // TODO fix the shadow mechanism
-                var shadowGenerator = new BABYLON.ShadowGenerator(512, light)
-                shadowGenerator.useBlurExponentialShadowMap = true;
-                shadowGenerator.useKernelBlur = true;
-                shadowGenerator.blurKernel = 64;
-                shadowGenerator.blurScale = 4;
-                
-        
-                // Add the bus in the casters
-                for (var index = 0; index < focusMeshes.length; index++) {
-                    shadowGenerator.getShadowMap().renderList.push(focusMeshes[index]);
+                //enabled
+                if (light.isEnabled() !== !lightConfig.disabled) {
+                    light.setEnabled(!lightConfig.disabled);
                 }
                 }
 
 
-                */
+                light.intensity = lightConfig.intensity || light.intensity;
+
+                //position. Some lights don't have position!!
+                if (light instanceof ShadowLight) {
+                    if (lightConfig.position && light.position) {
+                        this.extendClassWithConfig(light.position, lightConfig.position);
+                    }
+
+                    if (lightConfig.direction) {
+                        this.extendClassWithConfig(light.direction, lightConfig.direction);
+                    }
+
+                    if (lightConfig.enableShadows) {
+                        var shadowGenerator = new BABYLON.ShadowGenerator(512, light)
+                        this.extendClassWithConfig(shadowGenerator, lightConfig.shadowConfig || {});
+                        // add the focues meshes to the shadow list
+                        for (var index = 0; index < focusMeshes.length; index++) {
+                            shadowGenerator.getShadowMap().renderList.push(focusMeshes[index]);
+                        }
+                        console.log("shadows enabled");
+                    }
+                }
+
+                console.log(light);
             });
             });
         }
         }
     }
     }
@@ -226,7 +231,7 @@ export class DefaultViewer extends AbstractViewer {
             this.camera.addBehavior(behavior);
             this.camera.addBehavior(behavior);
         }
         }
 
 
-        // post attach configuration
+        // post attach configuration. Some functionalities require the attached camera.
         switch (type) {
         switch (type) {
             case CameraBehavior.AUTOROTATION:
             case CameraBehavior.AUTOROTATION:
                 break;
                 break;
@@ -246,9 +251,9 @@ export class DefaultViewer extends AbstractViewer {
 
 
     private extendClassWithConfig(object: any, config: any) {
     private extendClassWithConfig(object: any, config: any) {
         Object.keys(config).forEach(key => {
         Object.keys(config).forEach(key => {
-            if (object.hasOwnProperty(key)) {
+            if (key in object && typeof object[key] !== 'function') {
                 object[key] = config[key];
                 object[key] = config[key];
             }
             }
-        })
+        });
     }
     }
 }
 }