From 1c672e0f420f5b7b60c4ef5874c160d8d1791ef2 Mon Sep 17 00:00:00 2001 From: Cat Flynn Date: Sun, 26 Feb 2023 02:49:52 +0000 Subject: [PATCH] add denoise --- shader/quad/shader.frag | 48 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/shader/quad/shader.frag b/shader/quad/shader.frag index cadfb1e..45921df 100644 --- a/shader/quad/shader.frag +++ b/shader/quad/shader.frag @@ -6,7 +6,53 @@ in vec2 TexCoord; uniform sampler2D ourTexture; +#define INV_SQRT_OF_2PI 0.39894228040143267793994605993439 // 1.0/SQRT_OF_2PI +#define INV_PI 0.31830988618379067153776752674503 + +// https://github.com/BrutPitt/glslSmartDeNoise/blob/master/Shaders/frag.glsl +vec4 denoise(sampler2D tex, vec2 uv, float sigma, float kSigma, float threshold) +{ + float radius = round(kSigma*sigma); + float radQ = radius*radius; + + float invSigmaQx2 = .5 / (sigma*sigma); + float invSigmaQx2PI = INV_PI * invSigmaQx2; + + float invThresholdSqx2 = .5 / (threshold*threshold); + float invThresholdSqrt2PI = INV_SQRT_OF_2PI / threshold; + + vec4 centrPx = texture(tex,uv); + + float zBuff = 0.0; + vec4 aBuff = vec4(0.0); + vec2 size = vec2(textureSize(tex, 0)); + + vec2 d; + for (d.x=-radius; d.x <= radius; d.x++) + { + float pt = sqrt(radQ-d.x*d.x); + for(d.y=-pt; d.y <= pt; d.y++) + { + float blurFactor = exp(-dot(d,d)*invSigmaQx2)*invSigmaQx2PI; + + vec4 walkPx = texture(tex,uv+d/size); + + vec4 dC = walkPx-centrPx; + float deltaFactor = exp(-dot(dC.rgb,dC.rgb) * invThresholdSqx2) * invThresholdSqrt2PI * blurFactor; + + zBuff += deltaFactor; + aBuff += deltaFactor*walkPx; + } + } + return aBuff/zBuff; +} + void main() { - FragColor = texture(ourTexture, TexCoord); + float sigma = 2.2; + float kSigma = 10.0; + float threshold = 0.2; + + FragColor = denoise(ourTexture, TexCoord, sigma, kSigma, threshold); + //FragColor = texture(ourTexture, TexCoord); }