-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdenoise.frag
64 lines (52 loc) · 1.37 KB
/
denoise.frag
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/***
Port of https://www.shadertoy.com/view/4dfGDH
Adapted by CGVIRUS for the Olive-Editor Community
***/
#define MSIZE 30
uniform sampler2D tex;
varying vec2 vTexCoord;
uniform vec2 resolution;
const vec2 renderScale = vec2(1.,1.);
//Effects Parameter
uniform float sigma_s;
uniform float sigma_r;
float normpdf(in float x, in float sigma)
{
return 0.39894*exp(-0.5*x*x/(sigma*sigma))/sigma;
}
float normpdf3(in vec3 v, in float sigma)
{
return 0.39894*exp(-0.5*dot(v,v)/(sigma*sigma))/sigma;
}
void main()
{
vec2 uv = gl_FragCoord.xy / resolution.xy;
vec3 c = texture2D(tex, uv).rgb;
{
//declare stuff
int kSize = int(min((MSIZE-1)/2., 1.5*(sigma_s*0.5)*renderScale.x));
float kernel[MSIZE];
vec3 final_colour = vec3(0.0);
//create the 1-D kernel
float Z = 0.0;
for (int j = 0; j <= kSize; ++j)
{
kernel[kSize+j] = kernel[kSize-j] = normpdf(float(j), (sigma_s*0.5)*renderScale.x);
}
vec3 cc;
float factor;
float bZ = 1.0/normpdf(0.0, (sigma_r*.001));
//read out the texels
for (int i=-kSize; i <= kSize; ++i)
{
for (int j=-kSize; j <= kSize; ++j)
{
cc = texture2D(tex, uv + (vec2(float(i),float(j))) / resolution.xy).rgb;
factor = normpdf3(cc-c, (sigma_r*.001))*bZ*kernel[kSize+j]*kernel[kSize+i];
Z += factor;
final_colour += factor*cc;
}
}
gl_FragColor = vec4(final_colour/Z, texture2D( tex, uv ).a);
}
}