Browse Source

Move DepthRenderer as component

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

+ 2 - 1
Tools/Gulp/config.json

@@ -719,7 +719,8 @@
         },
         "depthRenderer": {
             "files": [
-                "../../src/Rendering/babylon.depthRenderer.js"
+                "../../src/Rendering/babylon.depthRenderer.js",
+                "../../src/Rendering/babylon.depthRendererSceneComponent.js"
             ],
             "dependUpon": [
                 "core"

+ 7 - 0
src/Rendering/babylon.depthRenderer.ts

@@ -19,6 +19,13 @@
          */
         constructor(scene: Scene, type: number = Engine.TEXTURETYPE_FLOAT, camera:Nullable<Camera> = null) {
             this._scene = scene;
+            // Register the G Buffer component to the scene.
+            let component = scene._getComponent(SceneComponentConstants.NAME_DEPTHRENDERER) as DepthRendererSceneComponent;
+            if (!component) {
+                component = new DepthRendererSceneComponent(scene);
+                scene._addComponent(component);
+            }
+
             this._camera = camera;
             var engine = scene.getEngine();
 

+ 109 - 0
src/Rendering/babylon.depthRendererSceneComponent.ts

@@ -0,0 +1,109 @@
+module BABYLON {
+    export interface Scene {
+        /** @hidden (Backing field) */
+        _depthRenderer: { [id: string]: DepthRenderer };
+
+        /**
+         * Creates a depth renderer a given camera which contains a depth map which can be used for post processing.
+         * @param camera The camera to create the depth renderer on (default: scene's active camera)
+         * @returns the created depth renderer
+         */
+        enableDepthRenderer(camera?: Nullable<Camera>): DepthRenderer;
+
+        /**
+         * Disables a depth renderer for a given camera
+         * @param camera The camera to disable the depth renderer on (default: scene's active camera)
+         */
+        disableDepthRenderer(camera?: Nullable<Camera>): void;
+    }
+
+    Scene.prototype.enableDepthRenderer = function(camera?: Nullable<Camera>): DepthRenderer {
+        camera = camera || this.activeCamera;
+        if (!camera) {
+            throw "No camera available to enable depth renderer";
+        }
+        if (!this._depthRenderer) {
+            this._depthRenderer = {};
+        }
+        if (!this._depthRenderer[camera.id]) {
+            var textureType = 0;
+            if (this.getEngine().getCaps().textureHalfFloatRender) {
+                textureType = Engine.TEXTURETYPE_HALF_FLOAT;
+            }
+            else if (this.getEngine().getCaps().textureFloatRender) {
+                textureType = Engine.TEXTURETYPE_FLOAT;
+            } else {
+                throw "Depth renderer does not support int texture type";
+            }
+            this._depthRenderer[camera.id] = new DepthRenderer(this, textureType, camera);
+        }
+
+        return this._depthRenderer[camera.id];
+    }
+
+    Scene.prototype.disableDepthRenderer = function(camera?: Nullable<Camera>): void {
+        camera = camera || this.activeCamera;
+        if (!camera || !this._depthRenderer || !this._depthRenderer[camera.id]) {
+            return;
+        }
+
+        this._depthRenderer[camera.id].dispose();
+        delete this._depthRenderer[camera.id];
+    }
+
+    /**
+     * Defines the Depth Renderer scene component responsible to manage a depth buffer usefull
+     * in several rendering techniques.
+     */
+    export class DepthRendererSceneComponent implements ISceneComponent {
+        /**
+         * The component name helpfull to identify the component in the list of scene components.
+         */
+        public readonly name = SceneComponentConstants.NAME_DEPTHRENDERER;
+
+        /**
+         * 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;
+        }
+
+        /**
+         * Registers the component in a given scene
+         */
+        public register(): void {
+            this.scene._gatherRenderTargetsStage.registerStep(SceneComponentConstants.STEP_GATHERRENDERTARGETS_DEPTHRENDERER, this, this._gatherRenderTargets);
+        }
+
+        /**
+         * Rebuilds the elements related to this component in case of
+         * context lost for instance.
+         */
+        public rebuild(): void {
+            // Nothing to do for this component
+        }
+
+        /**
+         * Disposes the component and the associated ressources
+         */
+        public dispose(): void {
+            for (var key in this.scene._depthRenderer) {
+                this.scene._depthRenderer[key].dispose();
+            }
+        }
+
+        private _gatherRenderTargets(renderTargets: SmartArrayNoDuplicate<RenderTargetTexture>): void {
+            if (this.scene._depthRenderer) {
+                for (var key in this.scene._depthRenderer) {
+                    renderTargets.push(this.scene._depthRenderer[key].getDepthMap());
+                }
+            }
+        }
+    }
+} 

+ 0 - 51
src/babylon.scene.ts

@@ -1072,8 +1072,6 @@
 
         private _debugLayer: DebugLayer;
 
-        private _depthRenderer: { [id: string]: DepthRenderer } = {};
-
         private _pickedDownMesh: Nullable<AbstractMesh>;
         private _pickedUpMesh: Nullable<AbstractMesh>;
         private _pickedDownSprite: Nullable<Sprite>;
@@ -4669,11 +4667,6 @@
                 step.action(this._renderTargets);
             }
 
-            // Depth renderer
-            for (var key in this._depthRenderer) {
-                this._renderTargets.push(this._depthRenderer[key].getDepthMap());
-            }
-
             // RenderPipeline
             if (this._postProcessRenderPipelineManager) {
                 this._postProcessRenderPipelineManager.update();
@@ -4861,46 +4854,6 @@
             }
         }
 
-        /**
-         * Creates a depth renderer a given camera which contains a depth map which can be used for post processing.
-         * @param camera The camera to create the depth renderer on (default: scene's active camera)
-         * @returns the created depth renderer
-         */
-        public enableDepthRenderer(camera?: Nullable<Camera>): DepthRenderer {
-            camera = camera || this.activeCamera;
-            if (!camera) {
-                throw "No camera available to enable depth renderer";
-            }
-            if (!this._depthRenderer[camera.id]) {
-                var textureType = 0;
-                if (this._engine.getCaps().textureHalfFloatRender) {
-                    textureType = Engine.TEXTURETYPE_HALF_FLOAT;
-                }
-                else if (this._engine.getCaps().textureFloatRender) {
-                    textureType = Engine.TEXTURETYPE_FLOAT;
-                } else {
-                    throw "Depth renderer does not support int texture type";
-                }
-                this._depthRenderer[camera.id] = new DepthRenderer(this, textureType, camera);
-            }
-
-            return this._depthRenderer[camera.id];
-        }
-
-        /**
-         * Disables a depth renderer for a given camera
-         * @param camera The camera to disable the depth renderer on (default: scene's active camera)
-         */
-        public disableDepthRenderer(camera?: Nullable<Camera>): void {
-            camera = camera || this.activeCamera;
-            if (!camera || !this._depthRenderer[camera.id]) {
-                return;
-            }
-
-            this._depthRenderer[camera.id].dispose();
-            delete this._depthRenderer[camera.id];
-        }
-
         /** 
          * Freeze all materials
          * A frozen material will not be updatable but should be faster to render
@@ -4952,10 +4905,6 @@
 
             this.resetCachedMaterial();
 
-            for (var key in this._depthRenderer) {
-                this._depthRenderer[key].dispose();
-            }
-
             // Smart arrays
             if (this.activeCamera) {
                 this.activeCamera._activeMeshes.dispose();

+ 2 - 0
src/babylon.sceneComponent.ts

@@ -12,6 +12,7 @@
         public static readonly NAME_GAMEPAD = "Gamepad";
         public static readonly NAME_SIMPLIFICATIONQUEUE = "SimplificationQueue";
         public static readonly NAME_GEOMETRYBUFFERRENDERER = "GeometryBufferRenderer";
+        public static readonly NAME_DEPTHRENDERER = "DepthRenderer";
 
         public static readonly STEP_ISREADYFORMESH_EFFECTLAYER = 0;
 
@@ -38,6 +39,7 @@
         public static readonly STEP_AFTERCAMERADRAW_LAYER = 4;
 
         public static readonly STEP_GATHERRENDERTARGETS_GEOMETRYBUFFERRENDERER = 0;
+        public static readonly STEP_GATHERRENDERTARGETS_DEPTHRENDERER = 1;
     }
 
     /**

BIN
tests/validation/ReferenceImages/depthRenderer.png


+ 6 - 0
tests/validation/config.json

@@ -529,6 +529,12 @@
       "referenceImage": "ssao2.png",
       "renderCount": 200,
       "excludeFromAutomaticTesting": true
+    },
+    {
+      "title": "DepthRenderer",
+      "playgroundId": "#3HPMAA#0",
+      "renderCount": 50,
+      "referenceImage": "depthRenderer.png"
     }
   ]
 }