Browse Source

Scene Component Tests and In Code Doc

sebastien 7 years ago
parent
commit
2529af61af

+ 1 - 1
src/Engine/babylon.engine.ts

@@ -1037,7 +1037,7 @@
         private _uintIndicesCurrentlySet = false;
         private _currentBoundBuffer = new Array<Nullable<WebGLBuffer>>();
         /** @hidden */
-        protected _currentFramebuffer: Nullable<WebGLFramebuffer>;
+        protected _currentFramebuffer: Nullable<WebGLFramebuffer> = null;
         private _currentBufferPointers = new Array<BufferPointer>();
         private _currentInstanceLocations = new Array<number>();
         private _currentInstanceBuffers = new Array<WebGLBuffer>();

+ 4 - 4
src/Layer/babylon.effectLayer.ts

@@ -118,11 +118,11 @@
             this._scene = scene || Engine.LastCreatedScene;
             let component = this._scene._getComponent(SceneComponentConstants.NAME_EFFECTLAYER) as EffectLayerSceneComponent;
             if (!component) {
-                component = new EffectLayerSceneComponent();
-                scene._addComponent(component);
+                component = new EffectLayerSceneComponent(this._scene);
+                this._scene._addComponent(component);
             }
 
-            this._engine = scene.getEngine();
+            this._engine = this._scene.getEngine();
             this._maxSize = this._engine.getCaps().maxTextureSize;
             this._scene.effectLayers.push(this);
 
@@ -601,7 +601,7 @@
                 mesh._processRendering(subMesh, this._effectLayerMapGenerationEffect, Material.TriangleFillMode, batch, hardwareInstancedRendering,
                     (isInstance, world) => this._effectLayerMapGenerationEffect.setMatrix("world", world));
             } else {
-                // Need to reset refresh rate of the shadowMap
+                // Need to reset refresh rate of the main map
                 this._mainTexture.resetRefreshCounter();
             }
         }

+ 12 - 7
src/Layer/babylon.effectLayerSceneComponent.ts

@@ -67,21 +67,26 @@
         private _previousStencilState = false;
 
         /**
-         * Registers the component in a given scene
+         * Creates a new instance of the component for the given scene
          * @param scene Defines the scene to register the component in
          */
