Files
Seele/res/shaders/fluid/SetBound.slang
T

81 lines
2.3 KiB
Plaintext
Raw Normal View History

2026-04-12 20:49:02 +02:00
import FluidGridData;
2026-09-03 20:08:45 +02:00
struct BoundaryPlane
{
float3 normal; // inward normal (points from solid toward fluid)
float d; // plane offset: dot(normal, cellPos) + d < 0 means solid
};
2026-04-12 20:49:02 +02:00
struct Params
{
int b;
2026-09-03 20:08:45 +02:00
uint numPlanes;
StructuredBuffer<BoundaryPlane> planes;
2026-04-12 20:49:02 +02:00
// read-write
FluidGridData<float> grid;
};
ParameterBlock<Params> params;
2026-09-03 20:08:45 +02:00
// Find the interior (fluid) neighbor of a solid cell by stepping along the
// dominant axis of the plane inward normal.
int3 getInteriorNeighbor(int3 pos, float3 normal)
{
float3 absN = abs(normal);
int3 step;
if (absN.x >= absN.y && absN.x >= absN.z)
step = int3(normal.x > 0 ? 1 : -1, 0, 0);
else if (absN.y >= absN.z)
step = int3(0, normal.y > 0 ? 1 : -1, 0);
else
step = int3(0, 0, normal.z > 0 ? 1 : -1);
return clamp(pos + step, int3(0), int3(gridParams.gridSize) - 1);
}
// Compute the boundary value scaling factor for velocity component b.
// For b=0 (scalar): factor = 1 (Neumann / copy).
// For b=1,2,3 (velocity x,y,z): reflects the normal component so that
// velocity normal to the plane is negated while tangential parts are preserved.
float getBoundaryFactor(int b, float3 normal)
{
if (b == 0) return 1.0f;
float nComp = (b == 1) ? normal.x : ((b == 2) ? normal.y : normal.z);
return 1.0f - 2.0f * nComp * nComp;
}
2026-04-12 20:49:02 +02:00
[shader("compute")]
2026-09-03 20:08:45 +02:00
[numthreads(4, 4, 4)]
2026-04-12 20:49:02 +02:00
void setBound(uint3 dispatchThreadID : SV_DispatchThreadID)
{
2026-09-03 20:08:45 +02:00
int3 pos = int3(dispatchThreadID);
if (any(uint3(pos) >= gridParams.gridSize))
return;
2026-04-12 20:49:02 +02:00
int b = params.b;
FluidGridData<float> grid = params.grid;
2026-09-03 20:08:45 +02:00
float3 cellPos = float3(pos);
2026-04-12 20:49:02 +02:00
2026-09-03 20:08:45 +02:00
int numAffecting = 0;
float valueSum = 0.0f;
for (uint p = 0; p < params.numPlanes; p++)
2026-04-15 16:56:19 +02:00
{
2026-09-03 20:08:45 +02:00
BoundaryPlane plane = params.planes[p];
float dist = dot(plane.normal, cellPos) + plane.d;
if (dist < 0.0f) // cell is on the solid side
{
int3 neighbor = getInteriorNeighbor(pos, plane.normal);
float neighborVal = grid[uint3(neighbor)];
float factor = getBoundaryFactor(b, plane.normal);
valueSum += factor * neighborVal;
numAffecting++;
}
2026-04-15 16:56:19 +02:00
}
2026-09-03 20:08:45 +02:00
if (numAffecting > 0)
2026-04-15 16:56:19 +02:00
{
2026-09-03 20:08:45 +02:00
grid[uint3(pos)] = valueSum / float(numAffecting);
2026-04-15 16:56:19 +02:00
}
2026-04-12 20:49:02 +02:00
}