From 99d25b5ef615a23ef63645fea87596b3384b5ede Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 15 Jun 2019 15:23:02 +0300 Subject: [PATCH] Make ambient occlusion edge detection threshold adjustable This may be necessary to avoid artifacts when running the postprocessor at a reduced resolution. --- shaderlib/ambientocclusion.glsl | 1 + shaderlib/ambientocclusion_combine.glsl | 2 +- source/ambientocclusion.cpp | 11 ++++++++++- source/ambientocclusion.h | 2 ++ 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/shaderlib/ambientocclusion.glsl b/shaderlib/ambientocclusion.glsl index 7f1e79a0..a9873b1d 100644 --- a/shaderlib/ambientocclusion.glsl +++ b/shaderlib/ambientocclusion.glsl @@ -12,6 +12,7 @@ uniform AmbientOcclusionParams vec3 sample_points[max_samples]; int n_samples; float occlusion_radius; + float edge_depth_threshold; }; #pragma MSP stage(fragment) diff --git a/shaderlib/ambientocclusion_combine.glsl b/shaderlib/ambientocclusion_combine.glsl index 05aea60a..c1e7f272 100644 --- a/shaderlib/ambientocclusion_combine.glsl +++ b/shaderlib/ambientocclusion_combine.glsl @@ -15,7 +15,7 @@ void main() vec2 sample_coord = texcoord+offset*tex_scale; float occ = texture(occlusion, sample_coord).r; float sample = texture(depth, sample_coord).r; - float z_range = occlusion_radius*length(offset)*0.1; + float z_range = occlusion_radius*length(offset)*edge_depth_threshold; float min_depth = project(vec3(center.xy, center.z+z_range)).z; float max_depth = project(vec3(center.xy, center.z-z_range)).z; if(sample>=min_depth && sample<=max_depth) diff --git a/source/ambientocclusion.cpp b/source/ambientocclusion.cpp index 0079009b..ed7d1636 100644 --- a/source/ambientocclusion.cpp +++ b/source/ambientocclusion.cpp @@ -47,6 +47,7 @@ AmbientOcclusion::AmbientOcclusion(unsigned w, unsigned h, float): set_n_samples(16); set_occlusion_radius(0.5f); set_darkness(1.0f); + set_edge_depth_threshold(0.1f); } float AmbientOcclusion::random(unsigned &seed) @@ -87,6 +88,11 @@ void AmbientOcclusion::set_darkness(float darkness) shdata.uniform("darkness", darkness); } +void AmbientOcclusion::set_edge_depth_threshold(float edt) +{ + shdata.uniform("edge_depth_threshold", edt); +} + void AmbientOcclusion::render(Renderer &renderer, const Texture2D &color, const Texture2D &depth) { texturing.attach(0, color); @@ -112,7 +118,8 @@ void AmbientOcclusion::render(Renderer &renderer, const Texture2D &color, const AmbientOcclusion::Template::Template(): n_samples(16), occlusion_radius(0.5f), - darkness(1.0f) + darkness(1.0f), + edge_depth_threshold(0.1f) { } AmbientOcclusion *AmbientOcclusion::Template::create(unsigned width, unsigned height) const @@ -121,6 +128,7 @@ AmbientOcclusion *AmbientOcclusion::Template::create(unsigned width, unsigned he ao->set_n_samples(n_samples); ao->set_occlusion_radius(occlusion_radius); ao->set_darkness(darkness); + ao->set_edge_depth_threshold(edge_depth_threshold); return ao.release(); } @@ -129,6 +137,7 @@ AmbientOcclusion::Template::Loader::Loader(Template &t): DataFile::DerivedObjectLoader(t) { add("darkness", &Template::darkness); + add("edge_depth_threshold", &Template::edge_depth_threshold); add("occlusion_radius", &Template::occlusion_radius); add("samples", &Template::n_samples); } diff --git a/source/ambientocclusion.h b/source/ambientocclusion.h index e7829c18..41918994 100644 --- a/source/ambientocclusion.h +++ b/source/ambientocclusion.h @@ -32,6 +32,7 @@ public: unsigned n_samples; float occlusion_radius; float darkness; + float edge_depth_threshold; Template(); @@ -56,6 +57,7 @@ private: public: void set_n_samples(unsigned); void set_occlusion_radius(float); + void set_edge_depth_threshold(float); // Deprecated void set_depth_ratio(float); -- 2.43.0