]> git.sesse.net Git - nageru/blob - diffusivity.frag
Allow symlinked frame files. Useful for testing.
[nageru] / diffusivity.frag
1 #version 450 core
2
3 in vec3 tc;
4 out float g;
5 const float eps_sq = 0.001 * 0.001;
6
7 uniform sampler2DArray flow_tex, diff_flow_tex;
8
9 // Relative weighting of smoothness term.
10 uniform float alpha;
11
12 uniform bool zero_diff_flow;
13
14 // This must be a macro, since the offset needs to be a constant expression.
15 #define get_flow(x_offs, y_offs) \
16         (textureOffset(flow_tex, tc, ivec2((x_offs), (y_offs))).xy + \
17         textureOffset(diff_flow_tex, tc, ivec2((x_offs), (y_offs))).xy)
18
19 #define get_flow_no_diff(x_offs, y_offs) \
20         textureOffset(flow_tex, tc, ivec2((x_offs), (y_offs))).xy
21
22 float diffusivity(float u_x, float u_y, float v_x, float v_y)
23 {
24         return alpha * inversesqrt(u_x * u_x + u_y * u_y + v_x * v_x + v_y * v_y + eps_sq);
25 }
26
27 void main()
28 {
29         // Find diffusivity (g) for this pixel, using central differences.
30         if (zero_diff_flow) {
31                 vec2 uv_x = get_flow_no_diff(1, 0) - get_flow_no_diff(-1,  0);
32                 vec2 uv_y = get_flow_no_diff(0, 1) - get_flow_no_diff( 0, -1);
33                 g = diffusivity(uv_x.x, uv_y.x, uv_x.y, uv_y.y);
34         } else {
35                 vec2 uv_x = get_flow(1, 0) - get_flow(-1,  0);
36                 vec2 uv_y = get_flow(0, 1) - get_flow( 0, -1);
37                 g = diffusivity(uv_x.x, uv_y.x, uv_x.y, uv_y.y);
38         }
39 }