载入中...
搜索中...
未找到
EverydayPatterns.cpp
浏览该文件的文档.
2#include <algorithm>
3#include <cmath>
4
5namespace eve::procgen::detail {
6namespace {
7constexpr float tau = 6.28318530718f;
8float noise(float u, float v, int x, int y, uint32_t seed) {
9 NoiseField n;
10 n.seed = seed;
11 n.periodX = x;
12 n.periodY = y;
13 return n.valueNoise(u * float(x), v * float(y));
14}
15float smooth(float lo, float hi, float x) {
16 const float t = std::clamp((x - lo) / (hi - lo), 0.f, 1.f);
17 return t * t * (3.f - 2.f * t);
18}
19struct Cell {
20 float distance, edge;
21};
22Cell cell(float u, float v, int count, uint32_t seed) {
23 NoiseField n;
24 n.seed = seed;
25 n.periodX = n.periodY = count;
26 const float x = u * float(count), y = v * float(count);
27 const int cx = int(std::floor(x)), cy = int(std::floor(y));
28 float first = 100.f, second = 100.f;
29 for (int dy = -1; dy <= 1; ++dy)
30 for (int dx = -1; dx <= 1; ++dx) {
31 const int ix = cx + dx, iy = cy + dy;
32 const float px = float(ix) + .15f + .7f * n.hash01(ix, iy);
33 NoiseField other = n;
34 other.seed ^= 0x9e3779b9u;
35 const float py = float(iy) + .15f + .7f * other.hash01(ix, iy);
36 const float d = (x - px) * (x - px) + (y - py) * (y - py);
37 if (d < first) {
38 second = first;
39 first = d;
40 } else
41 second = std::min(second, d);
42 }
43 return {std::sqrt(first), std::sqrt(second) - std::sqrt(first)};
44}
45} // namespace
46EverydaySample sampleEveryday(EverydayMaterial family, float u, float v, uint32_t seed, int repeats,
47 [[maybe_unused]] int bandwidth) {
48 const int fine = repeats * 48, medium = repeats * 12;
49 const float a = noise(u, v, 4, 4, seed), b = noise(u, v, medium, medium, seed + 13);
50 const float c = noise(u, v, fine, fine, seed + 71);
51 const float d = noise(u, v, std::max(2, fine / 2), std::max(2, fine / 2), seed + 133);
52 const float micro = .6f * c + .4f * d;
53 switch (family) {
55 const float x = std::sin(3.14159265f * (u - .25f));
56 const float y = std::sin(3.14159265f * (v - .5f)) * .65f;
57 const float radius = std::sqrt(x * x + y * y + .03f);
58 const float warp = .9f * (noise(u, v, 4, 6, seed + 3) - .5f) + .16f * (noise(u, v, 12, 8, seed + 4) - .5f);
59 const float phase = radius * float(repeats * 9) + warp * 1.7f;
60 NoiseField growth;
61 growth.seed = seed + 55;
62 growth.periodY = 6;
63 const int ringIndex = int(std::floor(phase));
64 const float position = phase - float(ringIndex);
65 const float width = .04f + .10f * growth.hash01(ringIndex, 0);
66 const float latewood = 1.f - smooth(width, width + .10f, position);
67 // Vessels follow the same deformed growth coordinate as the annual rings.
68 const float vessel = growth.valueNoise(phase * 9.f, v * 6.f);
69 const float pore = std::pow(std::max(0.f, 1.f - vessel * 2.1f), 3.f);
70 const float ringTone = growth.valueNoise(phase, v * 6.f);
71 return {.68f - .14f * latewood - .25f * pore + .35f * (ringTone - .5f) + .10f * (a - .5f),
72 .5f - .008f * latewood - .32f * pore + .012f * (micro - .5f), .36f + .42f * pore + .12f * latewood};
73 }
75 const int threads = std::max(2, (std::min(repeats * 10, std::max(2, fine / 2)) / 2) * 2);
76 const float x = u * float(threads), y = v * float(threads);
77 const float sx = .5f + .5f * std::cos(tau * x), sy = .5f + .5f * std::cos(tau * y);
78 // Alternating over/under yarns, rounded crowns and small twisted fibers.
79 const float over = .5f + .5f * std::cos(3.14159265f * x) * std::cos(3.14159265f * y);
80 const float warp = std::pow(sx, .6f) * (.32f + .68f * over);
81 const float weft = std::pow(sy, .6f) * (1.f - .68f * over);
82 const float crown = std::max(warp, weft);
83 const float fiber = noise(u, v, fine, fine, seed + 19);
84 return {.38f + .33f * crown + .12f * (fiber - .5f), .5f + .46f * (crown - .5f) + .035f * (fiber - .5f),
85 .65f + .30f * (1.f - crown)};
86 }
89 const bool aluminum = family == EverydayMaterial::Aluminum;
90 const int lines = repeats * (aluminum ? 90 : 70);
91 const uint32_t grainSeed = seed + (aluminum ? 207u : 43u);
92 const float groove =
93 .6f * noise(u, v, 8, lines, grainSeed) + .4f * noise(u, v, 24, lines / 2, grainSeed + 7);
94 const float scratch = std::pow(std::max(0.f, 1.f - groove * 2.2f), 3.f);
95 return {.5f + (aluminum ? .025f : .045f) * (groove - .5f), .5f - (aluminum ? .045f : .075f) * scratch,
96 .28f + .40f * groove};
97 }
99 // Polished steel has broad clean reflections; brushed grooves belong to metal/Al.
100 return {.5f + .018f * (a - .5f), .5f + .0015f * (micro - .5f), .25f + .12f * b};
102 // One short randomly oriented cellulose fiber per jittered lattice cell.
103 const int count = std::min(std::max(3, repeats * 12), std::max(3, fine / 2));
104 NoiseField hashes;
105 hashes.seed = seed + 91;
106 hashes.periodX = hashes.periodY = count;
107 const float px = u * float(count), py = v * float(count);
108 const int cx = int(std::floor(px)), cy = int(std::floor(py));
109 float fibers = 0.f;
110 for (int j = -1; j <= 1; ++j)
111 for (int i = -1; i <= 1; ++i) {
112 const int ix = cx + i, iy = cy + j;
113 const float h = hashes.hash01(ix, iy), angle = tau * h;
114 const float dx = px - float(ix) - h, dy = py - float(iy) - hashes.hash01(ix + 19, iy + 31);
115 const float along = dx * std::cos(angle) + dy * std::sin(angle);
116 const float across = -dx * std::sin(angle) + dy * std::cos(angle);
117 const float fiber =
118 (1.f - smooth(.025f, .11f, std::abs(across))) * (1.f - smooth(.20f, .48f, std::abs(along)));
119 fibers = std::max(fibers, fiber);
120 }
121 return {.64f + .10f * (a - .5f) + .12f * (micro - .5f) - .18f * fibers,
122 .5f + .05f * (micro - .5f) + .035f * fibers, .65f + .22f * micro};
123 }
125 // Seeded flowing veins with finite, tapering forks. No closed noise contours.
127 n.seed = seed + 311;
128 const float flow = v + .08f * (noise(u, v, 8, 8, seed + 19) - .5f);
129 const float across = u + v + .06f * (noise(u, v, 12, 12, seed + 20) - .5f);
130 const int veins = std::clamp(repeats, 2, 12);
131 float core = 0.f, halo = 0.f, forks = 0.f;
132 for (int i = 0; i < veins; ++i) {
133 const float phase = tau * n.hash01(i, 1);
134 const float center = (float(i) + .2f + .6f * n.hash01(i, 2)) / float(veins) +
135 .15f * std::sin(tau * flow + phase) +
136 .045f * std::sin(tau * 3.f * flow + phase * 1.7f);
137 const float width = .004f + .008f * noise(u, v, 8, 8, seed + uint32_t(i));
138 const float distance = std::abs((across - center) - std::floor(across - center + .5f));
139 const float strength = .65f + .35f * n.hash01(i, 3);
140 core = std::max(core, (1.f - smooth(width, width + .006f, distance)) * strength);
141 halo = std::max(halo, 1.f - smooth(width, .045f, distance));
142 for (int j = 0; j < 3; ++j) {
143 const float start = n.hash01(i, j + 10);
144 const float t = (flow - start) - std::floor(flow - start);
145 const float length = .18f + .24f * n.hash01(i, j + 20);
146 const float support = 1.f - smooth(length * .7f, length, t);
147 const float direction = n.hash01(i, j + 30) > .5f ? 1.f : -1.f;
148 const float branch = center + direction * t * (.35f + .45f * n.hash01(i, j + 40)) +
149 .012f * std::sin(tau * 5.f * t + phase) * smooth(0.f, .06f, t);
150 const float d = std::abs((across - branch) - std::floor(across - branch + .5f));
151 forks = std::max(forks, (1.f - smooth(.0015f, .005f, d)) * support);
152 }
153 }
154 const float cloud = .6f * noise(u, v, repeats * 3, repeats * 3, seed + 45) +
155 .4f * noise(u, v, repeats * 7, repeats * 7, seed + 46);
156 return {.68f - .30f * core - .16f * forks - .08f * halo - .32f * cloud, .5f + .003f * (micro - .5f),
157 .25f + .12f * core};
158 }
160 // Aggregate and mortar vary above and below the mean; pores are sparse accents.
161 const float aggregate = .6f * noise(u, v, repeats * 10, repeats * 10, seed + 51) +
162 .4f * noise(u, v, repeats * 22, repeats * 22, seed + 52);
163 const Cell pores = cell(u, v, std::max(3, repeats * 3), seed + 7);
164 const float pore = (1.f - smooth(.025f, .12f, pores.distance)) * smooth(.50f, .78f, a);
165 return {.52f + .28f * (a - .5f) + .32f * (aggregate - .5f) - .20f * pore,
166 .5f + .65f * (aggregate - .5f) + .12f * (micro - .5f) - .10f * pore, .62f + .26f * aggregate};
167 }
169 const float grains = .6f * noise(u, v, repeats * 12, repeats * 12, seed + 53) +
170 .4f * noise(u, v, repeats * 24, repeats * 24, seed + 54);
171 const Cell crust = cell(u, v, std::max(3, repeats * 8), seed + 37);
172 const float raised = 1.f - smooth(.10f, .42f, crust.distance);
173 return {.5f + .24f * (a - .5f) + .34f * (grains - .5f) + .10f * raised,
174 .5f + .65f * (grains - .5f) + .22f * (raised - .3f), .65f + .30f * grains};
175 }
177 const float grain = .6f * noise(u, v, repeats * 16, repeats * 16, seed + 61) + .4f * micro;
178 return {.5f + .04f * (a - .5f) + .04f * (grain - .5f), .5f + .42f * (grain - .5f), .65f + .3f * grain};
179 }
181 return {.5f + .025f * (a - .5f), .5f + .032f * (b - .5f) + .012f * (micro - .5f), .35f + .25f * b};
183 const float peel = .65f * noise(u, v, repeats * 14, repeats * 14, seed + 81) +
184 .35f * noise(u, v, repeats * 28, repeats * 28, seed + 82);
185 return {.5f + .025f * (a - .5f), .5f + .42f * (peel - .5f) + .06f * (micro - .5f), .35f + .35f * peel};
186 }
188 return {.5f + .008f * (a - .5f), .5f + .0005f * (micro - .5f), .5f + .03f * micro};
189 }
190 return {};
191}
192} // namespace eve::procgen::detail
double y
double x
Duration start
float cx
Definition CardTypes.cpp:36
float cy
Definition CardTypes.cpp:37
float warp
float grain
float strength
float u
Definition Grass.cpp:233
glm::vec3 n
Definition Grass.cpp:63
float v
HexVec3 across
std::int32_t second
std::int32_t c
std::int32_t first
int h
std::vector< Colorf > px
std::uint32_t width
float length
std::array< float, 3 > position
MeleePoint3 b
Definition MeleeHit.cpp:41
double radius
Definition MeleeHit.cpp:42
MeleePoint3 a
Definition MeleeHit.cpp:40
float distance
glm::dvec2 center
glm::dvec3 dx
float pore
float d
float t
RoadLaneDirection direction
float dy
Definition Shadow.cpp:151
int count
float phase
Cell cell
uint32_t seed
Definition TreeMesh.cpp:276
std::vector< std::string_view > lines
@ Cell
A cell was removed; the out-parameter holds it.
EverydaySample sampleEveryday(EverydayMaterial family, float u, float v, uint32_t seed, int repeats, int bandwidth)
Pure seeded surface sampling; no state, IO, callbacks or retained borrows.
EverydayMaterial
Everyday surface families; no scene, GPU or registry state is retained.
Deterministic 2D noise helpers for pixel textures and masks.
Definition NoiseField.h:24
float hash01(int ix, int iy) const
Lattice hash mapped to [0, 1].
float valueNoise(float x, float y) const
Smooth value noise.
Internal pigment, physical relief and microfacet variation in a common periodic UV domain.