7#include <glm/gtx/norm.hpp>
9#include <unordered_map>
19float triangleDistance(
const glm::vec3&
p,
const glm::vec3&
a,
const glm::vec3&
b,
const glm::vec3&
c) {
20 const glm::vec3
ab =
b -
a;
21 const glm::vec3
ac =
c -
a;
22 const glm::vec3
ap =
p -
a;
23 const float d1 = glm::dot(
ab,
ap);
24 const float d2 = glm::dot(
ac,
ap);
25 if (
d1 <= 0.f &&
d2 <= 0.f)
return glm::length(
p -
a);
27 const glm::vec3
bp =
p -
b;
28 const float d3 = glm::dot(
ab,
bp);
29 const float d4 = glm::dot(
ac,
bp);
30 if (
d3 >= 0.f &&
d4 <=
d3)
return glm::length(
p -
b);
35 return glm::length(
p - (
a +
ab *
t));
38 const glm::vec3
cp =
p -
c;
39 const float d5 = glm::dot(
ab,
cp);
40 const float d6 = glm::dot(
ac,
cp);
41 if (
d6 >= 0.f &&
d5 <=
d6)
return glm::length(
p -
c);
46 return glm::length(
p - (
a +
ac *
t));
52 return glm::length(
p - (
b + (
c -
b) *
t2));
58 return glm::length(
p - (
a +
ab *
v +
ac *
w));
62bool pointInsideMesh(
const glm::vec3&
p,
const std::vector<glm::vec3>&
pos,
const std::vector<uint32_t>&
idx,
67 const glm::vec3
direction = glm::normalize(glm::vec3(1.f, .37139067f, .127831f));
69 for (
int tri = 0;
tri < triCount; ++
tri) {
70 const glm::vec3
a =
pos[
idx[uint32_t(
tri) * 3 + 0]];
71 const glm::vec3
b =
pos[
idx[uint32_t(
tri) * 3 + 1]];
72 const glm::vec3
c =
pos[
idx[uint32_t(
tri) * 3 + 2]];
74 const glm::vec3
e1 =
b -
a;
75 const glm::vec3
e2 =
c -
a;
77 const float det = glm::dot(
e1,
h);
78 if (std::fabs(
det) < 1e-12f)
continue;
80 const glm::vec3
s =
p -
a;
83 const glm::vec3
q = glm::cross(
s,
e1);
87 if (
t > 1e-8f) ++hits;
89 return (hits & 1) != 0;
99 return c.x >= 0 &&
c.y >= 0 &&
c.z >= 0 &&
c.x <
dims.x &&
c.y <
dims.y &&
c.z <
dims.z;
106 return {
FLT_MAX, glm::vec3(0.f, 1.f, 0.f)};
111 const glm::ivec3
i0 = glm::min(glm::ivec3(glm::floor(
f)),
dims - glm::ivec3(2));
112 const glm::ivec3
i1 =
i0 + glm::ivec3(1);
113 const glm::vec3
t = glm::clamp(
f - glm::vec3(
i0), glm::vec3(0.f), glm::vec3(1.f));
122 const auto mix = [](
float a,
float b,
float u) {
return a + (
b -
a) *
u; };
130 const float outsideDistance = glm::length(
outside);
145 for (
int z = 0;
z <
dims.z; ++
z) {
146 for (
int y = 0;
y <
dims.y; ++
y) {
147 for (
int x = 0;
x <
dims.x; ++
x) {
148 const glm::vec3
p = sdf.
origin + glm::vec3(
float(
x),
float(
y),
float(
z)) * sdf.
cellSize;
162 for (
int z = 0;
z <
dims.z; ++
z) {
163 for (
int y = 0;
y <
dims.y; ++
y) {
164 for (
int x = 0;
x <
dims.x; ++
x) {
165 const glm::vec3
p = sdf.
origin + glm::vec3(
float(
x),
float(
y),
float(
z)) * sdf.
cellSize;
174 const glm::ivec3& dims) {
182 const glm::vec3 extent =
maxP -
minP;
183 const glm::vec3
pad = extent * 0.25f + glm::vec3(1e-3f);
184 const glm::vec3 total = extent +
pad * 2.f;
191 sdf.distances.assign(
size_t(sdf.voxelCount()),
FLT_MAX);
194 for (
int t = 0;
t < triCount; ++
t) {
198 const glm::vec3
tmin = glm::min(
a, glm::min(
b,
c));
199 const glm::vec3
tmax = glm::max(
a, glm::max(
b,
c));
200 const glm::ivec3
c0 = glm::clamp(glm::ivec3(glm::floor((
tmin - sdf.origin) /
cell)) - glm::ivec3(1),
201 glm::ivec3(0),
dims - glm::ivec3(1));
202 const glm::ivec3
c1 = glm::clamp(glm::ivec3(glm::floor((
tmax - sdf.origin) /
cell)) + glm::ivec3(1),
203 glm::ivec3(0),
dims - glm::ivec3(1));
204 for (
int z =
c0.z;
z <=
c1.z; ++
z) {
205 for (
int y =
c0.y;
y <=
c1.y; ++
y) {
206 for (
int x =
c0.x;
x <=
c1.x; ++
x) {
207 const glm::vec3
p = sdf.origin + glm::vec3(
float(
x),
float(
y),
float(
z)) *
cell;
217 for (
int z = 0;
z <
dims.z; ++
z) {
218 for (
int y = 0;
y <
dims.y; ++
y) {
219 for (
int x = 0;
x <
dims.x; ++
x) {
220 const glm::vec3
p = sdf.origin + glm::vec3(
float(
x),
float(
y),
float(
z)) *
cell;
221 const size_t i =
size_t(sdf.index(
x,
y,
z));
222 if (sdf.distances[i] >=
FLT_MAX) {
228 for (
int tri = 0;
tri < triCount; ++
tri) {
237 sdf.distances[i] = -sdf.distances[i];
252 grid.cellSize = std::max(
cellSize, 1e-4f);
253 const glm::vec3 extent = glm::vec3(sdf.
dims) * sdf.
cellSize;
254 const glm::vec3
cells = glm::ceil((extent + glm::vec3(2.f * grid.cellSize)) / grid.cellSize);
255 grid.origin = sdf.
origin - glm::vec3(grid.cellSize);
256 grid.dims = glm::max(glm::ivec3(1), glm::ivec3(
cells));
270 return glm::clamp(
c, glm::ivec3(0),
dims - glm::ivec3(1));
274 : params_(
params), maxParticles_(
std::
max(1, maxParticles)) {
275 particles_.resize(
size_t(maxParticles_));
276 densities_.resize(
size_t(maxParticles_));
277 lambdas_.resize(
size_t(maxParticles_));
278 gradSums_.resize(
size_t(maxParticles_));
279 cellNext_.resize(
size_t(maxParticles_));
285 cellHead_.assign(
size_t(grid_.
cellCount()), -1);
290 std::fill(cellHead_.begin(), cellHead_.end(), -1);
295 for (
int k = 0; k <
count && count_ < maxParticles_; ++k) {
297 const float z = (
float((0x9E3779B9u * uint32_t(k + 1)) % 10000u) / 10000.f) * 2.f - 1.f;
298 const float a = 2.399963229728653f *
float(k);
299 const float rr =
radius * std::sqrt(std::max(0.f, 1.f -
z *
z));
305 const float nl = glm::length(
n);
309 n = glm::vec3(0.f, 1.f, 0.f);
314 densities_[
size_t(count_)] = 0.f;
324 if (count_ <= 0 || sdf_.
voxelCount() <= 0)
return;
325 const int iters = std::max(1, substeps);
327 for (
int i = 0; i <
iters; ++i) {
331 for (
int k = 0; k <
pbf; ++k) {
333 computeDensitiesAndGrads();
335 applyPositionCorrections();
340void FluidSimulation::rebuildGrid() {
341 if (cellHead_.empty())
return;
342 std::fill(cellHead_.begin(), cellHead_.end(), -1);
343 for (
int i = 0; i < count_; ++i) {
344 const glm::ivec3
c = grid_.
cellOf(particles_[
size_t(i)].
pos);
351void FluidSimulation::computeDensitiesAndGrads() {
353 const float h2 =
h *
h;
354 for (
int i = 0; i < count_; ++i) {
355 const glm::vec3
pi = particles_[
size_t(i)].pos;
359 for (
int z = -1;
z <= 1; ++
z) {
360 for (
int y = -1;
y <= 1; ++
y) {
361 for (
int x = -1;
x <= 1; ++
x) {
362 const int cx =
c.x +
x;
363 const int cy =
c.y +
y;
364 const int cz =
c.z +
z;
367 for (
int j = cellHead_[
size_t(
cell)];
j >= 0;
j = cellNext_[
size_t(
j)]) {
368 const glm::vec3
dx =
pi - particles_[
size_t(
j)].pos;
369 const float r2 = glm::dot(
dx,
dx);
383void FluidSimulation::computeLambdas() {
385 for (
int i = 0; i < count_; ++i) {
387 const float C = std::max(0.f,
rho /
rho0 - 1.f);
388 const float gradSq = glm::dot(gradSums_[
size_t(i)], gradSums_[
size_t(i)]);
393void FluidSimulation::applyPositionCorrections() {
395 const float h2 =
h *
h;
398 for (
int i = 0; i < count_; ++i) {
399 const glm::vec3
pi = particles_[
size_t(i)].pos;
400 const float li = lambdas_[
size_t(i)];
402 glm::vec3 delta(0.f);
403 for (
int z = -1;
z <= 1; ++
z) {
404 for (
int y = -1;
y <= 1; ++
y) {
405 for (
int x = -1;
x <= 1; ++
x) {
406 const int cx =
c.x +
x;
407 const int cy =
c.y +
y;
408 const int cz =
c.z +
z;
411 for (
int j = cellHead_[
size_t(
cell)];
j >= 0;
j = cellNext_[
size_t(
j)]) {
412 if (
j == i)
continue;
413 const glm::vec3
dx =
pi - particles_[
size_t(
j)].pos;
414 const float r2 = glm::dot(
dx,
dx);
420 glm::vec3
p =
pi + delta /
rho0;
431 const float nl = glm::length(
n);
435 n = glm::vec3(0.f, 1.f, 0.f);
442void FluidSimulation::integrate(
float dt) {
444 const float h2 =
h *
h;
446 for (
int i = 0; i < count_; ++i) {
447 glm::vec3
p = particles_[
size_t(i)].pos;
448 glm::vec3
v = particles_[
size_t(i)].vel;
456 float cohesionWeight = 0.f;
458 const glm::ivec3
c = grid_.
cellOf(
p);
459 for (
int z = -1;
z <= 1; ++
z) {
460 for (
int y = -1;
y <= 1; ++
y) {
461 for (
int x = -1;
x <= 1; ++
x) {
462 const int cx =
c.x +
x;
463 const int cy =
c.y +
y;
464 const int cz =
c.z +
z;
467 for (
int j = cellHead_[
size_t(
cell)];
j >= 0;
j = cellNext_[
size_t(
j)]) {
468 if (
j == i)
continue;
469 const glm::vec3
dx =
p - particles_[
size_t(
j)].pos;
470 const float r2 = glm::dot(
dx,
dx);
499 if (cohesionWeight > 1e-6f)
504 v *= std::max(0.f, 1.f - params_.
damping *
dt);
513 const float nl = glm::length(
n);
517 n = glm::vec3(0.f, 1.f, 0.f);
520 const float vn = glm::dot(
v,
n);
542 bool operator==(
const EdgeKey&
rhs)
const {
return a ==
rhs.a &&
b ==
rhs.b; }
546 size_t operator()(
const EdgeKey&
edge)
const {
547 const size_t first = std::hash<uint32_t>{}(
edge.a);
548 const size_t second = std::hash<uint32_t>{}(
edge.b);
558EdgeKey
edgeKey(uint32_t
a, uint32_t
b) {
return {std::min(
a,
b), std::max(
a,
b)}; }
561 const glm::vec3
ab =
b -
a;
562 const glm::vec3
ac =
c -
a;
563 const glm::vec3
ap =
p -
a;
564 const float d1 = glm::dot(
ab,
ap);
565 const float d2 = glm::dot(
ac,
ap);
566 if (
d1 <= 0.f &&
d2 <= 0.f)
return {1.f, 0.f, 0.f};
568 const glm::vec3
bp =
p -
b;
569 const float d3 = glm::dot(
ab,
bp);
570 const float d4 = glm::dot(
ac,
bp);
571 if (
d3 >= 0.f &&
d4 <=
d3)
return {0.f, 1.f, 0.f};
575 const float v =
d1 / (
d1 -
d3);
576 return {1.f -
v,
v, 0.f};
579 const glm::vec3
cp =
p -
c;
580 const float d5 = glm::dot(
ab,
cp);
581 const float d6 = glm::dot(
ac,
cp);
582 if (
d6 >= 0.f &&
d5 <=
d6)
return {0.f, 0.f, 1.f};
586 const float w =
d2 / (
d2 -
d6);
587 return {1.f -
w, 0.f,
w};
593 return {0.f, 1.f -
w,
w};
599 return {1.f -
v -
w,
v,
w};
605 const std::vector<glm::vec2>& uvs) {
606 restPositions_.clear();
607 currentPositions_.clear();
608 previousPositions_.clear();
612 const auto fail = [&]() {
613 restPositions_.clear();
614 currentPositions_.clear();
615 previousPositions_.clear();
622 if (!uvs.empty() && uvs.size() !=
positions.size())
return false;
631 adjacency_.assign(
indices.size() / 3u, glm::ivec3(-1));
633 std::unordered_map<EdgeKey, std::vector<EdgeOwner>, EdgeHash>
edges;
635 const uint32_t base = uint32_t(
tri) * 3u;
638 if (glm::length2(glm::cross(
ab,
ac)) < 1e-16f)
return fail();
639 for (
int opposite = 0; opposite < 3; ++opposite) {
640 const uint32_t
a = indices_[base + uint32_t((opposite + 1) % 3)];
641 const uint32_t
b = indices_[base + uint32_t((opposite + 2) % 3)];
644 owners.push_back({
tri, opposite});
645 if (owners.size() > 2u)
return fail();
648 for (
const auto& [
key, owners] :
edges) {
650 if (owners.size() != 2u)
continue;
651 adjacency_[
size_t(owners[0].
triangle)][owners[0].oppositeVertex] = owners[1].triangle;
652 adjacency_[
size_t(owners[1].
triangle)][owners[1].oppositeVertex] = owners[0].triangle;
659 previousPositions_ = currentPositions_;
660 for (
size_t i = 0; i < restPositions_.size(); ++i)
661 currentPositions_[i] = glm::vec3(transform * glm::vec4(restPositions_[i], 1.f));
666 previousPositions_ = currentPositions_;
674 return !currentPositions_.empty() && currentPositions_.size() == previousPositions_.size() &&
675 indices_.size() >= 3u && indices_.size() % 3u == 0
u;
681 std::vector<glm::uvec3>
result;
683 for (
size_t i = 0; i + 2u < indices_.size(); i += 3u)
684 result.emplace_back(indices_[i], indices_[i + 1u], indices_[i + 2u]);
688glm::vec3 FluidSurfaceBinding::triangleNormal(uint32_t
triangle)
const {
689 const uint32_t base =
triangle * 3u;
690 const glm::vec3&
a = currentPositions_[indices_[base]];
691 const glm::vec3&
b = currentPositions_[indices_[base + 1u]];
692 const glm::vec3&
c = currentPositions_[indices_[base + 2u]];
693 const glm::vec3
n = glm::cross(
b -
a,
c -
a);
694 const float n2 = glm::length2(
n);
695 return n2 > 1e-16f ?
n / std::sqrt(
n2) :
glm::
vec3(0.f, 1.f, 0.f);
698glm::vec3 FluidSurfaceBinding::barycentric(uint32_t
triangle,
const glm::vec3&
point)
const {
699 const uint32_t base =
triangle * 3u;
700 const glm::vec3&
a = currentPositions_[indices_[base]];
701 const glm::vec3&
b = currentPositions_[indices_[base + 1u]];
702 const glm::vec3&
c = currentPositions_[indices_[base + 2u]];
703 const glm::vec3 v0 =
b -
a;
704 const glm::vec3 v1 =
c -
a;
705 const glm::vec3 v2 =
point -
a;
706 const float d00 = glm::dot(v0, v0);
707 const float d01 = glm::dot(v0, v1);
708 const float d11 = glm::dot(v1, v1);
709 const float d20 = glm::dot(v2, v0);
710 const float d21 = glm::dot(v2, v1);
712 if (std::fabs(
denom) < 1e-16f)
return {1.f, 0.f, 0.f};
715 return {1.f -
v -
w,
v,
w};
721 sample.location = location;
723 const uint32_t base = location.
triangle * 3u;
724 for (
int i = 0; i < 3; ++i) {
725 const uint32_t vertex = indices_[base + uint32_t(i)];
726 sample.position += currentPositions_[vertex] *
bary[i];
727 sample.previousPosition += previousPositions_[vertex] *
bary[i];
728 if (!uvs_.empty()) sample.uv += uvs_[vertex] *
bary[i];
730 sample.normal = triangleNormal(location.
triangle);
731 const glm::vec3
edge = currentPositions_[indices_[base + 1u]] - currentPositions_[indices_[base]];
732 const glm::vec3 tangent =
edge - sample.normal * glm::dot(
edge, sample.normal);
733 const float t2 = glm::length2(tangent);
734 sample.tangent =
t2 > 1e-16f ? tangent / std::sqrt(
t2) : glm::vec3(1.f, 0.f, 0.f);
735 sample.bitangent = glm::normalize(glm::cross(sample.normal, sample.tangent));
736 if (
dt > 1e-8f) sample.velocity = (sample.position - sample.previousPosition) /
dt;
743 float best = std::numeric_limits<float>::max();
746 const uint32_t base = uint32_t(
tri) * 3u;
747 const glm::vec3
bary =
749 currentPositions_[indices_[base + 1u]], currentPositions_[indices_[base + 2u]]);
750 const glm::vec3
point = currentPositions_[indices_[base]] *
bary.x +
751 currentPositions_[indices_[base + 1u]] *
bary.y +
752 currentPositions_[indices_[base + 2u]] *
bary.z;
753 const float distance2 = glm::distance2(worldPosition,
point);
754 if (distance2 <
best) {
756 location = {uint32_t(
tri),
bary};
759 if (maxDistance >= 0.f &&
best > maxDistance * maxDistance)
return false;
771 constexpr float epsilon = 1e-5f;
775 remaining -= sample.normal * glm::dot(remaining, sample.normal);
776 if (glm::length2(remaining) < epsilon * epsilon) {
777 result.remainingDisplacement = glm::vec3(0.f);
780 const glm::vec3
targetBary = barycentric(
result.location.triangle, sample.position + remaining);
783 for (
int i = 0; i < 3; ++i) {
791 result.location.barycentric /=
792 result.location.barycentric.x +
result.location.barycentric.y +
result.location.barycentric.z;
793 result.remainingDisplacement = glm::vec3(0.f);
799 const float t = std::clamp(
from / (
from -
to), 0.f, 1.f);
804 remaining *= 1.f -
t;
809 result.remainingDisplacement = remaining;
810 result.reachedBoundary =
true;
816 result.location.barycentric = barycentric(uint32_t(next),
edgeSample.position);
817 result.location.barycentric = glm::max(
result.location.barycentric, glm::vec3(0.f));
818 result.location.barycentric /=
819 result.location.barycentric.x +
result.location.barycentric.y +
result.location.barycentric.z;
821 result.remainingDisplacement = remaining;
839 const glm::vec4& destination) {
851 const float saturated = std::min(
source.a, 1.f - destination.a);
852 return glm::vec4(saturated, saturated, saturated, 1.f);
856 return glm::vec4(0.f);
864 "fluids.surface.blend"));
872 if (!auxiliaryCurrent_ || residentColorCurrent_)
874 "Current fluid frame has no host-visible surface color",
875 "fluids.surface.blend"));
876 if (sceneColor.size() != expected)
878 "Surface blend requires a matching RGBA8 scene image",
879 "fluids.surface.blend"));
882 glm::vec4
source, destination;
883 for (
size_t channel = 0; channel < 4u; ++channel) {
884 const auto component =
static_cast<glm::vec4::length_type
>(channel);
886 destination[component] =
float(sceneColor[
offset + channel]) / 255.f;
889 glm::clamp(
source * blendFluidColor(surfaceBlendSource_,
source, destination) +
890 destination * blendFluidColor(surfaceBlendDestination_,
source, destination),
891 glm::vec4(0.f), glm::vec4(1.f));
892 for (
size_t channel = 0; channel < 4u; ++channel)
893 color_[
offset + channel] =
uint8_t(
blended[
static_cast<glm::vec4::length_type
>(channel)] * 255.f + .5f);
895 residentColorCurrent_ =
false;
904 "fluids.surface.particleBlend"));
907 particleDepthWrite_ = depthWrite;
908 particleBlendConfigured_ =
true;
std::array< double, 10 > q
std::vector< std::uint32_t > indices
std::vector< float > positions
std::array< PixelCell, kPixelChunkSize *kPixelChunkSize > cells
RoadLaneDirection direction
std::map< Cell, int > best
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
static Result success(T value)
Construct a successful result owning value.
static Result failure(Status status)
Construct a failed result from a structured status.
void clear()
Remove all particles.
FluidSimulation(int maxParticles, const FluidParams ¶ms=FluidParams{})
Fluid simulation.
void setSdf(const MeshSdf &sdf)
Replace the collision surface. Particles keep their state.
int spawnDrop(const glm::vec3 ¢er, float radius, int count)
Spawn a roughly spherical drop of particles near the surface.
const MeshSdf & sdf() const
Sdf.
void step(float dt)
Advance the simulation by dt seconds (params.iterations substeps).
bool build(const std::vector< glm::vec3 > &positions, const std::vector< uint32_t > &indices, const std::vector< glm::vec2 > &uvs={})
Build topology and initialize the current and previous poses.
void setTransform(const glm::mat4 &transform)
Apply a new rigid pose and preserve the old pose for velocity queries.
SurfaceSample evaluate(const SurfaceLocation &location, float dt) const
Evaluate a surface address in the current and previous poses.
bool isValid() const
True when valid.
bool setDeformedPositions(const std::vector< glm::vec3 > &worldPositions)
Supply a new deformed world-space pose and preserve the previous pose.
int adjacentTriangle(uint32_t triangle, int oppositeVertex) const
Adjacent triangle.
int triangleCount() const
Triangle count.
SurfaceWalkResult walkAcrossSurface(const SurfaceLocation &start, const glm::vec3 &worldDisplacement, int maxCrossings=16) const
Move a location by a world-space displacement, crossing triangle edges.
std::vector< glm::uvec3 > triangles() const
Triangles.
void commitPose()
Make the current pose the previous pose, yielding zero surface velocity.
bool project(const glm::vec3 &worldPosition, float maxDistance, SurfaceLocation &outLocation) const
Find the closest material point on the current surface.
Result< void > configureSurfaceBlend(int source, int destination)
Atomically configures Fluid3D's final surface source and destination blend factors.
Result< void > compositeConfiguredSurfaceBlend(std::span< const uint8_t > sceneColor)
Composites the current surface over a matching RGBA8 scene using the configured Fluid3D factors.
Result< void > configureParticleBlend(int source, int destination, bool depthWrite)
Configures Fluid3D's particle-color target blending and depth writes atomically.
Uniform signed-distance voxel field over a box.
glm::ivec3 dims
Voxel resolution along each axis.
float cellSize
World-space size of one voxel.
glm::vec3 gradient(const glm::vec3 &p) const
Analytic trilinear gradient (outward normal) at p.
glm::vec3 origin
World-space position of voxel (0,0,0).
std::vector< float > distances
Signed distances, dims.x * dims.y * dims.z floats.
static MeshSdf makeSphere(const glm::vec3 ¢er, float radius, const glm::ivec3 &dims)
Bake an analytic sphere into the field.
MeshSdfSample sampleWithGradient(const glm::vec3 &p) const
Samples distance and analytic trilinear gradient from the same eight voxels.
int voxelCount() const
Voxel count.
float sample(const glm::vec3 &p) const
Trilinearly interpolated signed distance at p.
static MeshSdf makePlane(float planeY, const glm::ivec3 &dims, float halfExtent)
Bake a horizontal plane y = planeY into the field.
static MeshSdf makeFromTriangles(const std::vector< glm::vec3 > &positions, const std::vector< uint32_t > &indices, const glm::ivec3 &dims)
Voxelize a closed triangle mesh.
bool inBounds(const glm::ivec3 &c) const
In bounds.
int index(int x, int y, int z) const
Index.
Value vector(glm::dvec3 v)
GLSL compute kernels for the GPU surface-flow solver.
float fluidPoly6(float r2, float h)
Poly6 kernel value W(r2, h).
FluidBlendFactor
Unity/Fluid3D blend factors used by fluid render settings.
@ OneMinusDestinationColor
@ OneMinusDestinationAlpha
glm::vec3 fluidClampSpeed(const glm::vec3 &v, float maxSpeed)
Clamp a vector's magnitude to maxSpeed.
glm::vec3 fluidSpikyGrad(const glm::vec3 &dx, float h)
Spiky gradient of the kernel w.r.t. particle i position.
Tuning knobs of one fluid simulation.
float damping
Linear air damping applied each substep.
float restDensity
Rest density; normalized to 1.
float supportRadius
SPH support radius h (kernel cutoff), typically 4x particleRadius.
float particleRadius
Resting particle radius in world units.
float adhesion
Fluid-surface adhesion strength (contact angle / sticking).
int pbfIterations
PBF density-constraint relaxation passes per substep.
float cohesion
Fluid-fluid cohesion strength (droplet formation).
int iterations
Solver substeps per call to step(dt).
float viscosity
XSPH viscosity strength (0 = inviscid).
float maxVelocity
Velocity clamp after integration.
float yieldStress
Bingham yield stress: below this shear rate particles "freeze".
glm::vec3 gravity
Gravity vector in world units / s^2.
One simulated particle (CPU reference layout).
One trilinear SDF sample and its local-space analytic gradient.
Uniform grid used for neighbor queries.
bool inBounds(int x, int y, int z) const
In bounds.
static SimGrid make(const MeshSdf &sdf, float cellSize)
Build a grid covering the SDF domain plus one cell of padding.
float cellSize
Cell size in world units (== SPH support radius).
int cellCount() const
Cell count.
glm::vec3 origin
World position of cell (0,0,0).
glm::ivec3 cellOf(const glm::vec3 &p) const
Cell of.
int cellIndex(int x, int y, int z) const
Cell index.
glm::ivec3 dims
Cell counts per axis.
Stable material-space address of a point on a triangle surface.
Evaluated world-space frame and motion at a surface location.
Result of walking a material point across triangle adjacency.