载入中...
搜索中...
未找到
FogDensityField.cpp
浏览该文件的文档.
2#include <algorithm>
3#include <cmath>
4#include <glm/common.hpp>
5#include <glm/geometric.hpp>
6#include <glm/vec3.hpp>
7#include "common/Diagnostic.h"
13
14// FogDensityField.cpp
15namespace eve::graphics::fog {
16namespace {
17
18[[nodiscard]] float hash31(int x, int y, int z, std::uint32_t seed) noexcept {
19 std::uint32_t n = static_cast<std::uint32_t>(x) * 374761393u + static_cast<std::uint32_t>(y) * 668265263u +
20 static_cast<std::uint32_t>(z) * 2147483647u + seed * 1013904223u;
21 n = (n ^ (n >> 13)) * 1274126177u;
22 n ^= n >> 16;
23 return static_cast<float>(n & 0x00FFFFFFu) / static_cast<float>(0x01000000u);
24}
25
26} // namespace
27
29 if (width < 2 || height < 2 || depth < 2 || width > 256 || height > 256 || depth > 256) {
31 "density field dimensions must be in [2,256]", "size", {},
32 "graphics.fog"));
33 }
34 const glm::vec3 size = bounds.maximum - bounds.minimum;
35 if (!(size.x > 0.f && size.y > 0.f && size.z > 0.f) || !std::isfinite(size.x) || !std::isfinite(size.y) ||
36 !std::isfinite(size.z)) {
38 "world bounds must be finite with positive extent", "bounds", {},
39 "graphics.fog"));
40 }
41
42 width_ = width;
43 height_ = height;
44 depth_ = depth;
45 bounds_ = bounds;
46 cellSize_ = glm::vec3(size.x / static_cast<float>(width), size.y / static_cast<float>(height),
47 size.z / static_cast<float>(depth));
48 const std::size_t count =
49 static_cast<std::size_t>(width) * static_cast<std::size_t>(height) * static_cast<std::size_t>(depth);
50 density_.assign(count, 0.f);
51 curl_.assign(count, glm::vec3(0.f));
52 lightAssist_.assign(count, glm::vec3(0.f));
53 ++revision_;
54 return Result<void>::success();
55}
56
58 std::fill(density_.begin(), density_.end(), 0.f);
59 std::fill(curl_.begin(), curl_.end(), glm::vec3(0.f));
60 std::fill(lightAssist_.begin(), lightAssist_.end(), glm::vec3(0.f));
61 ++revision_;
62}
63
64std::size_t FogDensityField::index(int x, int y, int z) const noexcept {
65 x = std::clamp(x, 0, std::max(width_ - 1, 0));
66 y = std::clamp(y, 0, std::max(height_ - 1, 0));
67 z = std::clamp(z, 0, std::max(depth_ - 1, 0));
68 return (static_cast<std::size_t>(z) * static_cast<std::size_t>(height_) + static_cast<std::size_t>(y)) *
69 static_cast<std::size_t>(width_) +
70 static_cast<std::size_t>(x);
71}
72
73glm::vec3 FogDensityField::cellCenter(int x, int y, int z) const noexcept {
74 return bounds_.minimum + glm::vec3((static_cast<float>(x) + 0.5f) * cellSize_.x,
75 (static_cast<float>(y) + 0.5f) * cellSize_.y,
76 (static_cast<float>(z) + 0.5f) * cellSize_.z);
77}
78
79void FogDensityField::setDensity(int x, int y, int z, float density) {
80 if (density_.empty()) return;
81 density_[index(x, y, z)] = std::max(density, 0.f);
82 ++revision_;
83}
84
85float FogDensityField::densityAt(int x, int y, int z) const noexcept {
86 if (density_.empty()) return 0.f;
87 return density_[index(x, y, z)];
88}
89
90void FogDensityField::setCurlVelocity(int x, int y, int z, const glm::vec3& velocity) {
91 if (curl_.empty()) return;
92 curl_[index(x, y, z)] = velocity;
93 ++revision_;
94}
95
96glm::vec3 FogDensityField::curlVelocityAt(int x, int y, int z) const noexcept {
97 if (curl_.empty()) return {};
98 return curl_[index(x, y, z)];
99}
100
101void FogDensityField::setLightAssist(int x, int y, int z, const glm::vec3& assist) {
102 if (lightAssist_.empty()) return;
103 lightAssist_[index(x, y, z)] = assist;
104 ++revision_;
105}
106
107glm::vec3 FogDensityField::lightAssistAt(int x, int y, int z) const noexcept {
108 if (lightAssist_.empty()) return {};
109 return lightAssist_[index(x, y, z)];
110}
111
112void FogDensityField::sampleLattice(const glm::vec3& world, int& x0, int& y0, int& z0, int& x1, int& y1, int& z1,
113 float& fx, float& fy, float& fz) const noexcept {
114 const glm::vec3 local = (world - bounds_.minimum) / cellSize_ - glm::vec3(0.5f);
115 const float lx = std::clamp(local.x, 0.f, static_cast<float>(std::max(width_ - 1, 0)));
116 const float ly = std::clamp(local.y, 0.f, static_cast<float>(std::max(height_ - 1, 0)));
117 const float lz = std::clamp(local.z, 0.f, static_cast<float>(std::max(depth_ - 1, 0)));
118 x0 = static_cast<int>(std::floor(lx));
119 y0 = static_cast<int>(std::floor(ly));
120 z0 = static_cast<int>(std::floor(lz));
121 x1 = std::min(x0 + 1, width_ - 1);
122 y1 = std::min(y0 + 1, height_ - 1);
123 z1 = std::min(z0 + 1, depth_ - 1);
124 fx = lx - static_cast<float>(x0);
125 fy = ly - static_cast<float>(y0);
126 fz = lz - static_cast<float>(z0);
127}
128
129float FogDensityField::sampleDensity(const glm::vec3& world) const noexcept {
130 if (density_.empty() || !bounds_.contains(world)) {
131 // Soft falloff just outside bounds keeps ray edges continuous.
132 if (density_.empty()) return 0.f;
133 const glm::vec3 c = glm::clamp(world, bounds_.minimum, bounds_.maximum);
134 if (glm::length(world - c) > cellSize_.x) return 0.f;
135 }
136 int x0, y0, z0, x1, y1, z1;
137 float fx, fy, fz;
138 sampleLattice(world, x0, y0, z0, x1, y1, z1, fx, fy, fz);
139 const auto lerp = [](float a, float b, float t) { return a + (b - a) * t; };
140 const float c00 = lerp(densityAt(x0, y0, z0), densityAt(x1, y0, z0), fx);
141 const float c10 = lerp(densityAt(x0, y1, z0), densityAt(x1, y1, z0), fx);
142 const float c01 = lerp(densityAt(x0, y0, z1), densityAt(x1, y0, z1), fx);
143 const float c11 = lerp(densityAt(x0, y1, z1), densityAt(x1, y1, z1), fx);
144 return lerp(lerp(c00, c10, fy), lerp(c01, c11, fy), fz);
145}
146
147glm::vec3 FogDensityField::sampleCurlVelocity(const glm::vec3& world) const noexcept {
148 if (curl_.empty()) return {};
149 int x0, y0, z0, x1, y1, z1;
150 float fx, fy, fz;
151 sampleLattice(world, x0, y0, z0, x1, y1, z1, fx, fy, fz);
152 const auto lerp3 = [](const glm::vec3& a, const glm::vec3& b, float t) { return a + (b - a) * t; };
153 const glm::vec3 c00 = lerp3(curlVelocityAt(x0, y0, z0), curlVelocityAt(x1, y0, z0), fx);
154 const glm::vec3 c10 = lerp3(curlVelocityAt(x0, y1, z0), curlVelocityAt(x1, y1, z0), fx);
155 const glm::vec3 c01 = lerp3(curlVelocityAt(x0, y0, z1), curlVelocityAt(x1, y0, z1), fx);
156 const glm::vec3 c11 = lerp3(curlVelocityAt(x0, y1, z1), curlVelocityAt(x1, y1, z1), fx);
157 return lerp3(lerp3(c00, c10, fy), lerp3(c01, c11, fy), fz);
158}
159
160glm::vec3 FogDensityField::sampleLightAssist(const glm::vec3& world) const noexcept {
161 if (lightAssist_.empty()) return {};
162 int x0, y0, z0, x1, y1, z1;
163 float fx, fy, fz;
164 sampleLattice(world, x0, y0, z0, x1, y1, z1, fx, fy, fz);
165 const auto lerp3 = [](const glm::vec3& a, const glm::vec3& b, float t) { return a + (b - a) * t; };
166 const glm::vec3 c00 = lerp3(lightAssistAt(x0, y0, z0), lightAssistAt(x1, y0, z0), fx);
167 const glm::vec3 c10 = lerp3(lightAssistAt(x0, y1, z0), lightAssistAt(x1, y1, z0), fx);
168 const glm::vec3 c01 = lerp3(lightAssistAt(x0, y0, z1), lightAssistAt(x1, y0, z1), fx);
169 const glm::vec3 c11 = lerp3(lightAssistAt(x0, y1, z1), lightAssistAt(x1, y1, z1), fx);
170 return lerp3(lerp3(c00, c10, fy), lerp3(c01, c11, fy), fz);
171}
172
173Result<void> FogDensityField::seedHeightBand(float baseDensity, float baseHeight, float falloff, float curlScale,
174 std::uint32_t seed) {
175 if (density_.empty()) {
177 Diagnostic::error(DiagnosticCode::Failed, "density field is not allocated", "resize", {}, "graphics.fog"));
178 }
179 if (!std::isfinite(baseDensity) || baseDensity < 0.f || !std::isfinite(falloff) || falloff < 0.f ||
180 !std::isfinite(curlScale)) {
182 "height-band parameters must be finite and non-negative",
183 "seedHeightBand", {}, "graphics.fog"));
184 }
185
186 for (int z = 0; z < depth_; ++z) {
187 for (int y = 0; y < height_; ++y) {
188 for (int x = 0; x < width_; ++x) {
189 const glm::vec3 p = cellCenter(x, y, z);
190 const float h = std::max(p.y - baseHeight, 0.f);
191 float d = baseDensity * std::exp(-falloff * h);
192 const float n = 0.65f + 0.35f * hash31(x, y, z, seed);
193 d *= n;
194 setDensity(x, y, z, d);
195
196 // Analytic curl-noise style assist: divergence-free on average.
197 const float a = hash31(x + 17, y, z, seed) * 6.2831853f;
198 const float b = hash31(x, y + 31, z, seed) * 6.2831853f;
199 const glm::vec3 curl(curlScale * std::sin(a) * std::cos(b), curlScale * 0.35f * std::sin(a + b),
200 curlScale * std::cos(a) * std::sin(b));
201 setCurlVelocity(x, y, z, curl);
202 }
203 }
204 }
205 // setDensity already bumps revision many times; collapse to one bump.
206 ++revision_;
207 return Result<void>::success();
208}
209
210bool FogDensityField::isOccupied(int x, int y, int z, float threshold) const noexcept {
211 return densityAt(x, y, z) > threshold;
212}
213
214std::span<float> FogDensityField::densitySpan() noexcept { return density_; }
215std::span<const float> FogDensityField::densitySpan() const noexcept { return density_; }
216
217} // namespace eve::graphics::fog
218
219// FogFroxelBridge.cpp
220namespace eve::graphics::fog {
221namespace {
222
224[[nodiscard]] glm::vec3 reconstructFroxelCenter(const AtmosphereVolume& volume, int x, int y, int z,
225 const glm::mat4& invViewProj) {
226 const int width = std::max(volume.getWidth(), 1);
227 const int height = std::max(volume.getHeight(), 1);
228 const float ndcX = ((static_cast<float>(x) + 0.5f) / static_cast<float>(width)) * 2.f - 1.f;
229 const float ndcY = ((static_cast<float>(y) + 0.5f) / static_cast<float>(height)) * 2.f - 1.f;
230 auto unproject = [&](float ndcZ) {
231 const glm::vec4 homogeneous = invViewProj * glm::vec4(ndcX, ndcY, ndcZ, 1.f);
232 return glm::vec3(homogeneous) / homogeneous.w;
233 };
234 const glm::vec3 nearPoint = unproject(0.f);
235 const glm::vec3 farPoint = unproject(1.f);
236 const float span = std::max(volume.getFarDistance() - volume.getNearDistance(), 1e-6f);
237 const float depth01 = (volume.sliceDistance(z) - volume.getNearDistance()) / span;
238 return glm::mix(nearPoint, farPoint, depth01);
239}
240
241} // namespace
242
244 const glm::mat4& invViewProj, bool occupancySkip) {
245 if (volume.getWidth() <= 0 || field.width() <= 0) {
246 return Result<int>::failure(Diagnostic::error(DiagnosticCode::Failed, "froxel/density volumes are empty",
247 "inject", {}, "graphics.fog"));
248 }
249
250 int written = 0;
251 for (int z = 0; z < volume.getDepth(); ++z) {
252 for (int y = 0; y < volume.getHeight(); ++y) {
253 for (int x = 0; x < volume.getWidth(); ++x) {
254 const glm::vec3 world = reconstructFroxelCenter(volume, x, y, z, invViewProj);
255 const float density = field.sampleDensity(world);
256 if (occupancySkip && density < 1e-4f) continue;
257 auto& froxel = volume.at(x, y, z);
258 const float sigmaT = profile.extinctionAt(density);
259 froxel.extinction += sigmaT;
260 froxel.scattering += profile.scatteringAt(density);
261 froxel.anisotropy = profile.anisotropy();
262 froxel.emissive += field.sampleLightAssist(world);
263 ++written;
264 }
265 }
266 }
267 return Result<int>::success(written);
268}
269
271 const FogProfile& profile, const glm::mat4& invViewProj,
272 const glm::vec3& lightDir, const glm::vec3& lightColor, float intensity,
273 const BeerLightCache* beerCache) {
274 if (volume.getWidth() <= 0) {
276 Diagnostic::error(DiagnosticCode::Failed, "froxel volume empty", "integrate", {}, "graphics.fog"));
277 }
278 const float len = glm::length(lightDir);
279 const glm::vec3 dir = len > 1e-6f ? lightDir / len : glm::vec3(0.f, 1.f, 0.f);
280 const float phaseScale = profile.phase(glm::dot(dir, glm::vec3(0.f, 0.f, -1.f)));
281
282 if (beerCache && beerCache->valid()) {
283 for (int z = 0; z < volume.getDepth(); ++z) {
284 for (int y = 0; y < volume.getHeight(); ++y) {
285 for (int x = 0; x < volume.getWidth(); ++x) {
286 const glm::vec3 world = reconstructFroxelCenter(volume, x, y, z, invViewProj);
287 volume.setLightVisibility(x, y, z, beerCache->sampleTransmittance(field, world));
288 }
289 }
290 }
291 }
292
293 volume.integrate(lightColor * intensity, phaseScale);
294 return Result<void>::success();
295}
296
297} // namespace eve::graphics::fog
298
299// FogInteractor.cpp
300namespace eve::graphics::fog {
301namespace {
302
303[[nodiscard]] bool finiteVec(const glm::vec3& v) noexcept {
304 return std::isfinite(v.x) && std::isfinite(v.y) && std::isfinite(v.z);
305}
306
307[[nodiscard]] glm::vec3 safeNormalize(const glm::vec3& v, const glm::vec3& fallback) noexcept {
308 const float len2 = glm::dot(v, v);
309 if (len2 < 1e-12f) return fallback;
310 return v * (1.f / std::sqrt(len2));
311}
312
313} // namespace
314
315Result<void> FogInteractor::setProxies(std::vector<FogSolidProxy> proxies) {
316 for (std::size_t i = 0; i < proxies.size(); ++i) {
317 const auto& p = proxies[i];
318 if (!finiteVec(p.position) || !finiteVec(p.velocity) || !finiteVec(p.extents) || !finiteVec(p.axis) ||
319 !finiteVec(p.axisX) || !finiteVec(p.axisY) || !finiteVec(p.axisZ)) {
321 DiagnosticCode::InvalidArgument, "proxy vectors must be finite", "proxies", {}, "graphics.fog"));
322 }
323 if (p.extents.x <= 0.f || p.extents.y <= 0.f || p.extents.z <= 0.f) {
325 DiagnosticCode::InvalidArgument, "proxy extents must be positive", "extents", {}, "graphics.fog"));
326 }
327 if (!std::isfinite(p.drag) || p.drag < 0.f || p.drag > 1.f || !std::isfinite(p.wakeStrength) ||
328 p.wakeStrength < 0.f) {
330 "drag must be in [0,1] and wakeStrength >= 0", "drag", {},
331 "graphics.fog"));
332 }
333 }
334 proxies_ = std::move(proxies);
335 return Result<void>::success();
336}
337
338const FogSolidProxy& FogInteractor::proxyAt(std::size_t index) const { return proxies_.at(index); }
339
340float FogInteractor::signedDistance(const FogSolidProxy& proxy, const glm::vec3& world) const noexcept {
341 switch (proxy.shape) {
343 return glm::length(world - proxy.position) - proxy.extents.x;
344 }
346 const glm::vec3 a = proxy.position - proxy.axis;
347 const glm::vec3 b = proxy.position + proxy.axis;
348 const glm::vec3 pa = world - a;
349 const glm::vec3 ba = b - a;
350 const float h = std::clamp(glm::dot(pa, ba) / std::max(glm::dot(ba, ba), 1e-8f), 0.f, 1.f);
351 return glm::length(pa - ba * h) - proxy.extents.x;
352 }
353 case FogProxyShape::Obb: {
354 const glm::vec3 d = world - proxy.position;
355 const glm::vec3 local(glm::dot(d, proxy.axisX), glm::dot(d, proxy.axisY), glm::dot(d, proxy.axisZ));
356 const glm::vec3 q = glm::abs(local) - proxy.extents;
357 return glm::length(glm::max(q, glm::vec3(0.f))) + std::min(std::max(q.x, std::max(q.y, q.z)), 0.f);
358 }
359 }
360 return 1.f;
361}
362
363glm::vec3 FogInteractor::closestPoint(const FogSolidProxy& proxy, const glm::vec3& world) const noexcept {
364 const float sd = signedDistance(proxy, world);
365 if (sd >= 0.f) {
366 const glm::vec3 n = surfaceNormal(proxy, world);
367 return world - n * sd;
368 }
369 const glm::vec3 n = surfaceNormal(proxy, world);
370 return world - n * sd;
371}
372
373glm::vec3 FogInteractor::surfaceNormal(const FogSolidProxy& proxy, const glm::vec3& world) const noexcept {
374 constexpr float e = 1e-3f;
375 const float dx =
376 signedDistance(proxy, world + glm::vec3(e, 0.f, 0.f)) - signedDistance(proxy, world - glm::vec3(e, 0.f, 0.f));
377 const float dy =
378 signedDistance(proxy, world + glm::vec3(0.f, e, 0.f)) - signedDistance(proxy, world - glm::vec3(0.f, e, 0.f));
379 const float dz =
380 signedDistance(proxy, world + glm::vec3(0.f, 0.f, e)) - signedDistance(proxy, world - glm::vec3(0.f, 0.f, e));
381 return safeNormalize(glm::vec3(dx, dy, dz), glm::vec3(0.f, 1.f, 0.f));
382}
383
384bool FogInteractor::isSolidWorld(const glm::vec3& world) const noexcept {
385 for (const auto& p : proxies_) {
386 if (!p.enabled) continue;
387 if (signedDistance(p, world) <= 0.f) return true;
388 }
389 return false;
390}
391
393 if (!std::isfinite(dt) || dt < 0.f) {
395 Diagnostic::error(DiagnosticCode::InvalidArgument, "dt must be finite and >= 0", "dt", {}, "graphics.fog"));
396 }
397 if (grid.width() <= 0) {
399 Diagnostic::error(DiagnosticCode::Failed, "MAC grid is empty", "grid", {}, "graphics.fog"));
400 }
401
402 const FogWorldBounds& bounds = grid.bounds_;
403 const glm::vec3 cell = grid.cellSize_;
404
405 std::fill(grid.solid_.begin(), grid.solid_.end(), 0);
406
407 for (int z = 0; z < grid.depth_; ++z) {
408 for (int y = 0; y < grid.height_; ++y) {
409 for (int x = 0; x < grid.width_; ++x) {
410 const glm::vec3 world =
411 bounds.minimum + glm::vec3((x + 0.5f) * cell.x, (y + 0.5f) * cell.y, (z + 0.5f) * cell.z);
412 if (!isSolidWorld(world)) continue;
413 grid.solid_[grid.cellIndex(x, y, z)] = 1;
414 grid.setDensity(x, y, z, 0.f);
415
416 for (const auto& proxy : proxies_) {
417 if (!proxy.enabled) continue;
418 if (signedDistance(proxy, world) > cell.x) continue;
419 const glm::vec3 n = surfaceNormal(proxy, world);
420 const float vn = glm::dot(proxy.velocity, n);
421 // Windward push, leeward suction, tangential drag.
422 const glm::vec3 tangential = proxy.velocity - n * vn;
423 const glm::vec3 wake = n * (vn * proxy.wakeStrength) + tangential * (1.f - proxy.drag);
424 // Stamp onto adjacent faces (wake pair).
425 if (x + 1 < grid.width_) grid.u_[grid.uIndex(x + 1, y, z)] += wake.x * dt;
426 if (x > 0) grid.u_[grid.uIndex(x, y, z)] += wake.x * dt;
427 if (y + 1 < grid.height_) grid.v_[grid.vIndex(x, y + 1, z)] += wake.y * dt;
428 if (y > 0) grid.v_[grid.vIndex(x, y, z)] += wake.y * dt;
429 if (z + 1 < grid.depth_) grid.w_[grid.wIndex(x, y, z + 1)] += wake.z * dt;
430 if (z > 0) grid.w_[grid.wIndex(x, y, z)] += wake.z * dt;
431 }
432 }
433 }
434 }
435
436 // Zero velocity into solid faces.
437 for (int z = 0; z < grid.depth_; ++z) {
438 for (int y = 0; y < grid.height_; ++y) {
439 for (int i = 0; i <= grid.width_; ++i) {
440 const bool leftSolid = i > 0 && grid.solid_[grid.cellIndex(i - 1, y, z)];
441 const bool rightSolid = i < grid.width_ && grid.solid_[grid.cellIndex(i, y, z)];
442 if (leftSolid || rightSolid) grid.u_[grid.uIndex(i, y, z)] = 0.f;
443 }
444 }
445 }
446 for (int z = 0; z < grid.depth_; ++z) {
447 for (int j = 0; j <= grid.height_; ++j) {
448 for (int x = 0; x < grid.width_; ++x) {
449 const bool downSolid = j > 0 && grid.solid_[grid.cellIndex(x, j - 1, z)];
450 const bool upSolid = j < grid.height_ && grid.solid_[grid.cellIndex(x, j, z)];
451 if (downSolid || upSolid) grid.v_[grid.vIndex(x, j, z)] = 0.f;
452 }
453 }
454 }
455 for (int k = 0; k <= grid.depth_; ++k) {
456 for (int y = 0; y < grid.height_; ++y) {
457 for (int x = 0; x < grid.width_; ++x) {
458 const bool backSolid = k > 0 && grid.solid_[grid.cellIndex(x, y, k - 1)];
459 const bool frontSolid = k < grid.depth_ && grid.solid_[grid.cellIndex(x, y, k)];
460 if (backSolid || frontSolid) grid.w_[grid.wIndex(x, y, k)] = 0.f;
461 }
462 }
463 }
464 return Result<void>::success();
465}
466
467glm::vec3 FogInteractor::clipAdvection(const glm::vec3& startLocal, const glm::vec3& deltaLocal,
468 const FogWorldBounds& bounds, const glm::vec3& cellSize) const noexcept {
469 if (proxies_.empty()) return startLocal + deltaLocal;
470
471 const float len = glm::length(deltaLocal);
472 if (len < 1e-8f) return startLocal;
473
474 const int steps = std::max(1, static_cast<int>(std::ceil(len * 2.f)));
475 const glm::vec3 step = deltaLocal / static_cast<float>(steps);
476 glm::vec3 p = startLocal;
477 for (int i = 0; i < steps; ++i) {
478 const glm::vec3 next = p + step;
479 const glm::vec3 world = bounds.minimum + glm::vec3((next.x + 0.5f) * cellSize.x, (next.y + 0.5f) * cellSize.y,
480 (next.z + 0.5f) * cellSize.z);
481 if (isSolidWorld(world)) return p;
482 p = next;
483 }
484 return p;
485}
486
487} // namespace eve::graphics::fog
488
489// FogProfile.cpp
490namespace eve::graphics::fog {
491namespace {
492
493[[nodiscard]] bool finiteVec3(const glm::vec3& v) noexcept {
494 return std::isfinite(v.x) && std::isfinite(v.y) && std::isfinite(v.z);
495}
496
497[[nodiscard]] float henyeyGreenstein(float g, float cosTheta) noexcept {
498 const float g2 = g * g;
499 const float denom = 1.f + g2 - 2.f * g * cosTheta;
500 const float denom32 = denom * std::sqrt(std::max(denom, 1e-8f));
501 return (1.f - g2) / (4.f * 3.14159265f * std::max(denom32, 1e-8f));
502}
503
504} // namespace
505
506Result<void> FogProfile::configure(float extinctionPerDensity, const glm::vec3& albedo, float ambientOcclusion,
507 float ambientUp, float ambientDown, float anisotropy) {
508 if (!std::isfinite(extinctionPerDensity) || extinctionPerDensity < 0.f) {
510 "extinctionPerDensity must be finite and >= 0",
511 "extinctionPerDensity", {}, "graphics.fog"));
512 }
513 if (!finiteVec3(albedo) || albedo.x < 0.f || albedo.y < 0.f || albedo.z < 0.f || albedo.x > 1.f || albedo.y > 1.f ||
514 albedo.z > 1.f) {
516 "albedo components must be finite and in [0,1]", "albedo", {},
517 "graphics.fog"));
518 }
519 auto in01 = [](float v) { return std::isfinite(v) && v >= 0.f && v <= 1.f; };
520 if (!in01(ambientOcclusion) || !in01(ambientUp) || !in01(ambientDown)) {
522 "ambient terms must be finite and in [0,1]", "ambient", {},
523 "graphics.fog"));
524 }
525 if (!std::isfinite(anisotropy) || anisotropy < -0.99f || anisotropy > 0.99f) {
527 "anisotropy must be finite and in [-0.99,0.99]", "anisotropy",
528 {}, "graphics.fog"));
529 }
530
531 extinctionPerDensity_ = extinctionPerDensity;
532 albedo_ = albedo;
533 ambientOcclusion_ = ambientOcclusion;
534 ambientUp_ = ambientUp;
535 ambientDown_ = ambientDown;
536 anisotropy_ = anisotropy;
537 return Result<void>::success();
538}
539
540float FogProfile::extinctionAt(float density) const noexcept { return extinctionPerDensity_ * std::max(density, 0.f); }
541
542glm::vec3 FogProfile::scatteringAt(float density) const noexcept { return albedo_ * extinctionAt(density); }
543
544float FogProfile::phase(float cosTheta) const noexcept {
545 const float c = std::clamp(cosTheta, -1.f, 1.f);
546 const float g = anisotropy_;
547 // Dual-lobe: forward HG + mild isotropic back lobe for ambient fill.
548 return 0.85f * henyeyGreenstein(g, c) + 0.15f * henyeyGreenstein(-0.25f * g, c);
549}
550
551glm::vec3 FogProfile::ambientRadiance(const glm::vec3& skyUp, const glm::vec3& groundDown,
552 float density) const noexcept {
553 const float ao = 1.f - ambientOcclusion_ * std::clamp(density, 0.f, 1.f);
554 return ao * (ambientUp_ * skyUp + ambientDown_ * groundDown);
555}
556
557} // namespace eve::graphics::fog
double z
double y
double x
double volume
float ao
graphics::Texture * albedo
glm::vec4 p[6]
Stable, structured diagnostics shared by engine modules.
float density
tensor::Graph g
Definition GpuGraph.cpp:7
glm::vec3 n
Definition Grass.cpp:63
std::array< double, 10 > q
float v
std::int32_t c
int h
std::uint32_t height
std::uint32_t width
std::string local
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
ssq::Object wake
glm::dvec3 dx
glm::dvec3 velocity
glm::dvec3 dz
double dt
float d
int steps
float t
float dy
Definition Shadow.cpp:151
int count
Cell cell
uint32_t seed
Definition TreeMesh.cpp:276
float step
Definition TreeMesh.cpp:451
float size
Definition TreeMesh.cpp:158
V3 dir
Definition TreeMesh.cpp:151
std::uint32_t depth
uint32_t index
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
Camera-frustum volume used by the volumetric fog passes and CPU references.
Cached light-path transmittance used by Fast / Enhanced quality tiers.
float sampleTransmittance(const FogDensityField &field, const glm::vec3 &world) const noexcept
Sample cached transmittance at a world position (1 = fully lit).
World-space density bands, curl assist velocity, and lighting helpers.
const FogWorldBounds & bounds() const noexcept
float densityAt(int x, int y, int z) const noexcept
Density at.
bool isOccupied(int x, int y, int z, float threshold=1e-4f) const noexcept
Occupancy bit: true when density exceeds the threshold.
std::span< float > densitySpan() noexcept
Borrow density storage for MAC coupling (same resolution).
glm::vec3 sampleLightAssist(const glm::vec3 &world) const noexcept
Trilinear world-space light-assist sample.
glm::vec3 curlVelocityAt(int x, int y, int z) const noexcept
Curl velocity at.
glm::vec3 sampleCurlVelocity(const glm::vec3 &world) const noexcept
Trilinear world-space curl velocity sample.
void clear()
Clear density / curl / light-assist bands and bump revision.
glm::vec3 lightAssistAt(int x, int y, int z) const noexcept
Light assist at.
Result< void > seedHeightBand(float baseDensity, float baseHeight, float falloff, float curlScale, std::uint32_t seed)
Seed a height-banded density layer with optional curl noise.
Result< void > resize(int width, int height, int depth, const FogWorldBounds &bounds)
Allocate a regular lattice over world bounds.
void setDensity(int x, int y, int z, float density)
Write cell-centered density; indices are clamped.
void setCurlVelocity(int x, int y, int z, const glm::vec3 &velocity)
Write curl-assist velocity (m/s) at a cell center.
float sampleDensity(const glm::vec3 &world) const noexcept
Trilinear world-space density sample.
void setLightAssist(int x, int y, int z, const glm::vec3 &assist)
Accumulated lighting assist (pre-multiplied irradiance hint).
Result< void > integrate(AtmosphereVolume &volume, const FogDensityField &field, const FogProfile &profile, const glm::mat4 &invViewProj, const glm::vec3 &lightDir, const glm::vec3 &lightColor, float intensity, const BeerLightCache *beerCache)
Integrate froxels with optional Beer-cache visibility.
Result< int > inject(AtmosphereVolume &volume, const FogDensityField &field, const FogProfile &profile, const glm::mat4 &invViewProj, bool occupancySkip)
Inject density-field media into an AtmosphereVolume froxel grid.
Result< void > applyToGrid(MacFluidGrid &grid, float dt) const
Rasterize solids into the MAC solid mask, clear interior density, and inject wake / drag into face ve...
const FogSolidProxy & proxyAt(std::size_t index) const
Result< void > setProxies(std::vector< FogSolidProxy > proxies)
Replace the proxy set atomically after validation.
bool isSolidWorld(const glm::vec3 &world) const noexcept
True when a world point lies inside any enabled solid.
glm::vec3 clipAdvection(const glm::vec3 &startLocal, const glm::vec3 &deltaLocal, const FogWorldBounds &bounds, const glm::vec3 &cellSize) const noexcept
Clip a local-cell advection displacement against thin solids.
Unit-density optical profile for participating media.
Definition FogProfile.h:16
float ambientOcclusion() const noexcept
Ambient occlusion strength in [0,1].
Definition FogProfile.h:23
float extinctionAt(float density) const noexcept
Extinction for a sampled density value.
float extinctionPerDensity() const noexcept
Extinction coefficient per unit density (σ_t), non-negative.
Definition FogProfile.h:19
float anisotropy() const noexcept
Henyey–Greenstein anisotropy in [-0.99, 0.99].
Definition FogProfile.h:29
Result< void > configure(float extinctionPerDensity, const glm::vec3 &albedo, float ambientOcclusion, float ambientUp, float ambientDown, float anisotropy)
Atomically replace the optical profile.
glm::vec3 ambientRadiance(const glm::vec3 &skyUp, const glm::vec3 &groundDown, float density) const noexcept
Ambient term using up/down visibility and AO.
float phase(float cosTheta) const noexcept
Dual-lobe Henyey–Greenstein phase evaluated for cosθ.
float ambientDown() const noexcept
Downward ambient visibility scale in [0,1].
Definition FogProfile.h:27
glm::vec3 scatteringAt(float density) const noexcept
Scattering coefficient σ_s = albedo * σ_t.
float ambientUp() const noexcept
Upward ambient visibility scale in [0,1].
Definition FogProfile.h:25
const glm::vec3 & albedo() const noexcept
Single-scattering albedo in [0,1].
Definition FogProfile.h:21
MAC (Marker-And-Cell) staggered-grid Eulerian fog fluid.
Value at(size_t index) const
At.
Definition Json.cpp:359
One analytic solid that carves / wakes the fog domain.
Definition FogTypes.h:142
Axis-aligned world bounds for a fog simulation domain.
Definition FogTypes.h:96
glm::vec4 lightColor
glm::vec4 lightDir
glm::mat4 invViewProj
glm::vec4 bounds