浏览代码

using custom depth buffer limits Zfighting artifacts

Benjamin Guignabert 8 年之前
父节点
当前提交
a8482d6f0e

文件差异内容过多而无法显示
+ 6550 - 6550
dist/preview release/babylon.d.ts


文件差异内容过多而无法显示
+ 11 - 11
dist/preview release/babylon.js


+ 17 - 38
dist/preview release/babylon.max.js

@@ -42402,8 +42402,8 @@ var BABYLON;
             enumerable: true,
             configurable: true
         });
-        PostProcess.prototype.updateEffect = function (defines) {
-            this._effect = this._engine.createEffect({ vertex: this._vertexUrl, fragment: this._fragmentUrl }, ["position"], this._parameters, this._samplers, defines !== undefined ? defines : "");
+        PostProcess.prototype.updateEffect = function (defines, uniforms, samplers) {
+            this._effect = this._engine.createEffect({ vertex: this._vertexUrl, fragment: this._fragmentUrl }, ["position"], uniforms || this._parameters, samplers || this._samplers, defines !== undefined ? defines : "");
         };
         PostProcess.prototype.isReusable = function () {
             return this._reusable;
@@ -48013,18 +48013,10 @@ var BABYLON;
                 return this._samples;
             },
             set: function (n) {
-                var _this = this;
-                this._scene.postProcessRenderPipelineManager.detachCamerasFromRenderPipeline(this._name, this._scene.cameras);
+                this._ssaoPostProcess.updateEffect("#define SAMPLES " + n + "\n#define SSAO");
                 this._samples = n;
-                for (var i = 0; i < this._scene.cameras.length; i++) {
-                    var camera = this._scene.cameras[i];
-                    this._ssaoPostProcess.dispose(camera);
-                }
-                this._createSSAOPostProcess(this._ratio.ssaoRatio);
-                this.addEffect(new BABYLON.PostProcessRenderEffect(this._scene.getEngine(), this.SSAOBlurHRenderEffect, function () { return _this._blurHPostProcess; }, true));
-                this.addEffect(new BABYLON.PostProcessRenderEffect(this._scene.getEngine(), this.SSAOBlurVRenderEffect, function () { return _this._blurVPostProcess; }, true));
-                if (this._cameras)
-                    this._scene.postProcessRenderPipelineManager.attachCamerasToRenderPipeline(this._name, this._cameras);
+                this._sampleSphere = this._generateHemisphere();
+                this._firstUpdate = true;
             },
             enumerable: true,
             configurable: true
@@ -48034,19 +48026,10 @@ var BABYLON;
                 return this._expensiveBlur;
             },
             set: function (b) {
-                var _this = this;
-                this._scene.postProcessRenderPipelineManager.detachCamerasFromRenderPipeline(this._name, this._scene.cameras);
+                this._blurHPostProcess.updateEffect("#define BILATERAL_BLUR\n#define BILATERAL_BLUR_H\n#define SAMPLES 16\n#define EXPENSIVE " + (b ? "1" : "0") + "\n", null, ["textureSampler", "depthSampler"]);
+                this._blurVPostProcess.updateEffect("#define BILATERAL_BLUR\n#define SAMPLES 16\n#define EXPENSIVE " + (b ? "1" : "0") + "\n", null, ["textureSampler", "depthSampler"]);
                 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._ratio.blurRatio);
-                this.addEffect(new BABYLON.PostProcessRenderEffect(this._scene.getEngine(), this.SSAORenderEffect, function () { return _this._ssaoPostProcess; }, true));
-                this.addEffect(new BABYLON.PostProcessRenderEffect(this._scene.getEngine(), this.SSAORenderEffect, function () { return _this._ssaoPostProcess; }, true));
-                if (this._cameras)
-                    this._scene.postProcessRenderPipelineManager.attachCamerasToRenderPipeline(this._name, this._cameras);
+                this._firstUpdate = true;
             },
             enumerable: true,
             configurable: true
