Pārlūkot izejas kodu

functionnal SSAO

Benjamin Guignabert 8 gadi atpakaļ
vecāks
revīzija
cf8456833b

Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 3642 - 3641
dist/preview release/babylon.d.ts


Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 3642 - 3641
dist/preview release/babylon.module.d.ts


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

@@ -181,7 +181,8 @@
                 let textureOptions = { 
                     generateMipMaps: false, 
                     generateDepthBuffer: camera._postProcesses.indexOf(this) === 0, 
-                    generateStencilBuffer: false, //camera._postProcesses.indexOf(this) === 0 && this._engine.isStencilEnable,
+                    generateStencilBuffer: 
+                    camera._postProcesses.indexOf(this) === 0 && this._engine.isStencilEnable,
                     samplingMode: this.renderTargetSamplingMode, 
                     type: this._textureType 
                 };
@@ -194,7 +195,6 @@
  
                 this.onSizeChangedObservable.notifyObservers(this);
             }
-            this.samples = 4;
 
             this._textures.forEach(texture => {
                 if (texture.samples !== this.samples) {

+ 12 - 10
src/PostProcess/babylon.ssaoRenderingPipeline.ts

@@ -35,6 +35,8 @@
         @serialize()
         public totalStrength: number = 1.0;
 
+        public samples: number = 16.0;
+
         /**
         * The radius around the analyzed pixel used by the SSAO post-process. Default value is 0.0006
         * @type {number}
@@ -105,7 +107,7 @@
             var ssaoRatio = ratio.ssaoRatio || ratio;
             var combineRatio = ratio.combineRatio || ratio;
             this._ratio = {
-                ssaoRatio: ssaoRatio,
+                ssaoRatio: 1.0, //ssaoRatio,
                 combineRatio: combineRatio
             };
 
@@ -117,9 +119,9 @@
             // Set up pipeline
             this.addEffect(new PostProcessRenderEffect(scene.getEngine(), this.SSAOOriginalSceneColorEffect, () => { return this._originalColorPostProcess; }, true));
             this.addEffect(new PostProcessRenderEffect(scene.getEngine(), this.SSAORenderEffect, () => { return this._ssaoPostProcess; }, true));
-            // this.addEffect(new PostProcessRenderEffect(scene.getEngine(), this.SSAOBlurHRenderEffect, () => { return this._blurHPostProcess; }, true));
-            // this.addEffect(new PostProcessRenderEffect(scene.getEngine(), this.SSAOBlurVRenderEffect, () => { return this._blurVPostProcess; }, true));
-            // this.addEffect(new PostProcessRenderEffect(scene.getEngine(), this.SSAOCombineRenderEffect, () => { return this._ssaoCombinePostProcess; }, true));
+            this.addEffect(new PostProcessRenderEffect(scene.getEngine(), this.SSAOBlurHRenderEffect, () => { return this._blurHPostProcess; }, true));
+            this.addEffect(new PostProcessRenderEffect(scene.getEngine(), this.SSAOBlurVRenderEffect, () => { return this._blurVPostProcess; }, true));
+            this.addEffect(new PostProcessRenderEffect(scene.getEngine(), this.SSAOCombineRenderEffect, () => { return this._ssaoCombinePostProcess; }, true));
 
             // Finish
             scene.postProcessRenderPipelineManager.addPipeline(this);
@@ -192,7 +194,7 @@
         }
 
         private generateHemisphere(): number[] {
-            var numSamples = 16;
+            var numSamples = this.samples;
             var result = [];
             var vector, scale;
 
@@ -212,7 +214,7 @@
                    rand(-1.0, 1.0),
                    rand(0.0, 1.0));
                vector.normalize();
-               if (BABYLON.Vector3.Dot(vector, normal) < 0.15) {
+               if (BABYLON.Vector3.Dot(vector, normal) < 0.07) {
                    continue;
                }
                scale = i / numSamples;
@@ -228,7 +230,7 @@
         }
 
         private _createSSAOPostProcess(ratio: number): void {
-            var numSamples = 5;
+            var numSamples = this.samples;
 /*            var sampleSphere = [
                 0.5381, 0.1856, 0.4319,
                 0.1379, 0.2486, 0.4430,
@@ -278,7 +280,7 @@
                 effect.setFloat("base", this.base);
                 effect.setFloat("width", this._scene.getEngine().getRenderWidth());
                 effect.setFloat("height", this._scene.getEngine().getRenderHeight());
-                effect.setFloat("xViewport", Math.tan(this._scene.activeCamera.fov / 2) * this._scene.activeCamera.minZ * this._scene.getEngine().getAspectRatio(this._scene.activeCamera));
+                effect.setFloat("xViewport", Math.tan(this._scene.activeCamera.fov / 2) * this._scene.activeCamera.minZ * this._scene.getEngine().getAspectRatio(this._scene.activeCamera, true));
                 effect.setFloat("yViewport", Math.tan(this._scene.activeCamera.fov / 2) * this._scene.activeCamera.minZ );
                 effect.setMatrix("projection", this._scene.getProjectionMatrix());
 
@@ -315,8 +317,8 @@
 
             for (var x = 0; x < size; x++) {
                 for (var y = 0; y < size; y++) {
-                    randVector.x = rand(-1.0, 1.0);
-                    randVector.y = rand(-1.0, 1.0);
+                    randVector.x = rand(0.0, 1.0);
+                    randVector.y = rand(0.0, 1.0);
                     randVector.z = 0.0;
 
                     randVector.normalize();

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

@@ -19,7 +19,7 @@ module BABYLON {
             this._multiRenderTarget = new MultiRenderTarget("gBuffer", { width: engine.getRenderWidth(), height: engine.getRenderHeight() }, 2, this._scene, {generateMipMaps : [true]});
             // set default depth value to 1.0 (far away)
             this._multiRenderTarget.onClearObservable.add((engine: Engine) => {
-                engine.clear(new Color4(0.0, 0.0, 0.0, 1.0), true, true, true);
+                engine.clear(new Color4(1.0, 1.0, 1.0, 1.0), true, true, true);
             });
 
             // Custom render function

+ 20 - 15
src/Shaders/ssao.fragment.fx

@@ -1,9 +1,17 @@
-// SSAO Shader
+// SSAO Shader
 precision highp float;
 uniform sampler2D textureSampler;
 
 varying vec2 vUV;
 
+float perspectiveDepthToViewZ( const in float invClipZ, const in float near, const in float far ) {
+	return ( near * far ) / ( ( far - near ) * invClipZ - far );
+}
+
+float viewZToOrthographicDepth( const in float viewZ, const in float near, const in float far ) {
+	return ( viewZ + near ) / ( near - far );
+}
+
 #ifdef SSAO
 uniform sampler2D randomSampler;
 uniform sampler2D normalSampler;
@@ -39,14 +47,6 @@ vec3 normalFromDepth(float depth, vec2 coords)
 	return normalize(normal);
 }
 
-float perspectiveDepthToViewZ( const in float invClipZ, const in float near, const in float far ) {
-	return ( near * far ) / ( ( far - near ) * invClipZ - far );
-}
-
-float viewZToOrthographicDepth( const in float viewZ, const in float near, const in float far ) {
-	return ( viewZ + near ) / ( near - far );
-}
-
 void main()
 {
 	vec3 random = normalize(texture2D(randomSampler, vUV * randTextureTiles).rgb);
@@ -56,7 +56,7 @@ void main()
 	float linearDepth = - perspectiveDepthToViewZ(depth, n, f);
 	vec3 position = vec3(vUV, linearDepth);
 	vec3 normal = texture2D(normalSampler, vUV).rgb; 
-	float radiusDepth = linearDepth / radius;
+	float radiusDepth = radius; //linearDepth / radius;
 	float occlusion = 0.0;
 
 	vec3 vViewRay = vec3((vUV.x * 2.0 - 1.0)*xViewport, (vUV.y * 2.0 - 1.0)*yViewport, 1.0);
@@ -98,11 +98,11 @@ void main()
 	   float sampleDepth = texture(textureSampler, offset.xy).r;
 	   float linearSampleDepth = - perspectiveDepthToViewZ(texture(textureSampler, offset.xy).r, n, f);
 		// range check & accumulate:
-	   float rangeCheck = abs(origin.z - linearSampleDepth) < radiusDepth / 2.0 ? 1.0 : 0.0;
+	   float rangeCheck = abs(linearDepth - linearSampleDepth) < radiusDepth ? 1.0 : 0.0;
 	   // occlusion += (sampleDepth <= samplePosition.z ? 1.0 : 0.0) * rangeCheck;
 	  	difference = samplePosition.z - linearSampleDepth;
-	  	// occlusion += step(fallOff, difference) * (1.0 - smoothstep(fallOff, area, difference)) * rangeCheck;
-	  	occlusion += (difference > 0.0 ? 1.0 : 0.0) * rangeCheck;
+	  	occlusion += step(fallOff, difference) * (1.0 - smoothstep(fallOff, area, difference)) * rangeCheck;
+	  	//occlusion += (difference > 0.0 ? 1.0 : 0.0) * rangeCheck;
 
 	}
 
@@ -114,7 +114,7 @@ void main()
 	// gl_FragColor.g = result;
 	// gl_FragColor.b = result;
 	// gl_FragColor.a = 1.0;
-	ao = 1.0 - occlusion * samplesFactor;
+	ao = 1.0 - totalStrength * occlusion * samplesFactor;
 	gl_FragColor = vec4(ao, ao, ao, 1.0);
 }
 #endif
@@ -127,8 +127,12 @@ uniform float samplerOffsets[SAMPLES];
 void main()
 {
 
+	// TODO change
+	float n = 1.0;
+	float f = 100.0;
 	float texelsize = 1.0 / outSize;
 	float compareDepth = texture2D(depthSampler, vUV).r;
+	float linearDepth = - perspectiveDepthToViewZ(compareDepth, n, f);
 	float result = 0.0;
 	float weightSum = 0.0;
 
@@ -142,7 +146,8 @@ void main()
 		vec2 samplePos = vUV + sampleOffset;
 
 		float sampleDepth = texture2D(depthSampler, samplePos).r;
-		float weight = (1.0 / (0.0001 + abs(compareDepth - sampleDepth)));
+		float linearSampleDepth = - perspectiveDepthToViewZ(sampleDepth, n, f);
+		float weight = (1.0 / (0.0005 + abs(linearDepth - linearSampleDepth)));
 
 		result += texture2D(textureSampler, samplePos).r * weight;
 		weightSum += weight;