Added SSAO

This commit is contained in:
Antoine Pilote
2023-03-05 13:00:12 -05:00
parent e23f6ac14e
commit 47985f507a
12 changed files with 379 additions and 11 deletions

View File

@@ -0,0 +1,36 @@
#shader vertex
#version 460 core
layout(location = 0) in vec3 VertexPosition;
layout(location = 1) in vec2 UVPosition;
out flat vec2 UV;
void main()
{
UV = UVPosition;
gl_Position = vec4(VertexPosition, 1.0f);
}
#shader fragment
#version 460 core
out vec4 FragColor;
in vec2 UV;
uniform sampler2D u_Input;
void main() {
vec2 texelSize = 1.0 / vec2(textureSize(u_Input, 0));
float result = 0.0;
for (int x = -2; x < 2; ++x)
{
for (int y = -2; y < 2; ++y)
{
vec2 offset = vec2(float(x), float(y)) * texelSize;
result += texture(u_Input, UV + offset).r;
}
}
float channel = result / (4.0 * 4.0);
FragColor = vec4(channel, channel, channel, 1.0);
}

View File

@@ -37,6 +37,7 @@ uniform sampler2D m_Depth;
uniform sampler2D m_Albedo;
uniform sampler2D m_Material;
uniform sampler2D m_Normal;
uniform sampler2D m_SSAO;
// Lights
const int MaxLight = 29;
@@ -221,7 +222,7 @@ void main()
float metallic = texture(m_Material, UV).r;
float roughness = texture(m_Material, UV).b;
float ao = texture(m_Material, UV).g;
float ssao = texture(m_SSAO, UV).r;
vec3 N = normal;
vec3 V = normalize(u_EyePosition - worldPos);
vec3 R = reflect(-V, N);
@@ -280,8 +281,8 @@ void main()
vec3 kD = 1.0 - kS;
kD *= 1.0 - metallic;
vec3 ambient = (kD * albedo) * ao;
vec3 color = ambient + Lo;
vec3 ambient = (kD * albedo) * (ao);
vec3 color = (ambient * ssao) + Lo;
// Display CSM splits..
/*float depth = length(worldPos - u_EyePosition);

View File

@@ -0,0 +1,115 @@
#shader vertex
#version 460 core
layout(location = 0) in vec3 VertexPosition;
layout(location = 1) in vec2 UVPosition;
out flat vec2 UV;
out mat4 v_InvView;
out mat4 v_InvProjection;
out mat4 v_View;
uniform mat4 u_View;
uniform mat4 u_Projection;
void main()
{
v_View = u_View;
v_InvView = inverse(u_View);
v_InvProjection = inverse(u_Projection);
UV = UVPosition;
gl_Position = vec4(VertexPosition, 1.0f);
}
#shader fragment
#version 460 core
uniform sampler2D u_Depth;
uniform sampler2D u_Normal;
uniform sampler2D u_Noise;
uniform int u_KernelSize = 64;
uniform vec3 u_Samples[64];
uniform mat4 u_Projection;
uniform vec2 u_NoiseScale;
uniform float u_Radius = 0.5f;
uniform float u_Bias = 0.025f;
uniform float u_Area;
in vec2 UV;
in mat4 v_View;
in mat4 v_InvView;
in mat4 v_InvProjection;
out vec4 FragColor;
vec3 WorldPosFromDepth(float depth)
{
float z = depth * 2.0 - 1.0;
vec4 clipSpacePosition = vec4(UV * 2.0 - 1.0, z, 1.0);
vec4 viewSpacePosition = v_InvProjection * clipSpacePosition;
// Perspective division
viewSpacePosition /= viewSpacePosition.w;
vec4 worldSpacePosition = v_InvView * viewSpacePosition;
return worldSpacePosition.xyz;
}
vec3 ViewPosFromDepth(float depth)
{
float z = depth * 2.0 - 1.0;
vec4 clipSpacePosition = vec4(UV * 2.0 - 1.0, z, 1.0);
vec4 viewSpacePosition = v_InvProjection * clipSpacePosition;
viewSpacePosition.xyz /= viewSpacePosition.w;
return viewSpacePosition.xyz;
}
vec3 normal_from_depth(float depth, vec2 texcoords) {
const vec2 offset1 = vec2(0.0,0.0002);
const vec2 offset2 = vec2(0.0002,0.0);
float depth1 = texture(u_Depth, UV + offset1).r;
float depth2 = texture(u_Depth, UV + offset2).r;
vec3 p1 = vec3(offset1, depth1 - depth);
vec3 p2 = vec3(offset2, depth2 - depth);
vec3 normal = cross(p1, p2);
normal.z = -normal.z;
return normalize(normal);
}
void main()
{
float depth = texture(u_Depth, UV).r ;
vec3 fragPos = ViewPosFromDepth(depth);
vec3 normal = normal_from_depth(depth, UV) ;
vec3 randomVec = texture(u_Noise, UV * u_NoiseScale).xyz;
float radius_depth = u_Radius/depth;
float occlusion = 0.0;
vec3 position = vec3(UV, depth);
const float falloff = 0.0002;
const float area = 0.0075;
for(int i=0; i < 64; i++)
{
vec3 ray = radius_depth * reflect(u_Samples[i], randomVec);
vec3 hemi_ray = position + sign(dot(ray,normal)) * ray;
float occ_depth = texture(u_Depth, hemi_ray.xy).r;
float difference = depth - occ_depth ;
occlusion += step(falloff, difference) * (1.0 - smoothstep(falloff, u_Bias, difference));
}
float ao = 1.0 - 2.0 * occlusion * (1.0 / 64);
FragColor = vec4(ao, ao, ao , 1.0);
}

View File

@@ -507,6 +507,41 @@ namespace Nuake {
ImGui::PopStyleColor();
}
if (env->SSAOEnabled)
{
ImGui::TableNextColumn();
{
// Title
ImGui::Text("SSAO Radius");
ImGui::TableNextColumn();
ImGui::DragFloat("##SSAORadius", &env->mSSAO->Radius, 0.01f, 0.0f, 1.0f);
ImGui::TableNextColumn();
// Reset button
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(1, 1, 1, 0));
std::string resetRadius = ICON_FA_UNDO + std::string("##resetRadius");
if (ImGui::Button(resetRadius.c_str())) env->mSSAO->Radius = 0.5f;
ImGui::PopStyleColor();
}
ImGui::TableNextColumn();
{
// Title
ImGui::Text("SSAO Bias");
ImGui::TableNextColumn();
ImGui::DragFloat("##SSAOBias", &env->mSSAO->Bias, 0.0001f, 0.0f, 0.5f);
ImGui::TableNextColumn();
// Reset button
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(1, 1, 1, 0));
std::string resetBloomThreshold = ICON_FA_UNDO + std::string("##resetSSAOBias");
if (ImGui::Button(resetBloomThreshold.c_str())) env->mSSAO->Bias = 0.025f;
ImGui::PopStyleColor();
}
}
if (env->BloomEnabled)
{
ImGui::TableNextColumn();