Selaa lähdekoodia

low ratio now works

Benjamin Guignabert 8 vuotta sitten
vanhempi
commit
4e597f4a01

Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 931 - 925
dist/preview release/babylon.d.ts


Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 931 - 925
dist/preview release/babylon.module.d.ts


+ 1 - 1
src/Materials/Textures/babylon.multiRenderTarget.ts

@@ -45,7 +45,7 @@ module BABYLON {
                 if (options.samplingModes && options.samplingModes[i]) {
                     samplingModes.push(options.samplingModes[i]);
                 } else {
-                    samplingModes.push(Texture.TRILINEAR_SAMPLINGMODE);
+                    samplingModes.push(Texture.BILINEAR_SAMPLINGMODE);
                 }
             }
 

+ 36 - 4
src/PostProcess/babylon.ssao2RenderingPipeline.ts

@@ -42,6 +42,35 @@
         @serialize()
         private _samples: number = 8;
 
+        /**
+        * Are we using bilateral blur ?
+        * @type {boolean}
+        */
+        @serialize()
+        private _expensiveBlur: boolean = true;
+        public set expensiveBlur(b: boolean) {
+            this._scene.postProcessRenderPipelineManager.detachCamerasFromRenderPipeline(this._name, this._scene.cameras);
+
+            this._expensiveBlur = b;
+            for (var i = 0; i < this._scene.cameras.length; i++) {
+                var camera = this._scene.cameras[i];
+                this._blurHPostProcess.dispose(camera);
+                this._blurVPostProcess.dispose(camera);
+            }
+
+            this._createBlurPostProcess(this._ratio.ssaoRatio);
+            this.addEffect(new PostProcessRenderEffect(this._scene.getEngine(), this.SSAORenderEffect, () => { return this._ssaoPostProcess; }, true));
+            this.addEffect(new PostProcessRenderEffect(this._scene.getEngine(), this.SSAORenderEffect, () => { return this._ssaoPostProcess; }, true));
+
+            if (this._cameras)
+                this._scene.postProcessRenderPipelineManager.attachCamerasToRenderPipeline(this._name, this._cameras);
+        }
+
+        public get expensiveBlur(): boolean {
+            return this._expensiveBlur;
+        }
+
+
         public set samples(n: number) {
             this._scene.postProcessRenderPipelineManager.detachCamerasFromRenderPipeline(this._name, this._scene.cameras);
 
@@ -52,7 +81,8 @@
             }
 
             this._createSSAOPostProcess(this._ratio.ssaoRatio);
-            this.addEffect(new PostProcessRenderEffect(this._scene.getEngine(), this.SSAORenderEffect, () => { return this._ssaoPostProcess; }, true));
+            this.addEffect(new PostProcessRenderEffect(this._scene.getEngine(), this.SSAOBlurHRenderEffect, () => { return this._blurHPostProcess; }, true));
+            this.addEffect(new PostProcessRenderEffect(this._scene.getEngine(), this.SSAOBlurVRenderEffect, () => { return this._blurVPostProcess; }, true));
 
             if (this._cameras)
                 this._scene.postProcessRenderPipelineManager.attachCamerasToRenderPipeline(this._name, this._cameras);
@@ -165,12 +195,13 @@
         private _createBlurPostProcess(ratio: number): void {
             var samples = 16;
             var samplerOffsets = [];
+            var expensive = this.expensiveBlur;
 
             for (var i = -8; i < 8; i++) {
                 samplerOffsets.push(i * 2);
             }
 
-            this._blurHPostProcess = new PostProcess("BlurH", "ssao2", ["outSize", "samplerOffsets", "near", "far", "radius"], ["depthSampler"], ratio, null, Texture.TRILINEAR_SAMPLINGMODE, this._scene.getEngine(), false, "#define BILATERAL_BLUR\n#define BILATERAL_BLUR_H\n#define SAMPLES 16");
+            this._blurHPostProcess = new PostProcess("BlurH", "ssao2", ["outSize", "samplerOffsets", "near", "far", "radius"], ["depthSampler"], ratio, null, Texture.TRILINEAR_SAMPLINGMODE, this._scene.getEngine(), false, "#define BILATERAL_BLUR\n#define BILATERAL_BLUR_H\n#define SAMPLES 16\n#define EXPENSIVE " + (expensive ? "1" : "0") + "\n");
             this._blurHPostProcess.onApply = (effect: Effect) => {
                 effect.setFloat("outSize", this._ssaoCombinePostProcess.width);
                 effect.setFloat("near", this._scene.activeCamera.minZ);
@@ -183,7 +214,7 @@
                 }
             };
 
