Browse Source

Now serialize effect layers in scene serializer + parse in babylon file loader

Julien MOREAU-MATHIS 7 years ago
parent
commit
da51ef8112

+ 19 - 0
src/Layer/babylon.effectLayer.ts

@@ -176,6 +176,12 @@
         public abstract _disposeMesh(mesh: Mesh): void;
         public abstract _disposeMesh(mesh: Mesh): void;
 
 
         /**
         /**
+         * Serializes this layer (Glow or Highlight for example)
+         * @returns a serialized layer object
+         */
+        public abstract serialize?(): any;
+
+        /**
          * Initializes the effect layer with the required options.
          * Initializes the effect layer with the required options.
          * @param options Sets of none mandatory options to use with the layer (see IEffectLayerOptions for more information)
          * @param options Sets of none mandatory options to use with the layer (see IEffectLayerOptions for more information)
          */
          */
@@ -670,5 +676,18 @@
          public getClassName(): string {
          public getClassName(): string {
             return "EffectLayer";
             return "EffectLayer";
         }
         }
+
+        /**
+         * Creates an effect layer from parsed effect layer data
+         * @param parsedEffectLayer defines effect layer data
+         * @param scene defines the current scene
+         * @param rootUrl defines the root URL containing the effect layer information
+         * @returns a parsed effect Layer
+         */
+        public static Parse(parsedHightlightLayer: any, scene: Scene, rootUrl: string): EffectLayer {
+            var effectLayerType = Tools.Instantiate(parsedHightlightLayer.customType);
+            
+            return effectLayerType.Parse(parsedHightlightLayer, scene, rootUrl);
+        }
     }
     }
 } 
 } 

+ 3 - 1
src/Layer/babylon.glowLayer.ts

@@ -88,7 +88,7 @@
             return this._intensity;
             return this._intensity;
         }
         }
 
 
-        @serialize('options')
+        @serialize("options")
         private _options: IGlowLayerOptions;
         private _options: IGlowLayerOptions;
 
 
         private _intensity: number = 1.0;
         private _intensity: number = 1.0;
@@ -460,6 +460,8 @@
          */
          */
         public serialize(): any {
         public serialize(): any {
             var serializationObject = SerializationHelper.Serialize(this);
             var serializationObject = SerializationHelper.Serialize(this);
+            serializationObject.customType = "BABYLON.GlowLayer";
+
             var index;
             var index;
 
 
             // Included meshes
             // Included meshes

+ 86 - 0
src/Layer/babylon.highlightLayer.ts

@@ -141,11 +141,13 @@
         /**
         /**
          * Specifies whether or not the inner glow is ACTIVE in the layer.
          * Specifies whether or not the inner glow is ACTIVE in the layer.
          */
          */
+        @serialize()
         public innerGlow: boolean = true;
         public innerGlow: boolean = true;
 
 
         /**
         /**
          * Specifies whether or not the outer glow is ACTIVE in the layer.
          * Specifies whether or not the outer glow is ACTIVE in the layer.
          */
          */
+        @serialize()
         public outerGlow: boolean = true;
         public outerGlow: boolean = true;
 
 
         /**
         /**
@@ -165,6 +167,7 @@
         /**
         /**
          * Gets the horizontal size of the blur.
          * Gets the horizontal size of the blur.
          */
          */
+        @serialize()
         public get blurHorizontalSize(): number {
         public get blurHorizontalSize(): number {
             return this._horizontalBlurPostprocess.kernel
             return this._horizontalBlurPostprocess.kernel
         }
         }
@@ -172,6 +175,7 @@
         /**
         /**
          * Gets the vertical size of the blur.
          * Gets the vertical size of the blur.
          */
          */
+        @serialize()
         public get blurVerticalSize(): number {
         public get blurVerticalSize(): number {
             return this._verticalBlurPostprocess.kernel;
             return this._verticalBlurPostprocess.kernel;
         }
         }
@@ -188,6 +192,7 @@
 
 
         private _instanceGlowingMeshStencilReference = HighlightLayer.GlowingMeshStencilReference++;
         private _instanceGlowingMeshStencilReference = HighlightLayer.GlowingMeshStencilReference++;
 
 
+        @serialize("options")
         private _options: IHighlightLayerOptions;
         private _options: IHighlightLayerOptions;
         private _downSamplePostprocess: PassPostProcess;
         private _downSamplePostprocess: PassPostProcess;
         private _horizontalBlurPostprocess: GlowBlurPostProcess;
         private _horizontalBlurPostprocess: GlowBlurPostProcess;
@@ -668,5 +673,86 @@
 
 
             super.dispose();
             super.dispose();
         }
         }
