|
@@ -19,7 +19,6 @@ const SSAOShader = {
|
|
|
|
|
|
uniforms: {
|
|
uniforms: {
|
|
|
|
|
|
- 'tDiffuse': { value: null },
|
|
|
|
'tNormal': { value: null },
|
|
'tNormal': { value: null },
|
|
'tDepth': { value: null },
|
|
'tDepth': { value: null },
|
|
'tNoise': { value: null },
|
|
'tNoise': { value: null },
|
|
@@ -49,7 +48,6 @@ const SSAOShader = {
|
|
|
|
|
|
fragmentShader: /* glsl */`
|
|
fragmentShader: /* glsl */`
|
|
|
|
|
|
- uniform sampler2D tDiffuse;
|
|
|
|
uniform sampler2D tNormal;
|
|
uniform sampler2D tNormal;
|
|
uniform sampler2D tDepth;
|
|
uniform sampler2D tDepth;
|
|
uniform sampler2D tNoise;
|
|
uniform sampler2D tNoise;
|
|
@@ -128,47 +126,56 @@ const SSAOShader = {
|
|
void main() {
|
|
void main() {
|
|
|
|
|
|
float depth = getDepth( vUv );
|
|
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 );
|
|
|
|
|
|
+ }
|
|
|
|
|
|
}`
|
|
}`
|
|
|
|
|