Skip to content
Permalink
Browse files

Factor out ssao

  • Loading branch information...
Shinmera committed Apr 1, 2019
1 parent ae71cf7 commit 543231f4c452c26fed6a09b7d7ede3ffe1c976fb
Showing with 57 additions and 55 deletions.
  1. +55 −0 data/ssao.frag
  2. +2 −55 ssao.lisp
@@ -0,0 +1,55 @@
#version 330 core
out vec4 color;
in vec2 tex_coord;

uniform sampler2D position_map;
uniform sampler2D normal_map;
uniform sampler2D noise_map;

uniform mat4 projection_matrix;
uniform mat4 view_matrix;
uniform vec2 viewport_size;
uniform vec3 samples[64];
uniform int kernel_size = 64;
uniform float radius = 8;
uniform float bias = 5;

void main(){
vec2 noiseScale = viewport_size / 4.0;
// get input for SSAO algorithm
vec3 fragPos = texture(position_map, tex_coord).xyz;
vec3 normal = texture(normal_map, tex_coord).rgb;
vec3 randomVec = normalize(texture(noise_map, tex_coord * noiseScale).xyz);
// bring into view space
fragPos = vec3(view_matrix * vec4(fragPos, 1));
normal = normalize(transpose(inverse(mat3(view_matrix))) * normal);
// create TBN change-of-basis matrix: from tangent-space to view-space
vec3 tangent = normalize(randomVec - normal * dot(randomVec, normal));
vec3 bitangent = cross(normal, tangent);
mat3 TBN = mat3(tangent, bitangent, normal);
// iterate over the sample kernel and calculate occlusion factor
float occlusion = 0.0;
for(int i = 0; i < kernel_size; ++i){
// get sample position
vec3 ssample = TBN * samples[i]; // from tangent to view-space
ssample = fragPos + ssample * radius;

// project sample position (to sample texture) (to get position on screen/texture)
vec4 offset = vec4(ssample, 1.0);
offset = projection_matrix * offset; // from view to clip-space
offset.xyz /= offset.w; // perspective divide
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;
}
occlusion = 1.0 - (occlusion / kernel_size);

color = vec4(pow(occlusion, 2));
}
@@ -59,58 +59,5 @@
(setf (uniform program "viewport_size") (vec2 (width *context*) (height *context*)))))

(define-class-shader (ssao-pass :fragment-shader)
"#version 330 core
out vec4 color;
in vec2 tex_coord;
uniform sampler2D position_map;
uniform sampler2D normal_map;
uniform sampler2D noise_map;
uniform mat4 projection_matrix;
uniform mat4 view_matrix;
uniform vec2 viewport_size;
uniform vec3 samples[64];
uniform int kernel_size = 64;
uniform float radius = 8;
uniform float bias = 5;
void main(){
vec2 noiseScale = viewport_size / 4.0;
// get input for SSAO algorithm
vec3 fragPos = texture(position_map, tex_coord).xyz;
vec3 normal = texture(normal_map, tex_coord).rgb;
vec3 randomVec = normalize(texture(noise_map, tex_coord * noiseScale).xyz);
// bring into view space
fragPos = vec3(view_matrix * vec4(fragPos, 1));
normal = normalize(transpose(inverse(mat3(view_matrix))) * normal);
// create TBN change-of-basis matrix: from tangent-space to view-space
vec3 tangent = normalize(randomVec - normal * dot(randomVec, normal));
vec3 bitangent = cross(normal, tangent);
mat3 TBN = mat3(tangent, bitangent, normal);
// iterate over the sample kernel and calculate occlusion factor
float occlusion = 0.0;
for(int i = 0; i < kernel_size; ++i){
// get sample position
vec3 ssample = TBN * samples[i]; // from tangent to view-space
ssample = fragPos + ssample * radius;
// project sample position (to sample texture) (to get position on screen/texture)
vec4 offset = vec4(ssample, 1.0);
offset = projection_matrix * offset; // from view to clip-space
offset.xyz /= offset.w; // perspective divide
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;
}
occlusion = 1.0 - (occlusion / kernel_size);
color = vec4(pow(occlusion, 2));
}")
;; KLUDGE
(asdf:system-relative-pathname :trial "data/ssao.frag"))

0 comments on commit 543231f

Please sign in to comment.
You can’t perform that action at this time.