载入中...
搜索中...
未找到
FluidSdf.cpp
浏览该文件的文档.
1#include "fluids/FluidSdf.h"
2#include <algorithm>
3#include <array>
4#include <cfloat>
5#include <cmath>
6#include <cstdint>
7#include <glm/gtx/norm.hpp>
8#include <limits>
9#include <unordered_map>
13
14// FluidSdf.cpp
15namespace eve::fluids {
16namespace {
17
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);
26
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);
31
32 const float vc = d1 * d4 - d3 * d2;
33 if (vc <= 0.f && d1 >= 0.f && d3 <= 0.f) {
34 const float t = d1 / (d1 - d3);
35 return glm::length(p - (a + ab * t));
36 }
37
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);
42
43 const float vb = d5 * d2 - d1 * d6;
44 if (vb <= 0.f && d2 >= 0.f && d6 <= 0.f) {
45 const float t = d2 / (d2 - d6);
46 return glm::length(p - (a + ac * t));
47 }
48
49 const float va = d3 * d6 - d5 * d4;
50 if (va <= 0.f && d4 - d3 >= 0.f && d5 - d6 >= 0.f) {
51 const float t2 = (d4 - d3) / ((d4 - d3) + (d5 - d6));
52 return glm::length(p - (b + (c - b) * t2));
53 }
54
55 const float denom = 1.f / (va + vb + vc);
56 const float v = vb * denom;
57 const float w = vc * denom;
58 return glm::length(p - (a + ab * v + ac * w));
59}
60
62bool pointInsideMesh(const glm::vec3& p, const std::vector<glm::vec3>& pos, const std::vector<uint32_t>& idx,
63 int triCount) {
64 // Axis rays frequently pass through shared vertices/edges in symmetric meshes,
65 // double-counting one crossing. A fixed irrational-looking direction preserves
66 // deterministic baking while avoiding those systematic degeneracies.
67 const glm::vec3 direction = glm::normalize(glm::vec3(1.f, .37139067f, .127831f));
68 int hits = 0;
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]];
73 // Moller-Trumbore ray from p along the fixed skew direction.
74 const glm::vec3 e1 = b - a;
75 const glm::vec3 e2 = c - a;
76 const glm::vec3 h = glm::cross(direction, e2);
77 const float det = glm::dot(e1, h);
78 if (std::fabs(det) < 1e-12f) continue;
79 const float invDet = 1.f / det;
80 const glm::vec3 s = p - a;
81 const float u = invDet * glm::dot(s, h);
82 if (u < 0.f || u > 1.f) continue;
83 const glm::vec3 q = glm::cross(s, e1);
84 const float v = invDet * glm::dot(direction, q);
85 if (v < 0.f || u + v > 1.f) continue;
86 const float t = invDet * glm::dot(e2, q);
87 if (t > 1e-8f) ++hits;
88 }
89 return (hits & 1) != 0;
90}
91
92} // namespace
93
94int MeshSdf::voxelCount() const { return dims.x * dims.y * dims.z; }
95
96int MeshSdf::index(int x, int y, int z) const { return x + dims.x * (y + dims.y * z); }
97
98bool MeshSdf::inBounds(const glm::ivec3& c) const {
99 return c.x >= 0 && c.y >= 0 && c.z >= 0 && c.x < dims.x && c.y < dims.y && c.z < dims.z;
100}
101
102float MeshSdf::sample(const glm::vec3& p) const { return sampleWithGradient(p).distance; }
103
105 if (dims.x < 2 || dims.y < 2 || dims.z < 2 || !(cellSize > 0.f) || distances.size() != size_t(voxelCount()))
106 return {FLT_MAX, glm::vec3(0.f, 1.f, 0.f)};
107 const glm::vec3 maximum = origin + glm::vec3(dims - glm::ivec3(1)) * cellSize;
108 const glm::vec3 clampedPoint = glm::clamp(p, origin, maximum);
109 const glm::vec3 outside = p - clampedPoint;
110 const glm::vec3 f = (clampedPoint - origin) / cellSize;
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));
114 const float v000 = distances[size_t(index(i0.x, i0.y, i0.z))];
115 const float v100 = distances[size_t(index(i1.x, i0.y, i0.z))];
116 const float v010 = distances[size_t(index(i0.x, i1.y, i0.z))];
117 const float v110 = distances[size_t(index(i1.x, i1.y, i0.z))];
118 const float v001 = distances[size_t(index(i0.x, i0.y, i1.z))];
119 const float v101 = distances[size_t(index(i1.x, i0.y, i1.z))];
120 const float v011 = distances[size_t(index(i0.x, i1.y, i1.z))];
121 const float v111 = distances[size_t(index(i1.x, i1.y, i1.z))];
122 const auto mix = [](float a, float b, float u) { return a + (b - a) * u; };
123 const float x00 = mix(v000, v100, t.x), x10 = mix(v010, v110, t.x);
124 const float x01 = mix(v001, v101, t.x), x11 = mix(v011, v111, t.x);
125 const float y0 = mix(x00, x10, t.y), y1 = mix(x01, x11, t.y);
126 const float distance = mix(y0, y1, t.z);
127 const float dx = mix(mix(v100 - v000, v110 - v010, t.y), mix(v101 - v001, v111 - v011, t.y), t.z) / cellSize;
128 const float dy = mix(mix(v010 - v000, v110 - v100, t.x), mix(v011 - v001, v111 - v101, t.x), t.z) / cellSize;
129 const float dz = mix(mix(v001 - v000, v101 - v100, t.x), mix(v011 - v010, v111 - v110, t.x), t.y) / cellSize;
130 const float outsideDistance = glm::length(outside);
131 return outsideDistance > 1e-7f ? MeshSdfSample{distance + outsideDistance, outside / outsideDistance}
132 : MeshSdfSample{distance, {dx, dy, dz}};
133}
134
135glm::vec3 MeshSdf::gradient(const glm::vec3& p) const { return sampleWithGradient(p).gradient; }
136
137MeshSdf MeshSdf::makeSphere(const glm::vec3& center, float radius, const glm::ivec3& dims) {
138 MeshSdf sdf;
139 sdf.dims = dims;
140 const float margin = radius * 0.5f;
141 const float extent = 2.f * (radius + margin);
142 sdf.cellSize = extent / float(dims.x);
143 sdf.origin = center - glm::vec3(radius + margin);
144 sdf.distances.resize(size_t(sdf.voxelCount()));
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;
149 sdf.distances[size_t(sdf.index(x, y, z))] = glm::length(p - center) - radius;
150 }
151 }
152 }
153 return sdf;
154}
155
156MeshSdf MeshSdf::makePlane(float planeY, const glm::ivec3& dims, float halfExtent) {
157 MeshSdf sdf;
158 sdf.dims = dims;
159 sdf.cellSize = (2.f * halfExtent) / float(dims.x);
160 sdf.origin = glm::vec3(-halfExtent, planeY - halfExtent, -halfExtent);
161 sdf.distances.resize(size_t(sdf.voxelCount()));
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;
166 sdf.distances[size_t(sdf.index(x, y, z))] = p.y - planeY;
167 }
168 }
169 }
170 return sdf;
171}
172
173MeshSdf MeshSdf::makeFromTriangles(const std::vector<glm::vec3>& positions, const std::vector<uint32_t>& indices,
174 const glm::ivec3& dims) {
175 const int triCount = int(indices.size()) / 3;
176 glm::vec3 minP(FLT_MAX);
177 glm::vec3 maxP(-FLT_MAX);
178 for (const glm::vec3& v : positions) {
179 minP = glm::min(minP, v);
180 maxP = glm::max(maxP, v);
181 }
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;
185 const float cell = std::max({total.x / float(dims.x), total.y / float(dims.y), total.z / float(dims.z)});
186
187 MeshSdf sdf;
188 sdf.dims = dims;
189 sdf.cellSize = cell;
190 sdf.origin = minP - pad;
191 sdf.distances.assign(size_t(sdf.voxelCount()), FLT_MAX);
192
193 // Sweep each triangle over its expanded AABB and keep the min distance.
194 for (int t = 0; t < triCount; ++t) {
195 const glm::vec3 a = positions[indices[uint32_t(t) * 3 + 0]];
196 const glm::vec3 b = positions[indices[uint32_t(t) * 3 + 1]];
197 const glm::vec3 c = positions[indices[uint32_t(t) * 3 + 2]];
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;
208 const float d = triangleDistance(p, a, b, c);
209 float& slot = sdf.distances[size_t(sdf.index(x, y, z))];
210 slot = std::min(slot, d);
211 }
212 }
213 }
214 }
215
216 // Sign from even-odd raycast per voxel center.
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) {
223 // The narrow triangle sweep leaves deep interior and far exterior
224 // cells untouched. Resolve those cells exactly once during baking;
225 // substituting one cell width destroys the SDF magnitude and makes
226 // collision projection depend on grid resolution.
227 float distance = FLT_MAX;
228 for (int tri = 0; tri < triCount; ++tri) {
229 const glm::vec3 a = positions[indices[uint32_t(tri) * 3 + 0]];
230 const glm::vec3 b = positions[indices[uint32_t(tri) * 3 + 1]];
231 const glm::vec3 c = positions[indices[uint32_t(tri) * 3 + 2]];
232 distance = std::min(distance, triangleDistance(p, a, b, c));
233 }
234 sdf.distances[i] = distance;
235 }
236 if (pointInsideMesh(p, positions, indices, triCount)) {
237 sdf.distances[i] = -sdf.distances[i];
238 }
239 }
240 }
241 }
242 return sdf;
243}
244
245} // namespace eve::fluids
246
247// FluidSimulation.cpp
248namespace eve::fluids {
249
250SimGrid SimGrid::make(const MeshSdf& sdf, float cellSize) {
251 SimGrid grid;
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));
257 return grid;
258}
259
260int SimGrid::cellCount() const { return dims.x * dims.y * dims.z; }
261
262int SimGrid::cellIndex(int x, int y, int z) const { return x + dims.x * (y + dims.y * z); }
263
264bool SimGrid::inBounds(int x, int y, int z) const {
265 return x >= 0 && y >= 0 && z >= 0 && x < dims.x && y < dims.y && z < dims.z;
266}
267
268glm::ivec3 SimGrid::cellOf(const glm::vec3& p) const {
269 glm::ivec3 c = glm::ivec3(glm::floor((p - origin) / cellSize));
270 return glm::clamp(c, glm::ivec3(0), dims - glm::ivec3(1));
271}
272
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_));
280}
281
283 sdf_ = sdf;
284 grid_ = SimGrid::make(sdf_, params_.supportRadius);
285 cellHead_.assign(size_t(grid_.cellCount()), -1);
286}
287
289 count_ = 0;
290 std::fill(cellHead_.begin(), cellHead_.end(), -1);
291}
292
293int FluidSimulation::spawnDrop(const glm::vec3& center, float radius, int count) {
294 int added = 0;
295 for (int k = 0; k < count && count_ < maxParticles_; ++k) {
296 // Deterministic golden-ratio sphere fill so tests are stable.
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));
300 glm::vec3 p = center + glm::vec3(std::cos(a) * rr, radius * z, std::sin(a) * rr);
301 if (sdf_.voxelCount() > 0) {
302 const float d = sdf_.sample(p);
303 if (d < params_.particleRadius) {
304 glm::vec3 n = sdf_.gradient(p);
305 const float nl = glm::length(n);
306 if (nl > 1e-6f)
307 n /= nl;
308 else
309 n = glm::vec3(0.f, 1.f, 0.f);
310 p = p - n * (d - params_.particleRadius);
311 }
312 }
313 particles_[size_t(count_)] = FluidParticle{p, glm::vec3(0.f)};
314 densities_[size_t(count_)] = 0.f;
315 ++count_;
316 ++added;
317 }
318 return added;
319}
320
321void FluidSimulation::step(float dt) { step(dt, std::max(1, params_.iterations)); }
322
323void FluidSimulation::step(float dt, int substeps) {
324 if (count_ <= 0 || sdf_.voxelCount() <= 0) return;
325 const int iters = std::max(1, substeps);
326 const float sub = dt / float(iters);
327 for (int i = 0; i < iters; ++i) {
328 rebuildGrid();
329 integrate(sub);
330 const int pbf = std::max(0, params_.pbfIterations);
331 for (int k = 0; k < pbf; ++k) {
332 rebuildGrid();
333 computeDensitiesAndGrads();
334 computeLambdas();
335 applyPositionCorrections();
336 }
337 }
338}
339
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);
345 const int cell = grid_.cellIndex(c.x, c.y, c.z);
346 cellNext_[size_t(i)] = cellHead_[size_t(cell)];
347 cellHead_[size_t(cell)] = i;
348 }
349}
350
351void FluidSimulation::computeDensitiesAndGrads() {
352 const float h = params_.supportRadius;
353 const float h2 = h * h;
354 for (int i = 0; i < count_; ++i) {
355 const glm::vec3 pi = particles_[size_t(i)].pos;
356 const glm::ivec3 c = grid_.cellOf(pi);
357 float dens = 0.f;
358 glm::vec3 gradSum(0.f);
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;
365 if (!grid_.inBounds(cx, cy, cz)) continue;
366 const int cell = grid_.cellIndex(cx, cy, cz);
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);
370 if (r2 < h2) {
371 dens += fluidPoly6(r2, h);
373 }
374 }
375 }
376 }
377 }
378 densities_[size_t(i)] = dens;
379 gradSums_[size_t(i)] = gradSum;
380 }
381}
382
383void FluidSimulation::computeLambdas() {
384 const float rho0 = std::max(params_.restDensity, 1e-6f);
385 for (int i = 0; i < count_; ++i) {
386 const float rho = densities_[size_t(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)]);
389 lambdas_[size_t(i)] = -C / (gradSq + 1e-6f);
390 }
391}
392
393void FluidSimulation::applyPositionCorrections() {
394 const float h = params_.supportRadius;
395 const float h2 = h * h;
396 const float rho0 = std::max(params_.restDensity, 1e-6f);
397 const float radius = params_.particleRadius;
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)];
401 const glm::ivec3 c = grid_.cellOf(pi);
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;
409 if (!grid_.inBounds(cx, cy, cz)) continue;
410 const int cell = grid_.cellIndex(cx, cy, cz);
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);
415 if (r2 < h2) delta += (li + lambdas_[size_t(j)]) * fluidSpikyGrad(dx, h);
416 }
417 }
418 }
419 }
420 glm::vec3 p = pi + delta / rho0;
421 // Clamp the PBF position correction (standard PBF stabilization):
422 // overlapping particles would otherwise amplify the spiky gradient
423 // unboundedly and fling the drop apart.
424 const float maxDelta = 0.0005f * h;
425 const glm::vec3 correction = delta / rho0;
426 const float cl = glm::length(correction);
427 if (cl > maxDelta && cl > 1e-9f) p = pi + correction * (maxDelta / cl);
428 const float d = sdf_.sample(p);
429 {
430 glm::vec3 n = sdf_.gradient(p);
431 const float nl = glm::length(n);
432 if (nl > 1e-6f)
433 n /= nl;
434 else
435 n = glm::vec3(0.f, 1.f, 0.f);
436 p = p - n * (d - radius);
437 }
438 particles_[size_t(i)].pos = p;
439 }
440}
441
442void FluidSimulation::integrate(float dt) {
443 const float h = params_.supportRadius;
444 const float h2 = h * h;
445 const float radius = params_.particleRadius;
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;
449
450 // XSPH viscosity + Bingham yield stress + Akinci-style cohesion.
451 if (params_.viscosity > 0.f || params_.cohesion > 0.f || params_.yieldStress > 0.f) {
452 glm::vec3 viscAcc(0.f);
453 glm::vec3 cohesionAcc(0.f);
454 float shearAcc = 0.f;
455 float neighborWeight = 0.f;
456 float cohesionWeight = 0.f;
457 const float particleVolume = std::pow(2.f * radius, 3.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;
465 if (!grid_.inBounds(cx, cy, cz)) continue;
466 const int cell = grid_.cellIndex(cx, cy, cz);
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);
471 if (r2 < h2) {
472 const float weight = fluidPoly6(r2, h) * particleVolume;
473 if (params_.viscosity > 0.f || params_.yieldStress > 0.f) {
474 viscAcc += (particles_[size_t(j)].vel - v) * weight;
476 }
477 if (params_.yieldStress > 0.f)
478 shearAcc += glm::length(particles_[size_t(j)].vel - v) * weight;
479 if (params_.cohesion > 0.f) {
480 cohesionAcc -= dx * weight;
481 cohesionWeight += weight;
482 }
483 }
484 }
485 }
486 }
487 }
488 // Bingham plastic: effective viscosity grows as shear rate drops,
489 // so low-shear mud "freezes" and piles up instead of spreading.
490 float effectiveViscosity = params_.viscosity;
491 if (params_.yieldStress > 0.f && shearAcc > 1e-6f) effectiveViscosity += params_.yieldStress / shearAcc;
492 if (neighborWeight > 1e-6f) v += (viscAcc / neighborWeight) * std::clamp(effectiveViscosity * dt, 0.f, 1.f);
493 glm::vec3 cohesionNormal = sdf_.gradient(p);
494 if (glm::length(cohesionNormal) > 1e-6f)
495 cohesionNormal = glm::normalize(cohesionNormal);
496 else
497 cohesionNormal = glm::vec3(0.f, 1.f, 0.f);
499 if (cohesionWeight > 1e-6f)
500 v += fluidClampSpeed((cohesionAcc / cohesionWeight) * (params_.cohesion * 100.f * dt), radius);
501 }
502
503 v += params_.gravity * dt;
504 v *= std::max(0.f, 1.f - params_.damping * dt);
505 if (params_.adhesion > 0.f) v *= std::exp(-params_.adhesion * 300.f * dt);
506 v = fluidClampSpeed(v, params_.maxVelocity);
507 p += v * dt;
508
509 // Project onto the solid surface and kill inward normal velocity so
510 // gravity only drives tangential flow (water film stays on the model).
511 float d = sdf_.sample(p);
512 glm::vec3 n = sdf_.gradient(p);
513 const float nl = glm::length(n);
514 if (nl > 1e-6f)
515 n /= nl;
516 else
517 n = glm::vec3(0.f, 1.f, 0.f);
518 {
519 p = p - n * (d - radius);
520 const float vn = glm::dot(v, n);
521 v -= n * vn;
522 d = radius;
523 }
524 // Adhesion: pull the film toward the solid while within range.
525 if (params_.adhesion > 0.f && d < h) v += -n * (params_.adhesion * std::max(0.f, 1.f - d / h) * dt * 30.f);
526
527 particles_[size_t(i)].pos = p;
528 particles_[size_t(i)].vel = v;
529 }
530}
531
532} // namespace eve::fluids
533
534// FluidSurfaceBinding.cpp
535namespace eve::fluids {
536namespace {
537
538struct EdgeKey {
539 uint32_t a = 0;
540 uint32_t b = 0;
541
542 bool operator==(const EdgeKey& rhs) const { return a == rhs.a && b == rhs.b; }
543};
544
545struct EdgeHash {
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);
549 return first ^ (second + size_t(0x9e3779b9u) + (first << 6u) + (first >> 2u));
550 }
551};
552
553struct EdgeOwner {
554 int triangle = -1;
556};
557
558EdgeKey edgeKey(uint32_t a, uint32_t b) { return {std::min(a, b), std::max(a, b)}; }
559
560glm::vec3 closestPointBarycentric(const glm::vec3& p, const glm::vec3& a, const glm::vec3& b, const glm::vec3& c) {
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};
567
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};
572
573 const float vc = d1 * d4 - d3 * d2;
574 if (vc <= 0.f && d1 >= 0.f && d3 <= 0.f) {
575 const float v = d1 / (d1 - d3);
576 return {1.f - v, v, 0.f};
577 }
578
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};
583
584 const float vb = d5 * d2 - d1 * d6;
585 if (vb <= 0.f && d2 >= 0.f && d6 <= 0.f) {
586 const float w = d2 / (d2 - d6);
587 return {1.f - w, 0.f, w};
588 }
589
590 const float va = d3 * d6 - d5 * d4;
591 if (va <= 0.f && d4 - d3 >= 0.f && d5 - d6 >= 0.f) {
592 const float w = (d4 - d3) / ((d4 - d3) + (d5 - d6));
593 return {0.f, 1.f - w, w};
594 }
595
596 const float inv = 1.f / (va + vb + vc);
597 const float v = vb * inv;
598 const float w = vc * inv;
599 return {1.f - v - w, v, w};
600}
601
602} // namespace
603
604bool FluidSurfaceBinding::build(const std::vector<glm::vec3>& positions, const std::vector<uint32_t>& indices,
605 const std::vector<glm::vec2>& uvs) {
606 restPositions_.clear();
607 currentPositions_.clear();
608 previousPositions_.clear();
609 indices_.clear();
610 uvs_.clear();
611 adjacency_.clear();
612 const auto fail = [&]() {
613 restPositions_.clear();
614 currentPositions_.clear();
615 previousPositions_.clear();
616 indices_.clear();
617 uvs_.clear();
618 adjacency_.clear();
619 return false;
620 };
621 if (positions.empty() || indices.empty() || indices.size() % 3u != 0u) return false;
622 if (!uvs.empty() && uvs.size() != positions.size()) return false;
623 for (uint32_t index : indices)
624 if (index >= positions.size()) return false;
625
626 restPositions_ = positions;
627 currentPositions_ = positions;
628 previousPositions_ = positions;
629 indices_ = indices;
630 uvs_ = uvs;
631 adjacency_.assign(indices.size() / 3u, glm::ivec3(-1));
632
633 std::unordered_map<EdgeKey, std::vector<EdgeOwner>, EdgeHash> edges;
634 for (int tri = 0; tri < triangleCount(); ++tri) {
635 const uint32_t base = uint32_t(tri) * 3u;
636 const glm::vec3 ab = positions[indices_[base + 1u]] - positions[indices_[base]];
637 const glm::vec3 ac = positions[indices_[base + 2u]] - positions[indices_[base]];
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)];
642 const EdgeKey key = edgeKey(a, b);
643 auto& owners = edges[key];
644 owners.push_back({tri, opposite});
645 if (owners.size() > 2u) return fail();
646 }
647 }
648 for (const auto& [key, owners] : edges) {
649 (void)key;
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;
653 }
654 return true;
655}
656
657void FluidSurfaceBinding::setTransform(const glm::mat4& transform) {
658 if (!isValid()) return;
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));
662}
663
665 if (!isValid() || worldPositions.size() != currentPositions_.size()) return false;
666 previousPositions_ = currentPositions_;
667 currentPositions_ = worldPositions;
668 return true;
669}
670
671void FluidSurfaceBinding::commitPose() { previousPositions_ = currentPositions_; }
672
674 return !currentPositions_.empty() && currentPositions_.size() == previousPositions_.size() &&
675 indices_.size() >= 3u && indices_.size() % 3u == 0u;
676}
677
678int FluidSurfaceBinding::triangleCount() const { return int(indices_.size() / 3u); }
679
680std::vector<glm::uvec3> FluidSurfaceBinding::triangles() const {
681 std::vector<glm::uvec3> result;
682 result.reserve(size_t(triangleCount()));
683 for (size_t i = 0; i + 2u < indices_.size(); i += 3u)
684 result.emplace_back(indices_[i], indices_[i + 1u], indices_[i + 2u]);
685 return result;
686}
687
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);
696}
697
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);
711 const float denom = d00 * d11 - d01 * d01;
712 if (std::fabs(denom) < 1e-16f) return {1.f, 0.f, 0.f};
713 const float v = (d11 * d20 - d01 * d21) / denom;
714 const float w = (d00 * d21 - d01 * d20) / denom;
715 return {1.f - v - w, v, w};
716}
717
719 SurfaceSample sample;
720 if (!isValid() || location.triangle >= uint32_t(triangleCount())) return sample;
721 sample.location = location;
722 const glm::vec3 bary = location.barycentric;
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];
729 }
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;
737 return sample;
738}
739
740bool FluidSurfaceBinding::project(const glm::vec3& worldPosition, float maxDistance,
742 if (!isValid()) return false;
743 float best = std::numeric_limits<float>::max();
744 SurfaceLocation location;
745 for (int tri = 0; tri < triangleCount(); ++tri) {
746 const uint32_t base = uint32_t(tri) * 3u;
747 const glm::vec3 bary =
748 closestPointBarycentric(worldPosition, currentPositions_[indices_[base]],
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) {
755 best = distance2;
756 location = {uint32_t(tri), bary};
757 }
758 }
759 if (maxDistance >= 0.f && best > maxDistance * maxDistance) return false;
760 outLocation = location;
761 return true;
762}
763
765 const glm::vec3& worldDisplacement, int maxCrossings) const {
767 if (!isValid() || start.triangle >= uint32_t(triangleCount()) || maxCrossings < 0) return result;
769 result.valid = true;
770 glm::vec3 remaining = worldDisplacement;
771 constexpr float epsilon = 1e-5f;
772
773 for (int crossing = 0; crossing <= maxCrossings; ++crossing) {
774 const SurfaceSample sample = evaluate(result.location, 0.f);
775 remaining -= sample.normal * glm::dot(remaining, sample.normal);
776 if (glm::length2(remaining) < epsilon * epsilon) {
777 result.remainingDisplacement = glm::vec3(0.f);
778 return result;
779 }
780 const glm::vec3 targetBary = barycentric(result.location.triangle, sample.position + remaining);
781 int outside = -1;
782 float mostNegative = -epsilon;
783 for (int i = 0; i < 3; ++i) {
784 if (targetBary[i] < mostNegative) {
786 outside = i;
787 }
788 }
789 if (outside < 0) {
790 result.location.barycentric = glm::max(targetBary, glm::vec3(0.f));
791 result.location.barycentric /=
792 result.location.barycentric.x + result.location.barycentric.y + result.location.barycentric.z;
793 result.remainingDisplacement = glm::vec3(0.f);
794 return result;
795 }
796
797 const float from = result.location.barycentric[outside];
798 const float to = targetBary[outside];
799 const float t = std::clamp(from / (from - to), 0.f, 1.f);
800 glm::vec3 edgeBary = glm::mix(result.location.barycentric, targetBary, t);
801 edgeBary[outside] = 0.f;
802 edgeBary = glm::max(edgeBary, glm::vec3(0.f));
803 edgeBary /= edgeBary.x + edgeBary.y + edgeBary.z;
804 remaining *= 1.f - t;
805
806 const int next = adjacentTriangle(result.location.triangle, outside);
807 if (next < 0) {
808 result.location.barycentric = edgeBary;
809 result.remainingDisplacement = remaining;
810 result.reachedBoundary = true;
811 return result;
812 }
813
814 const SurfaceSample edgeSample = evaluate({result.location.triangle, edgeBary}, 0.f);
815 result.location.triangle = uint32_t(next);
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;
820 if (crossing == maxCrossings) {
821 result.remainingDisplacement = remaining;
822 return result;
823 }
824 }
825 return result;
826}
827
829 if (triangle >= adjacency_.size() || oppositeVertex < 0 || oppositeVertex > 2) return -1;
830 return adjacency_[triangle][oppositeVertex];
831}
832
833} // namespace eve::fluids
834
835// FluidSurfaceBlend.cpp
836namespace eve::fluids {
837
838glm::vec4 FluidSurfaceRenderer::blendFluidColor(FluidBlendFactor factor, const glm::vec4& source,
839 const glm::vec4& destination) {
840 switch (factor) {
841 case FluidBlendFactor::Zero: return glm::vec4(0.f);
842 case FluidBlendFactor::One: return glm::vec4(1.f);
843 case FluidBlendFactor::DestinationColor: return destination;
845 case FluidBlendFactor::OneMinusDestinationColor: return glm::vec4(1.f) - destination;
846 case FluidBlendFactor::SourceAlpha: return glm::vec4(source.a);
847 case FluidBlendFactor::OneMinusSourceColor: return glm::vec4(1.f) - source;
848 case FluidBlendFactor::DestinationAlpha: return glm::vec4(destination.a);
849 case FluidBlendFactor::OneMinusDestinationAlpha: return glm::vec4(1.f - destination.a);
851 const float saturated = std::min(source.a, 1.f - destination.a);
852 return glm::vec4(saturated, saturated, saturated, 1.f);
853 }
854 case FluidBlendFactor::OneMinusSourceAlpha: return glm::vec4(1.f - source.a);
855 }
856 return glm::vec4(0.f);
857}
858
861 destination < int(FluidBlendFactor::Zero) || destination > int(FluidBlendFactor::OneMinusSourceAlpha))
863 DiagnosticCode::InvalidArgument, "Fluid surface blend factors must be Unity BlendMode values in [0,10]",
864 "fluids.surface.blend"));
865 surfaceBlendSource_ = static_cast<FluidBlendFactor>(source);
866 surfaceBlendDestination_ = static_cast<FluidBlendFactor>(destination);
867 return Result<void>::success();
868}
869
871 const size_t expected = size_t(params_.width) * size_t(params_.height) * 4u;
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"));
880
881 for (size_t offset = 0; offset < expected; offset += 4u) {
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);
885 source[component] = float(color_[offset + channel]) / 255.f;
886 destination[component] = float(sceneColor[offset + channel]) / 255.f;
887 }
888 const glm::vec4 blended =
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);
894 }
895 residentColorCurrent_ = false;
896 return Result<void>::success();
897}
898
901 destination < int(FluidBlendFactor::Zero) || destination > int(FluidBlendFactor::OneMinusSourceAlpha))
903 DiagnosticCode::InvalidArgument, "Fluid particle blend factors must be Unity BlendMode values in [0,10]",
904 "fluids.surface.particleBlend"));
905 particleBlendSource_ = static_cast<FluidBlendFactor>(source);
906 particleBlendDestination_ = static_cast<FluidBlendFactor>(destination);
907 particleDepthWrite_ = depthWrite;
908 particleBlendConfigured_ = true;
909 return Result<void>::success();
910}
911
912} // namespace eve::fluids
double z
double y
double x
Duration start
float w
Definition AnimClip.cpp:738
const std::string & s
float cx
Definition CardTypes.cpp:36
float cy
Definition CardTypes.cpp:37
glm::vec4 p[6]
SQObjectPtr key
Definition ECS.cpp:155
int oppositeVertex
Definition FluidSdf.cpp:555
int triangle
Definition FluidSdf.cpp:554
float maximum[3]
uint32_t i1
Definition Grass.cpp:61
uint32_t i0
Definition Grass.cpp:61
float u
Definition Grass.cpp:233
glm::vec3 n
Definition Grass.cpp:63
std::array< double, 10 > q
std::uint32_t ab
std::uint32_t ac
std::vector< std::uint32_t > indices
std::vector< float > positions
float v
std::int32_t second
std::int32_t c
std::int32_t first
int h
size_t offset
std::string to
std::vector< Edge > edges
std::string from
Poco::JSON::Object::Ptr result
std::vector< RecipeParam > params
MeleePoint3 b
Definition MeleeHit.cpp:41
double radius
Definition MeleeHit.cpp:42
MeleePoint3 a
Definition MeleeHit.cpp:40
float distance
glm::dvec2 center
glm::dvec3 dx
glm::dvec3 dz
double dt
int idx
float f
std::array< PixelCell, kPixelChunkSize *kPixelChunkSize > cells
float d
float t
std::string slot
RoadLaneDirection direction
std::string_view source
float dy
Definition Shadow.cpp:151
int count
Cell cell
std::map< Cell, int > best
int margin
float step
Definition TreeMesh.cpp:451
double weight
uint32_t index
glm::vec3 point
glm::vec4 bary
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.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
void clear()
Remove all particles.
Definition FluidSdf.cpp:288
FluidSimulation(int maxParticles, const FluidParams &params=FluidParams{})
Fluid simulation.
Definition FluidSdf.cpp:273
void setSdf(const MeshSdf &sdf)
Replace the collision surface. Particles keep their state.
Definition FluidSdf.cpp:282
int spawnDrop(const glm::vec3 &center, float radius, int count)
Spawn a roughly spherical drop of particles near the surface.
Definition FluidSdf.cpp:293
const MeshSdf & sdf() const
Sdf.
void step(float dt)
Advance the simulation by dt seconds (params.iterations substeps).
Definition FluidSdf.cpp:321
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.
Definition FluidSdf.cpp:604
void setTransform(const glm::mat4 &transform)
Apply a new rigid pose and preserve the old pose for velocity queries.
Definition FluidSdf.cpp:657
SurfaceSample evaluate(const SurfaceLocation &location, float dt) const
Evaluate a surface address in the current and previous poses.
Definition FluidSdf.cpp:718
bool isValid() const
True when valid.
Definition FluidSdf.cpp:673
bool setDeformedPositions(const std::vector< glm::vec3 > &worldPositions)
Supply a new deformed world-space pose and preserve the previous pose.
Definition FluidSdf.cpp:664
int adjacentTriangle(uint32_t triangle, int oppositeVertex) const
Adjacent triangle.
Definition FluidSdf.cpp:828
int triangleCount() const
Triangle count.
Definition FluidSdf.cpp:678
SurfaceWalkResult walkAcrossSurface(const SurfaceLocation &start, const glm::vec3 &worldDisplacement, int maxCrossings=16) const
Move a location by a world-space displacement, crossing triangle edges.
Definition FluidSdf.cpp:764
std::vector< glm::uvec3 > triangles() const
Triangles.
Definition FluidSdf.cpp:680
void commitPose()
Make the current pose the previous pose, yielding zero surface velocity.
Definition FluidSdf.cpp:671
bool project(const glm::vec3 &worldPosition, float maxDistance, SurfaceLocation &outLocation) const
Find the closest material point on the current surface.
Definition FluidSdf.cpp:740
Result< void > configureSurfaceBlend(int source, int destination)
Atomically configures Fluid3D's final surface source and destination blend factors.
Definition FluidSdf.cpp:859
Result< void > compositeConfiguredSurfaceBlend(std::span< const uint8_t > sceneColor)
Composites the current surface over a matching RGBA8 scene using the configured Fluid3D factors.
Definition FluidSdf.cpp:870
Result< void > configureParticleBlend(int source, int destination, bool depthWrite)
Configures Fluid3D's particle-color target blending and depth writes atomically.
Definition FluidSdf.cpp:899
Uniform signed-distance voxel field over a box.
Definition FluidSdf.h:28
glm::ivec3 dims
Voxel resolution along each axis.
Definition FluidSdf.h:35
float cellSize
World-space size of one voxel.
Definition FluidSdf.h:33
glm::vec3 gradient(const glm::vec3 &p) const
Analytic trilinear gradient (outward normal) at p.
Definition FluidSdf.cpp:135
glm::vec3 origin
World-space position of voxel (0,0,0).
Definition FluidSdf.h:31
std::vector< float > distances
Signed distances, dims.x * dims.y * dims.z floats.
Definition FluidSdf.h:37
static MeshSdf makeSphere(const glm::vec3 &center, float radius, const glm::ivec3 &dims)
Bake an analytic sphere into the field.
Definition FluidSdf.cpp:137
MeshSdfSample sampleWithGradient(const glm::vec3 &p) const
Samples distance and analytic trilinear gradient from the same eight voxels.
Definition FluidSdf.cpp:104
int voxelCount() const
Voxel count.
Definition FluidSdf.cpp:94
float sample(const glm::vec3 &p) const
Trilinearly interpolated signed distance at p.
Definition FluidSdf.cpp:102
static MeshSdf makePlane(float planeY, const glm::ivec3 &dims, float halfExtent)
Bake a horizontal plane y = planeY into the field.
Definition FluidSdf.cpp:156
static MeshSdf makeFromTriangles(const std::vector< glm::vec3 > &positions, const std::vector< uint32_t > &indices, const glm::ivec3 &dims)
Voxelize a closed triangle mesh.
Definition FluidSdf.cpp:173
bool inBounds(const glm::ivec3 &c) const
In bounds.
Definition FluidSdf.cpp:98
int index(int x, int y, int z) const
Index.
Definition FluidSdf.cpp:96
GLSL compute kernels for the GPU surface-flow solver.
Definition FluidTarget.h:12
float fluidPoly6(float r2, float h)
Poly6 kernel value W(r2, h).
Definition FluidMath.h:65
FluidBlendFactor
Unity/Fluid3D blend factors used by fluid render settings.
glm::vec3 fluidClampSpeed(const glm::vec3 &v, float maxSpeed)
Clamp a vector's magnitude to maxSpeed.
Definition FluidMath.h:113
glm::vec3 fluidSpikyGrad(const glm::vec3 &dx, float h)
Spiky gradient of the kernel w.r.t. particle i position.
Definition FluidMath.h:78
Tuning knobs of one fluid simulation.
Definition FluidMath.h:26
float damping
Linear air damping applied each substep.
Definition FluidMath.h:44
float restDensity
Rest density; normalized to 1.
Definition FluidMath.h:32
float supportRadius
SPH support radius h (kernel cutoff), typically 4x particleRadius.
Definition FluidMath.h:30
float particleRadius
Resting particle radius in world units.
Definition FluidMath.h:28
float adhesion
Fluid-surface adhesion strength (contact angle / sticking).
Definition FluidMath.h:42
int pbfIterations
PBF density-constraint relaxation passes per substep.
Definition FluidMath.h:50
float cohesion
Fluid-fluid cohesion strength (droplet formation).
Definition FluidMath.h:40
int iterations
Solver substeps per call to step(dt).
Definition FluidMath.h:48
float viscosity
XSPH viscosity strength (0 = inviscid).
Definition FluidMath.h:36
float maxVelocity
Velocity clamp after integration.
Definition FluidMath.h:46
float yieldStress
Bingham yield stress: below this shear rate particles "freeze".
Definition FluidMath.h:38
glm::vec3 gravity
Gravity vector in world units / s^2.
Definition FluidMath.h:34
One simulated particle (CPU reference layout).
Definition FluidMath.h:54
One trilinear SDF sample and its local-space analytic gradient.
Definition FluidSdf.h:22
Uniform grid used for neighbor queries.
bool inBounds(int x, int y, int z) const
In bounds.
Definition FluidSdf.cpp:264
static SimGrid make(const MeshSdf &sdf, float cellSize)
Build a grid covering the SDF domain plus one cell of padding.
Definition FluidSdf.cpp:250
float cellSize
Cell size in world units (== SPH support radius).
int cellCount() const
Cell count.
Definition FluidSdf.cpp:260
glm::vec3 origin
World position of cell (0,0,0).
glm::ivec3 cellOf(const glm::vec3 &p) const
Cell of.
Definition FluidSdf.cpp:268
int cellIndex(int x, int y, int z) const
Cell index.
Definition FluidSdf.cpp:262
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.
uint32_t pad[2]