9inline float smoothstepClamp(
float e0,
float e1,
float x) {
10 if (e0 == e1)
return x < e0 ? 0.f : 1.f;
11 const float t = std::clamp((
x - e0) / (e1 - e0), 0.f, 1.f);
12 return t * t * (3.f - 2.f * t);
15inline float fade(
float t) {
return t * t * (3.f - 2.f * t); }
22 int wrap(
int i,
int period)
const {
23 if (period <= 0)
return i;
25 return m < 0 ?
m + period :
m;
28 float hash01(
int ix,
int iy)
const {
29 ix = wrap(ix, periodX);
30 iy = wrap(iy, periodY);
32 h ^= uint32_t(ix) * 374761393u;
33 h ^= uint32_t(iy) * 668265263u;
34 h = (
h ^ (
h >> 13)) * 1274126177u;
36 return float(
h & 0x00FFFFFFu) / float(0x00FFFFFFu);
39 float valueNoise(
float x,
float y)
const {
40 const int x0 = int(std::floor(
x));
41 const int y0 = int(std::floor(
y));
42 const float fx = fade(
x -
float(x0));
43 const float fy = fade(
y -
float(y0));
44 const float n00 = hash01(x0, y0);
45 const float n10 = hash01(x0 + 1, y0);
46 const float n01 = hash01(x0, y0 + 1);
47 const float n11 = hash01(x0 + 1, y0 + 1);
48 return n00 + (n10 - n00) * fx + (n01 - n00) * fy + (n00 - n10 - n01 + n11) * fx * fy;
51 float fbm(
float x,
float y,
int octaves,
float lacunarity,
float gain)
const {
52 octaves = std::max(1, octaves);
53 float sum = 0.f, amp = 1.f, norm = 0.f, freq = 1.f;
54 for (
int i = 0; i < octaves; ++i) {
55 sum += valueNoise(
x * freq,
y * freq) * amp;
60 return norm > 0.f ? sum / norm : 0.f;
87 float vx = 0.f, vz = 0.f;
92 const float px = (
x - vx * time) / cell;
93 const float pz = (
z - vz * time) / cell;
96 lat.seed = params_.
seed;
104 params_.
gain) - 0.5f;
105 const float wx =
px +
w * params_.
warp * 0.5f;
117 if (params_.
detail > 0.f) {
118 const float d = lat.fbm(wx * 3.f + 11.7f, wz * 3.f + 5.3f, 2, params_.
lacunarity,
122 return std::clamp(
c, 0.f, 1.f);
126 float extent)
const {
129 const float z = z0 + extent * (float(
y) / float(
height - 1));
131 const float px = x0 + extent * (float(
x) / float(
width - 1));
void setSeamless(bool seamless)
void setParams(const Params ¶ms)
void setWorldScale(float worldScale)
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).
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
void setWind(float speed, float angleRad)