Skip to content

Commit

Permalink
Change ssao to work on depth only.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinmera committed Mar 1, 2024
1 parent 0991c67 commit bb85505
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
45 changes: 23 additions & 22 deletions data/ssao.glsl
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
out vec4 color;
#section FRAGMENT_SHADER
out float color;
in vec2 uv;

uniform sampler2D position_map;
uniform sampler2D normal_map;
uniform sampler2D depth_map;
uniform sampler2D ssao_noise;
uniform sampler2D ssao_kernel;

uniform float radius = 8;
uniform float bias = 5;

vec3 view_position(vec2 coords) {
float depth = texture(depth_map, coords).r;
vec4 ndc = vec4(coords.x*2-1, coords.y*2-1, depth*2-1, 1);

vec4 vs_pos = inv_projection_matrix * ndc;
vs_pos.xyz = vs_pos.xyz / vs_pos.w;
return vs_pos.xyz;
}

void main(){
vec2 noiseScale = view_size / 4.0;
vec2 noise_scale = textureSize(depth_map, 0).xy / 4.0;
// get input for SSAO algorithm
vec3 fragPos = texture(position_map, uv).xyz;
vec3 normal = texture(normal_map, uv).rgb;
vec3 randomVec = normalize(texture(ssao_noise, uv * noiseScale).xyz);
// bring into view space
fragPos = vec3(view_matrix * vec4(fragPos, 1));
normal = normalize(transpose(inverse(mat3(view_matrix))) * normal);
vec3 view_position = view_position(position_map, uv);
vec3 view_normal = cross(dFdy(view_position.xyz), dFdx(view_position.xyz));
vec3 random_vec = normalize(texture(ssao_noise, uv * noise_scale).xyz);
// create TBN change-of-basis matrix: from tangent-space to view-space
vec3 tangent = normalize(randomVec - normal * dot(randomVec, normal));
vec3 tangent = normalize(random_vec - view_normal * dot(random_vec, view_normal));
vec3 bitangent = cross(normal, tangent);
mat3 TBN = mat3(tangent, bitangent, normal);
mat3 TBN = mat3(tangent, bitangent, view_normal);
// iterate over the sample kernel and calculate occlusion factor
float occlusion = 0.0;
int kernel_size = textureSize(ssao_kernel, 0).x;
for(int i = 0; i < kernel_size; ++i){
// get sample position
vec3 ssample = TBN * texelFetch(ssao_kernel, i).rgb; // from tangent to view-space
ssample = fragPos + ssample * radius;
ssample = view_position + ssample * radius;

// project sample position (to sample texture) (to get position on screen/texture)
vec4 offset = vec4(ssample, 1.0);
Expand All @@ -37,15 +43,10 @@ void main(){
offset.xy = offset.xy * 0.5 + 0.5; // transform to range 0.0 - 1.0

// get sample depth
vec3 samplePos = texture(position_map, offset.xy).xyz;
samplePos = vec3(view_matrix * vec4(samplePos, 1));
float sampleDepth = samplePos.z;

// range check & accumulate
float rangeCheck = smoothstep(0.0, 1.0, radius / abs(fragPos.z - sampleDepth));
occlusion += (sampleDepth >= ssample.z + bias ? 1.0 : 0.0) * rangeCheck;
float sample_depth = view_position(offset.xy).z;
float range_check = smoothstep(0.0, 1.0, radius / abs(view_position.z - sample_depth));
occlusion += (sample_depth >= ssample.z + bias ? 1.0 : 0.0) * range_check;
}
occlusion = 1.0 - (occlusion / kernel_size);

color = vec4(pow(occlusion, 2));
color = pow(occlusion, 2);
}
7 changes: 4 additions & 3 deletions renderer/ssao.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@
:internal-format :rgb32f)

(define-shader-pass ssao-pass (post-effect-pass)
((position-map :port-type input)
(normal-map :port-type input)
((depth-map :port-type input)
(occlusion :port-type output
:attachment :color-attachment0
:texspec (:internal-format :red
:min-filter :nearest
:mag-filter :nearest))
(ssao-kernel :port-type fixed-input :texture (// 'trial 'ssao-kernel))
(ssao-noise :port-type fixed-input :texture (// 'trial 'ssao-noise)))
(ssao-noise :port-type fixed-input :texture (// 'trial 'ssao-noise))
(radius :initform 8.0 :accessor radius :uniform T)
(bias :initform 5.0 :accessor radius :uniform T))
(:buffers (trial standard-environment-information))
(:shader-file (trial "ssao.glsl")))

0 comments on commit bb85505

Please sign in to comment.