-        public register(scene: Scene): void {
+        constructor(scene: Scene) {
             this.scene = scene;
             this._engine = scene.getEngine();
             this._effectLayers = scene.effectLayers = new Array<EffectLayer>();
+        }
 
-            scene._isReadyForMeshStage.registerStep(SceneComponentConstants.STEP_ISREADYFORMESH_EFFECTLAYER, this, this._isReadyForMesh);
+        /**
+         * Registers the component in a given scene
+         */
+        public register(): void {
+            this.scene._isReadyForMeshStage.registerStep(SceneComponentConstants.STEP_ISREADYFORMESH_EFFECTLAYER, this, this._isReadyForMesh);
 
-            scene._cameraDrawRenderTargetStage.registerStep(SceneComponentConstants.STEP_CAMERADRAWRENDERTARGET_EFFECTLAYER, this, this._renderMainTexture);
+            this.scene._cameraDrawRenderTargetStage.registerStep(SceneComponentConstants.STEP_CAMERADRAWRENDERTARGET_EFFECTLAYER, this, this._renderMainTexture);
 
-            scene._beforeCameraDrawStage.registerStep(SceneComponentConstants.STEP_BEFORECAMERADRAW_EFFECTLAYER, this, this._setStencil);
-            scene._afterCameraDrawStage.registerStep(SceneComponentConstants.STEP_AFTERCAMERADRAW_EFFECTLAYER, this, this._setStencilBack);
-            scene._afterCameraDrawStage.registerStep(SceneComponentConstants.STEP_AFTERCAMERADRAW_EFFECTLAYER_DRAW, this, this._draw);
+            this.scene._beforeCameraDrawStage.registerStep(SceneComponentConstants.STEP_BEFORECAMERADRAW_EFFECTLAYER, this, this._setStencil);
+            this.scene._afterCameraDrawStage.registerStep(SceneComponentConstants.STEP_AFTERCAMERADRAW_EFFECTLAYER, this, this._setStencilBack);
+            this.scene._afterCameraDrawStage.registerStep(SceneComponentConstants.STEP_AFTERCAMERADRAW_EFFECTLAYER_DRAW, this, this._draw);
         }
 
         /**

+ 1 - 1
src/Layer/babylon.layer.ts

@@ -65,7 +65,7 @@
             this._scene = <Scene>(scene || Engine.LastCreatedScene);
             let layerComponent = this._scene._getComponent(SceneComponentConstants.NAME_LAYER) as LayerSceneComponent;
             if (!layerComponent) {
-                layerComponent = new LayerSceneComponent();
+                layerComponent = new LayerSceneComponent(this._scene);
                 this._scene._addComponent(layerComponent);
             }
             this._scene.layers.push(this);

+ 9 - 4
src/Layer/babylon.layerSceneComponent.ts

@@ -25,16 +25,21 @@
         private _layers: Array<Layer>;
 
         /**
-         * Registers the component in a given scene
+         * Creates a new instance of the component for the given scene
          * @param scene Defines the scene to register the component in
          */
-        public register(scene: Scene): void {
+        constructor(scene: Scene) {
             this.scene = scene;
             this._engine = scene.getEngine();
             this._layers = scene.layers = new Array<Layer>();
+        }
 
-            scene._beforeCameraDrawStage.registerStep(SceneComponentConstants.STEP_BEFORECAMERADRAW_LAYER, this, this._drawBackground);
-            scene._afterCameraDrawStage.registerStep(SceneComponentConstants.STEP_AFTERCAMERADRAW_LAYER, this, this._drawForeground);
+        /**
+         * Registers the component in a given scene
+         */
+        public register(): void {
+            this.scene._beforeCameraDrawStage.registerStep(SceneComponentConstants.STEP_BEFORECAMERADRAW_LAYER, this, this._drawBackground);
+            this.scene._afterCameraDrawStage.registerStep(SceneComponentConstants.STEP_AFTERCAMERADRAW_LAYER, this, this._drawForeground);
         }
 
         /**

+ 1 - 1
src/LensFlare/babylon.lensFlareSystem.ts

@@ -21,7 +21,7 @@
             this._scene = scene || Engine.LastCreatedScene;
             let component = this._scene._getComponent(SceneComponentConstants.NAME_LENSFLARESYSTEM) as LensFlareSystemSceneComponent;
             if (!component) {
-                component = new LensFlareSystemSceneComponent();
+                component = new LensFlareSystemSceneComponent(this._scene);
                 scene._addComponent(component);
             }
 

+ 8 - 3
src/LensFlare/babylon.lensFlareSystemSceneComponent.ts

@@ -95,15 +95,20 @@
         private _lensFlareSystems: Array<LensFlareSystem>;
 
         /**
-         * Registers the component in a given scene
+         * Creates a new instance of the component for the given scene
          * @param scene Defines the scene to register the component in
          */
-        public register(scene: Scene): void {
+        constructor(scene: Scene) {
             this.scene = scene;
 
             this._lensFlareSystems = scene.lensFlareSystems = new Array<LensFlareSystem>();
+        }
 
-            scene._afterCameraDrawStage.registerStep(SceneComponentConstants.STEP_AFTERCAMERADRAW_LENSFLARESYSTEM, this, this._draw);
+        /**
+         * Registers the component in a given scene
+         */
+        public register(): void {
+            this.scene._afterCameraDrawStage.registerStep(SceneComponentConstants.STEP_AFTERCAMERADRAW_LENSFLARESYSTEM, this, this._draw);
         }
 
         /**

+ 21 - 11
src/babylon.scene.ts

@@ -1279,13 +1279,26 @@
         private _transientComponents: ISceneComponent[] = [];
 
         /**
+         * Registers the transient components if needed.
+         */
+        private _registerTransientComponents(): void {
+            // Register components that have been associated lately to the scene.
+            if (this._transientComponents.length > 0) {
+                for (let component of this._transientComponents) {
+                    component.register();
+                }
+                this._transientComponents = [];
+            }
+        }
+
+        /**
          * Add a component to the scene.
          * Note that the ccomponent could be registered on th next frame if this is called after 
          * the register component stage.
          */
         public _addComponent(component: ISceneComponent) {
-            this._transientComponents.push(component);
             this._components.push(component);
+            this._transientComponents.push(component);
         }
 
         /**
@@ -1305,19 +1318,19 @@
         /**
          * Defines the actions happening during the per mesh ready checks.
          */
-        public _isReadyForMeshStage = new Stage<MeshStageAction>();
+        public _isReadyForMeshStage = Stage.Create<MeshStageAction>();
         /**
          * Defines the actions happening during the per camera render target step.
          */
-        public _cameraDrawRenderTargetStage = new Stage<CameraStageAction>();
+        public _cameraDrawRenderTargetStage = Stage.Create<CameraStageAction>();
         /**
          * Defines the actions happening just before the active camera is drawing.
          */
-        public _beforeCameraDrawStage = new Stage<CameraStageAction>();
+        public _beforeCameraDrawStage = Stage.Create<CameraStageAction>();
         /**
          * Defines the actions happening just after the active camera is drawing.
          */
-        public _afterCameraDrawStage = new Stage<CameraStageAction>();
+        public _afterCameraDrawStage = Stage.Create<CameraStageAction>();
        
         /**
          * Creates a new Scene
@@ -2595,6 +2608,8 @@
 
         /** @hidden */
         public _checkIsReady() {
+            this._registerTransientComponents();
+
             if (this.isReady()) {
                 this.onReadyObservable.notifyObservers(this);
 
@@ -4628,12 +4643,7 @@
             }
 
             // Register components that have been associated lately to the scene.
-            if (this._transientComponents.length > 0) {
-                for (let component of this._transientComponents) {
-                    component.register(this);
-                }
-                this._transientComponents = [];
-            }
+            this._registerTransientComponents();
 
             this._activeParticles.fetchNewFrame();
             this._totalVertices.fetchNewFrame();

+ 16 - 2
src/babylon.sceneComponent.ts

@@ -41,9 +41,8 @@
 
         /**
          * Register the component to one instance of a scene.
-         * @param scene defines the scene to register the component for
          */
-        register(scene: Scene): void;
+        register(): void;
 
         /**
          * Adds all the element from the container to the scene
@@ -95,6 +94,21 @@
      */
     export class Stage<T extends Function> extends Array<{ index: number, component: ISceneComponent, action: T }> {
         /**
+         * Hide ctor from the rest of the world.
+         * @param items 
+         */
+        private constructor(items?: { index: number, component: ISceneComponent, action: T }[]) {
+            super(...<any>items)
+        }
+
+        /**
+         * Creates a new Stage.
+         */
+        static Create<T extends Function>(): Stage<T> {
+            return Object.create(Stage.prototype);
+        }
+
+        /**
          * Registers a step in an ordered way in the targeted stage.
          * @param index Defines the position to register the step in
          * @param component Defines the component attached to the step