Browse Source

Migrate ProceduralTexture to component

sebastien 7 năm trước cách đây
mục cha
commit
111eb4557f

+ 1 - 0
Tools/Gulp/config.json

@@ -684,6 +684,7 @@
         "procedural": {
             "files": [
                 "../../src/Materials/Textures/Procedurals/babylon.proceduralTexture.js",
+                "../../src/Materials/Textures/Procedurals/babylon.proceduralTextureSceneComponent.js",
                 "../../src/Materials/Textures/Procedurals/babylon.customProceduralTexture.js"
             ],
             "dependUpon": [

+ 5 - 1
src/Materials/Textures/Procedurals/babylon.proceduralTexture.ts

@@ -44,7 +44,11 @@
             super(null, scene, !generateMipMaps);
 
             scene = this.getScene()!;
-
+            let component = scene._getComponent(SceneComponentConstants.NAME_PROCEDURALTEXTURE);
+            if (!component) {
+                component = new ProceduralTextureSceneComponent(scene);
+                scene._addComponent(component);
+            }
             scene.proceduralTextures.push(this);
 
             this._engine = scene.getEngine();

+ 70 - 0
src/Materials/Textures/Procedurals/babylon.proceduralTextureSceneComponent.ts

@@ -0,0 +1,70 @@
+module BABYLON {
+    export interface AbstractScene {
+        /**
+         * The list of procedural textures added to the scene
+         * @see http://doc.babylonjs.com/how_to/how_to_use_procedural_textures
+         */
+        proceduralTextures: Array<ProceduralTexture>;
+    }
+
+    /**
+     * Defines the Procedural Texture scene component responsible to manage any Procedural Texture
+     * in a given scene.
+     */
+    export class ProceduralTextureSceneComponent implements ISceneComponent {
+        /**
+         * The component name helpfull to identify the component in the list of scene components.
+         */
+        public readonly name = SceneComponentConstants.NAME_PROCEDURALTEXTURE;
+
+        /**
+         * The scene the component belongs to.
+         */
+        public scene: Scene;
+
+        /**
+         * Creates a new instance of the component for the given scene
+         * @param scene Defines the scene to register the component in
+         */
+        constructor(scene: Scene) {
+            this.scene = scene;
+            this.scene.proceduralTextures = new Array<ProceduralTexture>();
+            scene.layers = new Array<Layer>();
+        }
+
+        /**
+         * Registers the component in a given scene
+         */
+        public register(): void {
+            this.scene._beforeClearStage.registerStep(SceneComponentConstants.STEP_BEFORECLEAR_PROCEDURALTEXTURE, this, this._beforeClear);
+        }
+
+        /**
+         * Rebuilds the elements related to this component in case of
+         * context lost for instance.
+         */
+        public rebuild(): void {
+            // Nothing to do here.
+        }
+
+        /**
+         * Disposes the component and the associated ressources.
+         */
+        public dispose(): void {
+            // Nothing to do here.
+        }
+
+        private _beforeClear(): void {
+            if (this.scene.proceduralTexturesEnabled) {
+                Tools.StartPerformanceCounter("Procedural textures", this.scene.proceduralTextures.length > 0);
+                for (var proceduralIndex = 0; proceduralIndex < this.scene.proceduralTextures.length; proceduralIndex++) {
+                    var proceduralTexture = this.scene.proceduralTextures[proceduralIndex];
+                    if (proceduralTexture._shouldRender()) {
+                        proceduralTexture.render();
+                    }
+                }
+                Tools.EndPerformanceCounter("Procedural textures", this.scene.proceduralTextures.length > 0);
+            }
+        }
+    }
+} 

+ 1 - 1
src/Particles/babylon.particleSystem.ts

@@ -1721,7 +1721,7 @@
                 serializationObject.limitVelocityDamping = particleSystem.limitVelocityDamping;
             }   
             
-            if (particleSystem.noiseTexture && particleSystem.noiseTexture instanceof ProceduralTexture) {
+            if (ProceduralTexture && particleSystem.noiseTexture && particleSystem.noiseTexture instanceof ProceduralTexture) {
                 const noiseTexture = particleSystem.noiseTexture as ProceduralTexture;
                 serializationObject.noiseTexture = noiseTexture.serialize();
             }

+ 8 - 16
src/babylon.scene.ts

@@ -898,11 +898,6 @@
         * Gets or sets a boolean indicating if procedural textures are enabled on this scene
         */
         public proceduralTexturesEnabled = true;
-        /**
-         * The list of procedural textures added to the scene
-         * @see http://doc.babylonjs.com/how_to/how_to_use_procedural_textures
-         */
-        public proceduralTextures = new Array<ProceduralTexture>();
 
         // Sound Tracks
         private _mainSoundTrack: SoundTrack;
@@ -1103,7 +1098,6 @@
             return null;
         }
 
-
         /**
          * @hidden
          * Defines the actions happening before camera updates.
@@ -1111,6 +1105,11 @@
         public _beforeCameraUpdateStage = Stage.Create<SimpleStageAction>();
         /**
          * @hidden
+         * Defines the actions happening before clear the canvas.
+         */
+        public _beforeClearStage = Stage.Create<SimpleStageAction>();
+        /**
+         * @hidden
          * Defines the actions happening before camera updates.
          */
         public _gatherRenderTargetsStage = Stage.Create<RenderTargetsStageAction>();
@@ -4548,16 +4547,8 @@
             this.onAfterRenderTargetsRenderObservable.notifyObservers(this);
             this.activeCamera = currentActiveCamera;
 
-            // Procedural textures
-            if (this.proceduralTexturesEnabled) {
-                Tools.StartPerformanceCounter("Procedural textures", this.proceduralTextures.length > 0);
-                for (var proceduralIndex = 0; proceduralIndex < this.proceduralTextures.length; proceduralIndex++) {
-                    var proceduralTexture = this.proceduralTextures[proceduralIndex];
-                    if (proceduralTexture._shouldRender()) {
-                        proceduralTexture.render();
-                    }
-                }
-                Tools.EndPerformanceCounter("Procedural textures", this.proceduralTextures.length > 0);
+            for (let step of this._beforeClearStage) {
+                step.action();
             }
 
             // Clear
@@ -4809,6 +4800,7 @@
             this._afterRenderingGroupDrawStage.clear();
             this._afterCameraDrawStage.clear();
             this._beforeCameraUpdateStage.clear();
+            this._beforeClearStage.clear();
             this._gatherRenderTargetsStage.clear();
             this._rebuildGeometryStage.clear();
             this._pointerMoveStage.clear();

+ 3 - 0
src/babylon.sceneComponent.ts

@@ -16,6 +16,7 @@
         public static readonly NAME_POSTPROCESSRENDERPIPELINEMANAGER = "PostProcessRenderPipelineManager";
         public static readonly NAME_SPRITE = "Sprite";
         public static readonly NAME_OUTLINERENDERER = "Outline";
+        public static readonly NAME_PROCEDURALTEXTURE = "ProceduralTexture";
 
         public static readonly STEP_ISREADYFORMESH_EFFECTLAYER = 0;
 
@@ -39,6 +40,8 @@
         public static readonly STEP_BEFORECAMERAUPDATE_SIMPLIFICATIONQUEUE = 0;
         public static readonly STEP_BEFORECAMERAUPDATE_GAMEPAD = 1;
 
+        public static readonly STEP_BEFORECLEAR_PROCEDURALTEXTURE = 0;
+
         public static readonly STEP_AFTERCAMERADRAW_EFFECTLAYER = 0;
         public static readonly STEP_AFTERCAMERADRAW_LENSFLARESYSTEM = 1;
         public static readonly STEP_AFTERCAMERADRAW_BOUNDINGBOXRENDERER = 2;