Skip to content

Commit

Permalink
fixes ambient occlusion bug affecting distances far from the scene or…
Browse files Browse the repository at this point in the history
…igin (#196)
  • Loading branch information
TheCodeTherapy authored Feb 26, 2025
1 parent 944d8da commit 3eb7066
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,15 @@ const EffectCompositer = {
vec4 clipSpacePosition = vec4(coord * 2.0 - 1.0, z, 1.0);
vec4 viewSpacePosition = projectionMatrixInv * clipSpacePosition;
vec4 worldSpacePosition = viewSpacePosition;
worldSpacePosition.xyz /= worldSpacePosition.w;
return worldSpacePosition.xyz;
if (abs(viewSpacePosition.w) < 1e-6) {
discard;
}
viewSpacePosition /= viewSpacePosition.w;
return (viewMatrixInv * viewSpacePosition).xyz;
}
vec3 computeNormal(vec3 worldPos, vec2 vUv) {
ivec2 p = ivec2(vUv * resolution);
float c0 = texelFetch(sceneDepth, p, 0).x;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
Uniform,
Vector2,
Vector3,
WebGLMultipleRenderTargets,
WebGLRenderTarget,
WebGLRenderer,
} from "three";
Expand Down Expand Up @@ -118,7 +117,7 @@ class N8SSAOPass extends Pass {
private writeTargetInternal: WebGLRenderTarget;
private readTargetInternal: WebGLRenderTarget;
private outputTargetInternal: WebGLRenderTarget;
private depthDownsampleTarget: WebGLMultipleRenderTargets | null;
private depthDownsampleTarget: WebGLRenderTarget | null;

private depthDownsampleQuad: FullScreenTriangle | null;
private effectShaderQuad: FullScreenTriangle | null;
Expand Down Expand Up @@ -227,22 +226,18 @@ class N8SSAOPass extends Pass {

private configureHalfResTargets(): void {
if (this.configuration.halfRes) {
this.depthDownsampleTarget = new WebGLMultipleRenderTargets(
this.width / 2,
this.height / 2,
2,
{
depthBuffer: false,
},
);
this.depthDownsampleTarget.texture[0].format = RedFormat;
this.depthDownsampleTarget.texture[0].type = FloatType;
this.depthDownsampleTarget.texture[0].minFilter = NearestFilter;
this.depthDownsampleTarget.texture[0].magFilter = NearestFilter;
this.depthDownsampleTarget.texture[1].format = RGBAFormat;
this.depthDownsampleTarget.texture[1].type = HalfFloatType;
this.depthDownsampleTarget.texture[1].minFilter = NearestFilter;
this.depthDownsampleTarget.texture[1].magFilter = NearestFilter;
this.depthDownsampleTarget = new WebGLRenderTarget(this.width / 2, this.height / 2, {
count: 2,
depthBuffer: false,
});
this.depthDownsampleTarget.textures[0].format = RedFormat;
this.depthDownsampleTarget.textures[0].type = FloatType;
this.depthDownsampleTarget.textures[0].minFilter = NearestFilter;
this.depthDownsampleTarget.textures[0].magFilter = NearestFilter;
this.depthDownsampleTarget.textures[1].format = RGBAFormat;
this.depthDownsampleTarget.textures[1].type = HalfFloatType;
this.depthDownsampleTarget.textures[1].minFilter = NearestFilter;
this.depthDownsampleTarget.textures[1].magFilter = NearestFilter;
this.depthDownsampleQuad = new FullScreenTriangle(new ShaderMaterial(DepthDownSample));
} else {
if (this.depthDownsampleTarget) {
Expand Down Expand Up @@ -439,10 +434,10 @@ class N8SSAOPass extends Pass {

effectShaderUniforms.sceneDiffuse.value = inputBuffer.texture;
effectShaderUniforms.sceneDepth.value = this.configuration.halfRes
? this.depthDownsampleTarget!.texture[0]
? this.depthDownsampleTarget!.textures[0]
: this.depthTexture;
effectShaderUniforms.sceneNormal.value = this.configuration.halfRes
? this.depthDownsampleTarget!.texture[1]
? this.depthDownsampleTarget!.textures[1]
: null;
effectShaderUniforms.projMat.value = this.camera.projectionMatrix;
effectShaderUniforms.viewMat.value = this.camera.matrixWorldInverse;
Expand Down Expand Up @@ -486,7 +481,7 @@ class N8SSAOPass extends Pass {
];
poissonBlurUniforms.tDiffuse.value = this.readTargetInternal.texture;
poissonBlurUniforms.sceneDepth.value = this.configuration.halfRes
? this.depthDownsampleTarget!.texture[0]
? this.depthDownsampleTarget!.textures[0]
: this.depthTexture;
poissonBlurUniforms.projMat.value = this.camera.projectionMatrix;
poissonBlurUniforms.viewMat.value = this.camera.matrixWorldInverse;
Expand Down Expand Up @@ -529,7 +524,7 @@ class N8SSAOPass extends Pass {
effectCompositerUniforms.logDepth.value = renderer.capabilities.logarithmicDepthBuffer;
effectCompositerUniforms.ortho.value = this.camera instanceof OrthographicCamera;
effectCompositerUniforms.downsampledDepth.value = this.configuration.halfRes
? this.depthDownsampleTarget!.texture[0]
? this.depthDownsampleTarget!.textures[0]
: this.depthTexture;
effectCompositerUniforms.resolution.value = this.r;
effectCompositerUniforms.blueNoise.value = this.bluenoise;
Expand Down

0 comments on commit 3eb7066

Please sign in to comment.