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

## 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:

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.

FluidSimulation.unitypackage

##### 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"
{
{
Pass
{
ZTest Always

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

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
{
return float4(result, 1.0);
}
ENDCG
}
}
}

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.

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

## Create an account

Register a new account

• 12
• 11
• 17
• 11
• 13
• ### Similar Content

• By owenjr
Hi there.
I'm pretty new to this and I don't know if it has been asked before, but here I go.
I'm developing a game using SFML and C++.
I would like to use the "Tiled" tool to load maps into my game but I don't actually find any tutorial or guide on how to exaclty use it (I know that I have to read an XML file and stuff). I just step into diverse projects that make all a mess.
Anyone knows where can I find good information to make my map loader by myself?

• Today again I painted the next drawing. I look at him and I understand that something is wrong in him. Can problems with the prospect or with black and white or with the composition or is everything terrible? help me figure it out, help.. My english is not very well, i'm from russia and i want to draw for game

• Hello guys,
I've released my game for the first time. I'm very excited about it and I hope you'll enjoy the game - Beer Ranger. It's a retro-like puzzle-platfromer which makes you think a lot or die trying. You have a squad of skilled dwarfs with special powers and your goal is tasty beer. There is a lot of traps as well as many solutions how to endure them - it is up to your choice how to complete the level!
Link to the project: Project site
Link to the Steam site with video: Beer Ranger
Have fun and please write feedback if you feel up to.
Some screens:

• Hello fellow devs!
Once again I started working on an 2D adventure game and right now I'm doing the character-movement/animation. I'm not a big math guy and I was happy about my solution, but soon I realized that it's flawed.
My player has 5 walking-animations, mirrored for the left side: up, upright, right, downright, down. With the atan2 function I get the angle between player and destination. To get an index from 0 to 4, I divide PI by 5 and see how many times it goes into the player-destination angle.

In Pseudo-Code:
angle = atan2(destination.x - player.x, destination.y - player.y) //swapped y and x to get mirrored angle around the y axis
index = (int) (angle / (PI / 5));
PlayAnimation(index); //0 = up, 1 = up_right, 2 = right, 3 = down_right, 4 = down

Besides the fact that when angle is equal to PI it produces an index of 5, this works like a charm. Or at least I thought so at first. When I tested it, I realized that the up and down animation is playing more often than the others, which is pretty logical, since they have double the angle.

What I'm trying to achieve is something like this, but with equal angles, so that up and down has the same range as all other directions.

I can't get my head around it. Any suggestions? Is the whole approach doomed?

Thank you in advance for any input!