From 39863e87276116d50dce7dd17f7095c8e1796dc1 Mon Sep 17 00:00:00 2001 From: Joshua Barretto Date: Thu, 27 Jan 2022 00:29:03 +0000 Subject: [PATCH] Remove colour banding --- CHANGELOG.md | 1 + assets/voxygen/shaders/include/random.glsl | 13 ++++++++++--- assets/voxygen/shaders/postprocess-frag.glsl | 12 ++++++++---- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e7ce8e0fd8..80949a285c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added arthropods - A 'point light glow' effect, making lanterns and other point lights more visually pronounced - Generate random name for site2 sites +- Shader dithering to remove banding from scenes with large colour gradients ### Changed diff --git a/assets/voxygen/shaders/include/random.glsl b/assets/voxygen/shaders/include/random.glsl index 482e631911..a3ce9c8137 100644 --- a/assets/voxygen/shaders/include/random.glsl +++ b/assets/voxygen/shaders/include/random.glsl @@ -10,9 +10,9 @@ float hash(vec4 p) { return (fract(p.x * p.y * (1.0 - p.z) * p.w * (p.x + p.y + p.z + p.w)) - 0.5) * 2.0; } -#define M1 1597334677U //1719413*929 -#define M2 3812015801U //140473*2467*11 -#define M3 2741598923U +#define M1 2047667443U +#define M2 3883706873U +#define M3 3961281721U float hash_one(uint q) { uint n = ((M3 * q) ^ M2) * M1; @@ -20,6 +20,13 @@ float hash_one(uint q) { return float(n) * (1.0 / float(0xffffffffU)); } +float hash_two(uvec2 q) { + q *= uvec2(M1, M2); + uint n = q.x ^ q.y; + n = n * (n ^ (n >> 15)); + return float(n) * (1.0 / float(0xffffffffU)); +} + float hash_fast(uvec3 q) { q *= uvec3(M1, M2, M3); diff --git a/assets/voxygen/shaders/postprocess-frag.glsl b/assets/voxygen/shaders/postprocess-frag.glsl index d80ae3d5a7..8882f42faf 100644 --- a/assets/voxygen/shaders/postprocess-frag.glsl +++ b/assets/voxygen/shaders/postprocess-frag.glsl @@ -197,15 +197,15 @@ void main() { // return; // } + vec2 sample_uv = uv; #ifdef EXPERIMENTAL_UNDERWARPER - vec2 uv = uv; if (medium.x == MEDIUM_WATER) { - uv += sin(uv.yx * 40 + tick.xx * 1.0) * 0.003; + sample_uv += sin(uv.yx * 40 + tick.xx * 1.0) * 0.003; } #endif - vec4 aa_color = aa_apply(t_src_color, s_src_color, uv * screen_res.xy, screen_res.xy); - + vec4 aa_color = aa_apply(t_src_color, s_src_color, sample_uv * screen_res.xy, screen_res.xy); + #ifdef EXPERIMENTAL_SOBEL vec3 s[8]; s[0] = aa_sample(uv, vec2(-1, 1)); @@ -282,5 +282,9 @@ void main() { } #endif + // Add a small amount of very cheap dithering noise to remove banding from gradients + // TODO: Instead of 256, detect the colour resolution of the display + final_color.rgb = max(vec3(0), final_color.rgb - hash_two(uvec2(uv * screen_res.xy)) / 256.0); + tgt_color = vec4(final_color.rgb, 1); }