]> git.tdb.fi Git - libs/gl.git/blobdiff - demos/forestpond/data/fluidsim_clamp.glsl
Implement water simulation and surface shader for forest pond
[libs/gl.git] / demos / forestpond / data / fluidsim_clamp.glsl
diff --git a/demos/forestpond/data/fluidsim_clamp.glsl b/demos/forestpond/data/fluidsim_clamp.glsl
new file mode 100644 (file)
index 0000000..93e8bbc
--- /dev/null
@@ -0,0 +1,21 @@
+import fluidsim;
+
+#pragma MSP stage(compute)
+void main()
+{
+       ivec2 size = imageSize(clamping_out);
+       ivec2 coord = ivec2(gl_GlobalInvocationID.xy)+ivec2(1, 1);
+       if(coord.x>=size.x-1 || coord.y>=size.y-1)
+               return;
+
+       float depth = get_depth(coord);
+       vec2 velocity = texelFetch(velocity_in, coord, 0).xy;
+       float left_vx = texelFetch(velocity_in, coord-ivec2(1, 0), 0).x;
+       float down_vy = texelFetch(velocity_in, coord-ivec2(0, 1), 0).y;
+
+       // Clamping step: prevent fluid depth from going negative
+       float total_out = max(velocity.x, 0.0)+max(velocity.y, 0.0)-min(left_vx, 0.0)-min(down_vy, 0.0);
+       float scale = min(max_flow_fraction/(total_out*delta_time), depth/residual_depth);
+
+       imageStore(clamping_out, coord, vec4(clamp(scale, 0.0, 1.0), 0.0, 0.0, 0.0));
+}