Skip to content

Commit

Permalink
GTAOPass: distance dependent horizon search (mrdoob#27370)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rabbid76 authored and AdaRoseCannon committed Jan 15, 2024
1 parent fa6f35b commit dccbd4b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
7 changes: 7 additions & 0 deletions examples/jsm/postprocessing/GTAOPass.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,13 @@ class GTAOPass extends Pass {

}

if ( parameters.distanceFallOff !== undefined ) {

this.gtaoMaterial.uniforms.distanceFallOff.value = parameters.distanceFallOff;
this.gtaoMaterial.needsUpdate = true;

}

if ( parameters.scale !== undefined ) {

this.gtaoMaterial.uniforms.scale.value = parameters.scale;
Expand Down
8 changes: 5 additions & 3 deletions examples/jsm/shaders/GTAOShader.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const GTAOShader = {
radius: { value: 0.25 },
distanceExponent: { value: 1. },
thickness: { value: 1. },
distanceFallOff: { value: 1. },
scale: { value: 1. },
sceneBoxMin: { value: new Vector3( - 1, - 1, - 1 ) },
sceneBoxMax: { value: new Vector3( 1, 1, 1 ) },
Expand Down Expand Up @@ -91,6 +92,7 @@ const GTAOShader = {
uniform float radius;
uniform float distanceExponent;
uniform float thickness;
uniform float distanceFallOff;
uniform float scale;
#if SCENE_CLIP_BOX == 1
uniform vec3 sceneBoxMin;
Expand Down Expand Up @@ -224,15 +226,15 @@ const GTAOShader = {
vec3 viewDelta = sampleSceneViewPos - viewPos;
if (abs(viewDelta.z) < thickness) {
float sampleCosHorizon = dot(viewDir, normalize(viewDelta));
cosHorizons.x = max(cosHorizons.x, sampleCosHorizon);
cosHorizons.x += max(0., (sampleCosHorizon - cosHorizons.x) * mix(1., 2. / float(j + 2), distanceFallOff));
}
sampleSceneUvDepth = getSceneUvAndDepth(viewPos - sampleViewOffset);
sampleSceneViewPos = getViewPosition(sampleSceneUvDepth.xy, sampleSceneUvDepth.z);
viewDelta = sampleSceneViewPos - viewPos;
if (abs(viewDelta.z) < thickness) {
float sampleCosHorizon = dot(viewDir, normalize(viewDelta));
cosHorizons.y = max(cosHorizons.y, sampleCosHorizon);
cosHorizons.y += max(0., (sampleCosHorizon - cosHorizons.y) * mix(1., 2. / float(j + 2), distanceFallOff));
}
}
Expand Down Expand Up @@ -353,7 +355,7 @@ function generateMagicSquareNoise( size = 5 ) {
data[ inx * 4 ] = ( randomVec.x * 0.5 + 0.5 ) * 255;
data[ inx * 4 + 1 ] = ( randomVec.y * 0.5 + 0.5 ) * 255;
data[ inx * 4 + 2 ] = 127;
data[ inx * 4 + 3 ] = 0;
data[ inx * 4 + 3 ] = 255;

}

Expand Down
Binary file modified examples/screenshots/webgl_postprocessing_gtao.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 2 additions & 4 deletions examples/webgl_postprocessing_gtao.html
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,7 @@
thickness: 1.,
scale: 1.,
samples: 16,
distanceFallOff: true,
clipRangeCheck: true,
depthRelativeBias: false,
nvAlignedSamples: false,
distanceFallOff: 1.,
screenSpaceRadius: false,
};
const pdParameters = {
Expand All @@ -158,6 +155,7 @@
gui.add( aoParameters, 'radius' ).min( 0.01 ).max( 1 ).step( 0.01 ).onChange( () => gtaoPass.updateGtaoMaterial( aoParameters ) );
gui.add( aoParameters, 'distanceExponent' ).min( 1 ).max( 4 ).step( 0.01 ).onChange( () => gtaoPass.updateGtaoMaterial( aoParameters ) );
gui.add( aoParameters, 'thickness' ).min( 0.01 ).max( 10 ).step( 0.01 ).onChange( () => gtaoPass.updateGtaoMaterial( aoParameters ) );
gui.add( aoParameters, 'distanceFallOff' ).min( 0 ).max( 1 ).step( 0.01 ).onChange( () => gtaoPass.updateGtaoMaterial( aoParameters ) );
gui.add( aoParameters, 'scale' ).min( 0.01 ).max( 2.0 ).step( 0.01 ).onChange( () => gtaoPass.updateGtaoMaterial( aoParameters ) );
gui.add( aoParameters, 'samples' ).min( 2 ).max( 32 ).step( 1 ).onChange( () => gtaoPass.updateGtaoMaterial( aoParameters ) );
gui.add( aoParameters, 'screenSpaceRadius' ).onChange( () => gtaoPass.updateGtaoMaterial( aoParameters ) );
Expand Down

0 comments on commit dccbd4b

Please sign in to comment.