4#include <glm/common.hpp>
5#include <glm/geometric.hpp>
15 const int count = std::max(levelCount, 1);
16 const int res = std::max(resolution, 2);
17 const float baseExtent = std::max(firstExtent, 1e-3f);
18 levels_.resize(std::size_t(
count));
19 for (
int i = 0; i <
count; ++i) {
21 level.extent = baseExtent * std::pow(2.f,
float(i));
22 level.voxelSize = (
level.extent * 2.f) /
float(res);
23 level.center = glm::vec3(0.f);
24 level.volume.resize(res, res, res);
31 level.center = glm::floor(cameraPosition /
level.voxelSize) *
level.voxelSize;
36 return levels_[std::size_t(std::clamp(
level, 0,
int(levels_.size()) - 1))];
40 return levels_[std::size_t(std::clamp(
level, 0,
int(levels_.size()) - 1))];
44 for (std::size_t i = 0; i < levels_.size(); ++i) {
46 const glm::vec3
d = glm::abs(worldPosition -
level.center);
54 if (
index < 0)
return {};
56 const glm::vec3 minCorner =
level.center - glm::vec3(
level.extent);
57 const glm::vec3 uvw = (worldPosition - minCorner) / (
level.extent * 2.f);
58 return level.volume.sampleMedia(uvw.x, uvw.y, uvw.z);
67 width_ = std::max(
width, 1);
68 height_ = std::max(
height, 1);
69 depth_ = std::max(
depth, 1);
70 media_.assign(std::size_t(width_) * std::size_t(height_) * std::size_t(depth_), {});
71 integrated_.assign(media_.size(), glm::vec4(0.f, 0.f, 0.f, 1.f));
75 std::fill(media_.begin(), media_.end(),
FogFroxel{});
76 std::fill(integrated_.begin(), integrated_.end(), glm::vec4(0.f, 0.f, 0.f, 1.f));
80 nearDistance_ = std::max(nearDistance, 1e-3f);
81 farDistance_ = std::max(farDistance, nearDistance_ + 1e-3f);
85 if (depth_ <= 0)
return nearDistance_;
86 const float t = (float(std::clamp(
z, 0, depth_ - 1)) + 0.5f) /
float(depth_);
87 return nearDistance_ * std::pow(farDistance_ / nearDistance_,
t);
91 if (depth_ <= 1)
return 0;
92 const float d = std::clamp(
distance, nearDistance_, farDistance_);
93 const float t = std::log(
d / nearDistance_) / std::log(farDistance_ / nearDistance_);
94 return std::clamp(
int(std::floor(
t *
float(depth_))), 0, depth_ - 1);
97std::size_t AtmosphereVolume::index(
int x,
int y,
int z)
const {
98 x = std::clamp(
x, 0, width_ - 1);
99 y = std::clamp(
y, 0, height_ - 1);
100 z = std::clamp(
z, 0, depth_ - 1);
101 return (std::size_t(
z) * std::size_t(height_) + std::size_t(
y)) * std::size_t(width_) + std::size_t(
x);
108 float heightFalloff,
float minWorldY,
float maxWorldY) {
109 const float sigma = std::max(baseExtinction, 0.f);
110 const glm::vec3 omega = glm::clamp(
albedo, glm::vec3(0.f), glm::vec3(1.f));
111 const float falloff = std::max(heightFalloff, 0.f);
112 for (
int y = 0;
y < height_; ++
y) {
113 const float ty = (float(
y) + 0.5f) /
float(height_);
114 const float worldY = minWorldY + (maxWorldY - minWorldY) * ty;
115 const float extinction = sigma * std::exp(-falloff * std::max(worldY - baseHeight, 0.f));
116 for (
int z = 0;
z < depth_; ++
z) {
117 for (
int x = 0;
x < width_; ++
x) {
127 float heightFalloff,
float minWorldY,
float maxWorldY,
129 const float sigma = std::max(baseExtinction, 0.f);
130 const glm::vec3 omega = glm::clamp(
albedo, glm::vec3(0.f), glm::vec3(1.f));
131 const float falloff = std::max(heightFalloff, 0.f);
132 auto unproject = [&](
float ndcX,
float ndcY,
float ndcZ) {
133 const glm::vec4 homogeneous =
invViewProj * glm::vec4(ndcX, ndcY, ndcZ, 1.f);
134 return glm::vec3(homogeneous) / homogeneous.w;
136 for (
int y = 0;
y < height_; ++
y) {
137 const float ndcY = ((float(
y) + 0.5f) /
float(height_)) * 2.f - 1.f;
138 for (
int x = 0;
x < width_; ++
x) {
139 const float ndcX = ((float(
x) + 0.5f) /
float(width_)) * 2.f - 1.f;
140 const glm::vec3 nearPoint = unproject(ndcX, ndcY, 0.f);
141 const glm::vec3 farPoint = unproject(ndcX, ndcY, 1.f);
142 for (
int z = 0;
z < depth_; ++
z) {
143 const float depth01 = (
sliceDistance(
z) - nearDistance_) / (farDistance_ - nearDistance_);
144 const glm::vec3 world = glm::mix(nearPoint, farPoint, depth01);
145 if (world.y < minWorldY || world.y > maxWorldY)
continue;
146 const float extinction = sigma * std::exp(-falloff * std::max(world.y - baseHeight, 0.f));
156 const glm::vec3 &worldMax) {
157 for (
int z = 0;
z < depth_; ++
z) {
158 for (
int y = 0;
y < height_; ++
y) {
159 for (
int x = 0;
x < width_; ++
x) {
160 const glm::vec3 uvw((
float(
x) + 0.5f) /
float(width_), (
float(
y) + 0.5f) /
float(height_),
161 (
float(
z) + 0.5f) /
float(depth_));
162 const glm::vec3 world = worldMin + (worldMax - worldMin) * uvw;
163 const float signedExtinction =
volume.sampleExtinction(world);
164 if (std::fabs(signedExtinction) <= 1e-8f)
continue;
167 f.extinction = std::max(0.f,
previous + signedExtinction);
168 if (signedExtinction > 0.f) {
169 f.scattering +=
volume.getAlbedo() * signedExtinction;
170 f.emissive +=
volume.getEmissive() * signedExtinction;
171 f.anisotropy =
volume.getAnisotropy();
183 auto unproject = [&](
float ndcX,
float ndcY,
float ndcZ) {
184 glm::vec4 world =
invViewProj * glm::vec4(ndcX, ndcY, ndcZ, 1.f);
185 return glm::vec3(world) / world.w;
187 for (
int y = 0;
y < height_; ++
y) {
188 const float v = (float(
y) + 0.5f) /
float(height_);
189 for (
int x = 0;
x < width_; ++
x) {
190 const float u = (float(
x) + 0.5f) /
float(width_);
191 const glm::vec3 nearPoint = unproject(
u * 2.f - 1.f,
v * 2.f - 1.f, 0.f);
192 const glm::vec3 farPoint = unproject(
u * 2.f - 1.f,
v * 2.f - 1.f, 1.f);
193 for (
int z = 0;
z < depth_; ++
z) {
194 const float depth01 = (
sliceDistance(
z) - nearDistance_) / (farDistance_ - nearDistance_);
195 const glm::vec3 world = glm::mix(nearPoint, farPoint, depth01);
196 const float signedExtinction =
volume.sampleExtinction(world);
197 if (std::fabs(signedExtinction) <= 1e-8f)
continue;
200 f.extinction = std::max(0.f,
previous + signedExtinction);
201 if (signedExtinction > 0.f) {
202 f.scattering +=
volume.getAlbedo() * signedExtinction;
203 f.emissive +=
volume.getEmissive() * signedExtinction;
204 f.anisotropy =
volume.getAnisotropy();
216 const glm::vec3 &worldMax,
float extinctionScale,
const glm::vec3 &
albedo,
218 const glm::vec3 omega = glm::clamp(
albedo, glm::vec3(0.f), glm::vec3(1.f));
219 for (
int z = 0;
z < depth_; ++
z) {
220 for (
int y = 0;
y < height_; ++
y) {
221 for (
int x = 0;
x < width_; ++
x) {
222 const glm::vec3 uvw((
float(
x) + 0.5f) /
float(width_), (
float(
y) + 0.5f) /
float(height_),
223 (
float(
z) + 0.5f) /
float(depth_));
224 const glm::vec3 world = worldMin + (worldMax - worldMin) * uvw;
239 for (
int z = 0;
z < depth_; ++
z) {
240 for (
int y = 0;
y < height_; ++
y) {
241 for (
int x = 0;
x < width_; ++
x) {
243 texture.sample((
float(
x) + 0.5f) /
float(width_), (
float(
y) + 0.5f) /
float(height_),
244 (
float(
z) + 0.5f) /
float(depth_));
245 const float scale = std::max(extinctionScale, 0.f);
257 const glm::vec3
light = glm::max(
lightColor, glm::vec3(0.f)) * std::max(phaseScale, 0.f);
258 for (
int y = 0;
y < height_; ++
y) {
259 for (
int x = 0;
x < width_; ++
x) {
260 glm::vec3 radiance(0.f);
261 float transmittance = 1.f;
262 float previousDistance = nearDistance_;
263 for (
int z = 0;
z < depth_; ++
z) {
265 const float stepLength = std::max(
distance - previousDistance, 0.f);
268 const float opticalDepth =
f.extinction * stepLength;
269 const float stepTransmittance = std::exp(-opticalDepth);
270 const float integral =
f.extinction > 1e-6f ? (1.f - stepTransmittance) /
f.extinction : stepLength;
271 radiance += transmittance * (
f.scattering *
light *
f.lightVisibility +
f.emissive) * integral;
272 transmittance *= stepTransmittance;
273 integrated_[
index(
x,
y,
z)] = glm::vec4(radiance, transmittance);
280 const glm::vec3 &worldMax,
const glm::vec3 &ambientLight) {
281 for (
int y = 0;
y < height_; ++
y) {
282 for (
int x = 0;
x < width_; ++
x) {
283 glm::vec3 radiance(0.f);
284 float transmittance = 1.f;
285 float previousDistance = nearDistance_;
286 for (
int z = 0;
z < depth_; ++
z) {
287 const glm::vec3 uvw((
float(
x) + 0.5f) /
float(width_), (
float(
y) + 0.5f) /
float(height_),
288 (
float(
z) + 0.5f) /
float(depth_));
289 const glm::vec3 world = worldMin + (worldMax - worldMin) * uvw;
290 glm::vec3 incident = glm::max(ambientLight, glm::vec3(0.f));
292 if (!
light.enabled ||
light.radius <= 0.f ||
light.intensity <= 0.f)
continue;
293 const float d = glm::length(world -
light.position);
294 if (
d >=
light.radius)
continue;
295 const float radial = 1.f -
d /
light.radius;
296 incident += glm::max(
light.color, glm::vec3(0.f)) *
light.intensity * radial * radial;
299 const float stepLength = std::max(
distance - previousDistance, 0.f);
302 const float opticalDepth =
f.extinction * stepLength;
303 const float stepTransmittance = std::exp(-opticalDepth);
304 const float integral =
f.extinction > 1e-6f ? (1.f - stepTransmittance) /
f.extinction : stepLength;
305 radiance += transmittance * (
f.scattering * incident *
f.lightVisibility +
f.emissive) * integral;
306 transmittance *= stepTransmittance;
307 integrated_[
index(
x,
y,
z)] = glm::vec4(radiance, transmittance);
314 at(
x,
y,
z).lightVisibility = std::clamp(visibility, 0.f, 1.f);
318 float rejectionThreshold) {
319 if (history.width_ != width_ || history.height_ != height_ || history.depth_ != depth_)
return integrated_.size();
320 const float baseWeight = std::clamp(historyWeight, 0.f, 1.f);
321 const float threshold = std::max(rejectionThreshold, 0.f);
322 std::size_t rejected = 0;
323 for (std::size_t i = 0; i < integrated_.size(); ++i) {
324 const glm::vec4
current = integrated_[i];
325 const glm::vec4
previous = history.integrated_[i];
326 const float currentLuma = glm::dot(glm::vec3(
current), glm::vec3(0.2126f, 0.7152f, 0.0722f));
327 const float previousLuma = glm::dot(glm::vec3(
previous), glm::vec3(0.2126f, 0.7152f, 0.0722f));
328 const float relativeDelta =
329 std::fabs(currentLuma - previousLuma) / std::max(std::max(currentLuma, previousLuma), 1e-4f);
330 const bool reject = relativeDelta > threshold;
331 if (reject) ++rejected;
332 const float weight = reject ? 0.f : baseWeight;
341 if (integrated_.empty())
return glm::vec4(0.f, 0.f, 0.f, 1.f);
342 const int x = std::clamp(
int(
u *
float(width_)), 0, width_ - 1);
343 const int y = std::clamp(
int(
v *
float(height_)), 0, height_ - 1);
348 if (media_.empty())
return {};
349 const int x = std::clamp(
int(
u *
float(width_)), 0, width_ - 1);
350 const int y = std::clamp(
int(
v *
float(height_)), 0, height_ - 1);
351 const int z = std::clamp(
int(
w *
float(depth_)), 0, depth_ - 1);
graphics::Texture * albedo
std::vector< eve::ProcgenProbeDesc > lights
std::array< float, 3 > scale
std::shared_ptr< const OceanSprayFrame > previous
std::map< std::string, std::vector< std::string > > graph
void configure(int levelCount, int resolution, float firstExtent)
Allocate nested cubic levels and center them at the origin.
FogFroxel sample(const glm::vec3 &worldPosition) const
Sample raw media from the finest containing level.
void updateCenter(const glm::vec3 &cameraPosition)
Snap all levels around a new camera position.
int levelForPosition(const glm::vec3 &worldPosition) const
Select the finest level containing a world position, or -1.
AtmosphereClipmapLevel & getLevel(int level)
Returns the level.
Camera-frustum volume used by the volumetric fog passes and CPU references.
void clear()
Remove all media and integrated lighting without changing dimensions.
void setDepthRange(float nearDistance, float farDistance)
Configure the world-distance range represented by logarithmic Z slices.
void setLightVisibility(int x, int y, int z, float visibility)
Set the main-light shadow visibility for one froxel.
FogFroxel & at(int x, int y, int z)
Read or write one froxel; indices are clamped to the grid.
void injectSparseVolume(const SparseVolumeTexture &texture, float extinctionScale=1.f)
Resample a pre-baked sparse volume into this working volume.
void injectHeightFogFrustum(float baseExtinction, const glm::vec3 &albedo, float baseHeight, float heightFalloff, float minWorldY, float maxWorldY, const glm::mat4 &invViewProj)
Add exponential-height fog by reconstructing each froxel in the active camera frustum.
void injectLocalVolumeFrustum(const FogVolume &volume, const glm::mat4 &invViewProj)
Voxelize one analytic local volume in the active camera frustum.
void injectHeightFog(float baseExtinction, const glm::vec3 &albedo, float baseHeight, float heightFalloff, float minWorldY, float maxWorldY)
Add a global exponential-height medium to every froxel.
const glm::vec4 & integratedAt(int x, int y, int z) const
Cumulative RGB in-scattering and A transmittance at a froxel.
std::size_t blendHistory(const AtmosphereVolume &history, float historyWeight, float rejectionThreshold)
Blend integrated lighting with a reprojected history volume.
void resize(int width, int height, int depth)
Resize the froxel grid and clear all media.
float sliceDistance(int z) const
World distance at the center of a Z slice.
void injectDensityGraph(const VolumeDensityGraph &graph, const glm::vec3 &worldMin, const glm::vec3 &worldMax, float extinctionScale, const glm::vec3 &albedo, float time=0.f)
Voxelize a procedural density graph into this volume.
void injectLocalVolume(const FogVolume &volume, const glm::vec3 &worldMin, const glm::vec3 &worldMax)
Voxelize one analytic local volume over the supplied world bounds.
FogFroxel sampleMedia(float u, float v, float w) const
Sample raw media with normalized volume coordinates.
int sliceForDistance(float distance) const
Convert a world distance into a clamped slice index.
glm::vec4 sampleIntegrated(float u, float v, float distance) const
Query cumulative fog for a transparent fragment.
void integrateLocalLights(const std::vector< VolumetricLight > &lights, const glm::vec3 &worldMin, const glm::vec3 &worldMax, const glm::vec3 &ambientLight)
Inject local lights and integrate the volume in one reference pass.
void integrate(const glm::vec3 &lightColor, float phaseScale=1.f)
Integrate scattering and transmittance along every view ray.
Analytic local participating-media volume injected into atmospheric fog.
Brick-sparse, pre-bakeable participating-media texture.
Compact acyclic program that evaluates procedural volume density.
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
One camera-centered, world-space atmosphere clipmap level.
Participating-media coefficients stored in one froxel.
Local light injected into participating media.