Improved SSAO

This commit is contained in:
antopilo
2023-03-13 22:18:00 -04:00
parent 16bb074a5f
commit e78edb7052
4 changed files with 86 additions and 9 deletions

View File

@@ -36,7 +36,9 @@ uniform mat4 u_Projection;
uniform vec2 u_NoiseScale;
uniform float u_Radius = 0.5f;
uniform float u_Bias = 0.025f;
uniform float u_Area;
uniform float u_Falloff = 0.0022;
uniform float u_Area = 0.0075;
uniform float u_Strength = 2.0f;
in vec2 UV;
in mat4 v_View;
in mat4 v_InvView;
@@ -92,24 +94,46 @@ void main()
float depth = texture(u_Depth, UV).r ;
vec3 fragPos = ViewPosFromDepth(depth);
vec3 normal = normal_from_depth(depth, UV) ;
if(depth > 0.9999999f)
{
FragColor = vec4(0, 0, 0, 0);
return;
}
vec3 goodNormal = normal_from_depth(depth, UV);
vec3 normal = texture(u_Normal, UV).rgb * 2.0 - 1.0;
vec4 normalT = v_View * vec4(normal, 1.0);
normalT *= -1.0f;
normal = normalT.xyz;
normal = normalize(normal);
normal = goodNormal;
normal = normalT.xyz;
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;
int skipped = 0;
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;
/*
if(dot(normal, normalize(ray)) < u_Falloff)
{
skipped++;
continue;
}*/
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 difference = depth - occ_depth + u_Bias;
float rangeCheck = smoothstep(0.0, 1.0, u_Radius * 10.0 / abs(depth - occ_depth));
//float rangeCheck = abs(depth - occ_depth) < u_Radius ? 1.0 : 0.0;
occlusion += ( occ_depth <= hemi_ray.z - u_Bias ? 1.0 : 0.0) * rangeCheck;
}
float ao = 1.0 - 2.0 * occlusion * (1.0 / 64);
float ao = 1.0 - u_Strength * occlusion * (1.0 / (64 ));
FragColor = vec4(ao, ao, ao , 1.0);
}

View File

@@ -548,6 +548,22 @@ namespace Nuake {
if (env->SSAOEnabled)
{
ImGui::TableNextColumn();
{
// Title
ImGui::Text("SSAO Strength");
ImGui::TableNextColumn();
ImGui::DragFloat("##SSAOStrength", &env->mSSAO->Strength, 0.1f, 0.0f, 10.0f);
ImGui::TableNextColumn();
// Reset button
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(1, 1, 1, 0));
std::string resetRadius = ICON_FA_UNDO + std::string("##resetStrength");
if (ImGui::Button(resetRadius.c_str())) env->mSSAO->Strength = 2.0f;
ImGui::PopStyleColor();
}
ImGui::TableNextColumn();
{
// Title
@@ -579,6 +595,38 @@ namespace Nuake {
if (ImGui::Button(resetBloomThreshold.c_str())) env->mSSAO->Bias = 0.025f;
ImGui::PopStyleColor();
}
ImGui::TableNextColumn();
{
// Title
ImGui::Text("SSAO Area");
ImGui::TableNextColumn();
ImGui::DragFloat("##SSAOArea", &env->mSSAO->Area, 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("##resetSSAOArea");
if (ImGui::Button(resetBloomThreshold.c_str())) env->mSSAO->Area = 0.0075f;
ImGui::PopStyleColor();
}
ImGui::TableNextColumn();
{
// Title
ImGui::Text("SSAO Falloff");
ImGui::TableNextColumn();
ImGui::DragFloat("##SSAOFalloff", &env->mSSAO->Falloff, 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("##resetSSAOFalloff");
if (ImGui::Button(resetBloomThreshold.c_str())) env->mSSAO->Falloff = 0.0022f;
ImGui::PopStyleColor();
}
}
if (env->BloomEnabled)

View File

@@ -101,6 +101,9 @@ namespace Nuake
shader->SetUniform1i("u_KernelSize", 64);
shader->SetUniform1f("u_Radius", Radius);
shader->SetUniform1f("u_Bias", Bias);
shader->SetUniform1f("u_Falloff", Falloff);
shader->SetUniform1f("u_Area", Area);
shader->SetUniform1f("u_Strength", Strength);
shader->SetUniformVec2("u_NoiseScale", Vector2(_size.x / 4, _size.y / 4) );
int i = 0;

View File

@@ -29,7 +29,9 @@ namespace Nuake
public:
float Radius = 0.07f;
float Bias = 0.003f;
float Area = 0.0075f;
float Falloff = 0.002f;
float Strength = 2.0f;
void Resize(const Vector2& size);
SSAO();
void Draw(FrameBuffer* gBuffer, const Matrix4& projection, const Matrix4& view);