Jump to content
  • Advertisement
Sign in to follow this  

2D How to correctly display Navier Stokes fluid simulation in Unity?

This topic is 376 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been attempting to write a Navier Stokes fluid simulation using the Cg language in Unity for a few times now with no luck. Every attempt has only resulted in a blank result. I don't really know how to fix the problem but from what I see, I think the problem might lie in the rendering-to-texture aspect and not the shader themselves so this question might be more Unity-related the shader-related. But just in case, I've read Chapter 38 from GPU Gems and while I don't understand the maths, I do understand the implementation of it. If I'm correct, the order for each timestep of the simulation is:

1. Advection

2. Force application

3. Projection (computing divergence, solving Poisson equations using Jacobi iterations, gradient subtraction)

So there's that. Multiple shaders would be required for each step and I need to have multiple textures to represent the state of fluid such as velocity, density and pressure. Those states each need to have two textures to read and write to, so I can swap them around after each step has been outputted to the correct texture. I also need a temporary divergence texture and a texture to display the final result. My first question is, what format should my textures be in? I've heard that you should use RGBA floating-point textures for each one but I don't know if that's really the case.

Now about the implementation. Like I've said before, I think all my shaders have been written properly as I've compared them to numerous sources including GPU Gems codes and it looks similar. My shaders are based on this three.js implementation and this one. I want my simulation to be setup so that the user can click on the screen to add 'ink' and drag the mouse around to 'add force and velocity' to it. While the second implementation is a working example of a Unity implementation, it's not setup to how I want it to be and the three.js implementation does exactly that. So I combined the Splat shader from the three.js implementation to my own project to achieve the 'add force' that I want but I'm not sure if it's working properly. I've attached my shaders below along with a Unity package file for those who have Unity.

If my shaders have all been written properly, then the problem lies in how to actually display it. Currently, I'm just inputting a density texture into my Render shader to display it but it's giving me a blank result. I don't know if there's anything I had to do first before displaying the fluid so if anyone knows, please let me know.



Share this post

Link to post
Share on other sites

I forgot to say this but my shader is actually registering mouse clicks from my C# code. When I right click, the density texture changes from blank to a red color while left clicking changes the velocity texture to a yellow color. I assume this is because my density texture is set to R float so it's only displaying the red channel while my velocity texture is in RG float, showing a yellow color. My other shaders is still available in the zip folder above but I'm going to share the shader that I'm using to render my simulation.

Shader "FluidSimulation/Render"
			ZTest Always

			#include "UnityCG.cginc"
			#pragma vertex vert
			#pragma fragment frag

			uniform sampler2D _Read;

			struct v2f
				float4 pos : SV_POSITION;
				float2 uv : TEXCOORD0;

			v2f vert(appdata_base v)
				v2f o;
				o.pos = UnityObjectToClipPos(v.vertex);
				o.uv = v.texcoord.xy;
				return o;

			float4 frag(v2f i) : SV_TARGET
				float3 result = tex2D(_Read, i.uv).xyz;
				return float4(result, 1.0);

Pretty simple shader that just takes in a texture I inputted from my C# code and display it. The reason why I'm posting this is because in other implementations, the shader does some minor modification before displaying the final result.

Example 1, Example 2

Is there any modifications that I need to make to display my fluid simulation correctly? Also wondering if my Splat shader will work with my other shaders or if I should try changing to some other form of adding forces such as this one.

Edited by PlazmaInteractive

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!