15constexpr float kPi = 3.14159265358979323846f;
18 float x = 0.f,
y = 0.f,
z = 0.f;
21V3
add(V3
a, V3
b) {
return {
a.
x +
b.x,
a.y +
b.y,
a.z +
b.z}; }
22V3
sub(V3
a, V3
b) {
return {
a.
x -
b.x,
a.y -
b.y,
a.z -
b.z}; }
23V3
mul(V3
a,
float s) {
return {
a.
x *
s,
a.y *
s,
a.z *
s}; }
24float dot(V3
a, V3
b) {
return a.x *
b.x +
a.y *
b.y +
a.z *
b.z; }
26V3
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}; }
28 const float n = std::sqrt(std::max(1e-12f,
dot(
a,
a)));
29 return mul(
a, 1.f /
n);
32float randomRange(std::mt19937& rng,
float lo,
float hi) {
return std::uniform_real_distribution<float>(lo, hi)(rng); }
34float hash01(uint32_t
value) {
40 return float(
value & 0x00ffffffu) / float(0x01000000u);
45 const V3 helper = std::fabs(
axis.y) < 0.92f ? V3{0.f, 1.f, 0.f} : V3{1.f, 0.f, 0.f};
50void addTaperedCylinder(MeshBuild&
out, V3
a, V3
b,
float r0,
float r1,
int sides) {
54 const uint32_t base = uint32_t(
out.getVertexCount());
55 for (
int ring = 0; ring < 2; ++ring) {
57 const float radius = ring ? r1 : r0;
58 for (
int i = 0; i <=
sides; ++i) {
59 const float t = float(i) / float(
sides);
64 out.addVertex(
p.x,
p.y,
p.z, radial.x, radial.y, radial.z, 0.005f +
t * 0.47f,
float(ring));
67 for (
int i = 0; i <
sides; ++i) {
68 const uint32_t
n = uint32_t(i + 1);
69 const uint32_t
i0 = base + uint32_t(i),
i1 = base +
n;
70 const uint32_t
i2 = base + uint32_t(
sides + 1) + uint32_t(i);
71 const uint32_t i3 = base + uint32_t(
sides + 1) +
n;
84 const float halfW =
card * randomRange(rng, 0.55f, 0.72f);
85 const float halfH =
card * randomRange(rng, 0.55f, 0.72f);
92 constexpr float kCardU0 = 0.52f;
93 constexpr float kCardU1 = 1.00f;
94 constexpr int kCols = 2;
95 constexpr int kRows = 3;
96 const int panel = int(randomRange(rng, 0.f,
float(kCols * kRows))) % (kCols * kRows);
97 const int col = panel % kCols;
98 const int row = panel / kCols;
99 const float inset = 0.04f;
100 const float cellWU = (kCardU1 - kCardU0) /
float(kCols);
101 const float cellWV = 1.f / float(kRows);
102 const float u0 = kCardU0 + (float(col) + inset) * cellWU;
103 const float u1 = kCardU0 + (float(col) + 1.f - inset) * cellWU;
104 const float v0 = (float(row) + inset) * cellWV;
105 const float v1 = (float(row) + 1.f - inset) * cellWV;
106 const bool flipU = randomRange(rng, 0.f, 1.f) > 0.5f;
107 const bool flipV = randomRange(rng, 0.f, 1.f) > 0.5f;
108 const float uv[4][2] = {
109 {flipU ? u1 : u0, flipV ? v1 : v0},
110 {flipU ? u0 : u1, flipV ? v1 : v0},
111 {flipU ? u0 : u1, flipV ? v0 : v1},
112 {flipU ? u1 : u0, flipV ? v0 : v1},
114 const uint32_t base = uint32_t(
out.getVertexCount());
115 for (
int i = 0; i < 4; ++i)
117 out.addTriangle(base, base + 1, base + 2);
118 out.addTriangle(base, base + 2, base + 3);
119 out.addTriangle(base + 2, base + 1, base);
120 out.addTriangle(base + 3, base + 2, base);
124 const uint32_t base = uint32_t(
out.getVertexCount());
125 for (
int y = 0;
y <= rings; ++
y) {
126 const float v = float(
y) / float(rings);
127 const float phi =
v * kPi;
129 const float u = float(
x) / float(
sides);
130 const float theta =
u * 2.f * kPi;
131 const V3
n = {std::sin(
phi) * std::cos(theta), std::cos(
phi), std::sin(
phi) * std::sin(theta)};
133 out.addVertex(
p.x,
p.y,
p.z,
n.x,
n.y,
n.z, 0.55f +
u * 0.45f,
v);
136 for (
int y = 0;
y < rings; ++
y) {
139 const uint32_t
a = base + uint32_t(
y *
sides +
x);
140 const uint32_t
b = base + uint32_t(
y *
sides +
nx);
141 const uint32_t
c = base + uint32_t((
y + 1) *
sides +
x);
142 const uint32_t
d = base + uint32_t((
y + 1) *
sides +
nx);
157struct ClusterSettings {
189int addFoliageClusters(MeshBuild&
out,
const std::vector<Tip>& anchors, std::mt19937& rng,
float crownRadius,
191 if (anchors.empty() ||
settings.limit <= 0)
return 0;
193 std::vector<size_t>
order(anchors.size());
194 for (
size_t i = 0; i <
order.size(); ++i)
order[i] = i;
195 for (
size_t i =
order.size(); i > 1; --i)
196 std::swap(
order[i - 1],
order[
size_t(randomRange(rng, 0.f,
float(i) - 1e-4f))]);
207 for (
size_t k = 0; k <
order.size() && clusters <
settings.limit; ++k) {
209 bool blocked =
false;
216 if (blocked)
continue;
218 const float radius =
baseRadius * (0.62f + 0.38f *
tip.scale) * randomRange(rng, 0.88f, 1.12f);
223 FoliageClusterDesc cluster;
224 cluster.seed =
seed ^ (uint32_t(clusters + 1) * 2654435761u);
225 cluster.centerX =
center.x;
226 cluster.centerY =
center.y;
227 cluster.centerZ =
center.z;
229 cluster.leafSize = leafSize *
settings.leafScale * randomRange(rng, 0.85f, 1.15f);
233 cluster.tiltDegrees =
settings.tilt;
234 cluster.maxLeavesPerPlane =
settings.leavesPerPlane;
235 cluster.normalRounding = 1.f;
236 cluster.doubleSided =
true;
251 float verticalAcceleration,
int segments) {
256 const V3
up{0.f, 1.f, 0.f};
257 for (
int i = 0; i <=
segments; ++i) {
258 const float t = float(i) / float(
segments);
260 const float primary =
curve *
t *
t;
261 const float back = curveBack * std::sin(2.f * kPi *
t) *
t * (1.f -
t);
264 path.points.push_back(
p);
284float stemRadius(
float t,
float r0,
float r1,
float taper) {
285 return r0 + (r1 - r0) * std::pow(std::clamp(
t, 0.f, 1.f),
taper);
290void addStem(MeshBuild&
out,
const StemPath&
path,
float r0,
float r1,
int sides,
const StemSurface& surface) {
291 const int sourceSegments = int(
path.points.size()) - 1;
292 const int segments = std::max(sourceSegments, surface.segments);
295 for (
int i = 0; i <=
segments; ++i) {
296 const float at = float(i) * float(sourceSegments) / float(
segments);
297 const int edge = std::min(
int(
at), sourceSegments - 1);
301 const int rings = int(
centers.size());
302 std::vector<float> arc(
size_t(rings), 0.f);
303 std::vector<V3> tangent(
size_t(rings), V3{});
304 for (
int i = 0; i < rings; ++i) {
305 const V3 before =
centers[size_t(std::max(0, i - 1))];
306 const V3 after =
centers[size_t(std::min(rings - 1, i + 1))];
307 tangent[size_t(i)] = norm(
sub(after, before));
310 const float length = std::max(arc.back(), 1e-5f);
311 const float phase = hash01(surface.seed ^ 0xb5297a4du) * 2.f * kPi;
312 const int stride =
sides + 1;
313 std::vector<V3>
positions(
size_t(rings * stride));
314 NoiseField coarse, fine;
315 coarse.seed = surface.seed ^ 0x68e31da4u;
317 fine.seed = surface.seed ^ 0x1b56c4e9u;
322 std::vector<Knot>
knots;
323 for (
int i = 0; i < surface.knots; ++i) {
324 const uint32_t
key = surface.seed ^ 0x9e3779b9u ^ (uint32_t(i + 1) * 0x85ebca6bu);
325 knots.push_back({2.f * kPi * hash01(
key),
length * (0.12f + 0.70f * hash01(
key ^ 0x514e28b7u)),
326 0.45f + 0.4f * hash01(
key ^ 0x7feb352du), r0 * (1.4f + 1.8f * hash01(
key ^ 0x846ca68bu)),
327 surface.knotStrength * (0.7f + 0.3f * hash01(
key ^ 0xa511e9b3u))});
329 const float noiseLength = std::max(r0 * surface.noiseScale, 1e-4f);
332 for (
int i = 0; i < rings; ++i) {
333 const V3
axis = tangent[size_t(i)];
337 if (
dot(transported, transported) < 1e-8f)
340 right = norm(transported);
343 const float t = arc[size_t(i)] /
length;
344 const float radius = stemRadius(
t, r0, r1, surface.taper);
345 const float foot = surface.rootFlare * std::exp(-3.f * arc[
size_t(i)] / surface.flareLength);
346 const float collar = surface.collar * std::exp(-2.5f * arc[
size_t(i)] / std::max(r0 * 2.f, 1e-4f));
347 for (
int j = 0; j <=
sides; ++j) {
350 const float roots = 0.22f + 1.25f * std::pow(std::max(0.f, std::cos(5.f *
angle +
phase)), 4.f) +
351 0.55f * std::pow(std::max(0.f, std::cos(3.f *
angle -
phase)), 6.f);
353 const float growthMask = std::min(1.f,
t * 20.f) * std::min(1.f, (1.f -
t) * 8.f);
355 surface.noiseAmount * (2.f * coarse.valueNoise(
u * 4.f, arc[
size_t(i)] / noiseLength) - 1.f);
357 surface.relief * (2.f * fine.valueNoise(
u * 8.f, arc[
size_t(i)] / (noiseLength * 0.4f)) - 1.f);
358 float localKnots = 0.f;
359 for (
const Knot& knot :
knots) {
360 const float angular = 2.f * std::sin((
angle - knot.angle) * 0.5f) / knot.width;
361 const float axial = (arc[size_t(i)] - knot.distance) / knot.height;
363 if (d2 >= 1.f)
continue;
366 const float falloff = 1.f - d2;
367 localKnots += knot.strength * falloff * falloff * (0.35f + 2.f * d2);
369 const float detail = (
body +
grain + std::clamp(localKnots, 0.f, 0.65f)) * growthMask;
374 const uint32_t base = uint32_t(
out.getVertexCount());
375 for (
int i = 0; i < rings; ++i) {
376 for (
int j = 0; j <=
sides; ++j) {
382 const V3
normal = norm(
cross(norm(around), norm(along)));
383 const V3
p =
positions[size_t(i * stride + j)];
388 for (
int i = 0; i + 1 < rings; ++i) {
389 for (
int j = 0; j <
sides; ++j) {
390 const uint32_t
a = base + uint32_t(i * stride + j);
391 const uint32_t
b =
a + 1;
392 const uint32_t
c =
a + uint32_t(stride);
393 const uint32_t
d =
c + 1;
399 const int row =
end ? rings - 1 : 0;
400 const V3
n =
mul(tangent[
size_t(row)],
end ? 1.f : -1.f);
402 const uint32_t cap = uint32_t(
out.getVertexCount());
403 out.addVertex(
c.x,
c.y,
c.z,
n.x,
n.y,
n.z, 0.24f,
end ? 1.f : 0.f);
404 for (
int j = 0; j <=
sides; ++j) {
405 const V3
p =
positions[size_t(row * stride + j)];
406 out.addVertex(
p.x,
p.y,
p.z,
n.x,
n.y,
n.z, 0.24f,
end ? 1.f : 0.f);
408 for (
int j = 0; j <
sides; ++j) {
410 out.addTriangle(cap, cap + uint32_t(j + 1), cap + uint32_t(j + 2));
412 out.addTriangle(cap, cap + uint32_t(j + 2), cap + uint32_t(j + 1));
418 const int segmentCount = int(
path.points.size()) - 1;
419 const float x = std::clamp(
t, 0.f, 1.f) * float(segmentCount);
420 const int i = std::min(
int(
x), segmentCount - 1);
421 const float local =
x - float(i);
439struct ColonizationSettings {
462void growSpaceColonizedTree(MeshBuild&
out,
const StemPath& trunk, std::mt19937& rng,
463 const ColonizationSettings&
settings, std::vector<Tip>& foliageAnchors) {
464 V3 crownCenter, ignoredDirection;
465 samplePath(trunk,
settings.foliageStart + (1.f -
settings.foliageStart) * 0.53f, crownCenter, ignoredDirection);
466 const float crownHalfHeight =
settings.height * (1.f -
settings.foliageStart) * 0.52f;
468 std::vector<V3> attractors;
469 attractors.reserve(
size_t(
settings.attractorCount));
470 while (
int(attractors.size()) <
settings.attractorCount) {
471 const V3
q{randomRange(rng, -1.f, 1.f), randomRange(rng, -1.f, 1.f), randomRange(rng, -1.f, 1.f)};
472 if (
dot(
q,
q) > 1.f)
continue;
474 attractors.push_back(
add(crownCenter, {
q.x *
settings.crownRadius,
475 q.y * crownHalfHeight + 0.12f * crownHalfHeight * (1.f -
q.y *
q.y),
479 std::vector<ColonyNode>
nodes;
482 const int seedCount = 9;
483 for (
int i = 0; i < seedCount; ++i) {
486 settings.foliageStart + (1.f -
settings.foliageStart) * (0.08f + 0.78f *
float(i) / float(seedCount - 1));
487 samplePath(trunk,
t,
p,
dir);
488 const float crownHeight = float(i) / float(seedCount - 1);
489 const float lengthScale = 1.f -
settings.lengthFalloff * crownHeight;
490 const float vigor = 1.f -
settings.radiusFalloff * crownHeight;
494 std::vector<uint8_t>
alive(
size_t(
settings.attractorCount), 1);
495 const float influenceSquared =
settings.influenceRadius *
settings.influenceRadius;
498 const float maxTurn =
settings.maxTurnAngle * kPi / 180.f;
499 const float maxTurnCos = std::cos(maxTurn);
500 const float maxTurnSin = std::sin(maxTurn);
501 const float cumulativeTurn =
settings.maxCumulativeAngle * kPi / 180.f;
502 const float cumulativeTurnCos = std::cos(cumulativeTurn);
503 const float cumulativeTurnSin = std::sin(cumulativeTurn);
504 for (
int iteration = 0; iteration <
settings.iterations; ++iteration) {
505 const size_t nodeCount =
nodes.size();
506 std::vector<V3> directionSums(nodeCount);
507 std::vector<int> directionCounts(nodeCount, 0);
508 int liveAttractors = 0;
509 for (
size_t ai = 0; ai < attractors.size(); ++ai) {
510 if (!
alive[ai])
continue;
513 float best = influenceSquared;
515 for (
size_t ni = 0; ni < nodeCount; ++ni) {
516 const V3 toward =
sub(attractors[ai],
nodes[ni].
p);
517 const float d =
dot(toward, toward);
518 if (
d < killSquared) {
526 const V3 attractionDirection = norm(toward);
527 if (
dot(attractionDirection,
nodes[ni].
dir) < -0.08f ||
538 directionSums[nearest] =
add(directionSums[nearest], norm(
sub(attractors[ai],
nodes[nearest].
p)));
539 ++directionCounts[nearest];
542 if (liveAttractors == 0)
break;
544 std::vector<ColonyNode> additions;
545 for (
size_t ni = 0; ni < nodeCount; ++ni) {
556 if (
nodes[ni].
depth > 0 && alignment < maxTurnCos) {
558 if (
dot(tangent, tangent) > 1e-8f)
565 if (headingAlignment < cumulativeTurnCos) {
567 if (
dot(tangent, tangent) > 1e-8f)
578 bool separated =
true;
579 for (
size_t oi = 0; oi <
nodes.size() && separated; ++oi)
581 for (
size_t oi = 0; oi < additions.size() && separated; ++oi)
582 separated =
distanceSquared(candidate, additions[oi].
p) >= separationSquared;
587 ++
nodes[ni].children;
590 if (additions.empty())
break;
591 nodes.insert(
nodes.end(), additions.begin(), additions.end());
594 for (
int i =
int(
nodes.size()) - 1; i >= 0; --i) {
600 const float twigRadius =
settings.trunkRadius * 0.055f;
601 for (
size_t i = 0; i <
nodes.size(); ++i) {
603 if (
node.parent < 0)
continue;
605 const float r0 = std::min(
settings.trunkRadius * 0.34f *
parent.vigor,
606 twigRadius *
parent.vigor * (1.f + std::sqrt(
float(
parent.descendants))));
607 const float r1 = std::min(r0 * 0.92f, twigRadius *
node.vigor * (1.f + std::sqrt(
float(
node.descendants))));
609 if (
node.children == 0) {
610 foliageAnchors.push_back({
node.p,
node.dir, 0.42f + 0.22f *
node.vigor});
611 }
else if (
node.depth >= 2 && (
node.depth % 2) == 0) {
616 if (hash01(uint32_t(i) * 747796405u + 2891336453u) <
coverage)
617 foliageAnchors.push_back({
node.p,
node.dir, 0.48f + 0.20f * (1.f -
node.budHeight)});
625 const std::string style =
params.getString(
"style",
"lowpoly");
626 const std::string leafMode =
params.getString(
"leafMode",
"cards");
627 const std::string branchAlgorithm =
params.getString(
"branchAlgorithm",
"weberPenn");
628 if (style !=
"lowpoly" && style !=
"realistic") {
629 error =
"mesh.tree: style must be lowpoly|realistic";
632 if (leafMode !=
"cards" && leafMode !=
"clusters" && leafMode !=
"canopy" && leafMode !=
"none") {
633 error =
"mesh.tree: leafMode must be cards|clusters|canopy|none";
636 if (branchAlgorithm !=
"weberPenn" && branchAlgorithm !=
"spaceColonization") {
637 error =
"mesh.tree: branchAlgorithm must be weberPenn|spaceColonization";
641 const bool realistic = style ==
"realistic";
642 const float height = std::max(0.5f,
params.getFloat(
"height", 6.f));
645 const float leafSize = std::max(0.02f,
params.getFloat(
"leafSize",
height * 0.075f));
646 const float density = std::clamp(
params.getFloat(
"leafDensity", 0.65f), 0.f, 1.f);
647 const float foliageStart = std::clamp(
params.getFloat(
"foliageStart", 0.35f), 0.1f, 0.9f);
648 const int branchLevels = std::clamp(
params.getInt(
"branchLevels", realistic ? 3 : 2), 1, 5);
649 const int branchCount = std::clamp(
params.getInt(
"branchCount", realistic ? 9 : 6), 2, 20);
650 const int sides = std::clamp(
params.getInt(
"radialSegments", realistic ? 10 : 6), 3, 24);
651 const int curveSegments = std::clamp(
params.getInt(
"curveSegments", realistic ? 9 : 5), 2, 20);
652 const float trunkCurve = std::clamp(
params.getFloat(
"trunkCurve", 0.10f), 0.f, 0.45f);
653 const float curveBack = std::clamp(
params.getFloat(
"curveBack", 0.16f), -0.5f, 0.5f);
654 const float branchCurve = std::clamp(
params.getFloat(
"branchCurve", 0.13f), 0.f, 0.5f);
655 const float branchAngle = std::clamp(
params.getFloat(
"branchAngle", 62.f), 5.f, 88.f);
656 const float angleVariation = std::clamp(
params.getFloat(
"branchAngleVariation", 12.f), 0.f, 40.f);
657 const float phyllotaxis =
params.getFloat(
"phyllotaxis", 137.5f) * kPi / 180.f;
658 const float tropism = std::clamp(
params.getFloat(
"tropism", 0.22f), -0.5f, 0.8f);
659 const float droop = std::clamp(
params.getFloat(
"droop", 0.18f), 0.f, 0.8f);
660 const float apicalDominance = std::clamp(
params.getFloat(
"apicalDominance", 0.62f), 0.f, 1.f);
661 const int attractorCount = std::clamp(
params.getInt(
"attractorCount", realistic ? 180 : 80), 12, 1200);
662 const int colonizationIterations = std::clamp(
params.getInt(
"colonizationIterations", realistic ? 46 : 30), 4, 160);
665 const float growthStep = std::max(0.01f,
params.getFloat(
"growthStep",
crownRadius * (realistic ? 0.105f : 0.14f)));
666 const float branchInertia = std::clamp(
params.getFloat(
"branchInertia", 1.20f), 0.f, 4.f);
667 const float maxTurnAngle = std::clamp(
params.getFloat(
"maxTurnAngle", realistic ? 16.f : 22.f), 2.f, 60.f);
669 const float branchLengthFalloff = std::clamp(
params.getFloat(
"branchLengthFalloff", 0.58f), 0.f, 0.9f);
670 const float branchRadiusFalloff = std::clamp(
params.getFloat(
"branchRadiusFalloff", 0.50f), 0.f, 0.9f);
674 const float trunkTipRatio = std::clamp(
params.getFloat(
"trunkTipRatio", 0.10f), 0.03f, 0.4f);
675 const float trunkTaper = std::clamp(
params.getFloat(
"trunkTaper", 0.85f), 0.5f, 2.f);
676 const float rootFlare = std::clamp(
params.getFloat(
"rootFlare", 0.70f), 0.f, 1.5f);
677 const float rootFlareHeight = std::clamp(
params.getFloat(
"rootFlareHeight", 0.10f), 0.03f, 0.25f);
678 const float barkRelief = std::clamp(
params.getFloat(
"barkRelief", 0.04f), 0.f, 0.12f);
679 const float trunkNoise = std::clamp(
params.getFloat(
"trunkNoise", 0.18f), 0.f, 0.35f);
680 const float trunkNoiseScale = std::clamp(
params.getFloat(
"trunkNoiseScale", 3.f), 1.f, 8.f);
681 const int trunkKnots = std::clamp(
params.getInt(
"trunkKnots", 3), 0, 8);
682 const float trunkKnotStrength = std::clamp(
params.getFloat(
"trunkKnotStrength", 0.35f), 0.f, 0.6f);
683 const int surfaceSegments = std::clamp(
params.getInt(
"surfaceSegments", 64), 8, 192);
684 const ClusterSettings clusters{
685 .size = std::clamp(
params.getFloat(
"clusterSize", 0.30f), 0.04f, 0.8f),
686 .leafScale = std::clamp(
params.getFloat(
"clusterLeafScale", 0.85f), 0.1f, 3.f),
687 .spacing = std::clamp(
params.getFloat(
"clusterSpacing", 0.80f), 0.25f, 3.f),
688 .separation = std::clamp(
params.getFloat(
"clusterSeparation", 0.55f), 0.1f, 3.f),
689 .tilt = std::clamp(
params.getFloat(
"clusterTilt", 26.f), 0.f, 80.f),
690 .planes = std::clamp(
params.getInt(
"clusterPlanes", 10), 1, 24),
691 .caps = std::clamp(
params.getInt(
"clusterCaps", 2), 0, 8),
692 .leavesPerPlane = std::clamp(
params.getInt(
"clusterLeaves", 28), 1, 256),
693 .limit = std::clamp(
params.getInt(
"clusterLimit", 120), 1, 512),
695 std::mt19937 rng(
params.getSeed());
698 const V3
root{0.f, 0.f, 0.f};
699 const float trunkAzimuth = randomRange(rng, 0.f, 2.f * kPi);
700 const V3 trunkBend{std::cos(trunkAzimuth), 0.f, std::sin(trunkAzimuth)};
702 makeCurvedStem(
root, {0.f, 1.f, 0.f}, trunkBend,
height, trunkCurve * randomRange(rng, 0.72f, 1.28f),
703 curveBack * randomRange(rng, 0.72f, 1.28f), 0.f, curveSegments + 2);
705 const V3 crossBend = cross(V3{0.f, 1.f, 0.f}, trunkBend);
706 for (
size_t i = 1; i < trunk.points.size(); ++i) {
707 const float t = float(i) / float(trunk.points.size() - 1);
708 trunk.points[i] = add(trunk.points[i], mul(crossBend,
height * trunkCurve * 0.65f *
t * std::sin(kPi *
t)));
710 const StemSurface trunkSurface{.taper = trunkTaper,
712 .flareLength =
height * rootFlareHeight,
713 .relief = barkRelief,
715 .noiseAmount = trunkNoise,
716 .noiseScale = trunkNoiseScale,
718 .knotStrength = trunkKnotStrength,
719 .segments = surfaceSegments};
722 std::vector<Tip> tips;
723 std::vector<Tip> foliageAnchors;
724 if (branchAlgorithm ==
"spaceColonization") {
725 const ColonizationSettings colonization{
731 .droop =
droop * 0.45f,
732 .inertia = branchInertia,
734 .iterations = colonizationIterations,
740 .lengthFalloff = branchLengthFalloff,
741 .radiusFalloff = branchRadiusFalloff,
747 growSpaceColonizedTree(
out, trunk, rng, colonization, foliageAnchors);
750 const int count = std::max(2, branchCount -
level * 2);
751 const float levelScale = std::pow(0.62f,
float(
level));
752 std::vector<Tip> next;
753 for (
int i = 0; i <
count; ++i) {
755 const float angle = float(i) * phyllotaxis + randomRange(rng, -0.20f, 0.20f) +
level;
759 samplePath(trunk,
h,
start, parentDirection);
761 const Tip&
parent = tips[size_t(i) % tips.size()];
763 parentDirection =
parent.dir;
765 V3 ringRight, ringForward;
766 basisFor(parentDirection, ringRight, ringForward);
767 const V3 radial = add(mul(ringRight, std::cos(
angle)), mul(ringForward, std::sin(
angle)));
768 const float levelAngle =
769 branchAngle - float(
level) * 7.f + randomRange(rng, -angleVariation, angleVariation);
770 const float radians = std::clamp(levelAngle, 5.f, 88.f) * kPi / 180.f;
771 V3
dir = norm(add(mul(parentDirection, std::cos(radians)), mul(radial, std::sin(radians))));
772 dir = norm(add(
dir, mul(V3{0.f, 1.f, 0.f}, apicalDominance * 0.24f *
h)));
773 const float relativeHeight =
775 const float lengthTaper = 1.f - branchLengthFalloff * relativeHeight;
776 const float radiusTaper = 1.f - branchRadiusFalloff * relativeHeight;
777 const float length =
crownRadius * randomRange(rng, 0.72f, 1.18f) * lengthTaper * levelScale;
778 const float parentRadius =
level == 0
780 : tips[size_t(i) % tips.size()].radius;
781 const float r = std::min(
trunkRadius * (0.36f * radiusTaper) * levelScale, parentRadius * 0.60f);
782 V3 bendRight, bendForward;
783 basisFor(
dir, bendRight, bendForward);
785 norm(add(mul(bendRight, std::cos(
angle + 1.1f)), mul(bendForward, std::sin(
angle + 1.1f))));
786 const float flexibility = 1.f + float(
level) * 0.38f;
787 const float downWeight =
droop * (0.35f + 0.65f * (1.f -
h)) * flexibility;
788 const float verticalAcceleration =
tropism * (0.55f + apicalDominance *
h) - downWeight;
789 const StemPath stem = makeCurvedStem(
start,
dir, bendAxis,
length,
790 branchCurve * flexibility * randomRange(rng, 0.65f, 1.35f),
791 curveBack * 0.55f * flexibility * randomRange(rng, 0.55f, 1.25f),
792 verticalAcceleration, std::max(2, curveSegments -
level));
793 const StemSurface branchSurface{.taper = 1.f,
794 .relief = barkRelief,
796 .seed =
params.getSeed() ^ uint32_t(
level * 131 + i * 977),
797 .noiseAmount = trunkNoise * 0.45f,
798 .noiseScale = trunkNoiseScale,
800 .segments = std::max(4, surfaceSegments / (4 <<
level))};
801 addStem(
out, stem,
r,
r * 0.22f, std::max(3,
sides -
level * 2), branchSurface);
803 const uint32_t coverageKey =
params.getSeed() ^ uint32_t(
level * 131 + i * 977);
804 if (hash01(coverageKey) <
coverage && stem.points.size() > 3) {
805 const size_t middle = stem.points.size() / 2;
806 const V3 middleDirection = norm(sub(stem.points[middle], stem.points[middle - 1]));
807 foliageAnchors.push_back(
808 {stem.points[middle], middleDirection, levelScale * (0.72f + 0.28f * (1.f - relativeHeight))});
810 next.push_back({stem.points.back(), stem.endDirection, levelScale,
r * 0.22f});
812 foliageAnchors.insert(foliageAnchors.end(), next.begin(), next.end());
817 int clusterCount = 0;
818 if (leafMode ==
"cards") {
819 const int perTip = int(std::round((realistic ? 12.f : 6.f) *
density));
820 for (
const Tip&
tip : foliageAnchors) {
821 for (
int i = 0; i < perTip; ++i) {
822 V3
c = add(
tip.p, {randomRange(rng, -1.f, 1.f) * crownRadius * 0.24f * tip.scale,
823 randomRange(rng, -0.25f, 0.55f) * crownRadius * tip.scale,
824 randomRange(rng, -1.f, 1.f) * crownRadius * 0.24f * tip.scale});
826 norm({randomRange(rng, -1.f, 1.f), randomRange(rng, -0.2f, 0.8f), randomRange(rng, -1.f, 1.f)});
827 addLeafCard(
out,
c, face, leafSize * randomRange(rng, 0.72f, 1.25f), randomRange(rng, 0.f, kPi),
831 }
else if (leafMode ==
"clusters" &&
density > 0.f) {
834 }
else if (leafMode ==
"canopy" &&
density > 0.f) {
837 int wanted = std::max(1,
int(std::round(
float(foliageAnchors.size()) * (0.28f +
density * 0.62f))));
838 if (branchAlgorithm ==
"spaceColonization") wanted = int(foliageAnchors.size());
839 const int stride = std::max(1,
int(foliageAnchors.size()) / wanted);
841 for (
int i =
int(foliageAnchors.size()) - 1; i >= 0 && emitted < wanted; i -= stride) {
842 const Tip&
tip = foliageAnchors[size_t(i)];
843 const float lobeMin = branchAlgorithm ==
"spaceColonization" ? 0.14f : 0.27f;
844 const float lobeMax = branchAlgorithm ==
"spaceColonization" ? 0.22f : 0.40f;
845 const float r =
crownRadius * randomRange(rng, lobeMin, lobeMax) * (0.72f + 0.28f *
density) *
846 (0.58f + 0.42f *
tip.scale);
847 V3
c = sub(
tip.p, mul(
tip.dir,
r * 0.22f));
848 c.y +=
r * randomRange(rng, 0.08f, 0.22f);
849 addCanopyBlob(
out,
c, {
r,
r * randomRange(rng, 0.72f, 1.15f),
r}, realistic ? 8 : 4, realistic ? 12 : 7);
854 out.setMeta(
"recipe",
"mesh.tree");
855 out.setMeta(
"style", style);
856 out.setMeta(
"leafMode", leafMode);
857 out.setMeta(
"branchAlgorithm", branchAlgorithm);
858 out.setMeta(
"seed", std::to_string(
params.getSeed()));
859 if (leafMode ==
"clusters")
out.setMeta(
"clusters", std::to_string(clusterCount));
861 error =
"mesh.tree: generated an empty mesh";
std::vector< V3 > centers
std::array< double, 10 > q
std::vector< float > positions
const ColourScope * parent
std::array< float, 3 > scale
OceanWakeSettings settings
std::shared_ptr< const std::vector< glm::vec2 > > points
RoadLaneDirection direction
std::map< Cell, int > best
std::vector< double > phi
CPU triangle mesh from procedural mesh recipes (e.g. marching cubes). Positions/normals are xyz-packe...
Owning, typed generation parameters.
ForceVector add(ForceVector a, ForceVector b)
double dot(ForceVector a, ForceVector b)
Vec3 mul(Vec3 a, float scale)
V3 cross(V3 a, V3 b)
Module-private road bake implementation helper.
int addFoliageCluster(MeshBuild &out, const FoliageClusterDesc &desc)
Append one blue-noise leaf cluster to a mesh.
bool generateTreeMesh(const Params ¶ms, MeshBuild &out, std::string &error)
Build a deterministic procedural tree. Registered as the mesh.tree recipe.
Result< void > accepted()
Build a successful write/validation result.
float distanceSquared(float ax, float ay, float bx, float by)
WidgetDesc card(std::vector< WidgetDesc > children, std::string id)
Bordered surface container with editor-friendly padding.
WidgetDesc row(std::vector< WidgetDesc > children, std::string id)
Horizontal elastic layout row.