Преглед на файлове

Add GeometryBuffer as a component

sebastien преди 7 години
родител
ревизия
17a19d7be5
променени са 4 файла, в които са добавени 137 реда и са изтрити 52 реда
  1. 1 1
      src/Mesh/babylon.meshSimplification.ts
  2. 117 0
      src/Rendering/babylon.geometryBufferRenderer.ts
  3. 11 51
      src/babylon.scene.ts
  4. 8 0
      src/babylon.sceneComponent.ts

+ 1 - 1
src/Mesh/babylon.meshSimplification.ts

@@ -38,7 +38,7 @@
          * @param type the type of simplification to run.
          * @param successCallback optional success callback to be called after the simplification finished processing all settings.
          */
-        simplify(settings: Array<ISimplificationSettings>, parallelProcessing: boolean, simplificationType: SimplificationType, successCallback?: (mesh?: Mesh, submeshIndex?: number) => void): Mesh;
+        simplify(settings: Array<ISimplificationSettings>, parallelProcessing?: boolean, simplificationType?: SimplificationType, successCallback?: (mesh?: Mesh, submeshIndex?: number) => void): Mesh;
     }
 
     Mesh.prototype.simplify = function(settings: Array<ISimplificationSettings>, parallelProcessing: boolean = true, simplificationType: SimplificationType = SimplificationType.QUADRATIC, successCallback?: (mesh?: Mesh, submeshIndex?: number) => void): Mesh {

+ 117 - 0
src/Rendering/babylon.geometryBufferRenderer.ts

@@ -1,4 +1,62 @@
 module BABYLON {
+    export interface Scene {
+        /** @hidden (Backing field) */
+        _geometryBufferRenderer: Nullable<GeometryBufferRenderer>;
+
+        /**
+         * Gets or Sets the current geometry buffer associated to the scene.
+         */
+        geometryBufferRenderer: Nullable<GeometryBufferRenderer>;
+
+        /**
+         * Enables a GeometryBufferRender and associates it with the scene
+         * @param ratio defines the scaling ratio to apply to the renderer (1 by default which means same resolution)
+         * @returns the GeometryBufferRenderer
+         */
+        enableGeometryBufferRenderer(ratio?: number): Nullable<GeometryBufferRenderer>;
+
+        /**
+         * Disables the GeometryBufferRender associated with the scene
+         */
+        disableGeometryBufferRenderer(): void;
+    }
+
+    Object.defineProperty(Scene.prototype, "geometryBufferRenderer", {
+        get: function (this:Scene) {
+            this._geometryBufferRenderer;
+        },
+        set: function (this:Scene, value: Nullable<GeometryBufferRenderer>) {
+            if (value && value.isSupported) {
+                this._geometryBufferRenderer = value;
+            };
+        },
+        enumerable: true,
+        configurable: true
+    });
+
+    Scene.prototype.enableGeometryBufferRenderer = function(ratio: number = 1): Nullable<GeometryBufferRenderer> {
+        if (this._geometryBufferRenderer) {
+            return this._geometryBufferRenderer;
+        }
+
+        this._geometryBufferRenderer = new GeometryBufferRenderer(this, ratio);
+        if (!this._geometryBufferRenderer.isSupported) {
+            this._geometryBufferRenderer = null;
+        }
+
+        return this._geometryBufferRenderer;
+    }
+
+    Scene.prototype.disableGeometryBufferRenderer = function(): void {
+        if (!this._geometryBufferRenderer) {
+            return;
+        }
+
+        this._geometryBufferRenderer.dispose();
+        this._geometryBufferRenderer = null;
+    }
+
+
     /**
      * This renderer is helpfull to fill one of the render target with a geometry buffer.
      */
@@ -67,6 +125,13 @@ module BABYLON {
             this._scene = scene;
             this._ratio = ratio;
 
+            // Register the G Buffer component to the scene.
+            let component = scene._getComponent(SceneComponentConstants.NAME_GEOMETRYBUFFER) as GeometryBufferSceneComponent;
+            if (!component) {
+                component = new GeometryBufferSceneComponent(scene);
+                scene._addComponent(component);
+            }
+
             // Render target
             this._createRenderTargets();
         }
@@ -269,4 +334,56 @@ module BABYLON {
             };
         }
     }
