]> git.tdb.fi Git - libs/gl.git/blob - demos/forestpond/data/fluidsim_velocity.glsl
Check the flat qualifier from the correct member
[libs/gl.git] / demos / forestpond / data / fluidsim_velocity.glsl
1 import fluidsim;
2
3 #pragma MSP stage(compute)
4 void main()
5 {
6         ivec2 size = imageSize(velocity_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         vec4 data = get_data(coord);
12         vec2 sd_right = get_depth(coord+ivec2(1, 0));
13         vec2 sd_up = get_depth(coord+ivec2(0, 1));
14
15         // Advection step: move velocity vectors 
16         vec2 uv_coord = (vec2(coord)+vec2(0.5))/size;
17         vec2 offset = vec2(0.5, -0.5)/size;
18         vec2 multi = delta_time/size;
19         vec2 v_right = vec2(data.x, textureLod(velocity_in, uv_coord+offset, 0).y);
20         vec2 v_up = vec2(textureLod(velocity_in, uv_coord-offset, 0).x, data.y);
21         vec2 new_velocity = vec2(textureLod(velocity_in, uv_coord-v_right*multi, 0).x,
22                 textureLod(velocity_in, uv_coord-v_up*multi, 0).y)*velocity_damping;
23
24         // Update step: change velocities based on surface level differences
25         vec2 source_depth = mix(vec2(sd_right.y, sd_up.y), vec2(data.w), greaterThan(vec2(data.z), vec2(sd_right.x, sd_up.x)));
26         new_velocity += (vec2(data.z)-vec2(sd_right.x, sd_up.x))*0.1*gravity*delta_time*min(source_depth/residual_depth, 1.0);
27
28         if(time>=drop_time && time<=drop_time+0.01 && data.w>1.0)
29         {
30                 ivec2 d = coord-drop_pos;
31                 if(d.x*d.x+d.y*d.y<4)
32                 {
33                         float v = cos(fract(time-drop_time)*628.3)*100.0;
34                         new_velocity.x += normalize(vec2(d.x+0.5, d.y)).x*v;
35                         new_velocity.y += normalize(vec2(d.x, d.y+0.5)).y*v;
36                 }
37         }
38
39         imageStore(velocity_out, coord, vec4(new_velocity, 0.0, 0.0));
40 }