Skip to main content
gamedev.net
🔒 Locked

2d fluid sim shader

Started by phillycheese81 Mar 31, 2011 at 11:14 PM 0 replies 1.6k views
Original Post
phillycheese81
phillycheese81
Hi everyone,
I have been working on a 2d fluid shader sim based on the code presented online over the past 10 years. everything is "working" but something is wrong... it doesnt "swirl" like the other sims I try as samples. I have even compared my code directly to 3-4 other peoples source code and its doing the same work.
below are the important parts of my setup... hopefully you can find the problem.
I am not currently doing any border checks because in the end i dont want borders but i do have it doing checks against arbitrary collision within the volume...(that code isnt included in this version) the obstacle checks dont fix the problem. the fluid flows around the obstacle but shows no sign of turbulence or vorticity.

even with that update though its still not doing much more than either moving in a straight line or sitting still... if you can i would really appreciate your help reviewing the below.

Thanks, .... oh ya... its in CG

fluidScreenshot.jpg

my current order of execution is:
-advect color source into color destination
-copy color destination into source

-advect velocity source into destination
-copy destintation into source

-add color (if button clicked)
-add velocity (if button clicked)

-clear divergence
-calculate divergence into divergence destination
-copy destination into source

-clear preasure
-calculate preasure (20 iteration jacobi) into preasure destination
-copy destination into source

-project gradient into velocity destination
-copy destination into source

-repeat

the following are the core pieces of code i am using...
the texture sim grid is 64x64
rdx is 1/resolution
dissipation is 0.9975 (but the sim is similar even with dissipation removed)

****************
//advect - when simSpeed is "1" there isnt any motion... when simSpeed is larger there is motion.
float2 pos = i.uv - (timestep * rdx * simSpeed * ((tex2D(_velocityTexSource, i.uv) - 0.5) * 2));

return ((dissipation * ((tex2D(_targetTex, pos) - 0.5) * 2)) + 1) * 0.5;

****************
//add color/velocity
if(i.uv.x > impulseLocationX - rdx && i.uv.x < impulseLocationX + rdx && i.uv.y > impulseLocationY - rdx && i.uv.y < impulseLocationY + rdx)
{
return (((impulseStrength + ((tex2D(_targetTex, i.uv) - 0.5) * 2)) + 1) * 0.5);
}
else
{
return tex2D(_targetTex, i.uv);
}

**************
//divergence
half4 vL, vR, vB, vT;

vL = (tex2D(_velocityTex, i.uv - half2(rdx, 0)) - 0.5) * 2;
vR = (tex2D(_velocityTex, i.uv + half2(rdx, 0)) - 0.5) * 2;
vB = (tex2D(_velocityTex, i.uv - half2(0, rdx)) - 0.5) * 2;
vT = (tex2D(_velocityTex, i.uv + half2(0, rdx)) - 0.5) * 2;

return ((0.5 * ((vR.x - vL.x) + (vT.y - vB.y))) + 1) * 0.5;

**************
//jacobi preasure calc
half4 xC, xL, xR, xB, xT, bC;

xL = (tex2D(_preasureTex, i.uv - half2(rdx, 0)) - 0.5) * 2;
xR = (tex2D(_preasureTex, i.uv + half2(rdx, 0)) - 0.5) * 2;
xB = (tex2D(_preasureTex, i.uv - half2(0, rdx)) - 0.5) * 2;
xT = (tex2D(_preasureTex, i.uv + half2(0, rdx)) - 0.5) * 2;
bC = (tex2D(_divergenceTex, i.uv) - 0.5) * 2;

return (((xL + xR + xB + xT - bC) / 4) + 1) * 0.5;

*************
//preasure gradient subtract
half pL, pR, pB, pT;

pL = (tex2D(_preasureTex, i.uv - half2(rdx, 0)) - 0.5) * 2;
pR = (tex2D(_preasureTex, i.uv + half2(rdx, 0)) - 0.5) * 2;
pB = (tex2D(_preasureTex, i.uv - half2(0, rdx)) - 0.5) * 2;
pT = (tex2D(_preasureTex, i.uv + half2(0, rdx)) - 0.5) * 2;

uNew = (tex2D(_velocityTex, i.uv) - 0.5) * 2;
uNew.xy -= 0.5 * half2(pR - pL, pT - pB);

return (uNew + 1) * 0.5;
Krypt0n
Krypt0n

Hi everyone,
I have been working on a 2d fluid shader sim based on the code presented online over the past 10 years. everything is "working" but something is wrong... it doesnt "swirl" like the other sims I try as samples.




if that's the case, then probably that is going wrong:


-calculate preasure (20 iteration jacobi) into preasure destination
[/quote]
check if you're doing properly ping-pong of the resources, and not just using the same src and dst for 20 times, which would result in pressure just propagated by one field and would cause no "swirl".

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.