Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Physics [RELEASED] FluXY, eulerian GPU fluid simulator

Discussion in 'Tools In Progress' started by arkano22, Aug 4, 2021.

  1. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,891
    Hi there,

    I've been working on and off on a multipurpose 2.5D fluid simulator. It allows you to simulate fire, ink, oil, fog, mud, and magic-like VFX stuff. It's cheap enough to be used liberally in a project.



    Simulation is performed in a 2D plane, that can optionally face the camera. Objects in the scene are projected to the plane and can affect fluid density/velocity, 3D forces/accelerations are converted to a 2D representation. This makes the fluid look 3D-ish, though it's only 2D.

    It all works on regular vertex/fragment shaders, shader model 3.0. No compute buffer support required. Works in Unity 2019.3 and up. For now it only supports the built-in render pipeline, though URP support will be there by launch.

    Features:
    • Advection, buoyancy, viscosity, turbulence and dissipation.
    • Rectangular fluid containers with periodic, solid or open boundary conditions at the edges.
    • 3D external forces/accelerations are converted to 2D space.
    • Objects can be projected onto the simulation container and affect fluid velocity and/or density.
    • Inertial forces.
    • Multicolored fluids (eg you can mix red, green and pink fluid in a single simulation).
    • Parallel solving of up to 16 different containers. This reduces the amount of CPU work to a minimum, as single Blit() can update all 16 containers at once.
    • LOD. Distant containers use lower-resolution buffers. A single memory pool can be shared by multiple containers, and their sizes adjusted depending on their relative level of detail to ensure constant memory usage.
    • Different resolution for velocity and density buffers.
    • Pressure projection uses a fast separable poisson filter (derived for 30 jacobi iterations). See:
    • One way coupling with rigidbodies: the fluid can move rigidbodies around.

    Thoughts, feedback? what would you use this for?

    kind regards,
     
    Last edited: Aug 4, 2021
    JoNax97, one_one, mgear and 4 others like this.
  2. funnymanwin

    funnymanwin

    Joined:
    Mar 20, 2020
    Posts:
    12
    A wonderful tool! Very similar to Fluid Ninga FX from Unreal Engine Marketplace. Ideal for simulating volumetric smoke or creating magic effects, when, for example, two wizards throw different colored fire into each other, the fire is mixed and extinguished. There are a lot of ideas, but there are not enough volumetric simulations. If you will add volumetric smoke, then this asset will become super useful and wonderful!
     
    Last edited: Dec 4, 2021
  3. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,891
    Thanks! It's indeed basically the same as Fluid Ninja, as far as physics simulation goes. I think FN was also inspired by the same talk (see the above post).

    Latest FluXY version allows for tessellated meshes and integration with ShaderGraph. It comes with a couple URP sample shaders for liquids (ponds, lakes, etc), which makes certain use cases simpler and easier:



    Rendering-wise it's a lot less spectacular than anything in Unreal, mainly due to Unity's fragmented rendering system and limited support for volumetrics. Volumetrics are only supported in HDRP and only allow for very low-resolution 3D textures.

    For this reason I'm working on a custom volumetric renderer for both URP and HDRP, so volumetric rendering is definitely coming in the future (probably still not as nicely integrated with lightning as Unreal's though).


    cheers,
     
    Last edited: Dec 6, 2021
    florianBrn and adamgolden like this.
  4. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,549
    This looks amazing - SM3, not compute, ..so it works for WebGL 2.0 ?
     
  5. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,891
    No compute, only vanilla vertex/fragment under SM3. So yes, should work for WebGL 2.0. Untested though! Let me know if there's any issues, I will look into it.
     
    adamgolden likes this.
  6. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,891
    Been working on some really cool stuff lately: arbitrary mesh support for containers, tangent space simulation, adhesion, surface tension, gravity maps and normal map support. This will be available in the next update, at no extra performance - or economic- cost :).

     
  7. Parabol83

    Parabol83

    Joined:
    Nov 19, 2013
    Posts:
    6
    Hi, I found a small bug in the FluxyTarget script. When you calculate the aspect ratio, you need to cast one of the values to float, otherwise it always returns 1.
     
    arkano22 likes this.
  8. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,891
    You're absolutely correct, thanks for reporting it! the fix will be included in the next release.

    For anyone reading this, it's line 164-ish on the FluxyTarget.cs script. Should be:

    Code (CSharp):
    1. float aspectRatio = densityTexture != null ? densityTexture.width / (float)densityTexture.height : 1;
     
    Parabol83 likes this.
  9. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,891
    Here's some progress on volumetric rendering for URP:


    Will take a while to polish it enough to be released, but it is in a rather advanced state :)
     
    LogaNRV, VincentAbert and florianBrn like this.
  10. VincentAbert

    VincentAbert

    Joined:
    May 2, 2020
    Posts:
    123
    It's really shaping up and looking exciting, I look forward to trying Fluxy out more in-depth... Do you think features such as the volumetrics will be brought to all pipeline ? (I'm specifically wondering about HDRP)

    Thanks for the great work !
     
  11. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,891
    HDRP already has volumetrics, although they use "froxels" and the resolution isn't great.

    URP volumetrics are implemented in Fluxy using ShaderGraph, so simply setting the graph target to HDRP should work. Only missing part is getting the main directional light from HDRP, which should also be doable.
     
    VincentAbert likes this.
  12. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,891
    Well, volumetrics are pretty much finished. Only polishing UI stuff/parameter sets now. Hoping to upload it to the store early next week :).

    Containers now support 3 different shapes: flat, custom mesh, and volume. In volume mode, the mesh generated is a rectangular prism with inwards-pointing normals, set up for ray marching shaders. This allows the camera to get inside volumes.

    The 2D density buffer (only X and Y axis) is ray marched in 3D by offsetting and extruding it in the Z axis. This gives the impression of a fully 3D volumetric fluid sim.

    There's a single ShaderGraph volumetric shader included, with several variants: unlit, point light, and directional light, all 3 with optional support for 3D noise. Unlit/no-noise being the cheapest, and point/noise the most expensive one. You can target them to both URP and HDRP. Here's how the volumetric material inspector looks like:



    Video time! (excuse the crappy frame rate, without video capture going on it runs much smoother).
     
  13. adventurefan

    adventurefan

    Joined:
    Jan 17, 2014
    Posts:
    230
    Volumetrics are pretty neat it 1.3! Appreciate the update. Wish it had an HDRP version setup out of the box for them though, but other than it's impressive how much has been added to this asset already.
     
  14. Spikebor

    Spikebor

    Joined:
    May 30, 2019
    Posts:
    280
    For next updates please setup samples for HDRP, I got that we can copy the URP graph, adjust a bit and make HDRP version, but for ignorant users they will give you low star because they see the volumetric samples are just pink.
     
  15. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,891
    Hi Spikebor,

    You're not the only one that gave me this feedback, so HDRP sample scenes are on the making :). There will be an update soon.
     
    Spikebor likes this.
  16. Spikebor

    Spikebor

    Joined:
    May 30, 2019
    Posts:
    280
    Thank you! I love this tool so much bro.
     
  17. toyhunter

    toyhunter

    Joined:
    Mar 5, 2018
    Posts:
    76
    Can't wait to put this into my game! When will it be available?
     
  18. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,891
    Do you mean volumetrics support? It's been available for a while now :)
     
  19. blueivy

    blueivy

    Joined:
    Mar 4, 2013
    Posts:
    630
    I'm not too sure how fluxy works, but do you think it would be possible to use your asset to create smoke from a destruction effect that follows a rigid body? Something sort of like this video? If it's already possible excuse my ignorant I'm a newb
     
  20. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,891
    Hi!

    Yes it's possible, it's even shown in the promotional video :).
     
    blueivy likes this.
  21. blueivy

    blueivy

    Joined:
    Mar 4, 2013
    Posts:
    630
    Welp silly me, you're right! That's good to hear!
     
  22. Bananzabro

    Bananzabro

    Joined:
    Oct 19, 2020
    Posts:
    21
    Hello there!
    First of all: amazing work! Thank you for this amazing asset!
    To my question though: i've been trying to achieve the dye effect, similar to game Jelly Dye.
    Watching their gameplay videos i came to a conclusion, that they are using Navier-Stocks
    differential equations to simulate fluids behaviour. So i've been struggling with writing a complex shader, based on that logic, until i saw your amazing asset.
    Can you please tell me:
    1. Is there any way to make colours more saturate?
    2. Is there a way to completely stop fluids from moving?
    3. Is there any way to bake the result in a texture? (save it as a byte array or something).
    4. How to set the face that fluid will be cast on at first?
    So basically all three questions are my current pain points in achieving Jelly Dye effect))

    Thanks in advance!
     
    Last edited: Apr 28, 2022
  23. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,891
    Hello!
    FluXY just deals with density for the r,g and b color channels. Whatever colors you use will be used as-is. If you want to oversaturate colors, you will need to use some kind of shader or post processing.

    Yes, cranking viscosity all the way up.

    FluXY works using render textures, both for densities and velocities. You can use them directly in your own materials/shaders. If you want to bring the texture data to the CPU (for storage as a file, for instance), you will have to do this yourself (by using ReadPixels, for instance).

    Whatever your mesh's texture coordinates are will determine this. At the end of the day, FluXY just dynamically updates a texture.

    kind regards,
     
  24. Bananzabro

    Bananzabro

    Joined:
    Oct 19, 2020
    Posts:
    21
    Thank you very much!
     
  25. blueivy

    blueivy

    Joined:
    Mar 4, 2013
    Posts:
    630
    Is volumetric rendering an URP/HDRP only feature or does it work with built in as well?
     
  26. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,891
    Hi!

    Volumetric rendering works in URP/HDRP only.
     
    blueivy likes this.
  27. florianBrn

    florianBrn

    Joined:
    Jul 31, 2019
    Posts:
    53
    Hey!

    First off, excellent work on the asset, the results are very impressive!
    I was wondering if there's any way to make the splats fade in when they appear to avoid that weird popping you sometimes get, like on the fire sample:


    I also noticed that the FluXYNoise/3DNoise menu freezes Unity forever (using 2021.3.0/HDRP) for some reason.
     
  28. gamerpc36

    gamerpc36

    Joined:
    Jul 10, 2017
    Posts:
    5
    Anyone getting a black screen when using HDRP with FLUXY? I am using the volumetric sample scenes and i have set the active target to HDRP in the volumetric shader graph.
     
  29. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,891
    Hi there!

    Depends on how you're rendering the density buffer, there's at least a couple ways to do this:

    A) If you're rendering raw density values (interpreting whatever is in the density buffer as color), you can reduce the target's density weight. This will make each target splat less density each frame, which means density will accumulate slowly and the splats won't be as "instant". However this can reduce the overall density of the fluid if density is advected away from the splat position very fast (for instance if buoyancy or gravity are high).

    B) if you're somehow mapping density to a color curve/gradient, simply change the gradient's alpha channel.

    That menu creates a 3d Worley-Perlin noise texture, which can be used to add noise to volumetrics among other uses. It will take quite a while to finish though (around 1 minute).
     
  30. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,891
    Make sure the surface options of your material are the same as they were in URP. Sometimes when switching to another target, surface options get reset (making transparent materials opaque, etc).
     
  31. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,891
    Largely depends on how extensible the HDRP water system is (haven't payed much attention to it so no idea).

    FluXY works with render targets and the simulation output is just a couple textures, so chances are it should be possible to integrate it with existing water systems.
     
  32. Arkdirfe

    Arkdirfe

    Joined:
    Feb 2, 2021
    Posts:
    1
    Hello. I was wondering if there is support for the 2D rendering pipeline (mostly for rendering directly to sprites, and sorting layer support), and more importantly for 2D colliders for purposes of Targets and Target Detectors. I've read through the documentation and I haven't seen it mentioned explicitly anywhere.
     
  33. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,891
    FluXY's output is just a couple textures: density and velocity. So you can use any pipeline and shader/material combination that you wish. By default it funnels these textures to a simple MeshRenderer using a quad mesh, but you can use them anywhere.

    Regarding Targets, they can be added to any object. They do not need to be a collider.

    If you wish to use target detectors, the included ones only work with 3D colliders. However the target system is designed to be extendable, you can inherit from FluxyTargetProvider and implement its GetTargets() method any way you want. This method's implementation for the included target detector script looks like this:

    Code (CSharp):
    1.  
    2. public override List<FluxyTarget> GetTargets()
    3. {
    4.       targets.Clear();
    5.       int targetCount = Physics.OverlapBoxNonAlloc(transform.position, size * 0.5f, colliders, Quaternion.identity, layers);
    6.  
    7.       for (int i = 0; i < targetCount; ++i)
    8.       {
    9.           if (colliders[i].TryGetComponent(out FluxyTarget target))
    10.               targets.Add(target);
    11.       }
    12.  
    13.       return targets;
    14. }
    As you can see all it does is call Physics.OverlapBox and then populate a list with the targets it finds. Doing the same for 2D colliders is trivial (just call Physics2D.OverlapBox instead).

    let me know if you need help. cheers!
     
  34. As1th

    As1th

    Joined:
    Oct 31, 2021
    Posts:
    1
    Is there a way to adjust fluid settings on this so that colours appear to blend together? For example, instead of these two red and blue sources being simply phobic to each other, they 'mix' to show up as purple where the 'collision' is strongest:
    upload_2022-7-27_17-49-10.png
     
  35. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,891
    Hi!

    Not out of the box. You could implement a sort of "smudge" step in the solver loop to do this yourself. To do this, subscribe to the solver's OnStep event, then take the frame buffer's "stateA" render texture (which is the main density buffer) and smudge/blur/apply your own processing to it.

    Also keep in mind that color blending/mixing in RGB does not behave like it does for real paint. So in RGB, mix(yellow, blue) != green, but a gray-ish color. Accurate paint mixing models are considerably complex and costly to implement.
     
    As1th likes this.
  36. cftcimert

    cftcimert

    Joined:
    Apr 4, 2018
    Posts:
    14
    How can I prevent the color of the fluid from gradually disappearing?
     
    Enjoy_Game likes this.
  37. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,891
    Hi!

    Set the container's dissipation to zero in all channels, and make sure to use Fixed precision for your density buffers (you can configure this in the FluxyStorage asset you're using). See the included "Fog" sample scene for reference.
     
    Enjoy_Game and cftcimert like this.
  38. cftcimert

    cftcimert

    Joined:
    Apr 4, 2018
    Posts:
    14
    This worked. Thanks for your reply
     
  39. cftcimert

    cftcimert

    Joined:
    Apr 4, 2018
    Posts:
    14
    Hi,

    I want to get the texture of the painted container from the render texture in Solver. But how do I find out which coordinates to get? I guess it depends on the size of the container.
     
  40. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,891

    Depends on how/where you want to read the texture. UV coordinates are always normalized (from 0 to 1) so the size of the texture doesn't really matter. Could you give more details about your use case? What are you doing this for?
     
  41. cftcimert

    cftcimert

    Joined:
    Apr 4, 2018
    Posts:
    14
    I take the color average of the area painted from the texture.
     
  42. cftcimert

    cftcimert

    Joined:
    Apr 4, 2018
    Posts:
    14
    In short, how can I find the texture coordinates of the red area.
     

    Attached Files:

  43. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,891
    You include FluidUtils.hlsl in your shader file and use the TileToUV() function. Given uv coordinates in the 0,1 range and a tile index, it will return the uv coordinates of the tile (the red area). See:
    http://fluxy.virtualmethodstudio.com/manual/1.3/rendering.html
     
  44. cftcimert

    cftcimert

    Joined:
    Apr 4, 2018
    Posts:
    14
    I guess I didn't fully explain. I'm trying to access the coordinates of the container field on framebuffer.satateA.
     
  45. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,891
    Yes, that's exactly what TileToUV() does.

    I assume you're doing this inside a shader, right? (since doing this in the CPU would involve reading the texture back from GPU memory which is *a lot* slower).

    In a shader, you get the framebuffer.stateA texture passed as _MainTex. That texture is divided into tiles, one for each container in the solver. So to get the uv coordinates for a specific tile/container, you pass the uv coordinates of your mesh (which are assumed to be in the 0-1 range) to TileToUV(), and it will return the uv coordinates of that container in the texture.

    See the BasicFluid.shader included with FluXY as reference. Let me know if you need further help,
     
  46. wechat_os_Qy0_8mRLeajpfTMHSb4xHngIg

    wechat_os_Qy0_8mRLeajpfTMHSb4xHngIg

    Joined:
    Apr 7, 2022
    Posts:
    3
    im learning to use fluxy, amazing addons but when i imported it into a urp project all the material became pink.After converting all built-in materials into URP, the tranceparent part of the volume fog became black! upload_2022-9-8_16-58-11.png what should i do to fix it?:(:(
     
  47. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,891
    Hi! This is to be expected, since most included sample scenes use built-in pipeline materials. Only the scenes whose name starts with "URP" are designed for URP. If you wish to run non-URP scenes in URP, you will need to convert the materials used in them to URP.

    If you remove the fog, do objects still appear black?

    Also make sure your render pipeline asset has depth texture enabled as stated in the manual: http://fluxy.virtualmethodstudio.com/manual/1.3/volumetrics.html
    otherwise the fog will be unable to properly intersect with the scene and will appear on top of other objects.

    As a side note, for further support requests please write to our support email.
     
  48. wechat_os_Qy0_8mRLeajpfTMHSb4xHngIg

    wechat_os_Qy0_8mRLeajpfTMHSb4xHngIg

    Joined:
    Apr 7, 2022
    Posts:
    3
    this is the sample sence and i didnt change anything except for the material converting. and the URP material also cant be rendered properly at first.
     
  49. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,891
    URP settings are not stored in the scene, but in your URP pipeline asset. This is not included with FluXY, you create your own URP pipeline with its own settings. Depending on which settings your pipeline is using, the exact same scene may be rendered differently.

    Did you enable the "Depth texture" checkbox as stated in the manual? This is disabled by default when you create a URP pipeline asset, you need to manually enable it: https://docs.unity3d.com/Packages/c....universal@12.1/manual/universalrp-asset.html

    What do you mean by "at first"? Are URP materials not being properly rendered?
     
  50. wechat_os_Qy0_8mRLeajpfTMHSb4xHngIg

    wechat_os_Qy0_8mRLeajpfTMHSb4xHngIg

    Joined:
    Apr 7, 2022
    Posts:
    3
    i opened it right now but nothing changed.and it seems that all the volume samples have this problem.

    yes i created a URP project then imported the addon, when i open the sample sence i find that all the materials(included urp materials) are pink.

    but i test the addon on my laptop it can run properly……maybe i should restart my desktop:oops::oops::oops: thank you for your patient answer.