载入中...
搜索中...
未找到
CloudField.cpp
浏览该文件的文档.
2#include <algorithm>
3#include <cmath>
6
7// CloudField.cpp
8namespace eve::procgen {
9namespace {
10
11inline float smoothstepClamp(float e0, float e1, float x) {
12 if (e0 == e1) return x < e0 ? 0.f : 1.f;
13 const float t = std::clamp((x - e0) / (e1 - e0), 0.f, 1.f);
14 return t * t * (3.f - 2.f * t);
15}
16
17} // namespace
18
19void CloudField::setParams(const Params &params) { params_ = params; }
20
21void CloudField::setSeed(uint32_t seed) { params_.seed = seed; }
22void CloudField::setWorldScale(float v) { params_.worldScale = std::max(1.f, v); }
23void CloudField::setCoverage(float v) { params_.coverage = std::clamp(v, 0.f, 1.f); }
24void CloudField::setSoftness(float v) { params_.softness = std::clamp(v, 0.f, 1.f); }
25void CloudField::setDetail(float v) { params_.detail = std::clamp(v, 0.f, 1.f); }
26void CloudField::setWind(float speed, float angleRad) {
27 params_.windSpeed = std::max(0.f, speed);
28 params_.windAngle = angleRad;
29}
30void CloudField::setOctaves(int v) { params_.octaves = std::max(1, v); }
31void CloudField::setWarp(float v) { params_.warp = std::max(0.f, v); }
32void CloudField::setSeamless(bool v) { params_.seamless = v; }
33
34void CloudField::windVelocity(float &vx, float &vz) const {
35 vx = std::cos(params_.windAngle) * params_.windSpeed;
36 vz = std::sin(params_.windAngle) * params_.windSpeed;
37}
38
39float CloudField::coverageAt(float x, float z, float time) const {
40 float vx = 0.f, vz = 0.f;
42
43 const float cell = params_.worldScale / float(kLattice);
44 // Drift with the wind; sample in lattice units (seamless wrap at kLattice).
45 const float px = (x - vx * time) / cell;
46 const float pz = (z - vz * time) / cell;
47
48 NoiseField noise;
49 noise.seed = params_.seed;
50 if (params_.seamless) {
51 noise.periodX = kLattice;
52 noise.periodY = kLattice;
53 }
54
55 // iq / noisecreater nested domain warp on sample coordinates, then
56 // Perlin–Worley coverage (NoiseCreator Cloud / Perlin-Worley hybrid).
57 const int warpOct = std::max(2, params_.octaves - 1);
58 const float amp = params_.warp;
59 const float qx = noise.fbm(px, pz, warpOct, params_.lacunarity, params_.gain) - 0.5f;
60 const float qz = noise.fbm(px + 5.2f, pz + 1.3f, warpOct, params_.lacunarity, params_.gain) - 0.5f;
61 const float rx =
62 noise.fbm(px + amp * qx + 1.7f, pz + amp * qz + 9.2f, warpOct, params_.lacunarity, params_.gain) - 0.5f;
63 const float rz =
64 noise.fbm(px + amp * qx + 8.3f, pz + amp * qz + 2.8f, warpOct, params_.lacunarity, params_.gain) - 0.5f;
65 const float sx = px + amp * rx;
66 const float sz = pz + amp * rz;
67
68 const float base = noise.perlinWorley(sx, sz, params_.octaves);
69 float c = smoothstepClamp(params_.coverage - params_.softness, params_.coverage + params_.softness, base);
70
71 // High-frequency wisps erode dense regions for a textured top.
72 if (params_.detail > 0.f) {
73 const float d = noise.fbmRotated(sx * 3.f + 11.7f, sz * 3.f + 5.3f, 2, params_.lacunarity, params_.gain);
74 c = c * (1.f - params_.detail) + (c * d) * params_.detail;
75 }
76 return std::clamp(c, 0.f, 1.f);
77}
78
79void CloudField::sample(float *out, int width, int height, float time, float x0, float z0, float extent) const {
80 if (!out || width <= 0 || height <= 0) return;
81 for (int y = 0; y < height; ++y) {
82 const float z = z0 + extent * (float(y) / float(std::max(1, height - 1)));
83 for (int x = 0; x < width; ++x) {
84 const float px = x0 + extent * (float(x) / float(std::max(1, width - 1)));
85 out[size_t(y * width + x)] = coverageAt(px, z, time);
86 }
87 }
88}
89
90} // namespace eve::procgen
91
92// CloudShadow.cpp
93namespace eve::procgen {
94
95void CloudShadow::setParams(const Params &params) { params_ = params; }
96
97void CloudShadow::setSunDirection(float x, float y, float z) {
98 const float len = std::sqrt(x * x + y * y + z * z);
99 if (len < 1e-6f) return;
100 params_.sunDirX = x / len;
101 params_.sunDirY = y / len;
102 params_.sunDirZ = z / len;
103}
104void CloudShadow::setCloudAltitude(float v) { params_.cloudAltitude = std::max(0.f, v); }
105void CloudShadow::setStrength(float v) { params_.strength = std::clamp(v, 0.f, 1.f); }
106
107void CloudShadow::cloudOffset(float &ox, float &oz) const {
108 const float ly = std::max(params_.sunDirY, 1e-4f);
109 const float s = params_.cloudAltitude / ly;
110 ox = params_.sunDirX * s;
111 oz = params_.sunDirZ * s;
112}
113
114float CloudShadow::coverageAt(float x, float z, float time) const {
115 if (params_.sunDirY <= 0.f) return 0.f; // sun below horizon → no cloud shadows
116 float ox = 0.f, oz = 0.f;
117 cloudOffset(ox, oz);
118 return params_.field.coverageAt(x + ox, z + oz, time);
119}
120
121float CloudShadow::shadowFactorAt(float x, float z, float time) const {
122 const float c = coverageAt(x, z, time);
123 return 1.f - c * std::clamp(params_.strength, 0.f, 1.f);
124}
125
126void CloudShadow::sampleCoverage(float *out, int width, int height, float time, float x0, float z0,
127 float extent) const {
128 if (!out || width <= 0 || height <= 0) return;
129 for (int y = 0; y < height; ++y) {
130 const float z = z0 + extent * (float(y) / float(std::max(1, height - 1)));
131 for (int x = 0; x < width; ++x) {
132 const float px = x0 + extent * (float(x) / float(std::max(1, width - 1)));
133 out[size_t(y * width + x)] = coverageAt(px, z, time);
134 }
135 }
136}
137
138void CloudShadow::sampleFactor(float *out, int width, int height, float time, float x0, float z0, float extent) const {
139 if (!out || width <= 0 || height <= 0) return;
140 for (int y = 0; y < height; ++y) {
141 const float z = z0 + extent * (float(y) / float(std::max(1, height - 1)));
142 for (int x = 0; x < width; ++x) {
143 const float px = x0 + extent * (float(x) / float(std::max(1, width - 1)));
144 out[size_t(y * width + x)] = shadowFactorAt(px, z, time);
145 }
146 }
147}
148
149} // namespace eve::procgen
double z
double y
double x
const std::string & s
float v
std::int32_t c
std::vector< Colorf > px
std::uint32_t height
std::uint32_t width
std::vector< RecipeParam > params
float d
float t
ScriptStructure out
Cell cell
uint32_t seed
Definition TreeMesh.cpp:276
double ox
void setSeamless(bool seamless)
void setParams(const Params &params)
void setWorldScale(float worldScale)
void setWarp(float warp)
float coverageAt(float x, float z, float time) const
Cloud coverage at world (x, z) and time t, in [0,1].
void windVelocity(float &vx, float &vz) const
Drift vector (world units/sec) implied by windSpeed + windAngle.
void setDetail(float detail)
void setSoftness(float softness)
void setOctaves(int octaves)
void setSeed(uint32_t seed)
static constexpr int kLattice
Internal lattice resolution per tile (also the noise wrap period).
Definition CloudField.h:65
void setCoverage(float coverage)
void sample(float *out, int width, int height, float time, float x0, float z0, float extent) const
const Params & params() const
Definition CloudField.h:41
void setWind(float speed, float angleRad)
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:38
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)
Deterministic 2D noise helpers for pixel textures and masks.
Definition NoiseField.h:24
float fbm(float x, float y, int octaves=4, float lacunarity=2.f, float gain=0.5f) const
Value-noise fBm.
float perlinWorley(float x, float y, int octaves=3, float jitter=1.f) const
Perlin–Worley hybrid for cloud / soft cellular shapes. Result is worleyNoise * fbmPerlin (NoiseCreato...
float fbmRotated(float x, float y, int octaves=4, float lacunarity=2.f, float gain=0.5f) const
Rotated value-noise fBm (axial-bias reduction). Each octave applies a fixed 2D rotation like noisecre...