Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSAOPass: Prevent color shift of background. #26594

Merged
merged 1 commit into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion examples/jsm/postprocessing/SSAOPass.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ class SSAOPass extends Pass {
blending: NoBlending
} );

this.ssaoMaterial.uniforms[ 'tDiffuse' ].value = this.beautyRenderTarget.texture;
this.ssaoMaterial.uniforms[ 'tNormal' ].value = this.normalRenderTarget.texture;
this.ssaoMaterial.uniforms[ 'tDepth' ].value = this.normalRenderTarget.depthTexture;
this.ssaoMaterial.uniforms[ 'tNoise' ].value = this.noiseTexture;
Expand Down
59 changes: 33 additions & 26 deletions examples/jsm/shaders/SSAOShader.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const SSAOShader = {

uniforms: {

'tDiffuse': { value: null },
'tNormal': { value: null },
'tDepth': { value: null },
'tNoise': { value: null },
Expand Down Expand Up @@ -49,7 +48,6 @@ const SSAOShader = {

fragmentShader: /* glsl */`

uniform sampler2D tDiffuse;
uniform sampler2D tNormal;
uniform sampler2D tDepth;
uniform sampler2D tNoise;
Expand Down Expand Up @@ -128,47 +126,56 @@ const SSAOShader = {
void main() {

float depth = getDepth( vUv );
float viewZ = getViewZ( depth );

vec3 viewPosition = getViewPosition( vUv, depth, viewZ );
vec3 viewNormal = getViewNormal( vUv );
if ( depth == 1.0 ) {

gl_FragColor = vec4( 1.0 ); // don't influence background

} else {

float viewZ = getViewZ( depth );

vec3 viewPosition = getViewPosition( vUv, depth, viewZ );
vec3 viewNormal = getViewNormal( vUv );

vec2 noiseScale = vec2( resolution.x / 4.0, resolution.y / 4.0 );
vec3 random = vec3( texture2D( tNoise, vUv * noiseScale ).r );
vec2 noiseScale = vec2( resolution.x / 4.0, resolution.y / 4.0 );
vec3 random = vec3( texture2D( tNoise, vUv * noiseScale ).r );

// compute matrix used to reorient a kernel vector
// compute matrix used to reorient a kernel vector

vec3 tangent = normalize( random - viewNormal * dot( random, viewNormal ) );
vec3 bitangent = cross( viewNormal, tangent );
mat3 kernelMatrix = mat3( tangent, bitangent, viewNormal );
vec3 tangent = normalize( random - viewNormal * dot( random, viewNormal ) );
vec3 bitangent = cross( viewNormal, tangent );
mat3 kernelMatrix = mat3( tangent, bitangent, viewNormal );

float occlusion = 0.0;
float occlusion = 0.0;

for ( int i = 0; i < KERNEL_SIZE; i ++ ) {
for ( int i = 0; i < KERNEL_SIZE; i ++ ) {

vec3 sampleVector = kernelMatrix * kernel[ i ]; // reorient sample vector in view space
vec3 samplePoint = viewPosition + ( sampleVector * kernelRadius ); // calculate sample point
vec3 sampleVector = kernelMatrix * kernel[ i ]; // reorient sample vector in view space
vec3 samplePoint = viewPosition + ( sampleVector * kernelRadius ); // calculate sample point

vec4 samplePointNDC = cameraProjectionMatrix * vec4( samplePoint, 1.0 ); // project point and calculate NDC
samplePointNDC /= samplePointNDC.w;
vec4 samplePointNDC = cameraProjectionMatrix * vec4( samplePoint, 1.0 ); // project point and calculate NDC
samplePointNDC /= samplePointNDC.w;

vec2 samplePointUv = samplePointNDC.xy * 0.5 + 0.5; // compute uv coordinates
vec2 samplePointUv = samplePointNDC.xy * 0.5 + 0.5; // compute uv coordinates

float realDepth = getLinearDepth( samplePointUv ); // get linear depth from depth texture
float sampleDepth = viewZToOrthographicDepth( samplePoint.z, cameraNear, cameraFar ); // compute linear depth of the sample view Z value
float delta = sampleDepth - realDepth;
float realDepth = getLinearDepth( samplePointUv ); // get linear depth from depth texture
float sampleDepth = viewZToOrthographicDepth( samplePoint.z, cameraNear, cameraFar ); // compute linear depth of the sample view Z value
float delta = sampleDepth - realDepth;

if ( delta > minDistance && delta < maxDistance ) { // if fragment is before sample point, increase occlusion
if ( delta > minDistance && delta < maxDistance ) { // if fragment is before sample point, increase occlusion

occlusion += 1.0;
occlusion += 1.0;

}

}

}
occlusion = clamp( occlusion / float( KERNEL_SIZE ), 0.0, 1.0 );

occlusion = clamp( occlusion / float( KERNEL_SIZE ), 0.0, 1.0 );
gl_FragColor = vec4( vec3( 1.0 - occlusion ), 1.0 );

gl_FragColor = vec4( vec3( 1.0 - occlusion ), 1.0 );
}

}`

Expand Down
Binary file modified examples/screenshots/webgl_postprocessing_ssao.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.