render: SSAO

This commit is contained in:
2022-02-05 19:25:44 +08:00
parent 05a85ac27f
commit 92b0ace7a2
9 changed files with 289 additions and 26 deletions

View File

@ -4,7 +4,7 @@ uniform sampler2D tex;
in vec3 fragPosWorld;
in float fragPosLightspaceZ;
in float fragDepthNDC;
in float fragDepthView;
in vec2 fragTexCoord;
in vec3 fragNormal;
in float fragLight;
@ -19,9 +19,9 @@ const float gamma = 2.2;
void main() {
outputPosition.xyz = fragPosWorld;
outputPosition.w = fragPosLightspaceZ;
outputPosition.w = fragDepthView;
outputNormal.xyz = fragNormal;
outputNormal.w = fragDepthNDC;
outputNormal.w = fragPosLightspaceZ;
outputColor = texture(tex, fragTexCoord);
outputColor = vec4(pow(outputColor.rgb, vec3(gamma)), outputColor.a);
}

View File

@ -14,7 +14,7 @@ layout (location = 3) in float light;
out vec3 fragPosWorld;
out float fragPosLightspaceZ;
out float fragDepthNDC;
out float fragDepthView;
out vec2 fragTexCoord;
out vec3 fragNormal;
out float fragLight;
@ -25,14 +25,16 @@ void main() {
fragNormal = normalize(normal);
fragLight = light;
gl_Position = projection * view * model * vec4(vert, 1);
fragDepthNDC = gl_Position.z / gl_Position.w;
vec4 pos4 = model * vec4(vert, 1);
fragPosWorld = pos4.xyz / pos4.w;
vec4 fragPosLightspace = lightspace * vec4(fragPosWorld, 1.0f);
fragPosLightspaceZ = fragPosLightspace.z / fragPosLightspace.w *0.5f + 0.5f;
vec4 posView = view * pos4;
fragDepthView = posView.z;
gl_Position = projection * posView;
fragPosWorld -= viewPos;
}

View File

@ -6,6 +6,8 @@ uniform vec3 viewPos;
uniform vec3 sun;
uniform vec4 fogColor;
uniform sampler2D ssaoAmbient; // Ambient strength from SSAO pass
// G-Buffers
uniform sampler2D gPos;
uniform sampler2D gNorm;
@ -40,12 +42,12 @@ void loadGBuffer() {
fragNormal = fragGNormal.xyz;
if (fragNormal == vec3(0.0f, 0.0f, 0.0f))
discard;
fragPosLightspaceZ = fragGNormal.w;
gl_FragDepth = fragGNormal.w * 0.5 + 0.5;
vec4 fragGPos = texture(gPos, fragPosScreen);
fragPos = vec4(fragGPos.xyz + viewPos, 1.0f);
fragPosLightspaceZ = fragGPos.w;
fragColor = texture(gColor, fragPosScreen);
fragPosLightspace = lightspace * fragPos;
@ -56,7 +58,7 @@ void main() {
loadGBuffer();
light = ambient;
light = ambient * texture(ssaoAmbient, fragPosScreen).r;
lightSun();

View File

@ -0,0 +1,78 @@
#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;
layout (location = 0) out float output;
// 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;
}
void main() {
loadGBuffer();
vec4 randval = texture(rand, gl_FragCoord.xy / randSize);
vec3 tangent = normalize(randval.xyz - fragNormal*dot(randval.xyz, fragNormal));
vec3 bitangent = cross(fragNormal, tangent);
mat3 TBN = mat3(tangent, bitangent, fragNormal);
float occlusion = 0;
for (int i = 0; i < SSAO_SAMPLE_COUNT; i++) {
vec3 samplePos = TBN * samples[i];
samplePos = fragPos + samplePos * radius;
vec4 viewpos = view * vec4(samplePos, 1.0);
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;
}
output = 1.0 - (occlusion / SSAO_SAMPLE_COUNT);
}

View File

@ -0,0 +1,12 @@
#version 330
layout (location = 0) in vec2 vert;
layout (location = 1) in vec2 texCoord;
out vec2 fragPosScreen;
void main() {
gl_Position = vec4(vert, 0.0f, 1);
fragPosScreen = texCoord;
}

View File

@ -0,0 +1,20 @@
#version 330
uniform sampler2D input;
uniform vec2 screenSize;
in vec2 fragPosScreen;
layout (location = 0) out float output;
void main() {
output = 0;
for (int i = -3; i <= 3; i++)
for (int j = -3; j <= 3; j++)
output += texture(input, fragPosScreen + vec2(i, j) / screenSize).r;
output /= 49.0;
}

View File

@ -0,0 +1,12 @@
#version 330
layout (location = 0) in vec2 vert;
layout (location = 1) in vec2 texCoord;
out vec2 fragPosScreen;
void main() {
gl_Position = vec4(vert, 0.0f, 1);
fragPosScreen = texCoord;
}