I am trying to replicate my working 2d fluid shadertoy in a compute shader in Unity, with the hopes of moving it to 3D soon. When I replicate the algorithm the same way, I get some very weird behavior (seen in this video I took). I have tried to debug everything I can think of, but I can't figure out why they aren't the same. I am visualizing the vector matrix in this capture (the same as pressing Space while viewing my shadertoy).
I created a pastebin with the code that I am using to perform the Navier-Stokes equations that drive the velocity matrix. The meat of the simulation boils down to:
float4 S(RWTexture2D<float4> target, uint2 id)
{
return target[(id.xy + resolution)%resolution];
}
void Fluid(RWTexture2D<float4> target, uint2 id, float2 offset, float4 values, inout float2 velocity, inout float pressure, inout float divergence, inout float neighbors)
{
float2 v = S(target, id.xy + offset);
float4 s = S(target, id.xy + offset.xy - v);
float2 o= normalize(offset);
velocity += o * (s.w - values.w);
pressure += s.w;
divergence += dot(o, s.xy);
++neighbors;
}
void StepVelocity(uint3 id, RWTexture2D<float4> write, RWTexture2D<float4> read, bool addJet)
{
//sample our current values, then sample the values from the cell our velocity is coming from
float4 values = S(read, id.xy);
values = S(read, id.xy - values.xy);
float2 velocity = float2(0,0);
float pressure = 0;
float neighbors = 0;
float divergence = 0;
//sample neighboring cells
Fluid(read, id.xy, float2(0, 1), values, velocity, pressure, divergence, neighbors);
Fluid(read, id.xy, float2(0, -1), values, velocity, pressure, divergence, neighbors);
Fluid(read, id.xy, float2(1, 0), values, velocity, pressure, divergence, neighbors);
Fluid(read, id.xy, float2(-1, 0), values, velocity, pressure, divergence, neighbors);
velocity = velocity / neighbors;
divergence = divergence / neighbors;
pressure = pressure/neighbors;
values.w = pressure-divergence;
values.xy -= velocity;
if (addJet && distance(id.xy, float2(resolution / 2.0, resolution / 2.0)) < 10)
values = float4(0, .25, 0, values.w);
write[id.xy] = values;
}
It should be pretty straightforward, i did my best to comment the shadertoy excessively to make it easy to understand (they are the same code, setup for different environments). Here is the C# code that drives drives the simulation.
I know its an inconvenient request to ask someone to dive into my code, but I am not at all sure what I am doing wrong and its driving me crazy. I have the exact same algorithm working on shadertoy but it behaves very strangely in Unity compute shaders and I can't figure out why. Every time I "step" i make sure to switch the read/write textures as to cleanly move forward in the simulation and not interfere.
Any ideas/tips towards fixing my issue would be greatly appreciated.