Browse Source

use output width for kernel blur, use coc when doing dof kernel blur

Trevor Baron 7 years ago
parent
commit
d5f66f1536

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

@@ -68,7 +68,11 @@
 			super(name, "kernelBlur", ["delta", "direction", "cameraMinMaxZ"], ["circleOfConfusionSampler"], options, camera, samplingMode, engine, reusable, null, textureType, "kernelBlur", {varyingCount: 0, depCount: 0}, true);
 			this._staticDefines = defines;
 			this.onApplyObservable.add((effect: Effect) => {
-				effect.setFloat2('delta', (1 / this.width) * this.direction.x, (1 / this.height) * this.direction.y);
+				if(this._outputTexture){
+					effect.setFloat2('delta', (1 / this._outputTexture.width) * this.direction.x, (1 / this._outputTexture.height) * this.direction.y);
+				}else{
+					effect.setFloat2('delta', (1 / this.width) * this.direction.x, (1 / this.height) * this.direction.y);
+				}
 			});
 
             this.kernel = kernel;

+ 2 - 2
src/PostProcess/babylon.depthOfFieldEffect.ts

@@ -109,11 +109,11 @@ module BABYLON {
                 }
             }
             var adjustedKernelSize = kernelSize/Math.pow(2, blurCount-1);
-            var ratio = 1.0/Math.pow(2, 0);
+            var ratio = 1.0;
             for(var i = 0;i<blurCount;i++){
                 var blurY = new DepthOfFieldBlurPostProcess("verticle blur", scene, new Vector2(0, 1.0), adjustedKernelSize, ratio, null, this._circleOfConfusion, i == 0 ? this._circleOfConfusion : null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, pipelineTextureType, blockCompilation);
                 blurY.autoClear = false;
-                ratio = 1.0/Math.pow(2, i);
+                ratio = 0.75/Math.pow(2, i);
                 var blurX = new DepthOfFieldBlurPostProcess("horizontal blur", scene, new Vector2(1.0, 0), adjustedKernelSize, ratio, null,  this._circleOfConfusion, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false, pipelineTextureType, blockCompilation);
                 blurX.autoClear = false;
                 this._depthOfFieldBlurY.push(blurY);

+ 2 - 2
src/PostProcess/babylon.depthOfFieldMergePostProcess.ts

@@ -42,13 +42,13 @@ module BABYLON {
          * @param blockCompilation If compilation of the shader should not be done in the constructor. The updateEffect method can be used to compile the shader at a later time. (default: false)
          */
         constructor(name: string, originalFromInput:PostProcess, circleOfConfusion:PostProcess, private blurSteps:Array<PostProcess>, options: number | PostProcessOptions, camera: Nullable<Camera>, samplingMode?: number, engine?: Engine, reusable?: boolean, textureType: number = Engine.TEXTURETYPE_UNSIGNED_INT, blockCompilation = false) {
-            super(name, "depthOfFieldMerge", ["bloomWeight"], ["circleOfConfusionSampler", "blurStep0", "blurStep1", "blurStep2", "bloomBlur"], options, camera, samplingMode, engine, reusable, null, textureType, undefined, null, true);
+            super(name, "depthOfFieldMerge", [], ["circleOfConfusionSampler", "blurStep0", "blurStep1", "blurStep2"], options, camera, samplingMode, engine, reusable, null, textureType, undefined, null, true);
             this.onApplyObservable.add((effect: Effect) => {
                 effect.setTextureFromPostProcess("textureSampler", originalFromInput);
                 effect.setTextureFromPostProcessOutput("circleOfConfusionSampler", circleOfConfusion);
                 blurSteps.forEach((step,index)=>{
                     effect.setTextureFromPostProcessOutput("blurStep"+(blurSteps.length-index-1), step);
-                });    
+                });  
             });
 
             if(!blockCompilation){

+ 2 - 0
src/PostProcess/babylon.postProcessManager.ts

@@ -154,8 +154,10 @@
                 } else {
                     if (targetTexture) {
                         engine.bindFramebuffer(targetTexture, faceIndex, undefined, undefined, forceFullscreenViewport);
+                        pp._outputTexture = targetTexture;
                     } else {
                         engine.restoreDefaultFramebuffer();
+                        pp._outputTexture = null;
                     }
                 }
 

+ 6 - 2
src/Shaders/ShadersInclude/kernelBlurFragment.fx

@@ -1,6 +1,10 @@
 #ifdef DOF
-    sampleDepth = sampleDistance(sampleCoord{X});
-    factor = clamp(1.0-((centerSampleDepth - sampleDepth)/centerSampleDepth),0.0,1.0);
+    if(sampleCoord{X} == sampleCenter){
+        factor = 1.;
+    }else{
+        factor = sampleCoC(sampleCoord{X});
+    }
+    
     computedWeight = KERNEL_WEIGHT{X} * factor;
     sumOfWeights += computedWeight;
 #else

+ 6 - 2
src/Shaders/ShadersInclude/kernelBlurFragment2.fx

@@ -1,6 +1,10 @@
 #ifdef DOF
-    sampleDepth = sampleDistance(sampleCoord{X});
-    factor = clamp(1.0-((centerSampleDepth - sampleDepth)/centerSampleDepth),0.0,1.0);
+    if(sampleCenter + delta * KERNEL_DEP_OFFSET{X} == sampleCenter){
+        factor = 1.;
+    }else{
+        factor = sampleCoC(sampleCenter + delta * KERNEL_DEP_OFFSET{X});
+    }
+
     computedWeight = KERNEL_DEP_WEIGHT{X} * factor;
     sumOfWeights += computedWeight;
 #else

+ 4 - 0
src/Shaders/kernelBlur.fragment.fx

@@ -14,6 +14,10 @@ varying vec2 sampleCenter;
 		float depth = texture2D(circleOfConfusionSampler, offset).g; // depth value from DepthRenderer: 0 to 1 
 		return cameraMinMaxZ.x + (cameraMinMaxZ.y - cameraMinMaxZ.x)*depth; // actual distance from the lens 
 	}
+	float sampleCoC(const in vec2 offset) {
+		float coc = texture2D(circleOfConfusionSampler, offset).r; 
+		return coc; // actual distance from the lens 
+	}
 #endif
 
 #include<kernelBlurVaryingDeclaration>[0..varyingCount]