From 143c271f231ee4547a0266c9951da586645cf3b1 Mon Sep 17 00:00:00 2001 From: "Raoul v. R" Date: Wed, 14 Nov 2018 22:56:46 +0100 Subject: [PATCH] Always divide by sample count. This prevents incorrect shadows in some cases. --- src/effects/SSAOEffect.js | 14 ++++++++------ src/effects/glsl/ssao/shader.frag | 10 ++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/effects/SSAOEffect.js b/src/effects/SSAOEffect.js index ed29b37f3..5f0bed4f9 100644 --- a/src/effects/SSAOEffect.js +++ b/src/effects/SSAOEffect.js @@ -59,8 +59,9 @@ export class SSAOEffect extends Effect { blendFunction: settings.blendFunction, defines: new Map([ - ["RINGS", "0"], - ["SAMPLES", "0"] + ["RINGS_INT", "0"], + ["SAMPLES_INT", "0"], + ["SAMPLES_FLOAT", "0.0"] ]), uniforms: new Map([ @@ -149,7 +150,7 @@ export class SSAOEffect extends Effect { get samples() { - return Number.parseInt(this.defines.get("SAMPLES")); + return Number.parseInt(this.defines.get("SAMPLES_INT")); } @@ -165,7 +166,8 @@ export class SSAOEffect extends Effect { value = Math.floor(value); - this.defines.set("SAMPLES", value.toFixed(0)); + this.defines.set("SAMPLES_INT", value.toFixed(0)); + this.defines.set("SAMPLES_FLOAT", value.toFixed(1)); this.updateAngleStep(); this.updateRadiusStep(); @@ -179,7 +181,7 @@ export class SSAOEffect extends Effect { get rings() { - return Number.parseInt(this.defines.get("RINGS")); + return Number.parseInt(this.defines.get("RINGS_INT")); } @@ -195,7 +197,7 @@ export class SSAOEffect extends Effect { value = Math.floor(value); - this.defines.set("RINGS", value.toFixed(0)); + this.defines.set("RINGS_INT", value.toFixed(0)); this.updateAngleStep(); } diff --git a/src/effects/glsl/ssao/shader.frag b/src/effects/glsl/ssao/shader.frag index 14a14cd77..08b1fc5ba 100644 --- a/src/effects/glsl/ssao/shader.frag +++ b/src/effects/glsl/ssao/shader.frag @@ -49,30 +49,28 @@ float getAmbientOcclusion(const in vec3 p, const in vec3 n, const in float depth vec2 radius = radiusStep; float angle = rand(uv + seed) * PI2; float occlusionSum = 0.0; - int sampleCount = 0; // Collect samples along a discrete spiral pattern. - for(int i = 0; i < SAMPLES; ++i) { + for(int i = 0; i < SAMPLES_INT; ++i) { vec2 coord = uv + vec2(cos(angle), sin(angle)) * radius; radius += radiusStep; angle += ANGLE_STEP; float sampleDepth = readDepth(coord); + float proximity = abs(depth - sampleDepth); if(sampleDepth <= distanceCutoff.y) { - float falloff = 1.0 - smoothstep(proximityCutoff.x, proximityCutoff.y, abs(depth - sampleDepth)); + float falloff = 1.0 - smoothstep(proximityCutoff.x, proximityCutoff.y, proximity); vec3 sampleViewPosition = getViewPosition(coord, sampleDepth, getViewZ(sampleDepth)); occlusionSum += getOcclusion(p, n, sampleViewPosition) * falloff; - ++sampleCount; - } } - return (sampleCount == 0) ? 0.0 : occlusionSum / float(sampleCount); + return occlusionSum / SAMPLES_FLOAT; }