Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

2D stable Fluid Simulation

Discussion in 'Made With Unity' started by Noisecrime, Sep 27, 2011.

  1. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    2,007
    Hi all,

    Recently been working on a dynamic fluid simulation for a project and thought i'd share a little interactive demo from it.

    Its pretty cpu intensive so i've posted a video as well as the demo.

    Its based on the work of Jos Stam (Real-Time Fluid Dynamics for Games) and currently implemented on the cpu, though i'm hoping it will be possible to convert it into running on the GPU in Unity.

    The demo features basic density/paint and velocity method but also the ability to add an image into the density to create interesting effects. There are some basic instructions in the demo, but ultimately its all about playing around with the values.

    The default setting is to use a 256x256 simulation grid, though you'll need a fast cpu to run it. If its too slow, just drop down to 128x128, though the images will suffer.

    Hope you enjoy it.
     
    Last edited: Sep 27, 2011
  2. DavidDebnar

    DavidDebnar

    Joined:
    Mar 19, 2011
    Posts:
    115
    +1! I would be happy if you could post a link on some pages about Jos Stams work :). I'm impressed :D.

    -- David
     
  3. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    2,007
    thx. I';ve added a link to the pdf in my original post.
     
  4. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Pretty cool. Wish this could be used to add boat wakes to one of the Unity ocean waters.
     
  5. nullstar

    nullstar

    Joined:
    Jul 2, 2010
    Posts:
    186
    Very cool. Good work :)
     
  6. spinaljack

    spinaljack

    Joined:
    Mar 18, 2010
    Posts:
    984
    Really pretty but not much in terms of game applications? I saw a game of pong once that used fluid dynamics for effects but what else could you use it for?
     
  7. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    2,007
    Interesting question, I wonder if people said the same about physics ;)

    You're right in many ways, it is mainly useful as a visual effect (especially in 2D), but don't forget there are two aspects here, first is the fluid solver, then there is the display output. No reason why you couldn't use just the fluid solver to move 'particles' (i.e anything from ducks to houses if you wanted) around, following the flow/current in the velocity map. This would not only provide better realism, it could easily be used as a specific game mechanic.

    Of course it is somewhat limited by being 2D, the next thing on my list would be to investigate SPH which can give you realistic 3D fluid. However for the time being pretty visuals are just what I need ;)
     
  8. multivac

    multivac

    Joined:
    Oct 27, 2009
    Posts:
    133
    I've always wanted to implement Stam's method into Unity, but never found time for it, good job!
    It's funny that this is called a fluid solver, but no one ever uses it for water:)
    Or is it strictly for compressible fluids?
    Could this be used to simulate flowing water, say, with a heightfield for obstacles, dams breaking, rivers flooding and similar stuff?
     
  9. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    2,007
    I think its just non-compressible, but frankly i've got so many terms and names floating around in my head now i'm not so sure ;)

    It could be used in the manor you say, although I don't think with an actual height map as it just purely 2D and the algorithm doesn't account for heights. It can be extended to 3D, but would not be optimal for what you want. However you can use stripped down NavierStokes equations for that.

    This is an old NavierStokes Unity Demo (converted from an even older Director demo). Simply left click to interact with the water, which will react to the two obstacles. Actually there are more obstacles, i just haven't placed objects in there, since the obstacles themselves are defined in a map/array of bools. I never finished this demo, which is why reflections and other stuff isn't in. It was just to compare performance against Director as I was moving over to Unity.

    The code came from a great 'lecture' by Yann L ( he had some of the best graphic threads on gamedev.net back in the day) which includes easy to use/follow source.

    solver.h
    solver.cpp
    normals.cpp
     
    Last edited: Sep 27, 2011
  10. VIC20

    VIC20

    Joined:
    Jan 19, 2008
    Posts:
    2,682
  11. Afisicos

    Afisicos

    Joined:
    Nov 13, 2010
    Posts:
    326
    very nice!
     
  12. techmage

    techmage

    Joined:
    Oct 31, 2009
    Posts:
    2,133
    this is really cool

    do you have any intentions to put this on the asset store I can think of couple things to use this for that could be valuable
     
    Last edited: Jan 6, 2013