瀏覽代碼

handle bloom disposal/state

Trevor Baron 7 年之前
父節點
當前提交
bfc5c20ecc

+ 17 - 1
src/PostProcess/RenderPipeline/Pipelines/babylon.defaultRenderingPipeline.ts

@@ -128,6 +128,13 @@
             }
             this._bloomScale = value;
 
+            // recreate bloom and dispose old as this setting is not dynamic
+            var oldBloom = this.bloom;
+            this.bloom = new BloomEffect(this._scene, this.bloomScale, this.bloomKernel, this._defaultPipelineTextureType);
+            for (var i = 0; i < this._cameras.length; i++) {
+                oldBloom.disposeEffects(this._cameras[i]);
+            }
+
             this._buildPipeline();
         }
 
@@ -311,6 +318,8 @@
 
             this.depthOfField = new DepthOfFieldEffect(this._scene, null, this._depthOfFieldBlurLevel, this._defaultPipelineTextureType, true);
             
+            this.bloom = new BloomEffect(this._scene, this.bloomScale, this.bloomKernel, this._defaultPipelineTextureType, true);
+
             this.chromaticAberration = new ChromaticAberrationPostProcess("ChromaticAberration", engine.getRenderWidth(), engine.getRenderHeight(), 1.0, null, Texture.BILINEAR_SAMPLINGMODE, engine, false, this._defaultPipelineTextureType, true);
             this._chromaticAberrationEffect = new PostProcessRenderEffect(engine, this.ChromaticAberrationPostProcessId, () => { return this.chromaticAberration; }, true);
             
@@ -387,7 +396,9 @@
             }
 
             if (this.bloomEnabled) {
-                this.bloom = new BloomEffect(this._scene, this.bloomScale, this.bloomKernel);
+                if(!this.bloom._isReady()){
+                    this.bloom._updateEffects();
+                }
                 this.addEffect(this.bloom);
             }
 
@@ -437,6 +448,10 @@
                     if(this.depthOfField){
                         this.depthOfField.disposeEffects(camera);
                     }
+
+                    if(this.bloom){
+                        this.bloom.disposeEffects(camera);
+                    }
     
                     if(this.chromaticAberration){
                         this.chromaticAberration.dispose(camera);
@@ -450,6 +465,7 @@
             if(disposeNonRecreated){
                 (<any>this.sharpen) = null;
                 (<any>this.depthOfField) = null;
+                (<any>this.bloom) = null;
                 (<any>this.chromaticAberration) = null;
             } 
         }

+ 47 - 13
src/PostProcess/babylon.bloomEffect.ts

@@ -4,6 +4,8 @@ module BABYLON {
      */
     export class BloomEffect extends PostProcessRenderEffect{
         private _effects: Array<PostProcess> = [];
+        private blurX:BlurPostProcess;
+        private blurY:BlurPostProcess;
         public _merge:DepthOfFieldMergePostProcess;
 
         /**
@@ -19,25 +21,57 @@ module BABYLON {
                 return this._effects;
             }, true);
             
-            var blurX = new BlurPostProcess("horizontal blur", new Vector2(1.0, 0), 10.0, bloomScale, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, pipelineTextureType);
-            blurX.alwaysForcePOT = true;
-            blurX.onActivateObservable.add(() => {
-                let dw = blurX.width / scene.getEngine().getRenderWidth(true);
-                blurX.kernel = bloomKernel * dw;
+            this.blurX = new BlurPostProcess("horizontal blur", new Vector2(1.0, 0), 10.0, bloomScale, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, pipelineTextureType, undefined, blockCompilation);
+            this.blurX.alwaysForcePOT = true;
+            this.blurX.onActivateObservable.add(() => {
+                let dw = this.blurX.width / scene.getEngine().getRenderWidth(true);
+                this.blurX.kernel = bloomKernel * dw;
             });
 
-            var blurY = new BlurPostProcess("vertical blur", new Vector2(0, 1.0), 10.0, bloomScale, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, pipelineTextureType);
-            blurY.alwaysForcePOT = true;
-            blurY.autoClear = false;
-            blurY.onActivateObservable.add(() => {
-                let dh = blurY.height / scene.getEngine().getRenderHeight(true);
-                blurY.kernel = bloomKernel * dh;
+            this.blurY = new BlurPostProcess("vertical blur", new Vector2(0, 1.0), 10.0, bloomScale, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, pipelineTextureType, undefined, blockCompilation);
+            this.blurY.alwaysForcePOT = true;
+            this.blurY.autoClear = false;
+            this.blurY.onActivateObservable.add(() => {
+                let dh = this.blurY.height / scene.getEngine().getRenderHeight(true);
+                this.blurY.kernel = bloomKernel * dh;
             });
             
-            this._merge = new DepthOfFieldMergePostProcess("depthOfFieldMerge", {originalFromInput: blurX, bloom: {blurred: blurY, weight: 0}}, 1, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, pipelineTextureType, blockCompilation);
+            this._merge = new DepthOfFieldMergePostProcess("depthOfFieldMerge", {originalFromInput: this.blurX, bloom: {blurred: this.blurY, weight: 0}}, 1, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, pipelineTextureType, blockCompilation);
             this._merge.autoClear = false;
 
-            this._effects = [blurX, blurY, this._merge]
+            this._effects = [this.blurX, this.blurY, this._merge]
+        }
+
+        /**
+         * Disposes each of the internal effects for a given camera.
+         * @param camera The camera to dispose the effect on.
+         */
+        public disposeEffects(camera:Camera){
+            this.blurX.dispose(camera);
+            this.blurY.dispose(camera);
+            this._merge.dispose(camera);
+        }
+        
+        /**
+         * Internal
+         */
+        public _updateEffects(){
+            for(var effect in this._effects){
+                this._effects[effect].updateEffect();
+            }
+        }
+
+        /**
+         * Internal
+         * @returns if all the contained post processes are ready.
+         */
+        public _isReady(){
+            for(var effect in this._effects){
+                if(!this._effects[effect].isReady()){
+                    return false;
+                }
+            }
+            return true;
         }
     }
 }

+ 1 - 0
src/PostProcess/babylon.blurPostProcess.ts

@@ -174,6 +174,7 @@
 				defines += `#define PACKEDFLOAT 1`;
 			}
 
+			this.blockCompilation = false;
             super.updateEffect(defines, null, null, {
 				varyingCount: varyingCount,
 				depCount: depCount