I don't suffer from insanity, I enjoy every minute of it
Ramblings of
Alex Lovett
RSS
Twitter
Tumblr
Youtube
LinkedIn

Navigation:

Up a Level - Store - Experiments

Documents:

Lumen - Website - WheelReview - Painting_with_Light - Drawing - Food - GKN - Inspiration - House - Valideus - RoundTree - Reality_2.0 - Math_Art - Lilly - Unity5 - GameDesign - WishList - Fridge - XFactor
Tags: - Programming - Games - Shadowood - Unity
Show comments

Fluid Simulation, ah where to even begin

I'm busy working on a game and wanted to add some nice subtle touches. To have particles in the air or embers emitted from destroyed 'items' and have them not just dart about the screen as is per usual with particles. But to interact with each other, with objects in the scene that are moving in and with the users touch input on the screen, thus bringing them to life a bit. Air is basically a fluid so essentially it is a fluid dynamics problem. One I had the naivety to think I could solve simply

:-)



How hard can it be to just have particles move about and push each other around.

Surely someone has already done this and I can just use that.

Now first off there are various kinds of approach to fluids sims, all with trade offs. Some good at 2D bad at 3D, some good at gases and turbulent spirals but bad at contiguous bodies of water e.g. with surface tension. The approach for modeling say a bucket filling up with water versus a room full of hot smoke are very different.

For Unity I have found a couple of libraries

FluidSim by Philip Heckinger ( Pheck )
What appears to be a good, well implemented version, best I have found for Unity for 2D fluids

A 2D CPU or GPU accelerated implementation using 'Eulerian' method of dividing a space into cells with velocity/pressure vectors

I inquired with the creator and he had added 'vorticity confinement' but for his uses decided to remove it for performance it added little
And has not added 'viscous diffusion'
--

Very similar to the implementation here by 'Scrawk' done in Unity3D

However this implementation is incomplete and I am too stupid to work out how to augment it ;-D


it is incomplete as it is itself based upon an implementation by Philip Rideout who'm decided to leave out Viscous Diffusion and Vorticity Confinement as he did not require them ( though fluidsim above also omits these things )

The Little Grasshopper - Simple Fluid Simulation ( written in C )



Which is itself based upon the work here in GPU Gems 1 by Mark J. Harris:



And some have found inspiration from the work of Jonathon M. Cohen of NVIDIA Research here:



And in this research here by Jos Stam for Alias
Real-Time Fluid Dynamics for Games

A couple of people I found used Jos Stams work

This here which seems broken by VIC20

And work by NoiseCrime ( whom wants to keep the efforts to himself "part of a bigger project and as such i'm unable to release the code at present" )

Another article by Mick West
Practical Fluid Mechanics



And then theres the very impressive work of Grant Kot:

Who has produced a realtime sim combined various methods and the only one I have seen that tackles surface tensions and manages to merge a good gas/swirly swim with solid water like properties too. Check GFlow HD for iPad ( free ) though I didn't fancy attempting to port this to C# and all the performance implication that might have.

And then there is Fluvio for Unity3D by Thinksquirrel Software

Which is a 3D particle based approach using what he claims is SPH ( Smoothed Particle Hydrodynamics ) but I have failed to see any evidence of a fluid sim at work here. It just looks awful. I'm possibly wrong and it is amazing but the demos and videos show it doing a poor job at pretty much everything. Understandably doing 3D fluids in Unity is going to be tricky and theres nothing else on the store that does what Fluvio does... but I'm left unimpressed by the demos and how coarse they are, they barely exhibit any of the behavior I would define a fluid by. More like a series of balls attached together with weak springs... and It's not exactly 'cheap' either. Maybe I am missing something! as the free demo only lets you use 100 particles kinda making it impossible to test It's metal.

And quite annoyingly the guys at Unity seem to have solved exactly what I need with a SPH Fluid solution for Shuriken particles here:
Look for video here 'Fluid Solver'
( what an awful website Unity! )

---

Ok I purchased the Unity Fluidsim and have started trying to make sense of it and bend it to my will!:



Got it running fairly well on the phone thankfully, I only need it low res ( unlike the high res in the video ) and it is working fine with all the post effects and a ton of particles, which is surprising for a phone! ( iPhone 5 )



