]> git.tdb.fi Git - libs/gl.git/blob - demos/forestpond/data/fluidsim_clamp.glsl
93e8bbcfaa9f8f3c4d2a7a0588b7e992af23b1a1
[libs/gl.git] / demos / forestpond / data / fluidsim_clamp.glsl
1 import fluidsim;
2
3 #pragma MSP stage(compute)
4 void main()
5 {
6         ivec2 size = imageSize(clamping_out);
7         ivec2 coord = ivec2(gl_GlobalInvocationID.xy)+ivec2(1, 1);
8         if(coord.x>=size.x-1 || coord.y>=size.y-1)
9                 return;
10
11         float depth = get_depth(coord);
12         vec2 velocity = texelFetch(velocity_in, coord, 0).xy;
13         float left_vx = texelFetch(velocity_in, coord-ivec2(1, 0), 0).x;
14         float down_vy = texelFetch(velocity_in, coord-ivec2(0, 1), 0).y;
15
16         // Clamping step: prevent fluid depth from going negative
17         float total_out = max(velocity.x, 0.0)+max(velocity.y, 0.0)-min(left_vx, 0.0)-min(down_vy, 0.0);
18         float scale = min(max_flow_fraction/(total_out*delta_time), depth/residual_depth);
19
20         imageStore(clamping_out, coord, vec4(clamp(scale, 0.0, 1.0), 0.0, 0.0, 0.0));
21 }