-
Notifications
You must be signed in to change notification settings - Fork 17
/
variances.glsl
61 lines (47 loc) · 1.98 KB
/
variances.glsl
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
uniform float N_SLOPE_VARIANCE;
uniform sampler2D spectrum_1_2_Sampler;
uniform sampler2D spectrum_3_4_Sampler;
uniform int FFT_SIZE;
uniform vec4 GRID_SIZES;
uniform float slopeVarianceDelta;
uniform float c;
varying vec2 uv;
#ifdef _VERTEX_
void main() {
uv = gl_Vertex.zw;
gl_Position = vec4(gl_Vertex.xy, 0.0, 1.0);
}
#else
#define M_PI 3.14159265
vec2 getSlopeVariances(vec2 k, float A, float B, float C, vec2 spectrumSample) {
float w = 1.0 - exp(A * k.x * k.x + B * k.x * k.y + C * k.y * k.y);
vec2 kw = k * w;
return kw * kw * dot(spectrumSample, spectrumSample) * 2.0;
}
void main() {
const float SCALE = 10.0;
float a = floor(uv.x * N_SLOPE_VARIANCE);
float b = floor(uv.y * N_SLOPE_VARIANCE);
float A = pow(a / (N_SLOPE_VARIANCE - 1.0), 4.0) * SCALE;
float C = pow(c / (N_SLOPE_VARIANCE - 1.0), 4.0) * SCALE;
float B = (2.0 * b / (N_SLOPE_VARIANCE - 1.0) - 1.0) * sqrt(A * C);
A = -0.5 * A;
B = - B;
C = -0.5 * C;
vec2 slopeVariances = vec2(slopeVarianceDelta);
for (int y = 0; y < FFT_SIZE; ++y) {
for (int x = 0; x < FFT_SIZE; ++x) {
int i = x >= FFT_SIZE / 2 ? x - FFT_SIZE : x;
int j = y >= FFT_SIZE / 2 ? y - FFT_SIZE : y;
vec2 k = 2.0 * M_PI * vec2(i, j);
vec4 spectrum12 = texture2D(spectrum_1_2_Sampler, vec2(float(x) + 0.5, float(y) + 0.5) / float(FFT_SIZE));
vec4 spectrum34 = texture2D(spectrum_3_4_Sampler, vec2(float(x) + 0.5, float(y) + 0.5) / float(FFT_SIZE));
slopeVariances += getSlopeVariances(k / GRID_SIZES.x, A, B, C, spectrum12.xy);
slopeVariances += getSlopeVariances(k / GRID_SIZES.y, A, B, C, spectrum12.zw);
slopeVariances += getSlopeVariances(k / GRID_SIZES.z, A, B, C, spectrum34.xy);
slopeVariances += getSlopeVariances(k / GRID_SIZES.w, A, B, C, spectrum34.zw);
}
}
gl_FragColor = slopeVariances.xxxy;
}
#endif