载入中...
搜索中...
未找到
BushMesh.cpp
浏览该文件的文档.
3
4#include <algorithm>
5#include <cmath>
6#include <cstdint>
7#include <random>
8#include <utility>
9#include <vector>
10
11namespace eve::procgen {
12namespace {
13
14constexpr float kPi = 3.14159265358979323846f;
15
16// tex.foliage atlas layout:
17// u in [0, 0.22] brown bark for twigs (same look as tree trunks),
18// u in [0.24, 0.50] opaque leafy fill for ellipsoid blobs,
19// u in [0.52, 1] 2×3 = 6 leaf-card panels (each 8–16 blue-noise ovate stamps).
20constexpr float kBarkUMin = 0.00f;
21constexpr float kBarkUMax = 0.22f;
22constexpr float kBlobUMin = 0.24f;
23constexpr float kBlobUMax = 0.50f;
24constexpr float kCardAtlasU0 = 0.52f;
25constexpr float kCardAtlasU1 = 1.00f;
26constexpr int kLeafCardCols = 2;
27constexpr int kLeafCardRows = 3;
28constexpr int kLeafCardPanels = kLeafCardCols * kLeafCardRows;
29
30struct V3 {
31 float x = 0.f, y = 0.f, z = 0.f;
32};
33
34struct FoliageLobe {
37 float yaw = 0.f;
38};
39
40V3 add(V3 a, V3 b) { return {a.x + b.x, a.y + b.y, a.z + b.z}; }
41V3 sub(V3 a, V3 b) { return {a.x - b.x, a.y - b.y, a.z - b.z}; }
42V3 mul(V3 a, float s) { return {a.x * s, a.y * s, a.z * s}; }
43float dot(V3 a, V3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
44V3 cross(V3 a, V3 b) { return {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x}; }
45V3 norm(V3 a) {
46 const float n = std::sqrt(std::max(1e-12f, dot(a, a)));
47 return mul(a, 1.f / n);
48}
49
50float randomRange(std::mt19937 &rng, float lo, float hi) { return std::uniform_real_distribution<float>(lo, hi)(rng); }
51
52float random01(std::mt19937 &rng) { return randomRange(rng, 0.f, 1.f); }
53
54void basisFor(V3 axis, V3 &right, V3 &forward) {
55 axis = norm(axis);
56 const V3 helper = std::fabs(axis.y) < 0.92f ? V3{0.f, 1.f, 0.f} : V3{1.f, 0.f, 0.f};
57 right = norm(cross(helper, axis));
58 forward = norm(cross(axis, right));
59}
60
61// Closed ellipsoid. The per-vertex normal is the analytic ellipsoid normal so the
62// bush shades smoothly under directional light. UVs fall inside [uMin, uMax].
63void addEllipsoidBlob(MeshBuild &out, V3 c, V3 r, int rings, int sides, float uMin, float uMax,
64 float phase = 0.f, float irregularity = 0.55f) {
65 const uint32_t base = uint32_t(out.getVertexCount());
66 for (int y = 0; y <= rings; ++y) {
67 const float v = float(y) / float(rings);
68 const float phi = v * kPi;
69 for (int x = 0; x < sides; ++x) {
70 const float u = float(x) / float(sides);
71 const float theta = u * 2.f * kPi;
72 const V3 n = {std::sin(phi) * std::cos(theta), std::cos(phi), std::sin(phi) * std::sin(theta)};
73 // Two low-frequency harmonics break the unmistakable primitive-sphere
74 // silhouette without producing noisy, faceted normals. Keep both poles
75 // fixed so neighbouring lobes still close cleanly.
76 const float equator = std::sin(phi);
77 const float swell = 1.f + equator * irregularity *
78 (0.14f * std::sin(theta * 3.f + phase) +
79 0.065f * std::sin(theta * 5.f - phase * 0.7f));
80 const V3 p = {c.x + n.x * r.x * swell, c.y + n.y * r.y, c.z + n.z * r.z * swell};
81 // Ellipsoid normal = N/R divided by its length (finite since radii > 0).
82 const V3 normal = norm({n.x / r.x, n.y / r.y, n.z / r.z});
83 out.addVertex(p.x, p.y, p.z, normal.x, normal.y, normal.z, uMin + u * (uMax - uMin), v);
84 }
85 }
86 for (int y = 0; y < rings; ++y) {
87 for (int x = 0; x < sides; ++x) {
88 const int nx = (x + 1) % sides;
89 const uint32_t a = base + uint32_t(y * sides + x);
90 const uint32_t b = base + uint32_t(y * sides + nx);
91 const uint32_t c = base + uint32_t((y + 1) * sides + x);
92 const uint32_t d = base + uint32_t((y + 1) * sides + nx);
93 out.addTriangle(a, b, c);
94 out.addTriangle(b, d, c);
95 }
96 }
97}
98
99struct Shoot {
100 std::vector<V3> centers;
102};
103
104V3 shootPoint(const Shoot &shoot, float t) {
105 const float at = std::clamp(t, 0.f, 1.f) * float(shoot.centers.size() - 1);
106 const int edge = std::min(int(at), int(shoot.centers.size()) - 2);
107 return add(shoot.centers[size_t(edge)],
108 mul(sub(shoot.centers[size_t(edge + 1)], shoot.centers[size_t(edge)]), at - float(edge)));
109}
110
111float shootRadius(const Shoot &shoot, float t) {
112 return shoot.radius + (shoot.tipRadius - shoot.radius) * std::pow(t, 0.85f);
113}
114
115// Continuous transported-frame sweep, shared rings, seam-safe arc-length UVs.
116// Each child starts at a sampled parent centre with a smaller local radius.
117Shoot addShoot(MeshBuild &out, V3 start, V3 end, V3 departure, float r0, float r1, int segments, int sides, float curve,
118 float phase) {
119 Shoot shoot{{}, r0, r1};
120 const float length = std::sqrt(dot(sub(end, start), sub(end, start)));
121 const V3 control = add(start, mul(norm(departure), length * (0.28f + curve * 0.6f)));
122 for (int i = 0; i <= segments; ++i) {
123 const float t = float(i) / float(segments), q = 1.f - t;
124 shoot.centers.push_back(add(add(mul(start, q * q), mul(control, 2.f * q * t)), mul(end, t * t)));
125 }
126 const int stride = sides + 1;
127 std::vector<V3> positions(size_t((segments + 1) * stride));
128 std::vector<float> arc(size_t(segments + 1), 0.f);
129 V3 right{}, forward{};
130 for (int i = 0; i <= segments; ++i) {
131 const V3 before = shoot.centers[size_t(std::max(0, i - 1))];
132 const V3 after = shoot.centers[size_t(std::min(segments, i + 1))];
133 const V3 axis = norm(sub(after, before));
134 const V3 transported = sub(right, mul(axis, dot(right, axis)));
135 if (dot(transported, transported) < 1e-8f)
136 basisFor(axis, right, forward);
137 else {
138 right = norm(transported);
139 forward = norm(cross(axis, right));
140 }
141 if (i > 0) {
142 const V3 delta = sub(shoot.centers[size_t(i)], shoot.centers[size_t(i - 1)]);
143 arc[size_t(i)] = arc[size_t(i - 1)] + std::sqrt(dot(delta, delta));
144 }
145 const float t = float(i) / float(segments);
146 for (int j = 0; j <= sides; ++j) {
147 const float angle = float(j % sides) / float(sides) * 2.f * kPi;
148 const V3 radial = add(mul(right, std::cos(angle)), mul(forward, std::sin(angle)));
149 const float relief = 1.f + 0.045f * std::sin(angle * 3.f + phase + t * 5.f) +
150 0.025f * std::cos(angle * 5.f - phase + t * 8.f);
151 const float collar = 1.f + 0.16f * std::exp(-t * 16.f);
152 positions[size_t(i * stride + j)] =
153 add(shoot.centers[size_t(i)], mul(radial, shootRadius(shoot, t) * relief * collar));
154 }
155 }
156 const uint32_t base = uint32_t(out.getVertexCount());
157 for (int i = 0; i <= segments; ++i) {
158 for (int j = 0; j <= sides; ++j) {
159 const int col = j % sides;
160 const V3 around = sub(positions[size_t(i * stride + (col + 1) % sides)],
161 positions[size_t(i * stride + (col + sides - 1) % sides)]);
162 const V3 along = sub(positions[size_t(std::min(segments, i + 1) * stride + col)],
163 positions[size_t(std::max(0, i - 1) * stride + col)]);
164 const V3 n = norm(cross(norm(around), norm(along)));
165 const V3 p = positions[size_t(i * stride + j)];
166 out.addVertex(p.x, p.y, p.z, n.x, n.y, n.z,
167 kBarkUMin + 0.003f + float(j) / float(sides) * (kBarkUMax - kBarkUMin - 0.006f),
168 arc[size_t(i)] / std::max(arc.back(), 1e-5f));
169 }
170 }
171 for (int i = 0; i < segments; ++i) {
172 for (int j = 0; j < sides; ++j) {
173 const uint32_t a = base + uint32_t(i * stride + j), b = a + 1;
174 const uint32_t c = a + uint32_t(stride), d = c + 1;
175 out.addTriangle(a, b, c);
176 out.addTriangle(b, d, c);
177 }
178 }
179 // End caps use separate axial normals; the side rings stay smooth.
180 for (int cap = 0; cap < 2; ++cap) {
181 const int ring = cap ? segments : 0;
182 const V3 n = norm(cap ? sub(shoot.centers.back(), shoot.centers[size_t(segments - 1)])
183 : sub(shoot.centers[0], shoot.centers[1]));
184 const V3 center = shoot.centers[size_t(ring)];
185 const uint32_t first = uint32_t(out.getVertexCount());
186 out.addVertex(center.x, center.y, center.z, n.x, n.y, n.z, 0.11f, float(cap));
187 for (int j = 0; j < sides; ++j) {
188 const V3 p = positions[size_t(ring * stride + j)];
189 out.addVertex(p.x, p.y, p.z, n.x, n.y, n.z, 0.11f, float(cap));
190 }
191 for (int j = 0; j < sides; ++j) {
192 const uint32_t a = first + 1 + uint32_t(j), b = first + 1 + uint32_t((j + 1) % sides);
193 if (cap)
194 out.addTriangle(first, a, b);
195 else
196 out.addTriangle(first, b, a);
197 }
198 }
199 return shoot;
200}
201
202// Leaf card: transparent quad sampling one of six atlas panels. Each panel holds
203// 8–16 solid ovate leaves placed with blue-noise spacing + random rotation.
204void addLeafCard(MeshBuild &out, std::mt19937 &rng, V3 c, V3 direction, float size) {
205 V3 right, up;
206 basisFor(norm(direction), right, up);
207 const float twist = randomRange(rng, 0.f, 2.f * kPi);
208 right = add(mul(right, std::cos(twist)), mul(up, std::sin(twist)));
209 up = norm(cross(norm(direction), right));
210 const V3 normal = norm(cross(right, up));
211 // Quad card 25% larger than the base leafSize.
212 const float card = size * 1.25f;
213 const float halfW = card * randomRange(rng, 0.55f, 0.72f);
214 const float halfH = card * randomRange(rng, 0.55f, 0.72f);
215 const V3 r = mul(right, halfW);
216 const V3 h = mul(up, halfH);
217 const V3 center = add(c, mul(normal, size * 0.04f));
218 const V3 points[4] = {sub(sub(center, r), h), add(sub(center, h), r), add(add(center, r), h),
219 add(sub(center, r), h)};
220
221 // Randomly assign one of the six pre-baked leaf-card panels.
222 const int panel = int(randomRange(rng, 0.f, float(kLeafCardPanels))) % kLeafCardPanels;
223 const int col = panel % kLeafCardCols;
224 const int row = panel / kLeafCardCols;
225 const float inset = 0.04f;
226 const float cellWU = (kCardAtlasU1 - kCardAtlasU0) / float(kLeafCardCols);
227 const float cellWV = 1.f / float(kLeafCardRows);
228 const float u0 = kCardAtlasU0 + (float(col) + inset) * cellWU;
229 const float u1 = kCardAtlasU0 + (float(col) + 1.f - inset) * cellWU;
230 const float v0 = (float(row) + inset) * cellWV;
231 const float v1 = (float(row) + 1.f - inset) * cellWV;
232 const bool flipU = random01(rng) > 0.5f;
233 const bool flipV = random01(rng) > 0.5f;
234 const float uv[4][2] = {
235 {flipU ? u1 : u0, flipV ? v1 : v0},
236 {flipU ? u0 : u1, flipV ? v1 : v0},
237 {flipU ? u0 : u1, flipV ? v0 : v1},
238 {flipU ? u1 : u0, flipV ? v0 : v1},
239 };
240 const uint32_t base = uint32_t(out.getVertexCount());
241 for (int i = 0; i < 4; ++i) {
242 out.addVertex(points[i].x, points[i].y, points[i].z, normal.x, normal.y, normal.z, uv[i][0],
243 uv[i][1]);
244 }
245 out.addTriangle(base, base + 1, base + 2);
246 out.addTriangle(base, base + 2, base + 3);
247 out.addTriangle(base + 2, base + 1, base);
248 out.addTriangle(base + 3, base + 2, base);
249}
250
251} // namespace
252
253bool generateBushMesh(const Params &params, MeshBuild &out, std::string &error) {
254 const std::string style = params.getString("style", "mound");
255 const std::string leafMode = params.getString("leafMode", "mixed");
256 if (style != "mound" && style != "sphere") {
257 error = "mesh.bush: style must be mound|sphere";
258 return false;
259 }
260 if (leafMode != "clusters" && leafMode != "blobs" && leafMode != "cards" && leafMode != "mixed" &&
261 leafMode != "none") {
262 error = "mesh.bush: leafMode must be clusters|blobs|cards|mixed|none";
263 return false;
264 }
265
266 const float height = std::max(0.3f, params.getFloat("height", 1.4f));
267 const float width = std::max(0.4f, params.getFloat("width", 2.2f));
268 const float halfW = width * 0.5f;
269 const int blobs = std::clamp(params.getInt("blobs", 24), 1, 96);
270 const int rings = std::clamp(params.getInt("rings", 3), 2, 10);
271 const int sides = std::clamp(params.getInt("radialSegments", 7), 4, 24);
272 const float density = std::clamp(params.getFloat("leafDensity", 0.62f), 0.f, 1.f);
273 const float leafSize = std::max(0.02f, params.getFloat("leafSize", height * 0.16f));
274 const float lobeScale = std::clamp(params.getFloat("lobeScale", 0.68f), 0.35f, 1.25f);
275 const float irregularity = std::clamp(params.getFloat("irregularity", 0.62f), 0.f, 1.f);
276 const int twigs = std::clamp(params.getInt("twigs", 4), 0, 16);
277 const float twigLen = std::max(0.05f, params.getFloat("twigLength", height * 0.30f));
278 const float twigScale = std::clamp(twigLen / (height * 0.30f), 0.35f, 1.65f);
279 const bool sphere = style == "sphere";
280
281 std::mt19937 rng(params.getSeed());
282 out.clear();
283
284 // Basal shoots use their own deterministic RNG. Leaf-only controls do not
285 // change woody geometry, and each terminal anchor is reached by a shoot.
286 std::mt19937 wood(params.getSeed() ^ 0x51ed270bu);
287 struct LeafRun {
288 Shoot shoot;
289 float start;
290 float scale;
291 };
292 std::vector<LeafRun> leafRuns;
293 const float branchR = std::clamp(params.getFloat("twigRadius", width * 0.012f), 0.001f, width * 0.08f);
294 const float spread = std::clamp(params.getFloat("stemSpread", 0.22f), 0.02f, 0.45f);
295 const float branchStart = std::clamp(params.getFloat("branchingStart", 0.14f), 0.08f, 0.55f);
296 const float curve = std::clamp(params.getFloat("branchCurve", 0.3f), 0.f, 0.6f);
297 const float falloff = std::clamp(params.getFloat("branchRadiusFalloff", 0.5f), 0.3f, 0.7f);
298 const int levels = std::clamp(params.getInt("branchLevels", 3), 1, 3);
299 const int segments = std::clamp(params.getInt("branchSegments", 12), 4, 24);
300 const int branchCount = std::clamp(params.getInt("branchCount", 4), 2, 6);
301 const float foliageStart = std::clamp(params.getFloat("foliageStart", 0.10f), 0.05f, 0.5f);
302 for (int i = 0; i < twigs; ++i) {
303 const float angle = float(i) * 2.399963f + randomRange(wood, -0.35f, 0.35f);
304 const float rootRadius = halfW * spread * std::sqrt(random01(wood));
305 const V3 root{std::cos(angle) * rootRadius, 0.f, std::sin(angle) * rootRadius};
306 const float reach = halfW * (i == 0 ? 0.06f : randomRange(wood, 0.20f, 0.60f)) * twigScale;
307 const V3 tip{std::cos(angle) * reach, height * randomRange(wood, 0.48f, 0.84f) * twigScale,
308 std::sin(angle) * reach};
309 const float r0 = branchR * randomRange(wood, 0.8f, 1.3f);
310 const Shoot main = addShoot(out, root, tip, {std::cos(angle) * 0.3f, 1.f, std::sin(angle) * 0.3f}, r0,
311 r0 * 0.12f, segments, sides, curve, angle);
312 leafRuns.push_back({main, 0.36f, 1.f});
313 const int branches = levels < 2 ? 0 : branchCount - int(random01(wood) * 1.999f);
314 for (int j = 0; j < branches; ++j) {
315 const float at =
316 std::clamp(branchStart + float(j) * (0.65f - branchStart) / float(std::max(1, branches - 1)) +
317 randomRange(wood, -0.035f, 0.035f),
318 0.08f, 0.75f);
319 const V3 base = shootPoint(main, at);
320 const V3 tangent = norm(sub(shootPoint(main, at + 0.03f), shootPoint(main, at)));
321 const float yaw = angle + (j & 1 ? -1.f : 1.f) * randomRange(wood, 0.5f, 1.15f);
322 const float reachSide = halfW * randomRange(wood, 0.23f, 0.42f) * (1.f - at * 0.35f) * twigScale;
323 const V3 end{base.x + std::cos(yaw) * reachSide,
324 base.y + height * randomRange(wood, 0.12f, 0.28f) * (1.f - at * 0.45f) * twigScale,
325 base.z + std::sin(yaw) * reachSide};
326 const float childR = shootRadius(main, at) * falloff;
327 const Shoot child = addShoot(out, base, end, tangent, childR, childR * 0.13f, std::max(4, segments * 2 / 3),
328 std::max(4, sides - 2), curve, yaw);
329 leafRuns.push_back({child, 0.20f, 0.85f});
330 if (levels < 3) continue;
331 for (int k = 0; k < 3; ++k) {
332 const float t = 0.32f + float(k) * 0.22f + randomRange(wood, -0.035f, 0.035f);
333 const V3 node = shootPoint(child, t);
334 const float azimuth = yaw + (k ? -0.8f : 0.8f);
335 const float shootLength = halfW * randomRange(wood, 0.09f, 0.22f);
336 const V3 bud{node.x + std::cos(azimuth) * shootLength,
337 node.y + height * randomRange(wood, 0.06f, 0.18f),
338 node.z + std::sin(azimuth) * shootLength};
339 const V3 dir = sub(shootPoint(child, t + 0.04f), node);
340 const float fineR = shootRadius(child, t) * falloff;
341 Shoot fine = addShoot(out, node, bud, dir, fineR, fineR * 0.1f, 4, 4, curve, azimuth);
342 leafRuns.push_back({std::move(fine), 0.15f, 0.7f});
343 }
344 }
345 }
346 std::vector<FoliageLobe> foliageLobes;
347 foliageLobes.reserve(size_t(blobs));
348 if (leafMode != "none") {
349 if (!leafRuns.empty()) {
350 // Candidate sites follow the leafy portions of real shoots, including
351 // mid-sections. Small overlapping patches form leaf bands rather than
352 // one large ball at each endpoint. The budget never changes the wood.
353 std::vector<FoliageLobe> candidates;
354 for (const auto &run : leafRuns) {
355 for (int site = 0; site < 3; ++site) {
356 const float t =
357 run.start + (0.95f - run.start) * (float(site) + randomRange(rng, 0.15f, 0.85f)) / 3.f;
358 const V3 center = shootPoint(run.shoot, t);
359 if (center.y < height * foliageStart) continue;
360 const V3 tangent = sub(shootPoint(run.shoot, std::min(1.f, t + 0.03f)), center);
361 const float r = halfW * 0.19f * lobeScale * run.scale * randomRange(rng, 0.85f, 1.15f);
362 const float ry = std::min(r * 0.85f, height * 0.15f * lobeScale);
363 candidates.push_back(
364 {center, {r * 1.25f, ry, r * 0.8f}, std::atan2(-tangent.z, tangent.x) * 180.f / kPi});
365 }
366 }
367 std::shuffle(candidates.begin(), candidates.end(), rng);
368 for (const auto &candidate : candidates) {
369 bool crowded = false;
370 for (const auto &placed : foliageLobes) {
371 const V3 d = sub(candidate.center, placed.center);
372 const float spacing = std::min(candidate.radius.x, placed.radius.x) * 0.55f;
373 if (dot(d, d) < spacing * spacing) {
374 crowded = true;
375 break;
376 }
377 }
378 if (crowded) continue;
379 foliageLobes.push_back(candidate);
380 if (int(foliageLobes.size()) >= blobs) break;
381 }
382 // An explicitly high foliageStart may exclude every shoot. Keep the
383 // woody plant observable and return no floating leaf-only fallback.
384 } else {
385 for (int i = 0; i < blobs; ++i) {
386 const float radial = halfW * 0.65f * std::sqrt(random01(rng));
387 const float theta = randomRange(rng, 0.f, 2.f * kPi);
388 const float heightFactor = 1.f - (radial / halfW) * (radial / halfW);
389 const float cy = height * (sphere ? 0.35f + 0.30f * random01(rng)
390 : 0.16f + heightFactor * randomRange(rng, 0.25f, 0.5f));
391 const float rx = halfW * 0.19f * lobeScale * randomRange(rng, 0.85f, 1.15f);
392 foliageLobes.push_back(
393 {{std::cos(theta) * radial, cy, std::sin(theta) * radial}, {rx, height * 0.15f * lobeScale, rx}});
394 }
395 }
396 }
397 if (leafMode == "blobs" || leafMode == "mixed") {
398 for (const auto &lobe : foliageLobes)
399 addEllipsoidBlob(out, lobe.center, lobe.radius, rings, sides, kBlobUMin, kBlobUMax,
400 randomRange(rng, 0.f, 2.f * kPi), irregularity);
401 }
402
403 // Reuse the tree's geometric leaves and rounded normals. Build a unit
404 // sphere, then transform both geometry and normals to each authored lobe.
405 if (leafMode == "clusters" && density > 0.f) {
406 const float canopyBlend = std::clamp(params.getFloat("canopyNormalBlend", 0.45f), 0.f, 0.8f);
407 for (size_t i = 0; i < foliageLobes.size(); ++i) {
408 const auto &lobe = foliageLobes[i];
410 desc.seed = params.getSeed() ^ (uint32_t(i + 1) * 2654435761u);
411 desc.radius = 1.f;
412 const float scale = std::max({lobe.radius.x, lobe.radius.y, lobe.radius.z});
413 desc.leafSize = leafSize / scale;
414 desc.leafSpacing = 0.8f * (1.45f - 0.75f * density);
415 desc.planes = std::clamp(params.getInt("clusterPlanes", 10), 1, 24);
416 desc.maxLeavesPerPlane = std::clamp(params.getInt("clusterLeaves", 48), 1, 256);
417 MeshBuild cluster;
418 addFoliageCluster(cluster, desc);
419 const int first = out.getVertexCount();
420 if (!out.appendTransformed(&cluster, lobe.center.x, lobe.center.y, lobe.center.z, lobe.yaw, lobe.radius.x,
421 lobe.radius.y, lobe.radius.z)) {
422 error = "mesh.bush: could not append leaf cluster";
423 return false;
424 }
425 // Blend local ellipsoid normals with the whole crown envelope so
426 // overlapping patches read as one shrub instead of separate balls.
427 auto &normals = out.normals();
428 for (int v = first; v < out.getVertexCount(); ++v) {
429 const V3 local{out.getNormalX(v), out.getNormalY(v), out.getNormalZ(v)};
430 const V3 envelope = norm({out.getPositionX(v) / (halfW * halfW),
431 (out.getPositionY(v) - height * 0.42f) / (height * height * 0.25f),
432 out.getPositionZ(v) / (halfW * halfW)});
433 const V3 normal = norm(add(mul(local, 1.f - canopyBlend), mul(envelope, canopyBlend)));
434 normals[size_t(v) * 3] = normal.x;
435 normals[size_t(v) * 3 + 1] = normal.y;
436 normals[size_t(v) * 3 + 2] = normal.z;
437 }
438 }
439 }
440
441 // Leaf cards across the canopy (+50% count vs prior 14× density packing).
442 if ((leafMode == "cards" || leafMode == "mixed") && !foliageLobes.empty() && density > 0.f) {
443 const int cards = std::max(1, int(std::round(float(blobs) * 21.f * density)));
444 for (int i = 0; i < cards; ++i) {
445 const FoliageLobe &lobe = foliageLobes[size_t(i) % foliageLobes.size()];
446 const float theta = randomRange(rng, 0.f, 2.f * kPi);
447 const float ny = randomRange(rng, -0.45f, 1.f);
448 const float radial = std::sqrt(std::max(0.f, 1.f - ny * ny));
449 const V3 face = norm({std::cos(theta) * radial, ny, std::sin(theta) * radial});
450 const V3 c{lobe.center.x + face.x * lobe.radius.x * 1.05f,
451 lobe.center.y + face.y * lobe.radius.y * 1.05f,
452 lobe.center.z + face.z * lobe.radius.z * 1.05f};
453 addLeafCard(out, rng, c, face, leafSize * randomRange(rng, 0.85f, 1.35f));
454 }
455 }
456
457 out.setMeta("recipe", "mesh.bush");
458 out.setMeta("style", style);
459 out.setMeta("leafMode", leafMode);
460 out.setMeta("seed", std::to_string(params.getSeed()));
461 if (out.empty()) {
462 error = "mesh.bush: generated an empty mesh";
463 return false;
464 }
465 return true;
466}
467
468} // namespace eve::procgen
double z
double y
double x
Duration start
int tip
Definition AnimSmr.cpp:121
int root
Definition AnimSmr.cpp:119
const std::string & s
float halfW
float halfH
std::vector< V3 > centers
Definition BushMesh.cpp:100
float yaw
Definition BushMesh.cpp:37
float tipRadius
Definition BushMesh.cpp:101
float twist
float cy
Definition CardTypes.cpp:37
float uv
glm::vec4 p[6]
scene::NodeDesc desc
float density
float u
Definition Grass.cpp:233
glm::vec3 n
Definition Grass.cpp:63
std::array< double, 10 > q
double r
std::vector< float > normals
std::vector< float > positions
float v
HexVec3 up
HexVec3 right
std::int32_t c
std::int32_t first
int h
std::uint32_t height
std::uint32_t width
float length
JointPoint2D axis
std::string local
std::array< float, 3 > scale
std::vector< RecipeParam > params
MeleePoint3 b
Definition MeleeHit.cpp:41
double radius
Definition MeleeHit.cpp:42
MeleePoint3 a
Definition MeleeHit.cpp:40
Texture * normal
glm::dvec2 center
std::weak_ptr< Run > run
Definition OnnxGpgpu.cpp:25
std::string error
Definition Package.cpp:174
std::shared_ptr< const std::vector< glm::vec2 > > points
float d
float t
const RoadNode * node
RoadLaneDirection direction
ScriptStructure out
size_t end
std::map< std::array< int, 4 >, std::vector< const Subject * > > candidates
Definition Sensing.cpp:32
float phase
bool placed
Heightmap curve
int spacing
int segments
Definition TreeMesh.cpp:281
float relief
Definition TreeMesh.cpp:274
int sides
Definition TreeMesh.cpp:459
float collar
Definition TreeMesh.cpp:275
float size
Definition TreeMesh.cpp:158
float foliageStart
Definition TreeMesh.cpp:441
V3 dir
Definition TreeMesh.cpp:151
std::size_t at
std::vector< double > phi
V forward
CPU triangle mesh from procedural mesh recipes (e.g. marching cubes). Positions/normals are xyz-packe...
Definition MeshBuild.h:20
Owning, typed generation parameters.
Definition Params.h:27
int main(int argc, char **argv)
Definition main.cpp:46
ForceVector add(ForceVector a, ForceVector b)
Definition ForceMath.h:13
double dot(ForceVector a, ForceVector b)
Definition ForceMath.h:14
Vec3 sub(Vec3 a, Vec3 b)
Vec3 mul(Vec3 a, float scale)
V3 cross(V3 a, V3 b)
Module-private road bake implementation helper.
Definition RoadBake.cpp:29
bool generateBushMesh(const Params &params, MeshBuild &out, std::string &error)
Build a deterministic procedural small bush. Registered as the mesh.bush recipe.
Definition BushMesh.cpp:253
int addFoliageCluster(MeshBuild &out, const FoliageClusterDesc &desc)
Append one blue-noise leaf cluster to a mesh.
WidgetDesc card(std::vector< WidgetDesc > children, std::string id)
Bordered surface container with editor-friendly padding.
Definition Widget.cpp:594
WidgetDesc row(std::vector< WidgetDesc > children, std::string id)
Horizontal elastic layout row.
Definition Widget.cpp:870
Recipe for one stylized leaf cluster ("foliage card bundle").
std::uint32_t seed
RNG stream seed; the same seed and inputs reproduce the same leaves.