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";
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";
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));
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";
281 std::mt19937 rng(
params.getSeed());
286 std::mt19937 wood(
params.getSeed() ^ 0x51ed270bu);
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);
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) {
316 std::clamp(branchStart +
float(j) * (0.65f - branchStart) /
float(std::max(1, branches - 1)) +
317 randomRange(wood, -0.035f, 0.035f),
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),
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});
346 std::vector<FoliageLobe> foliageLobes;
347 foliageLobes.reserve(
size_t(blobs));
348 if (leafMode !=
"none") {
349 if (!leafRuns.empty()) {
354 for (
const auto &
run : leafRuns) {
355 for (
int site = 0; site < 3; ++site) {
357 run.start + (0.95f -
run.start) * (
float(site) + randomRange(rng, 0.15f, 0.85f)) / 3.f;
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);
364 {
center, {
r * 1.25f, ry,
r * 0.8f}, std::atan2(-tangent.z, tangent.x) * 180.f / kPi});
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;
378 if (crowded)
continue;
379 foliageLobes.push_back(candidate);
380 if (
int(foliageLobes.size()) >= blobs)
break;
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}});
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);
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];
412 const float scale = std::max({lobe.radius.x, lobe.radius.y, lobe.radius.z});
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);
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";
428 for (
int v =
first;
v <
out.getVertexCount(); ++
v) {
433 const V3
normal = norm(add(mul(
local, 1.f - canopyBlend), mul(envelope, canopyBlend)));
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));
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()));
462 error =
"mesh.bush: generated an empty mesh";