@@ -48087,10 +48070,10 @@ var BABYLON;
         SSAO2RenderingPipeline.prototype._createBlurPostProcess = function (ssaoRatio, blurRatio) {
             var _this = this;
             var samples = 16;
-            var samplerOffsets = [];
+            this._samplerOffsets = [];
             var expensive = this.expensiveBlur;
             for (var i = -8; i < 8; i++) {
-                samplerOffsets.push(i * 2);
+                this._samplerOffsets.push(i * 2);
             }
             this._blurHPostProcess = new BABYLON.PostProcess("BlurH", "ssao2", ["outSize", "samplerOffsets", "near", "far", "radius"], ["depthSampler"], ssaoRatio, null, BABYLON.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 = function (effect) {
@@ -48100,10 +48083,10 @@ var BABYLON;
                 effect.setFloat("radius", _this.radius);
                 effect.setTexture("depthSampler", _this._depthTexture);
                 if (_this._firstUpdate) {
-                    effect.setArray("samplerOffsets", samplerOffsets);
+                    effect.setArray("samplerOffsets", _this._samplerOffsets);
                 }
             };
-            this._blurVPostProcess = new BABYLON.PostProcess("BlurV", "ssao2", ["outSize", "samplerOffsets", "near", "far", "radius"], ["depthSampler"], blurRatio, null, BABYLON.Texture.TRILINEAR_SAMPLINGMODE, this._scene.getEngine(), false, "#define BILATERAL_BLUR\n#define SAMPLES 16\n#define EXPENSIVE " + (expensive ? "1" : "0") + "\n");
+            this._blurVPostProcess = new BABYLON.PostProcess("BlurV", "ssao2", ["outSize", "samplerOffsets", "near", "far", "radius"], ["depthSampler"], blurRatio, null, BABYLON.Texture.TRILINEAR_SAMPLINGMODE, this._scene.getEngine(), false, "#define BILATERAL_BLUR\n#define BILATERAL_BLUR_V\n#define SAMPLES 16\n#define EXPENSIVE " + (expensive ? "1" : "0") + "\n");
             this._blurVPostProcess.onApply = function (effect) {
                 effect.setFloat("outSize", _this._ssaoCombinePostProcess.height);
                 effect.setFloat("near", _this._scene.activeCamera.minZ);
@@ -48111,7 +48094,7 @@ var BABYLON;
                 effect.setFloat("radius", _this.radius);
                 effect.setTexture("depthSampler", _this._depthTexture);
                 if (_this._firstUpdate) {
-                    effect.setArray("samplerOffsets", samplerOffsets);
+                    effect.setArray("samplerOffsets", _this._samplerOffsets);
                     _this._firstUpdate = false;
                 }
             };
@@ -48129,11 +48112,8 @@ var BABYLON;
             var i = 0;
             var normal = new BABYLON.Vector3(0, 0, 1);
             while (i < numSamples) {
-                vector = new BABYLON.Vector3(rand(-1.0, 1.0), rand(-1.0, 1.0), rand(0.0, 1.0));
+                vector = new BABYLON.Vector3(rand(-1.0, 1.0), rand(-1.0, 1.0), rand(0.30, 1.0));
                 vector.normalize();
-                if (BABYLON.Vector3.Dot(vector, normal) < 0.07) {
-                    continue;
-                }
                 scale = i / numSamples;
                 scale = lerp(0.1, 1.0, scale * scale);
                 vector.scaleInPlace(scale);
@@ -48145,8 +48125,7 @@ var BABYLON;
         SSAO2RenderingPipeline.prototype._createSSAOPostProcess = function (ratio) {
             var _this = this;
             var numSamples = this.samples;
-            var sampleSphere = this._generateHemisphere();
-            var samplesFactor = 1.0 / numSamples;
+            this._sampleSphere = this._generateHemisphere();
             this._ssaoPostProcess = new BABYLON.PostProcess("ssao2", "ssao2", [
                 "sampleSphere", "samplesFactor", "randTextureTiles", "totalStrength", "radius",
                 "base", "range", "projection", "near", "far", "texelSize",
@@ -48154,10 +48133,10 @@ var BABYLON;
             ], ["randomSampler", "normalSampler"], ratio, null, BABYLON.Texture.BILINEAR_SAMPLINGMODE, this._scene.getEngine(), false, "#define SAMPLES " + numSamples + "\n#define SSAO");
             this._ssaoPostProcess.onApply = function (effect) {
                 if (_this._firstUpdate) {
-                    effect.setArray3("sampleSphere", sampleSphere);
-                    effect.setFloat("samplesFactor", samplesFactor);
+                    effect.setArray3("sampleSphere", _this._sampleSphere);
                     effect.setFloat("randTextureTiles", 4.0);
                 }
+                effect.setFloat("samplesFactor", 1 / _this.samples);
                 effect.setFloat("totalStrength", _this.totalStrength);
                 effect.setFloat2("texelSize", 1 / _this._ssaoPostProcess.width, 1 / _this._ssaoPostProcess.height);
                 effect.setFloat("radius", _this.radius);

文件差异内容过多而无法显示
+ 6550 - 6550
dist/preview release/babylon.module.d.ts


文件差异内容过多而无法显示
+ 11 - 11
dist/preview release/babylon.worker.js


+ 3 - 6
src/PostProcess/babylon.ssao2RenderingPipeline.ts

@@ -165,7 +165,7 @@
 
             // Set up assets
             this._createRandomTexture();
-            this._depthTexture = scene.enableGeometryBufferRenderer().getGBuffer().depthTexture; 
+            this._depthTexture = scene.enableGeometryBufferRenderer().getGBuffer().textures[0]; 
             this._normalTexture = scene.enableGeometryBufferRenderer().getGBuffer().textures[1];
 
             this._originalColorPostProcess = new PassPostProcess("SSAOOriginalSceneColor", 1.0, null, Texture.BILINEAR_SAMPLINGMODE, scene.getEngine(), false);
@@ -220,7 +220,7 @@
             var expensive = this.expensiveBlur;
 
             for (var i = -8; i < 8; i++) {
-                this._samplerOffsets.push(i * 2);
+                this._samplerOffsets.push(i * 2 + 0.5);
             }
 
             this._blurHPostProcess = new PostProcess("BlurH", "ssao2", ["outSize", "samplerOffsets", "near", "far", "radius"], ["depthSampler"], ssaoRatio, 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");
@@ -270,11 +270,8 @@
                vector = new BABYLON.Vector3(
                    rand(-1.0, 1.0),
                    rand(-1.0, 1.0),
-                   rand(0.0, 1.0));
+                   rand(0.30, 1.0));
                vector.normalize();
-               if (BABYLON.Vector3.Dot(vector, normal) < 0.07) {
-                   continue;
-               }
                scale = i / numSamples;
                scale = lerp(0.1, 1.0, scale*scale);
                vector.scaleInPlace(scale);

+ 1 - 3
src/Rendering/babylon.geometryBufferRenderer.ts

@@ -66,8 +66,6 @@ module BABYLON {
                     this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
                     this._effect.setMatrix("view", scene.getViewMatrix());
 
-                    this._effect.setFloat("far", scene.activeCamera.maxZ);
-
                     // Alpha test
                     if (material && material.needAlphaTesting()) {
                         var alphaTexture = material.getAlphaTestTexture();
@@ -156,7 +154,7 @@ module BABYLON {
                 this._cachedDefines = join;
                 this._effect = this._scene.getEngine().createEffect("geometry",
                     attribs,
-                    ["world", "mBones", "viewProjection", "diffuseMatrix", "view", "far"],
+                    ["world", "mBones", "viewProjection", "diffuseMatrix", "view"],
                     ["diffuseSampler"], join);
             }
 

+ 2 - 4
src/Shaders/geometry.fragment.fx

@@ -4,6 +4,7 @@ precision highp float;
 precision highp int;
 
 in vec3 vNormalV;
+in vec4 vViewPos;
 
 #ifdef ALPHATEST
 in vec2 vUV;
@@ -13,16 +14,13 @@ uniform sampler2D diffuseSampler;
 layout(location = 0) out vec4 color0;
 layout(location = 1) out vec4 color1;
 
-uniform float far;
-
 void main() {
 #ifdef ALPHATEST
 	if (texture(diffuseSampler, vUV).a < 0.4)
 		discard;
 #endif
 
-    float depth = (gl_FragCoord.z / gl_FragCoord.w) / far;
-    color0 = vec4(depth, depth * depth, 0.0, 1.0);
+    color0 = vec4(vViewPos.z / vViewPos.w, 0.0, 0.0, 0.0);
     color1 = vec4(normalize(vNormalV), 1.0);
     //color2 = vec4(vPositionV, 1.0);
 }

+ 2 - 0
src/Shaders/geometry.vertex.fx

@@ -25,6 +25,7 @@ uniform mat4 viewProjection;
 uniform mat4 view;
 
 out vec3 vNormalV;
+out vec4 vViewPos;
 
 void main(void)
 {
@@ -33,6 +34,7 @@ void main(void)
 #include<bonesVertex>
 
 	vNormalV = normalize(vec3((view * finalWorld) * vec4(normal, 0.0)));
+	vViewPos = view * finalWorld * vec4(position, 1.0);
 	gl_Position = viewProjection * finalWorld * vec4(position, 1.0);
 
 #if defined(ALPHATEST) || defined(BASIC_RENDER)

+ 58 - 26
src/Shaders/ssao2.fragment.fx

@@ -40,14 +40,13 @@ uniform mat4 projection;
 void main()
 {
 	vec3 random = texture2D(randomSampler, vUV * randTextureTiles).rgb;
-	float depth = texture(textureSampler, vUV).r;
-	float linearDepth = - perspectiveDepthToViewZ(depth, near, far);
+	float depth = abs(texture(textureSampler, vUV).r);
 	vec3 normal = texture2D(normalSampler, vUV).rgb; 
 	float occlusion = 0.0;
-	float correctedRadius = min(radius, minZAspect * linearDepth / near);
+	float correctedRadius = min(radius, minZAspect * depth / near);
 
 	vec3 vViewRay = vec3((vUV.x * 2.0 - 1.0)*xViewport, (vUV.y * 2.0 - 1.0)*yViewport, 1.0);
-	vec3 origin = vViewRay * linearDepth;
+	vec3 origin = vViewRay * depth;
 	vec3 rvec = random * 2.0 - 1.0;
 	rvec.z = 0.0;
 	vec3 tangent = normalize(rvec - normal * dot(rvec, normal));
@@ -56,7 +55,7 @@ void main()
 
 	float difference;
 
-	if (linearDepth > maxZ) {
+	if (depth > maxZ) {
 		gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
 		return;
 	}
@@ -77,14 +76,12 @@ void main()
 	   }
 	  
 		// get sample linearDepth:
-	   float sampleDepth = texture(textureSampler, offset.xy).r;
-	   float linearSampleDepth = - perspectiveDepthToViewZ(sampleDepth, near, far);
+	   float sampleDepth = abs(texture(textureSampler, offset.xy).r);
 		// range check & accumulate:
-	   float rangeCheck = abs(linearDepth - linearSampleDepth) < correctedRadius ? 1.0 : 0.0;
-	   difference = samplePosition.z - linearSampleDepth;
-	   float errorFactor = max(0.0, pow((maxZ-linearDepth) / maxZ, 0.25));
-	  //occlusion += step(fallOff, difference) * (1.0 - smoothstep(fallOff, area, difference)) * rangeCheck * errorFactor;
-	   occlusion += (difference >= 1e-5 ? 1.0 : 0.0) * rangeCheck * errorFactor;
+	   float rangeCheck = abs(depth - sampleDepth) < correctedRadius ? 1.0 : 0.0;
+	   difference = samplePosition.z - sampleDepth;
+	  //occlusion += step(fallOff, difference) * (1.0 - smoothstep(fallOff, area, difference)) * rangeCheck;
+	   occlusion += (difference >= 1e-5 ? 1.0 : 0.0) * rangeCheck;
 	}
 
 
@@ -92,7 +89,7 @@ void main()
 
 	float ao = 1.0 - totalStrength * occlusion * samplesFactor;
 	float result = clamp(ao + base, 0.0, 1.0);
-	gl_FragColor = vec4(result, result, result, 1.0);
+	gl_FragColor = vec4(vec3(result), 1.0);
 }
 #endif
 
@@ -128,20 +125,56 @@ vec4 blur13(sampler2D image, vec2 uv, float resolution, vec2 direction) {
   return color;
 }
 
-vec4 linearUpsample(sampler2D image, vec2 uv, float resolution, vec2 direction) {
-	float texel = dot(uv, direction) * resolution;
-	float before = float(floor(texel));
-	float weight = texel - before;
-	vec4 color = mix(texture2D(image, uv - vec2(weight / resolution) * direction), texture2D(image, uv + vec2((1.0 - weight) / resolution) * direction), weight);
+vec4 blur13Bilateral(sampler2D image, vec2 uv, float resolution, vec2 direction) {
+  vec4 color = vec4(0.0);
+  vec2 off1 = vec2(1.411764705882353) * direction;
+  vec2 off2 = vec2(3.2941176470588234) * direction;
+  vec2 off3 = vec2(5.176470588235294) * direction;
+
+  float compareDepth = abs(texture2D(depthSampler, uv).r);
+  float sampleDepth;
+  float weight;
+  float weightSum = 0.1964825501511404 * 30.0;
+
+  color += texture2D(image, uv) * 0.1964825501511404 * 30.0;
+
+  sampleDepth = abs(texture2D(depthSampler, uv + (off1 / resolution)).r);
+  weight = clamp(1.0 / ( 0.003 + abs(compareDepth - sampleDepth)), 0.0, 30.0);
+  weightSum += 0.2969069646728344 * weight;
+  color += texture2D(image, uv + (off1 / resolution)) * 0.2969069646728344 * weight;
+
+  sampleDepth = abs(texture2D(depthSampler, uv - (off1 / resolution)).r);
+  weight = clamp(1.0 / ( 0.003 + abs(compareDepth - sampleDepth)), 0.0, 30.0);
+  weightSum += 0.2969069646728344 * weight;
+  color += texture2D(image, uv - (off1 / resolution)) * 0.2969069646728344 * weight;
+
+  sampleDepth = abs(texture2D(depthSampler, uv + (off2 / resolution)).r);
+  weight = clamp(1.0 / ( 0.003 + abs(compareDepth - sampleDepth)), 0.0, 30.0);
+  weightSum += 0.09447039785044732 * weight;
+  color += texture2D(image, uv + (off2 / resolution)) * 0.09447039785044732 * weight;
+
+  sampleDepth = abs(texture2D(depthSampler, uv - (off2 / resolution)).r);
+  weight = clamp(1.0 / ( 0.003 + abs(compareDepth - sampleDepth)), 0.0, 30.0);
+  weightSum += 0.09447039785044732 * weight;
+  color += texture2D(image, uv - (off2 / resolution)) * 0.09447039785044732 * weight;
+
+  sampleDepth = abs(texture2D(depthSampler, uv + (off3 / resolution)).r);
+  weight = clamp(1.0 / ( 0.003 + abs(compareDepth - sampleDepth)), 0.0, 30.0);
+  weightSum += 0.010381362401148057 * weight;
+  color += texture2D(image, uv + (off3 / resolution)) * 0.010381362401148057 * weight;
+
+  sampleDepth = abs(texture2D(depthSampler, uv - (off3 / resolution)).r);
+  weight = clamp(1.0 / ( 0.003 + abs(compareDepth - sampleDepth)), 0.0, 30.0);
+  weightSum += 0.010381362401148057 * weight;
+  color += texture2D(image, uv - (off3 / resolution)) * 0.010381362401148057 * weight;
 
-	return color;
+  return color / weightSum;
 }
 
 void main()
 {
 	#if EXPENSIVE
-	float compareDepth = texture2D(depthSampler, vUV).r;
-	float linearDepth = - perspectiveDepthToViewZ(compareDepth, near, far);
+	float compareDepth = abs(texture2D(depthSampler, vUV).r);
 	float texelsize = 1.0 / outSize;
 	float result = 0.0;
 	float weightSum = 0.0;
@@ -157,9 +190,8 @@ void main()
 		#endif
 		vec2 samplePos = vUV + sampleOffset;
 
-		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);
+		float sampleDepth = abs(texture2D(depthSampler, samplePos).r);
+		float weight = clamp(1.0 / ( 0.003 + abs(compareDepth - sampleDepth)), 0.0, 30.0);
 
 		result += texture2D(textureSampler, samplePos).r * weight;
 		weightSum += weight;
@@ -172,10 +204,10 @@ void main()
 	vec4 color;
 	#ifdef BILATERAL_BLUR_H
 	vec2 direction = vec2(1.0, 0.0);
-	color = blur13(textureSampler, vUV, outSize, direction);
+	color = blur13Bilateral(textureSampler, vUV, outSize, direction);
 	#else
 	vec2 direction = vec2(0.0, 1.0);
-	color = blur13(textureSampler, vUV, outSize, direction);
+	color = blur13Bilateral(textureSampler, vUV, outSize, direction);
 	#endif
 
 	gl_FragColor.rgb = vec3(color.r);