+
+    /**
+     * Defines the Geometry Buffer scene component responsible to manage a G-Buffer usefull
+     * in several rendering techniques.
+     */
+    export class GeometryBufferSceneComponent implements ISceneComponent {
+        /**
+         * The component name helpfull to identify the component in the list of scene components.
+         */
+        public readonly name = SceneComponentConstants.NAME_GEOMETRYBUFFER;
+
+        /**
+         * 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_GEOMETRYBUFFER, 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 {
+            // Nothing to do for this component
+        }
+
+        private _gatherRenderTargets(renderTargets: SmartArrayNoDuplicate<RenderTargetTexture>): void {
+            if (this.scene._geometryBufferRenderer) {
+                renderTargets.push(this.scene._geometryBufferRenderer.getGBuffer());
+            }
+        }
+    }
 } 

+ 11 - 51
src/babylon.scene.ts

@@ -1073,22 +1073,6 @@
         private _debugLayer: DebugLayer;
 
         private _depthRenderer: { [id: string]: DepthRenderer } = {};
-        private _geometryBufferRenderer: Nullable<GeometryBufferRenderer>;
-
-        /**
-         * Gets the current geometry buffer associated to the scene.
-         */
-        public get geometryBufferRenderer(): Nullable<GeometryBufferRenderer> {
-            return this._geometryBufferRenderer;
-        }
-        /**
-         * Sets the current geometry buffer for the scene.
-         */
-        public set geometryBufferRenderer(geometryBufferRenderer: Nullable<GeometryBufferRenderer>) {
-            if (geometryBufferRenderer && geometryBufferRenderer.isSupported) {
-                this._geometryBufferRenderer = geometryBufferRenderer;
-            }
-        }
 
         private _pickedDownMesh: Nullable<AbstractMesh>;
         private _pickedUpMesh: Nullable<AbstractMesh>;
@@ -1166,6 +1150,11 @@
         public _beforeCameraUpdateStage = Stage.Create<SimpleStageAction>();
         /**
          * @hidden
+         * Defines the actions happening before camera updates.
+         */
+        public _gatherRenderTargetsStage = Stage.Create<RenderTargetsStageAction>();
+        /**
+         * @hidden
          * Defines the actions happening during the per mesh ready checks.
          */
         public _isReadyForMeshStage = Stage.Create<MeshStageAction>();
@@ -4675,16 +4664,16 @@
                 }
             }
 
+            // Collects render targets from external components.
+            for (let step of this._gatherRenderTargetsStage) {
+                step.action(this._renderTargets);
+            }
+
             // Depth renderer
             for (var key in this._depthRenderer) {
                 this._renderTargets.push(this._depthRenderer[key].getDepthMap());
             }
 
-            // Geometry renderer
-            if (this._geometryBufferRenderer) {
-                this._renderTargets.push(this._geometryBufferRenderer.getGBuffer());
-            }
-
             // RenderPipeline
             if (this._postProcessRenderPipelineManager) {
                 this._postProcessRenderPipelineManager.update();
@@ -4912,36 +4901,6 @@
             delete this._depthRenderer[camera.id];
         }
 
-        /**
-         * Enables a GeometryBufferRender and associates it with the scene
-         * @param ratio defines the scaling ratio to apply to the renderer (1 by default which means same resolution)
-         * @returns the GeometryBufferRenderer
-         */
-        public enableGeometryBufferRenderer(ratio: number = 1): Nullable<GeometryBufferRenderer> {
-            if (this._geometryBufferRenderer) {
-                return this._geometryBufferRenderer;
-            }
-
-            this._geometryBufferRenderer = new GeometryBufferRenderer(this, ratio);
-            if (!this._geometryBufferRenderer.isSupported) {
-                this._geometryBufferRenderer = null;
-            }
-
-            return this._geometryBufferRenderer;
-        }
-
-        /**
-         * Disables the GeometryBufferRender associated with the scene
-         */
-        public disableGeometryBufferRenderer(): void {
-            if (!this._geometryBufferRenderer) {
-                return;
-            }
-
-            this._geometryBufferRenderer.dispose();
-            this._geometryBufferRenderer = null;
-        }
-
         /** 
          * Freeze all materials
          * A frozen material will not be updatable but should be faster to render
@@ -4982,6 +4941,7 @@
             this._afterRenderingGroupDrawStage.clear();
             this._afterCameraDrawStage.clear();
             this._beforeCameraUpdateStage.clear();
+            this._gatherRenderTargetsStage.clear();
             for (let component of this._components) {
                 component.dispose();
             }

+ 8 - 0
src/babylon.sceneComponent.ts

@@ -11,6 +11,7 @@
         public static readonly NAME_PARTICLESYSTEM = "ParticleSystem";
         public static readonly NAME_GAMEPAD = "Gamepad";
         public static readonly NAME_SIMPLIFICATIONQUEUE = "SimplificationQueue";
+        public static readonly NAME_GEOMETRYBUFFER = "GeometryBuffer";
 
         public static readonly STEP_ISREADYFORMESH_EFFECTLAYER = 0;
 
@@ -35,6 +36,8 @@
         public static readonly STEP_AFTERCAMERADRAW_BOUNDINGBOXRENDERER = 2;
         public static readonly STEP_AFTERCAMERADRAW_EFFECTLAYER_DRAW = 3;
         public static readonly STEP_AFTERCAMERADRAW_LAYER = 4;
+
+        public static readonly STEP_GATHERRENDERTARGETS_GEOMETRYBUFFER = 0;
     }
 
     /**
@@ -127,6 +130,11 @@
     export type SimpleStageAction = () => void;
 
     /** 
+     * Strong typing of a render target action.
+     */
+    export type RenderTargetsStageAction = (renderTargets: SmartArrayNoDuplicate<RenderTargetTexture>) => void;
+
+    /** 
      * Repressentation of a stage in the scene (Basically a list of ordered steps) 
      * @hidden
      */