Browse Source

Texture Parsers

sebastien 6 years ago
parent
commit
a50c0ed9c1

+ 3 - 0
src/Animations/animation.ts

@@ -9,6 +9,7 @@ import { Nullable } from "types";
 import { Scene } from "scene";
 import { Scene } from "scene";
 import { IAnimatable } from "Tools/tools";
 import { IAnimatable } from "Tools/tools";
 import { Node } from "node";
 import { Node } from "node";
+import { Texture } from "Materials/Textures/texture";
 
 
     /**
     /**
      * Represents the range of an animation
      * Represents the range of an animation
@@ -1242,3 +1243,5 @@ import { Node } from "node";
             }
             }
         }
         }
     }
     }
+
+    Texture._AnimationParser = Animation.Parse;

+ 1 - 1
src/Cameras/camera.ts

@@ -30,7 +30,7 @@ declare type TargetCamera = import("./targetCamera").TargetCamera;
     export class Camera extends Node {
     export class Camera extends Node {
         /** @hidden */
         /** @hidden */
         public static _createDefaultParsedCamera = (name: string, scene: Scene): Camera => {
         public static _createDefaultParsedCamera = (name: string, scene: Scene): Camera => {
-            throw "UniversalCamera needs to be imported before being deserialization can create a default camera.";
+            throw "UniversalCamera needs to be imported before deserialization can create a default camera.";
         }
         }
 
 
         /**
         /**

+ 2 - 0
src/Materials/Textures/cubeTexture.ts

@@ -293,3 +293,5 @@ import { Constants } from "Engine/constants";
             }, this);
             }, this);
         }
         }
     }
     }
+
+    Texture._CubeTextureParser = CubeTexture.Parse;

+ 4 - 0
src/Materials/Textures/mirrorTexture.ts

@@ -276,3 +276,7 @@ import { Constants } from "Engine/constants";
             this.scene.imageProcessingConfiguration.onUpdateParameters.remove(this._imageProcessingConfigChangeObserver);
             this.scene.imageProcessingConfiguration.onUpdateParameters.remove(this._imageProcessingConfigChangeObserver);
         }
         }
     }
     }
+
+    Texture._CreateMirror = (name: string, renderTargetSize: number, scene: Scene, generateMipMaps: boolean): MirrorTexture => {
+        return new MirrorTexture(name, renderTargetSize, scene, generateMipMaps);
+    };

+ 4 - 0
src/Materials/Textures/renderTargetTexture.ts

@@ -964,3 +964,7 @@ import { Constants } from "Engine/constants";
             }
             }
         }
         }
     }
     }
+
+    Texture._CreateRenderTargetTexture = (name: string, renderTargetSize: number, scene: Scene, generateMipMaps: boolean): MirrorTexture => {
+        return new RenderTargetTexture(name, renderTargetSize, scene, generateMipMaps);
+    };

+ 94 - 81
src/Materials/Textures/texture.ts

@@ -3,22 +3,39 @@ import { Observable } from "Tools/observable";
 import { Tools } from "Tools/tools";
 import { Tools } from "Tools/tools";
 import { Nullable } from "types";
 import { Nullable } from "types";
 import { Scene } from "scene";
 import { Scene } from "scene";
-import { Matrix, Vector3 } from "Math/math";
-//import { Engine } from "Engine/engine";
+import { Matrix, Vector3, Plane } from "Math/math";
 import { Material } from "Materials/material";
 import { Material } from "Materials/material";
 import { BaseTexture } from "Materials/Textures/baseTexture";
 import { BaseTexture } from "Materials/Textures/baseTexture";
 import { Constants } from "Engine/constants";
 import { Constants } from "Engine/constants";
+import { _AlphaState } from "States";
 
 
-// import { Animation } from "Animations/animation";
-// import { RenderTargetTexture } from "Materials/Textures/renderTargetTexture";
-// import { MirrorTexture } from "Materials/Textures/mirrorTexture";
-// import { CubeTexture } from "Materials/Textures/cubeTexture";
+declare type Animation = import("Animations/animation").Animation;
+declare type CubeTexture = import("Materials/Textures/cubeTexture").CubeTexture;
+declare type MirrorTexture = import("Materials/Textures/mirrorTexture").MirrorTexture;
+declare type RenderTargetTexture = import ("Materials/Textures/renderTargetTexture").RenderTargetTexture;
 
 
     /**
     /**
      * This represents a texture in babylon. It can be easily loaded from a network, base64 or html input.
      * This represents a texture in babylon. It can be easily loaded from a network, base64 or html input.
      * @see http://doc.babylonjs.com/babylon101/materials#texture
      * @see http://doc.babylonjs.com/babylon101/materials#texture
      */
      */
     export class Texture extends BaseTexture {
     export class Texture extends BaseTexture {
+        /** @hidden */
+        public static _AnimationParser = (jsonAnimation: any): Animation => {
+            throw "Animation needs to be imported before being deserialized.";
+        }
+        /** @hidden */
+        public static _CubeTextureParser = (jsonTexture: any, scene: Scene, rootUrl: string): CubeTexture => {
+            throw "CubeTexture needs to be imported before being deserialized.";
+        }
+        /** @hidden */
+        public static _CreateMirror = (name: string, renderTargetSize: number, scene: Scene, generateMipMaps: boolean): MirrorTexture => {
+            throw "MirrorTexture needs to be imported before being deserialized.";
+        }
+        /** @hidden */
+        public static _CreateRenderTargetTexture = (name: string, renderTargetSize: number, scene: Scene, generateMipMaps: boolean): RenderTargetTexture => {
+            throw "RenderTargetTexture needs to be imported before being deserialized.";
+        }
+
         /** nearest is mag = nearest and min = nearest and mip = linear */
         /** nearest is mag = nearest and min = nearest and mip = linear */
         public static readonly NEAREST_SAMPLINGMODE = Constants.TEXTURE_NEAREST_SAMPLINGMODE;
         public static readonly NEAREST_SAMPLINGMODE = Constants.TEXTURE_NEAREST_SAMPLINGMODE;
         /** nearest is mag = nearest and min = nearest and mip = linear */
         /** nearest is mag = nearest and min = nearest and mip = linear */
@@ -602,81 +619,77 @@ import { Constants } from "Engine/constants";
          * @returns The parsed texture if successful
          * @returns The parsed texture if successful
          */
          */
         public static Parse(parsedTexture: any, scene: Scene, rootUrl: string): Nullable<BaseTexture> {
         public static Parse(parsedTexture: any, scene: Scene, rootUrl: string): Nullable<BaseTexture> {
-            // if (parsedTexture.customType) {
-            //     var customTexture = Tools.Instantiate(parsedTexture.customType);
-            //     // Update Sampling Mode
-            //     var parsedCustomTexture: any = customTexture.Parse(parsedTexture, scene, rootUrl);
-            //     if (parsedTexture.samplingMode && parsedCustomTexture.updateSamplingMode && parsedCustomTexture._samplingMode) {
-            //         if (parsedCustomTexture._samplingMode !== parsedTexture.samplingMode) {
-            //             parsedCustomTexture.updateSamplingMode(parsedTexture.samplingMode);
-            //         }
-            //     }
-            //     return parsedCustomTexture;
-            // }
-
-            // if (parsedTexture.isCube) {
-            //     //return CubeTexture.Parse(parsedTexture, scene, rootUrl);
-            //     return <any> 0;
-            // }
-
-            // if (!parsedTexture.name && !parsedTexture.isRenderTarget) {
-            //     return null;
-            // }
-
-            // var texture = SerializationHelper.Parse(() => {
-            //     var generateMipMaps: boolean = true;
-            //     if (parsedTexture.noMipmap) {
-            //         generateMipMaps = false;
-            //     }
-            //     if (parsedTexture.mirrorPlane) {
-            //         // var mirrorTexture = new MirrorTexture(parsedTexture.name, parsedTexture.renderTargetSize, scene, generateMipMaps);
-            //         // mirrorTexture._waitingRenderList = parsedTexture.renderList;
-            //         // mirrorTexture.mirrorPlane = Plane.FromArray(parsedTexture.mirrorPlane);
-
-            //         // return mirrorTexture;
-            //         return;
-            //     } else if (parsedTexture.isRenderTarget) {
-            //         // var renderTargetTexture = new RenderTargetTexture(parsedTexture.name, parsedTexture.renderTargetSize, scene, generateMipMaps);
-            //         // renderTargetTexture._waitingRenderList = parsedTexture.renderList;
-
-            //         // return renderTargetTexture;
-            //         return;
-            //     } else {
-            //         var texture: Texture;
-            //         if (parsedTexture.base64String) {
-            //             texture = Texture.CreateFromBase64String(parsedTexture.base64String, parsedTexture.name, scene, !generateMipMaps);
-            //         } else {
-            //             let url = rootUrl + parsedTexture.name;
-
-            //             if (Texture.UseSerializedUrlIfAny && parsedTexture.url) {
-            //                 url = parsedTexture.url;
-            //             }
-            //             texture = new Texture(url, scene, !generateMipMaps, parsedTexture.invertY);
-            //         }
-
-            //         return texture;
-            //     }
-            // }, parsedTexture, scene);
-
-            // // Update Sampling Mode
-            // if (parsedTexture.samplingMode) {
-            //     var sampling: number = parsedTexture.samplingMode;
-            //     if (texture._samplingMode !== sampling) {
-            //         texture.updateSamplingMode(sampling);
-            //     }
-            // }
-
-            // // Animations
-            // if (parsedTexture.animations) {
-            //     for (var animationIndex = 0; animationIndex < parsedTexture.animations.length; animationIndex++) {
-            //         var parsedAnimation = parsedTexture.animations[animationIndex];
-
-            //         texture.animations.push(Animation.Parse(parsedAnimation));
-            //     }
-            // }
-
-            // return texture;
-            return <any>0;
+            if (parsedTexture.customType) {
+                var customTexture = Tools.Instantiate(parsedTexture.customType);
+                // Update Sampling Mode
+                var parsedCustomTexture: any = customTexture.Parse(parsedTexture, scene, rootUrl);
+                if (parsedTexture.samplingMode && parsedCustomTexture.updateSamplingMode && parsedCustomTexture._samplingMode) {
+                    if (parsedCustomTexture._samplingMode !== parsedTexture.samplingMode) {
+                        parsedCustomTexture.updateSamplingMode(parsedTexture.samplingMode);
+                    }
+                }
+                return parsedCustomTexture;
+            }
+
+            if (parsedTexture.isCube) {
+                return Texture._CubeTextureParser(parsedTexture, scene, rootUrl);
+            }
+
+            if (!parsedTexture.name && !parsedTexture.isRenderTarget) {
+                return null;
+            }
+
+            var texture = SerializationHelper.Parse(() => {
+                var generateMipMaps: boolean = true;
+                if (parsedTexture.noMipmap) {
+                    generateMipMaps = false;
+                }
+                if (parsedTexture.mirrorPlane) {
+                    var mirrorTexture = Texture._CreateMirror(parsedTexture.name, parsedTexture.renderTargetSize, scene, generateMipMaps);
+                    mirrorTexture._waitingRenderList = parsedTexture.renderList;
+                    mirrorTexture.mirrorPlane = Plane.FromArray(parsedTexture.mirrorPlane);
+
+                    return mirrorTexture;
+                } else if (parsedTexture.isRenderTarget) {
+                    var renderTargetTexture = Texture._CreateRenderTargetTexture(parsedTexture.name, parsedTexture.renderTargetSize, scene, generateMipMaps);
+                    renderTargetTexture._waitingRenderList = parsedTexture.renderList;
+
+                    return renderTargetTexture;
+                } else {
+                    var texture: Texture;
+                    if (parsedTexture.base64String) {
+                        texture = Texture.CreateFromBase64String(parsedTexture.base64String, parsedTexture.name, scene, !generateMipMaps);
+                    } else {
+                        let url = rootUrl + parsedTexture.name;
+
+                        if (Texture.UseSerializedUrlIfAny && parsedTexture.url) {
+                            url = parsedTexture.url;
+                        }
+                        texture = new Texture(url, scene, !generateMipMaps, parsedTexture.invertY);
+                    }
+
+                    return texture;
+                }
+            }, parsedTexture, scene);
+
+            // Update Sampling Mode
+            if (parsedTexture.samplingMode) {
+                var sampling: number = parsedTexture.samplingMode;
+                if (texture._samplingMode !== sampling) {
+                    texture.updateSamplingMode(sampling);
+                }
+            }
+
+            // Animations
+            if (parsedTexture.animations) {
+                for (var animationIndex = 0; animationIndex < parsedTexture.animations.length; animationIndex++) {
+                    var parsedAnimation = parsedTexture.animations[animationIndex];
+
+                    texture.animations.push(Texture._AnimationParser(parsedAnimation));
+                }
+            }
+
+            return texture;
         }
         }
 
 
         /**
         /**