Skip to content

Commit

Permalink
Always divide by sample count.
Browse files Browse the repository at this point in the history
This prevents incorrect shadows in some cases.
  • Loading branch information
vanruesc committed Nov 14, 2018
1 parent a9af7ef commit 143c271
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
14 changes: 8 additions & 6 deletions src/effects/SSAOEffect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -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"));

}

Expand All @@ -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();

Expand All @@ -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"));

}

Expand All @@ -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();

}
Expand Down
10 changes: 4 additions & 6 deletions src/effects/glsl/ssao/shader.frag
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}

Expand Down

0 comments on commit 143c271

Please sign in to comment.