+
+        /**
+          * Gets the class name of the effect layer
+          * @returns the string with the class name of the effect layer
+          */
+         public getClassName(): string {
+            return "HighlightLayer";
+        }
+
+        /**
+         * Serializes this highlight layer
+         * @returns a serialized highlight layer object
+         */
+        public serialize(): any {
+            var serializationObject = SerializationHelper.Serialize(this);
+            serializationObject.customType = "BABYLON.HighlightLayer";
+
+            // Highlighted meshes
+            serializationObject.meshes = [];
+
+            if (this._meshes) {
+                for (var m in this._meshes) {
+                    var mesh = this._meshes[m];
+
+                    if (mesh) {
+                        serializationObject.meshes.push({
+                            glowEmissiveOnly: mesh.glowEmissiveOnly,
+                            color: mesh.color.asArray(),
+                            meshId: mesh.mesh.id
+                        });
+                    }
+                }
+            }
+
+            // Excluded meshes
+            serializationObject.excludedMeshes = [];
+
+            if (this._excludedMeshes) {
+                for (var e in this._excludedMeshes) {
+                    var excludedMesh = this._excludedMeshes[e];
+
+                    if (excludedMesh) {
+                        serializationObject.excludedMeshes.push(excludedMesh.mesh.id);
+                    }
+                }
+            }
+
+            return serializationObject;
+        }
+
+        /**
+         * Creates a HighLight layer from parsed hightlight layer data
+         * @param parsedHightlightLayer defines the hightlight layer data
+         * @param scene defines the current scene
+         * @param rootUrl defines the root URL containing the hightlight layer information
+         * @returns a parsed hightlight layer
+         */
+        public static Parse(parsedHightlightLayer: any, scene: Scene, rootUrl: string): HighlightLayer {
+            var hl = SerializationHelper.Parse(() => new HighlightLayer(parsedHightlightLayer.name, scene, parsedHightlightLayer.options), parsedHightlightLayer, scene, rootUrl);
+            var index;
+
+            // Excluded meshes
+            for (index = 0; index < parsedHightlightLayer.excludedMeshes.length; index++) {
+                var mesh = scene.getMeshByID(parsedHightlightLayer.excludedMeshes[index]);
+                if (mesh) {
+                    hl.addExcludedMesh(<Mesh> mesh);
+                }
+            }
+
+            // Included meshes
+            for (index = 0; index < parsedHightlightLayer.meshes.length; index++) {
+                var highlightedMesh = parsedHightlightLayer.meshes[index];
+                var mesh = scene.getMeshByID(highlightedMesh.meshId);
+
+                if (mesh) {
+                    hl.addMesh(<Mesh> mesh, Color3.FromArray(highlightedMesh.color), highlightedMesh.glowEmissiveOnly);
+                }
+            }
+
+            return hl;
+        }
     }
     }
 } 
 } 

+ 7 - 0
src/Loading/Plugins/babylon.babylonFileLoader.ts

@@ -357,6 +357,13 @@
                 }
                 }
             }
             }
 
 
+            // Effect layers
+            if (parsedData.effectLayers) {
+                for (index = 0; index < parsedData.effectLayers.length; index++) {
+                    EffectLayer.Parse(parsedData.effectLayers[index], scene, rootUrl);
+                }
+            }
+
             // Actions (scene)
             // Actions (scene)
             if (parsedData.actions !== undefined && parsedData.actions !== null) {
             if (parsedData.actions !== undefined && parsedData.actions !== null) {
                 ActionManager.Parse(parsedData.actions, null, scene);
                 ActionManager.Parse(parsedData.actions, null, scene);

+ 10 - 0
src/Tools/babylon.sceneSerializer.ts

@@ -305,6 +305,16 @@
                 }
                 }
             }
             }
 
 
+            // Effect layers
+            serializationObject.effectLayers = [];
+
+            for (index = 0; index < scene.effectLayers.length; index++) {
+                var layer = scene.effectLayers[index];
+                if (layer.serialize) {
+                    serializationObject.effectLayers.push(layer.serialize());
+                }
+            }
+
             return serializationObject;
             return serializationObject;
         }
         }