2022-02-05 19:25:44 +08:00
|
|
|
#version 330
|
|
|
|
|
|
|
|
#define SSAO_SAMPLE_COUNT 32
|
|
|
|
const float radius = 0.5, bias = 0;
|
|
|
|
|
|
|
|
uniform vec3 viewPos;
|
|
|
|
uniform mat4 view, projection;
|
|
|
|
uniform float time; // current time in seconds, for extra noise
|
|
|
|
|
|
|
|
uniform sampler2D rand;
|
|
|
|
uniform vec2 randSize; // in pixels
|
|
|
|
|
|
|
|
// SSAO samples (normalized, in tangent space)
|
|
|
|
layout (std140) uniform uboSamples {
|
|
|
|
vec3 samples[SSAO_SAMPLE_COUNT];
|
|
|
|
};
|
|
|
|
|
|
|
|
// G-Buffers
|
|
|
|
uniform sampler2D gPos;
|
|
|
|
uniform sampler2D gNorm;
|
|
|
|
// Fragment information from G-Buffers
|
|
|
|
vec3 fragPos; // World position
|
|
|
|
vec3 fragNormal; // World normal
|
|
|
|
float fragDepthView;
|
|
|
|
|
|
|
|
|
|
|
|
in vec2 fragPosScreen;
|
2022-02-16 20:33:16 +08:00
|
|
|
layout (location = 0) out float outputColor;
|
2022-02-05 19:25:44 +08:00
|
|
|
|
|
|
|
|
|
|
|
// A random number generator from the web.
|
|
|
|
// https://stackoverflow.com/questions/12964279/whats-the-origin-of-this-glsl-rand-one-liner
|
|
|
|
float whatever(vec2 co){
|
|
|
|
return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void loadGBuffer() {
|
|
|
|
vec4 fragGNormal = texture(gNorm, fragPosScreen);
|
|
|
|
fragNormal = fragGNormal.xyz;
|
|
|
|
if (fragNormal == vec3(0.0f, 0.0f, 0.0f))
|
|
|
|
discard;
|
|
|
|
|
|
|
|
vec4 fragGPos = texture(gPos, fragPosScreen);
|
|
|
|
fragPos = fragGPos.xyz + viewPos;
|
|
|
|
fragDepthView = fragGPos.w;
|
|
|
|
gl_FragDepth = fragGPos.w * 0.5 + 0.5;
|
|
|
|
}
|
|
|
|
|
2022-02-26 14:05:55 +08:00
|
|
|
vec3 viewTransform(vec3 from) {
|
|
|
|
vec4 temp = view * vec4(from, 1);
|
|
|
|
return temp.xyz / temp.w;
|
|
|
|
}
|
|
|
|
|
2022-02-05 19:25:44 +08:00
|
|
|
|
|
|
|
void main() {
|
|
|
|
|
|
|
|
loadGBuffer();
|
|
|
|
|
|
|
|
vec4 randval = texture(rand, gl_FragCoord.xy / randSize);
|
|
|
|
|
2022-02-26 14:05:55 +08:00
|
|
|
//vec3 fragNormalView = viewTransform(fragNormal);
|
|
|
|
vec3 fragPosView = viewTransform(fragPos);
|
|
|
|
|
2022-02-05 19:25:44 +08:00
|
|
|
vec3 tangent = normalize(randval.xyz - fragNormal*dot(randval.xyz, fragNormal));
|
|
|
|
vec3 bitangent = cross(fragNormal, tangent);
|
2022-02-26 14:05:55 +08:00
|
|
|
mat3 TBN = mat3(view) * mat3(tangent, bitangent, fragNormal) * radius;
|
2022-02-05 19:25:44 +08:00
|
|
|
|
|
|
|
float occlusion = 0;
|
|
|
|
for (int i = 0; i < SSAO_SAMPLE_COUNT; i++) {
|
2022-02-26 14:05:55 +08:00
|
|
|
vec4 viewpos = vec4(fragPosView + TBN * samples[i], 1.0f);
|
2022-02-05 19:25:44 +08:00
|
|
|
|
|
|
|
vec4 ndcpos = projection * viewpos;
|
|
|
|
ndcpos.xyz /= ndcpos.w;
|
|
|
|
|
|
|
|
float sampleDepth = texture(gPos, ndcpos.xy*0.5 + 0.5).w;
|
|
|
|
|
|
|
|
float rangeCheck = smoothstep(0.0, 1.0, radius / abs(fragDepthView - sampleDepth));
|
|
|
|
occlusion += (sampleDepth >= viewpos.z/viewpos.w + bias ? 1.0 : 0.0) * rangeCheck;
|
|
|
|
}
|
|
|
|
|
2022-02-16 20:33:16 +08:00
|
|
|
outputColor = 1.0 - (occlusion / SSAO_SAMPLE_COUNT);
|
2022-02-05 19:25:44 +08:00
|
|
|
}
|