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 planes; // read-write FluidGridData grid; }; ParameterBlock 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 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) { // For all quantities (velocity components AND scalars: pressure, // divergence, density) the boundary cell takes the (factor-scaled) // interior neighbor value. For scalars the factor is 1 (Neumann / copy), // which is the correct boundary condition for the pressure Poisson solve // (zero normal gradient) and for the divergence. Do NOT zero the scalar // boundary: zeroing the pressure shell breaks the Poisson Neumann // condition, corrupts the projection, and injects energy into the // velocity field (observed as |vel|_sum runaway). grid[uint3(pos)] = valueSum / float(numAffecting); } }