Files
Seele/res/shaders/fluid/SetBound.slang
T
2026-09-03 20:08:45 +02:00

81 lines
2.3 KiB
Plaintext

import FluidGridData;
struct BoundaryPlane
{
float3 normal; // inward normal (points from solid toward fluid)
float d; // plane offset: dot(normal, cellPos) + d < 0 means solid
};
struct Params
{
int b;
uint numPlanes;
StructuredBuffer<BoundaryPlane> planes;
// read-write
FluidGridData<float> grid;
};
ParameterBlock<Params> params;
// 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;
}
[shader("compute")]
[numthreads(4, 4, 4)]
void setBound(uint3 dispatchThreadID : SV_DispatchThreadID)
{
int3 pos = int3(dispatchThreadID);
if (any(uint3(pos) >= gridParams.gridSize))
return;
int b = params.b;
FluidGridData<float> grid = params.grid;
float3 cellPos = float3(pos);
int numAffecting = 0;
float valueSum = 0.0f;
for (uint p = 0; p < params.numPlanes; p++)
{
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++;
}
}
if (numAffecting > 0)
{
grid[uint3(pos)] = valueSum / float(numAffecting);
}
}