载入中...
搜索中...
未找到
AtmosphereClipmap.cpp
浏览该文件的文档.
2#include <algorithm>
3#include <cmath>
4#include <glm/common.hpp>
5#include <glm/geometric.hpp>
10
11// AtmosphereClipmap.cpp
12namespace eve::graphics {
13
14void AtmosphereClipmap::configure(int levelCount, int resolution, float firstExtent) {
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) {
20 AtmosphereClipmapLevel &level = levels_[std::size_t(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);
25 level.volume.setDepthRange(level.voxelSize, level.extent * 2.f);
26 }
27}
28
29void AtmosphereClipmap::updateCenter(const glm::vec3 &cameraPosition) {
30 for (AtmosphereClipmapLevel &level : levels_) {
31 level.center = glm::floor(cameraPosition / level.voxelSize) * level.voxelSize;
32 }
33}
34
36 return levels_[std::size_t(std::clamp(level, 0, int(levels_.size()) - 1))];
37}
38
40 return levels_[std::size_t(std::clamp(level, 0, int(levels_.size()) - 1))];
41}
42
43int AtmosphereClipmap::levelForPosition(const glm::vec3 &worldPosition) const {
44 for (std::size_t i = 0; i < levels_.size(); ++i) {
45 const AtmosphereClipmapLevel &level = levels_[i];
46 const glm::vec3 d = glm::abs(worldPosition - level.center);
47 if (d.x <= level.extent && d.y <= level.extent && d.z <= level.extent) return int(i);
48 }
49 return -1;
50}
51
52FogFroxel AtmosphereClipmap::sample(const glm::vec3 &worldPosition) const {
53 const int index = levelForPosition(worldPosition);
54 if (index < 0) return {};
55 const AtmosphereClipmapLevel &level = levels_[std::size_t(index)];
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);
59}
60
61} // namespace eve::graphics
62
63// AtmosphereVolume.cpp
64namespace eve::graphics {
65
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));
72}
73
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));
77}
78
79void AtmosphereVolume::setDepthRange(float nearDistance, float farDistance) {
80 nearDistance_ = std::max(nearDistance, 1e-3f);
81 farDistance_ = std::max(farDistance, nearDistance_ + 1e-3f);
82}
83
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);
88}
89
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);
95}
96
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);
102}
103
104FogFroxel &AtmosphereVolume::at(int x, int y, int z) { return media_[index(x, y, z)]; }
105const FogFroxel &AtmosphereVolume::at(int x, int y, int z) const { return media_[index(x, y, z)]; }
106
107void AtmosphereVolume::injectHeightFog(float baseExtinction, const glm::vec3 &albedo, float baseHeight,
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) {
118 FogFroxel &f = at(x, y, z);
119 f.extinction += extinction;
120 f.scattering += omega * extinction;
121 }
122 }
123 }
124}
125
126void AtmosphereVolume::injectHeightFogFrustum(float baseExtinction, const glm::vec3 &albedo, float baseHeight,
127 float heightFalloff, float minWorldY, float maxWorldY,
128 const glm::mat4 &invViewProj) {
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;
135 };
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));
147 FogFroxel &froxel = at(x, y, z);
148 froxel.extinction += extinction;
149 froxel.scattering += omega * extinction;
150 }
151 }
152 }
153}
154
155void AtmosphereVolume::injectLocalVolume(const FogVolume &volume, const glm::vec3 &worldMin,
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;
165 FogFroxel &f = at(x, y, z);
166 const float previous = f.extinction;
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();
172 } else if (previous > 1e-6f) {
173 const float scale = f.extinction / previous;
174 f.scattering *= scale;
175 f.emissive *= scale;
176 }
177 }
178 }
179 }
180}
181
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;
186 };
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;
198 FogFroxel &f = at(x, y, z);
199 const float previous = f.extinction;
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();
205 } else if (previous > 1e-6f) {
206 const float scale = f.extinction / previous;
207 f.scattering *= scale;
208 f.emissive *= scale;
209 }
210 }
211 }
212 }
213}
214
215void AtmosphereVolume::injectDensityGraph(const VolumeDensityGraph &graph, const glm::vec3 &worldMin,
216 const glm::vec3 &worldMax, float extinctionScale, const glm::vec3 &albedo,
217 float time) {
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;
225 const float extinction = graph.evaluate(world, time) * extinctionScale;
226 FogFroxel &f = at(x, y, z);
227 const float previous = f.extinction;
228 f.extinction = std::max(0.f, previous + extinction);
229 if (extinction >= 0.f)
230 f.scattering += omega * extinction;
231 else if (previous > 1e-6f)
232 f.scattering *= f.extinction / previous;
233 }
234 }
235 }
236}
237
239 for (int z = 0; z < depth_; ++z) {
240 for (int y = 0; y < height_; ++y) {
241 for (int x = 0; x < width_; ++x) {
242 const FogFroxel source =
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);
246 FogFroxel &target = at(x, y, z);
247 target.extinction += source.extinction * scale;
248 target.scattering += source.scattering * scale;
249 target.emissive += source.emissive * scale;
250 target.anisotropy = source.anisotropy;
251 }
252 }
253 }
254}
255
256void AtmosphereVolume::integrate(const glm::vec3 &lightColor, float phaseScale) {
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) {
264 const float distance = sliceDistance(z);
265 const float stepLength = std::max(distance - previousDistance, 0.f);
266 previousDistance = distance;
267 const FogFroxel &f = at(x, y, z);
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);
274 }
275 }
276 }
277}
278
279void AtmosphereVolume::integrateLocalLights(const std::vector<VolumetricLight> &lights, const glm::vec3 &worldMin,
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));
291 for (const VolumetricLight &light : lights) {
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;
297 }
298 const float distance = sliceDistance(z);
299 const float stepLength = std::max(distance - previousDistance, 0.f);
300 previousDistance = distance;
301 const FogFroxel &f = at(x, y, z);
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);
308 }
309 }
310 }
311}
312
313void AtmosphereVolume::setLightVisibility(int x, int y, int z, float visibility) {
314 at(x, y, z).lightVisibility = std::clamp(visibility, 0.f, 1.f);
315}
316
317std::size_t AtmosphereVolume::blendHistory(const AtmosphereVolume &history, float historyWeight,
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;
333 integrated_[i] = glm::mix(current, previous, weight);
334 }
335 return rejected;
336}
337
338const glm::vec4 &AtmosphereVolume::integratedAt(int x, int y, int z) const { return integrated_[index(x, y, z)]; }
339
340glm::vec4 AtmosphereVolume::sampleIntegrated(float u, float v, float distance) const {
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);
345}
346
347FogFroxel AtmosphereVolume::sampleMedia(float u, float v, float w) const {
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);
352 return at(x, y, z);
353}
354
355} // namespace eve::graphics
double z
double y
double x
LogicalId target
float w
Definition AnimClip.cpp:738
double volume
graphics::Texture * albedo
std::vector< eve::ProcgenProbeDesc > lights
float u
Definition Grass.cpp:233
float v
std::uint32_t height
std::uint32_t width
std::array< float, 3 > scale
float distance
int level
std::shared_ptr< const OceanSprayFrame > previous
std::map< std::string, std::vector< std::string > > graph
Definition Package.cpp:173
Texture * texture
float f
float d
float t
std::string_view source
double current
Light3D::Data * light
Definition Shadow.cpp:147
Vec extinction
int count
std::uint32_t depth
std::size_t at
double weight
uint32_t index
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.
Definition FogVolume.h:12
Brick-sparse, pre-bakeable participating-media texture.
Compact acyclic program that evaluates procedural volume density.
卡牌游戏 UI 工具模块:工厂 + 脚本绑定入口。 功能参考 ycarowr/UiCard:扇形手牌布局、抽牌/洗牌、悬浮放大、拖拽到落牌区、 敌方手牌(背面/偷看)、费用不足置灰,以及可实时调节的布局...
Definition Animation.h:25
One camera-centered, world-space atmosphere clipmap level.
Participating-media coefficients stored in one froxel.
Local light injected into participating media.
glm::vec4 lightColor
glm::mat4 invViewProj