[sharing is fun] Fluid simulation on the GPU

    Aug 14 2014 | 9:06 am
    Attached is a Max patch that implements a simple fluid simulation using gen - jit.gl.pix. The simulation runs on the GPU and improves speed comparing to the MSA Fluid java version.
    In this exemple, the fluid is controlled using an optical flow shader (from Andrew Benson) applied on the live camera image. The source colors are given by a background "mask" image; you can also create static obstacles by sending an obstacle mask texture (dynamic obstacles that takes into account obstacle velocities are not implemented in this exemple). Boundary Conditions are managed within a 1 pixel border that is added to the source textures and removed before displaying the simulation : if you choose a resolution of 320x240, the real size of the simulation is 322x242.
    To create this example, I've started from Philip Rideout exemple, based on the Mark Harris article from GPU Gems 1. I would thanks Rajan Craveri that shared these links with me.
    The fluid algorithm uses 20 Jacobi iterations (40 will be better); this part of the algorithm is very heavy on frame rate and the most efficient (and inelegant) solution I've found was to put 20 jit.gl.pix modules in series. If some jitter gurus know a better way to do iteration, or to improve the efficiency of this exemple, I will be happy to update this patch.
    [edit]: I have fixed a bug into the patcher, fluid_gpu1.zip is the correct one

    • Aug 14 2014 | 10:33 am
      this is genuinely awesome. will study. thanks.
    • Aug 14 2014 | 10:19 pm
      Hi JMC,
      Very impressive and nice. Only problem is that on my MBP Retina, Max UI freezes when I open the camera (but the patch keeps on running, so I can see the fluids in jit.window).
      Thanks for sharing.
    • Aug 14 2014 | 10:32 pm
      wow very cool, tanks man !
    • Aug 15 2014 | 3:42 pm
    • Aug 15 2014 | 6:39 pm
      Sweet Jesus that's awesome!
    • Aug 20 2014 | 7:34 am
      Wonderful, thanks a lot!
    • Aug 22 2014 | 10:22 pm
      Demonstrates the power of the Jitter dance !!!
      Thanks and respect
    • Dec 04 2014 | 4:47 pm
      I'm totally in love with this, really clarifies optical flow for me, but I may have found a bug. Has anyone used it with Yosemite yet? The patch is fine on Maverick but in Yosemite there appears a continuous "current" from left to right across the window, as if something is continuously moving in front of the cam. The jit.grab feed shows no unique noise. Any idea what would cause this glitch?
    • Dec 05 2014 | 2:10 am
      I'm on Yosemite and get the same left to right flow. Don't have Mavericks anymore so I can't see how it is supposed to be, but it is very cool anyways! Seems there are still some critical openGL bugs to iron out, though I get the same flow in Max6 in Yosemete.
    • Dec 06 2014 | 9:49 pm
      thank for share , problem here i think ,, is few ppl like to share Jitter patch , i m an artist share is gold FOR ART, important is not fx, but what happen in GENERAL Idea IN CONTENT
    • Dec 06 2014 | 10:53 pm
      Hello, @JESSERICKE, @CHRISTOPHER OVERSTREET: In the patcher there is a trick to add borders before computing fluid and remove them after. This trick uses the fact that with jit.gl.pix you can have 2 inputs with different dims. I've encountered this issue on a Mac Pro with AMD FirePro, and maybe with Yosemite the same problem occurs. If someone knows how to do this differently, I will be happy to know how, because I use this trick in different situations. I will try soon to make a patcher without the add border feature and post it here.
    • Dec 08 2014 | 12:04 am
      I am an artist in the classical sense Picasso and Braque had a studio in the same room both two great artists painted canvas identical no1 could understand what was Picasso or what was Braque the two artists were laughing at this Today it seems that the art becomes jealous copyright
      but this is not art but another thing
    • Dec 08 2014 | 1:52 am
      I am a big fan of Anton von Werner. He was a great Artist! He didn't care much about FX himself. In his villa at the Wannsee, he had locked some young boys doing all the boring work for him! So he could contemplate more about the General Idea, the Content! Compared to him, Picasso and Braque were only dilettants. These guys still did lots of FX by themselves and believed that art had some relation to its medium.
    • Dec 08 2014 | 2:44 pm
      workshops art were born in Italy a master and many boys help him in th project. An example Raffaello Sanzio in the Vatican rooms "Raphael Rooms" Raphael call about 20 young artist each one had his ability in a sector the project was develop by Raphael when Raphael died at age 37 these guys could not produce anything else so important, like before The art is a journey for awareness FX are only 0,1 content idea is 99,9
    • Dec 09 2014 | 6:03 pm
      Hi CMJ. Thanks for the reply. I'm still getting acclimated to pix and gen and such things, and certainly can't recommend a better technique. But I look forward to the updated patch and anything else you're working on. For someone new to script like myself, such patches are very instructive.
    • Mar 25 2015 | 1:02 pm
      thanks for the patch! any luck with updating it to yosemite? any ideas how to fix the continuous flow? thanks again, zb
    • Jan 13 2016 | 3:20 pm
      A little late to the party here, but also wanted to express my gratitude for the work here! Think I might be having too much fun with this patch :)
      Quick pix-newbie question: with obstacles (e.g. the plus sign in the patch) is it possible to see the affects of the obstacle (altered flow) but not mix in the image of the obstacle itself in the output? i.e. make the obstacle invisible on the output? I'm playing around with an idea and just want the "disturb" fluid as the image result. Maybe remap the obstacle to the alpha channel and use the alpha channel as the obstacle?
      Thanks in advance for any pointers! Time for me to learn pix!
      Cheers, David
    • Jan 14 2016 | 8:45 pm
      @David in the patch the obstacle acts at two levels: in the fluid algorithm and in the color propagation. There are no fluid in the obstacle zone as the algorithm is a 2D model, what you see is not the obstacle image but the effect of the obstacle. To do what you want, maybe you can add a new step in the fluid algorithm to slowdown the velocity in the obstacle area. See the exemple attached : fluid_gpu_obstacles.maxpat. You can adjust how your obstacle will slowdown the fluid.
      @JESSERICKE, @CHRISTOPHER, @ZBGNWBRZZNSK I've added a trigger to choose to use or not the trick to hide the border. maybe it works now in Yosemite.
    • Jan 14 2016 | 11:10 pm
      Thank you! Beautiful and very educational.
    • Jan 29 2016 | 9:34 am
      ohoo. Wonderful! Interesting find indeed!
    • Jun 25 2016 | 1:34 pm
      This is really amazing - thanks so much for sharing, JMC! I've adapted it a bit and am using it in a project I'm bringing to Burning Man this year: https://www.facebook.com/themusicalperiscope/
      Will credit you everywhere!
      Best, Yuli
    • Jul 29 2016 | 5:29 am
      Love the patch! Very Cool!
    • Jan 05 2017 | 8:14 pm
      Amazing patch, thanks for sharing ! where do you edit the background color or make the flow brighter thanks in advance.
    • Jan 30 2017 | 10:40 am
      I've been trying to implement this for week, thanks :D
    • Jun 16 2017 | 2:39 pm
      Brilliant patch! Any further advancements? :)
    • May 24 2018 | 11:35 am
      Hi JMC,
      Thanks for the work on this patch!
      Is there a way to contact you through email or such? I would like to get in contact concerning a project!
    • May 25 2018 | 8:12 pm
      You can reach him here: http://www.blueyeti.fr/
    • May 26 2018 | 2:42 pm
      Hello Joris,
      you can contact me at jmc[at]blueyeti.fr
    • Nov 24 2018 | 7:04 am
      Wow! This is amazing!!! Thanks.
    • Nov 24 2018 | 12:51 pm
      Don't forget that in 2018 we have Amazing Max Tutorials'solution with using code from Shadertoy. Where there are tons of fluids. And very very good ones. https://www.youtube.com/watch?v=WpcsiuqrjwQ
    • Sep 16 2019 | 10:14 am
      Brilliant patch, thanks so much JMC!
      I have adapted the algorithm a bit and built a basic GUI to allow easy application of colour and velocity matrices. An alpha channel is derived from the output, allowing basic but effective background compositing (note: this version does not support obstacles). Try setting all the sources to your webcam...
    • Sep 16 2019 | 9:09 pm
      A note: If you're running GL3, then you'll find an error message, since autoclear for jit.gl.texture is an obsolete message (not sure what/if there is a workaround)....