-            this._blurVPostProcess = new PostProcess("BlurV", "ssao2", ["outSize", "samplerOffsets", "near", "far", "radius"], ["depthSampler"], ratio, null, Texture.TRILINEAR_SAMPLINGMODE, this._scene.getEngine(), false, "#define BILATERAL_BLUR\n#define SAMPLES 16");
+            this._blurVPostProcess = new PostProcess("BlurV", "ssao2", ["outSize", "samplerOffsets", "near", "far", "radius"], ["depthSampler"], ratio, null, Texture.TRILINEAR_SAMPLINGMODE, this._scene.getEngine(), false, "#define BILATERAL_BLUR\n#define SAMPLES 16\n#define EXPENSIVE " + (expensive ? "1" : "0") + "\n");
             this._blurVPostProcess.onApply = (effect: Effect) => {
                 effect.setFloat("outSize", this._ssaoCombinePostProcess.height);
                 effect.setFloat("near", this._scene.activeCamera.minZ);
@@ -243,7 +274,7 @@
             this._ssaoPostProcess = new PostProcess("ssao2", "ssao2",
                                                     [
                                                         "sampleSphere", "samplesFactor", "randTextureTiles", "totalStrength", "radius",
-                                                        "base", "range", "projection", "near", "far",
+                                                        "base", "range", "projection", "near", "far", "texelSize",
                                                         "xViewport", "yViewport"
                                                     ],
                                                     ["randomSampler", "normalSampler"],
@@ -259,6 +290,7 @@
                 }
 
                 effect.setFloat("totalStrength", this.totalStrength);
+                effect.setFloat2("texelSize", 1 / this._ssaoPostProcess.width, 1 / this._ssaoPostProcess.height);
                 effect.setFloat("radius", this.radius);
                 effect.setFloat("base", this.base);
                 effect.setFloat("near", this._scene.activeCamera.minZ);

+ 1 - 1
src/Rendering/babylon.geometryRenderer.ts

@@ -16,7 +16,7 @@ module BABYLON {
             var engine = scene.getEngine();
 
             // Render target
-            this._multiRenderTarget = new MultiRenderTarget("gBuffer", { width: engine.getRenderWidth() * ratio, height: engine.getRenderHeight() * ratio }, 2, this._scene, { generateMipMaps : true, generateDepthTexture: true });
+            this._multiRenderTarget = new MultiRenderTarget("gBuffer", { width: engine.getRenderWidth() * ratio, height: engine.getRenderHeight() * ratio }, 2, this._scene, { generateMipMaps : false, generateDepthTexture: true });
             this._multiRenderTarget.wrapU = Texture.CLAMP_ADDRESSMODE;
             this._multiRenderTarget.wrapV = Texture.CLAMP_ADDRESSMODE;
             this._multiRenderTarget.refreshRate = 1;

+ 14 - 14
src/Shaders/ssao2.fragment.fx

@@ -31,15 +31,16 @@ uniform float totalStrength;
 uniform float base;
 uniform float xViewport;
 uniform float yViewport;
+uniform vec2 texelSize;
 
 uniform mat4 projection;
 
 void main()
 {
 	vec3 random = texture2D(randomSampler, vUV * randTextureTiles).rgb;
-	float depth = texture2D(textureSampler, vUV).r;
+	float depth = texture(textureSampler, vUV).r;
 	float linearDepth = - perspectiveDepthToViewZ(depth, near, far);
-	vec3 normal = texture2D(normalSampler, vUV).rgb; 
+	vec3 normal = texture2D(normalSampler, vUV, 0.0).rgb; 
 	float occlusion = 0.0;
 
 	vec3 vViewRay = vec3((vUV.x * 2.0 - 1.0)*xViewport, (vUV.y * 2.0 - 1.0)*yViewport, 1.0);
@@ -65,7 +66,7 @@ void main()
 	  
 		// get sample linearDepth:
 	   float sampleDepth = texture(textureSampler, offset.xy).r;
-	   float linearSampleDepth = - perspectiveDepthToViewZ(texture(textureSampler, offset.xy).r, near, far);
+	   float linearSampleDepth = - perspectiveDepthToViewZ(sampleDepth, near, far);
 		// range check & accumulate:
 	   float rangeCheck = abs(linearDepth - linearSampleDepth) < radius ? 1.0 : 0.0;
 	  	difference = samplePosition.z - linearSampleDepth;
@@ -124,21 +125,16 @@ vec4 linearUpsample(sampler2D image, vec2 uv, float resolution, vec2 direction)
 	return color;
 }
 
-float gaussianKernel[16] = float[16](
-0.001014, 0.003314, 0.009248, 0.022042, 0.044857, 0.077951, 0.115676, 0.146586, 0.146586, 0.115676, 0.077951, 0.044857, 0.022042, 0.009248, 0.003314, 0.001014
-);
-
 void main()
 {
-	float texelsize = 1.0 / outSize;
+	#if EXPENSIVE
 	float compareDepth = texture2D(depthSampler, vUV).r;
 	float linearDepth = - perspectiveDepthToViewZ(compareDepth, near, far);
+	float texelsize = 1.0 / outSize;
 	float result = 0.0;
-
-	vec4 color;
 	float weightSum = 0.0;
 
-/* 	for (int i = 0; i < SAMPLES; ++i)
+	for (int i = 0; i < SAMPLES; ++i)
 	{
 		#ifdef BILATERAL_BLUR_H
 		vec2 direction = vec2(1.0, 0.0);
@@ -151,14 +147,17 @@ void main()
 
 		float sampleDepth = texture2D(depthSampler, samplePos).r;
 		float linearSampleDepth = - perspectiveDepthToViewZ(sampleDepth, near, far);
-		float weight = clamp(1.0 / ( 0.003 + abs(linearDepth - linearSampleDepth)), 0.0, 30.0) * gaussianKernel[i];
+		float weight = clamp(1.0 / ( 0.003 + abs(linearDepth - linearSampleDepth)), 0.0, 30.0);
 
 		result += texture2D(textureSampler, samplePos).r * weight;
 		weightSum += weight;
 	}
 
-	result /= weightSum; */
-
+	result /= weightSum;
+	gl_FragColor.rgb = vec3(result);
+	gl_FragColor.a = 1.0;
+	#else
+	vec4 color;
 	#ifdef BILATERAL_BLUR_H
 	vec2 direction = vec2(1.0, 0.0);
 	color = blur13(textureSampler, vUV, outSize, direction);
@@ -169,5 +168,6 @@ void main()
 
 	gl_FragColor.rgb = vec3(color.r);
 	gl_FragColor.a = 1.0;
+	#endif
 }
 #endif