]> git.sesse.net Git - nageru/blob - smoothness.frag
Calculate smoothness and set up the equations. Still fairly buggy (missing 0..1 norma...
[nageru] / smoothness.frag
1 #version 450 core
2
3 in vec2 tc;
4 out float smoothness_x, smoothness_y;
5 const float eps_sq = 0.001 * 0.001;
6
7 uniform sampler2D flow_tex, diff_flow_tex;
8
9 // The base flow needs to be normalized.
10 // TODO: Should we perhaps reduce this to a separate two-component
11 // texture when calculating the derivatives?
12 vec2 normalize_flow(vec3 flow)
13 {
14         return flow.xy / flow.z;
15 }
16
17 // This must be a macro, since the offset needs to be a constant expression.
18 #define get_flow(x_offs, y_offs) \
19         (normalize_flow(textureOffset(flow_tex, tc, ivec2((x_offs), (y_offs))).xyz) + \
20         textureOffset(diff_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 -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         float g, g_right, g_up;
30
31         // These are shared between some of the diffusivities.
32         vec2 flow_0_0 = get_flow(0, 0);
33         vec2 flow_1_1 = get_flow(1, 1);
34
35         // Find diffusivity (g) for this pixel, using central differences.
36         {
37                 vec2 uv_x = get_flow(1, 0) - get_flow(-1,  0);
38                 vec2 uv_y = get_flow(0, 1) - get_flow( 0, -1);
39                 g = diffusivity(uv_x.x, uv_y.x, uv_x.y, uv_y.y);
40         }
41
42         // Now find diffusivity for the pixel to the right.
43         {
44                 vec2 uv_x = get_flow(2, 0) - flow_0_0;
45                 vec2 uv_y = flow_1_1 - get_flow( 1, -1);
46                 g_right = diffusivity(uv_x.x, uv_y.x, uv_x.y, uv_y.y);
47         }
48
49         // And up.
50         {
51                 vec2 uv_x = flow_1_1 - get_flow(-1,  1);
52                 vec2 uv_y = get_flow(0, 2) - flow_0_0;
53                 g_up = diffusivity(uv_x.x, uv_y.x, uv_x.y, uv_y.y);
54         }
55
56         smoothness_x = 0.5 * (g + g_right);
57         smoothness_y = 0.5 * (g + g_up);
58 }