Explorar o código

Merge pull request #4030 from julien-moreau/master

Beginning glow layer serialization
David Catuhe %!s(int64=7) %!d(string=hai) anos
pai
achega
a4ccfce5af

+ 20 - 1
src/Layer/babylon.effectLayer.ts

@@ -54,18 +54,27 @@
         protected _emissiveTextureAndColor: { texture: Nullable<BaseTexture>, color: Color4 } = { texture: null, color: new Color4() };
 
         /**
+         * The name of the layer
+         */
+        @serialize()
+        public name: string;
+
+        /**
          * The clear color of the texture used to generate the glow map.
          */
+        @serializeAsColor4()
         public neutralColor: Color4 = new Color4();
 
         /**
          * Specifies wether the highlight layer is enabled or not.
          */
+        @serialize()
         public isEnabled: boolean = true;
 
         /**
          * Gets the camera attached to the layer.
          */
+        @serializeAsCameraReference()
         public get camera(): Nullable<Camera> {
             return this._effectLayerOptions.camera;
         }
@@ -102,8 +111,10 @@
          */
         constructor(
             /** The Friendly of the effect in the scene */
-            public name: string, 
+            name: string,
             scene: Scene) {
+            this.name = name;
+
             this._scene = scene || Engine.LastCreatedScene;
             this._engine = scene.getEngine();
             this._maxSize = this._engine.getCaps().maxTextureSize;
@@ -651,5 +662,13 @@
             this.onAfterComposeObservable.clear();
             this.onSizeChangedObservable.clear();
         }
+
+        /**
+          * Gets the class name of the effect layer
+          * @returns the string with the class name of the effect layer
+          */
+         public getClassName(): string {
+            return "EffectLayer";
+        }
     }
 } 

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

@@ -68,6 +68,7 @@
         /**
          * Gets the kernel size of the blur.
          */
+        @serialize()
         public get blurKernelSize(): number {
             return this._horizontalBlurPostprocess1.kernel;
         }
@@ -82,11 +83,14 @@
         /**
          * Gets the glow intensity.
          */
+        @serialize()
         public get intensity(): number {
             return this._intensity;
         }
 
+        @serialize('options')
         private _options: IGlowLayerOptions;
+
         private _intensity: number = 1.0;
         private _horizontalBlurPostprocess1: BlurPostProcess;
         private _verticalBlurPostprocess1: BlurPostProcess;
@@ -115,7 +119,7 @@
          * @param scene The scene to use the layer in
          * @param options Sets of none mandatory options to use with the layer (see IGlowLayerOptions for more information)
          */
-        constructor(public name: string, scene: Scene, options?: Partial<IGlowLayerOptions>) {
+        constructor(name: string, scene: Scene, options?: Partial<IGlowLayerOptions>) {
             super(name, scene);
             this.neutralColor = new Color4(0, 0, 0, 1);
 
@@ -441,5 +445,78 @@
             this.removeIncludedOnlyMesh(mesh);
             this.removeExcludedMesh(mesh);
         }
+
+        /**
+          * Gets the class name of the effect layer
+          * @returns the string with the class name of the effect layer
+          */
+         public getClassName(): string {
+            return "GlowLayer";
+        }
+
+        /**
+         * Serializes this glow layer
+         * @returns a serialized glow layer object
+         */
+        public serialize(): any {
+            var serializationObject = SerializationHelper.Serialize(this);
+            var index;
+
+            // Included meshes
+            serializationObject.includedMeshes = [];
+
+            if (this._includedOnlyMeshes.length) {
+                for (index = 0; index < this._includedOnlyMeshes.length; index++) {
+                    var mesh = this._scene.getMeshByUniqueID(this._includedOnlyMeshes[index]);
+                    if (mesh) {
+                        serializationObject.includedMeshes.push(mesh.id);
+                    }
+                }
+            }
+
+            // Excluded meshes
+            serializationObject.excludedMeshes = [];
+
+            if (this._excludedMeshes.length) {
+                for (index = 0; index < this._excludedMeshes.length; index++) {
+                    var mesh = this._scene.getMeshByUniqueID(this._excludedMeshes[index]);
+                    if (mesh) {
+                        serializationObject.excludedMeshes.push(mesh.id);
+                    }
+                }
+            }
+
+            return serializationObject;
+        }
+
+        /**
+         * Creates a Glow Layer from parsed glow layer data
+         * @param parsedGlowLayer defines glow layer data
+         * @param scene defines the current scene
+         * @param rootUrl defines the root URL containing the glow layer information
+         * @returns a parsed Glow Layer
+         */
+        public static Parse(parsedGlowLayer: any, scene: Scene, rootUrl: string): GlowLayer {
+            var gl = SerializationHelper.Parse(() => new GlowLayer(parsedGlowLayer.name, scene, parsedGlowLayer.options), parsedGlowLayer, scene, rootUrl);
+            var index;
+
+            // Excluded meshes
+            for (index = 0; index < parsedGlowLayer.excludedMeshes.length; index++) {
+                var mesh = scene.getMeshByID(parsedGlowLayer.excludedMeshes[index]);
+                if (mesh) {
+                    gl.addExcludedMesh(<Mesh>mesh);
+                }
+            }
+
+            // Included meshes
+            for (index = 0; index < parsedGlowLayer.includedMeshes.length; index++) {
+                var mesh = scene.getMeshByID(parsedGlowLayer.includedMeshes[index]);
+                if (mesh) {
+                    gl.addIncludedOnlyMesh(<Mesh>mesh);
+                }
+            }
+
+            return gl;
+        }
     }
 } 

+ 18 - 0
src/Tools/babylon.decorators.ts

@@ -22,6 +22,7 @@
                 switch (propertyType) {
                     case 0:     // Value
                     case 6:     // Mesh reference
+                    case 11:    // Camera reference
                         (<any>destination)[property] = sourceProperty;
                         break;
                     case 1:     // Texture
@@ -182,6 +183,9 @@
         return generateSerializableMember(10, sourceName); // quaternion member
     }
 
+    export function serializeAsCameraReference(sourceName?: string) {
+        return generateSerializableMember(11, sourceName); // camera reference member
+    }
 
     export class SerializationHelper {
 
@@ -236,6 +240,12 @@
                         case 9:     // Image Processing
                             serializationObject[targetPropertyName] = (<ImageProcessingConfiguration>sourceProperty).serialize();
                             break;
+                        case 10:    // Quaternion
+                            serializationObject[targetPropertyName] = (<Quaternion>sourceProperty).asArray();
+                            break;
+                        case 11:    // Camera reference
+                            serializationObject[targetPropertyName] = (<Camera>sourceProperty).id;
+                            break;
                     }
                 }
             }
@@ -300,6 +310,14 @@
                         case 9:     // Image Processing
                             dest[property] = ImageProcessingConfiguration.Parse(sourceProperty);
                             break;
+                        case 10:    // Quaternion
+                            dest[property] = Quaternion.FromArray(sourceProperty);
+                            break;
+                        case 11:    // Camera reference
+                            if (scene) {
+                                dest[property] = scene.getCameraByID(sourceProperty);
+                            }
+                            break;
                     }
                 }
             }