Main problem now is getting it to interact with touch input, how to get it to push the fluid around. It has build in collision detection of sorts but it is totally rubbish so I will likely have to do it all myself... that is once I figure out how this is all setup internally *starts prodding and poking the code looking very confused

With no description and no code and no sign of any further work on it, just a video showing how cool it is ;-P *raises fist to the sky*

This is way more involving than I had hoped for, I just wanted to be able to touch the fluid and swish it about... simple I thought. I just spent most of the day writing multi touch code to detect angle and velocity to even get started on the problem. The way this 'Fluidsim' works is a bit nuts in terms of updating various properties per frame, it just polls at regular intervals even if nothing has changed, fair enough, but I want it to be a bit more performant than that and update only when something has changed and only draw the thing that has changed not loop thru all objects process each one. Nightmare.

Next problem how to make a tap push fluid around it in every direction. Seems so simple but yet again, nope hard as a pixel/point can only have one direction at a time naturally. So to have it move in all you need a circular shape with each pixels direction set to say from the center of the circle, not too complicated that is if you are used to writing shader code. Dealing with colors and directions as numbers/vectors hurts my brain immeasurably. Well figured that out and it was ridiculously simple in the end once my brain wrapped around it and of course first solved it in an over clever complicated and highly convoluted manner.



Here is a video of multi touch input running on my desktop computer:




And now after many days banging my head against the proverbial wall, here are particles suspended in fluid *drum roll*
now just to make it look nice, like dust glinting in sunbeams, and yes it is lagging and a bit jittery but I'm working on that


Never in my life have I struggled so much, long days with nothing to show for them, mindless trying different combination of things. But I prevail victorious, my stoicism knows no bounds.
Turns out the difficulty I was having was two fold, one a bug in Unity itself which was fixed 2 days before I started working on this!!!! but I had neglected to update. And secondly a continuing bug that prevents ReadPixels to work when Anti Aliasing is enabled on iOS. Then it was only am after of mere... 10's of hours to get it working properly ;-P

Now I need to work on the performance, using object pools and trying to augment the existing particle system instead of lots of floating quads each with expensive transforms and a heavy 3D physics engine attached to each particle.

I'm curious as to if an image based approach also running on the GPU would have been easier and likely more performant... tempting.. even if it does largely negate the work I have done

Projects like TC Particles using DirectX 11 looks amazing :


Though $100 and more importantly limited to Windows machines not good, $350 for the source code

It says it has a fallback to Unity built in Shuriken particles with no DX11, though who knows what benefits and drawbacks this brings.

Watch this video and be amazed ( presuming you are in the year range 2013-2016 otherwise you will be moderately amused at how basic it is )



--

Just got it working with Unity built in Particle System ( Shuriken ) which has sped it up immensely as expected so now handles many thousands more particles with ease. And used the Unity Profiler to track down and remove a bunch of GC crippling code that was stuffing the garbage collecting with megabytes a second of 'new' operations, so easy to slip up and do that

:-)

I love you built in Profiler!



Ah balls... I just very cleverly made some 'optimized' code then realized due to a limitation out of my hands ( Shuriken ) it intact makes it slower, then I make some more very clever code for combining touch movements and running them at a custom regular interval instead of instantly / irregularly and often too often.... and then I realize the premise of the whole optimization was based on an incorrect assumption and have in fact worked really hard at undoing someone else's very clever optimisations because I assumed it was being stupid. FAIL

In any case despite most of my efforts being counter productive, the fluid sim is running even faster now

:-P



And after finding several ways of making it slower, I now know ( hopefully ) how to make it even fasterer! except now I'm shattered and being attacked by wasps ( they enter the window after a certain time en masse to harass and make me goto bed )


Show comments for 'Fluidity'
You have reached the end of this page - But there's more! Click Older for more
Subscribe to my News Feed.. or screw you then!
If life gives you Lemons find the guy who's life gave them Vodka
Copyright © 2006 - 2020 - Alex Lovett
Site and content designed, built and massaged by
Alex Lovett
( HD6 / HeliosDoubleSix )
contact me by email:
Page Rendered in: 0.066 seconds, like a boss