载入中...
搜索中...
未找到
CloudShadow.cpp
浏览该文件的文档.
2
3#include <algorithm>
4#include <cmath>
5
6namespace eve::procgen {
7
8void CloudShadow::setParams(const Params &params) { params_ = params; }
9
10void CloudShadow::setSunDirection(float x, float y, float z) {
11 const float len = std::sqrt(x * x + y * y + z * z);
12 if (len < 1e-6f) return;
13 params_.sunDirX = x / len;
14 params_.sunDirY = y / len;
15 params_.sunDirZ = z / len;
16}
17void CloudShadow::setCloudAltitude(float v) { params_.cloudAltitude = std::max(0.f, v); }
18void CloudShadow::setStrength(float v) { params_.strength = std::clamp(v, 0.f, 1.f); }
19
20void CloudShadow::cloudOffset(float &ox, float &oz) const {
21 const float ly = std::max(params_.sunDirY, 1e-4f);
22 const float s = params_.cloudAltitude / ly;
23 ox = params_.sunDirX * s;
24 oz = params_.sunDirZ * s;
25}
26
27float CloudShadow::coverageAt(float x, float z, float time) const {
28 if (params_.sunDirY <= 0.f) return 0.f; // sun below horizon → no cloud shadows
29 float ox = 0.f, oz = 0.f;
30 cloudOffset(ox, oz);
31 return params_.field.coverageAt(x + ox, z + oz, time);
32}
33
34float CloudShadow::shadowFactorAt(float x, float z, float time) const {
35 const float c = coverageAt(x, z, time);
36 return 1.f - c * std::clamp(params_.strength, 0.f, 1.f);
37}
38
39void CloudShadow::sampleCoverage(float *out, int width, int height, float time, float x0, float z0,
40 float extent) const {
41 if (!out || width <= 0 || height <= 0) return;
42 for (int y = 0; y < height; ++y) {
43 const float z = z0 + extent * (float(y) / float(height - 1));
44 for (int x = 0; x < width; ++x) {
45 const float px = x0 + extent * (float(x) / float(width - 1));
46 out[size_t(y * width + x)] = coverageAt(px, z, time);
47 }
48 }
49}
50
51void CloudShadow::sampleFactor(float *out, int width, int height, float time, float x0, float z0,
52 float extent) const {
53 if (!out || width <= 0 || height <= 0) return;
54 for (int y = 0; y < height; ++y) {
55 const float z = z0 + extent * (float(y) / float(height - 1));
56 for (int x = 0; x < width; ++x) {
57 const float px = x0 + extent * (float(x) / float(width - 1));
58 out[size_t(y * width + x)] = shadowFactorAt(px, z, time);
59 }
60 }
61}
62
63} // namespace eve::procgen
int y
Definition Grass.cpp:135
int z
Definition Grass.cpp:135
float height
Definition Grass.cpp:235
int x
Definition Grass.cpp:135
std::vector< Colorf > px
uint32_t c
int width
int v
uint32_t s
Definition Weather.cpp:28
float coverageAt(float x, float z, float time) const
Cloud coverage at world (x, z) and time t, in [0,1].
void sampleFactor(float *out, int width, int height, float time, float x0, float z0, float extent) const
Fill a buffer with shadow light-multiplier factors for a world region.
const Params & params() const
Definition CloudShadow.h:35
void setSunDirection(float x, float y, float z)
void setParams(const Params &params)
float coverageAt(float x, float z, float time) const
Cloud coverage projected onto ground (x, z) at time t, in [0,1].
void sampleCoverage(float *out, int width, int height, float time, float x0, float z0, float extent) const
Fill a buffer with projected coverage for a world region.
void cloudOffset(float &ox, float &oz) const
World-space cloud-point offset implied by sun + altitude.
float shadowFactorAt(float x, float z, float time) const
Light multiplier in [0,1]: 1 = fully lit, (1-strength) = fully shadowed.
void setCloudAltitude(float altitude)
void setStrength(float strength)