载入中...
搜索中...
未找到
FoliageCluster.cpp
浏览该文件的文档.
2
3#include "procgen/PointSet.h"
4
5#include <algorithm>
6#include <cmath>
7#include <cstdint>
8#include <random>
9#include <vector>
10
11namespace eve::procgen {
12namespace {
13
14constexpr float kPi = 3.14159265358979323846f;
15
16struct V3 {
17 float x = 0.f, y = 0.f, z = 0.f;
18};
19
20V3 add(V3 a, V3 b) { return {a.x + b.x, a.y + b.y, a.z + b.z}; }
21V3 sub(V3 a, V3 b) { return {a.x - b.x, a.y - b.y, a.z - b.z}; }
22V3 mul(V3 a, float s) { return {a.x * s, a.y * s, a.z * s}; }
23float dot(V3 a, V3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
24V3 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}; }
25
26V3 norm(V3 a) {
27 const float n = std::sqrt(std::max(1e-12f, dot(a, a)));
28 return mul(a, 1.f / n);
29}
30
31float randomRange(std::mt19937 &rng, float lo, float hi) { return std::uniform_real_distribution<float>(lo, hi)(rng); }
32
34void basisFor(V3 axis, V3 &right, V3 &forward) {
35 axis = norm(axis);
36 const V3 helper = std::fabs(axis.y) < 0.92f ? V3{0.f, 1.f, 0.f} : V3{1.f, 0.f, 0.f};
37 right = norm(cross(helper, axis));
38 forward = norm(cross(axis, right));
39}
40
41struct V2 {
42 float x = 0.f, y = 0.f;
43};
44
60std::vector<V2> samplePlaneCentres(std::uint32_t seed, float radius, float minDistance, int maxLeaves) {
61 std::vector<V2> centres;
62 if (radius <= 0.f || minDistance <= 0.f || maxLeaves <= 0) return centres;
63
64 // Hexagonal packing fits about 2A / (sqrt(3) d^2) centres in area A. Solving
65 // for the disk makes the *disk* land on the leaf cap; the enclosing square
66 // then saturates above it, so the budget below is only a safety cap and the
67 // fill is never truncated (a truncated Bridson fill is spatially biased).
68 const float capSpacing = std::sqrt(2.f * kPi * radius * radius / (std::sqrt(3.f) * float(maxLeaves)));
69 const float spacing = std::max(minDistance, capSpacing);
70 const int squareSide = std::max(2, int(std::ceil(2.f * radius)));
71 const float squareArea = float(squareSide) * float(squareSide);
72 const int squareBudget = int(1.3f * float(maxLeaves) * squareArea / std::max(kPi * radius * radius, 1e-6f)) + 8;
73
74 const PointSet square = poissonDiskPoints(squareSide, squareSide, spacing, seed, squareBudget);
75 const float half = 0.5f * float(squareSide);
76 // `poissonDiskPoints` samples the XZ plane, so the second area axis is z.
77 for (const ProcgenPoint &point : square.points()) {
78 const float dx = point.x - half;
79 const float dz = point.z - half;
80 if (dx * dx + dz * dz > radius * radius) continue;
81 centres.push_back({dx, dz});
82 if (int(centres.size()) >= maxLeaves) break;
83 }
84 return centres;
85}
86// Rectangle cards carry an alpha-cutout leaf. Sphere normals remain independent
87// of the card orientation, so random tilt does not fragment the crown lighting.
88template <typename NormalAt>
89void addLeafCard(MeshBuild &out, V3 center, V3 right, V3 forward, float size, float aspect, float roll, float u0,
90 float u1, float v0, float v1, bool doubleSided, const NormalAt &normalAt) {
91 const float c = std::cos(roll), s = std::sin(roll);
92 const V3 axisX = mul(add(mul(right, c), mul(forward, s)), size * aspect);
93 const V3 axisY = mul(add(mul(right, -s), mul(forward, c)), size);
94 constexpr float xs[4] = {-0.5f, 0.5f, 0.5f, -0.5f};
95 constexpr float ys[4] = {-0.5f, -0.5f, 0.5f, 0.5f};
96 const uint32_t base = uint32_t(out.getVertexCount());
97 for (int i = 0; i < 4; ++i) {
98 const V3 p = add(center, add(mul(axisX, xs[i]), mul(axisY, ys[i])));
99 const V3 n = normalAt(p);
100 out.addVertex(p.x, p.y, p.z, n.x, n.y, n.z, u0 + (xs[i] + 0.5f) * (u1 - u0), v0 + (ys[i] + 0.5f) * (v1 - v0));
101 }
102 out.addTriangle(base, base + 1, base + 2);
103 out.addTriangle(base, base + 2, base + 3);
104 if (doubleSided) {
105 out.addTriangle(base, base + 2, base + 1);
106 out.addTriangle(base, base + 3, base + 2);
107 }
108}
109
110} // namespace
111
113 const float radius = std::max(1e-3f, desc.radius);
114 const float leafSize = std::max(1e-3f, desc.leafSize);
115 const float leafSpacing = std::max(0.05f, desc.leafSpacing);
116 const int planes = std::clamp(desc.planes, 1, 24);
117 const int capPlanes = std::clamp(desc.capPlanes, 0, 8);
118 const float tiltMax = std::clamp(desc.tiltDegrees, 0.f, 80.f) * kPi / 180.f;
119 const float planeOffset = std::clamp(desc.planeOffset, 0.f, 0.6f) * radius;
120 const float scaleVar = std::clamp(desc.planeScaleVariation, 0.f, 0.6f);
121 const float leafJitter = std::clamp(desc.leafJitter, 0.f, 0.5f) * radius;
122 const float sizeVar = std::clamp(desc.leafScaleVariation, 0.f, 0.6f);
123 const int maxLeaves = std::clamp(desc.maxLeavesPerPlane, 1, 256);
124 const float rounding = std::clamp(desc.normalRounding, 0.f, 1.f);
125 const float uvMin = std::min(desc.uvMin, desc.uvMax);
126 const float uvMax = std::max(desc.uvMin, desc.uvMax);
127
128 const V3 center{desc.centerX, desc.centerY, desc.centerZ};
129 std::mt19937 rng(desc.seed);
130
131 // Requested blue-noise spacing. `samplePlaneCentres` widens it further when
132 // it would otherwise overshoot the per-plane leaf cap.
133 const float minDistance = leafSize * leafSpacing;
134
135 // Vertex normals come from the cluster sphere so a bundle of flat cards
136 // still shades as one rounded mass. Near the centre the sphere direction is
137 // ill-conditioned, so the blend falls back to the plane normal there.
138 const float roundingDistance = std::max(1e-4f, radius * 0.35f);
139 const auto normalAt = [&](V3 p, V3 planeNormal) {
140 const V3 radial = sub(p, center);
141 const float length = std::sqrt(dot(radial, radial));
142 const float weight = rounding * std::clamp(length / roundingDistance, 0.f, 1.f);
143 if (length < 1e-5f || weight <= 0.f) return planeNormal;
144 const V3 blended = add(mul(mul(radial, 1.f / length), weight), mul(planeNormal, 1.f - weight));
145 if (dot(blended, blended) < 1e-6f) return planeNormal;
146 return norm(blended);
147 };
148
149 const int planeCount = planes + capPlanes;
150 const float yawPhase = randomRange(rng, 0.f, 2.f * kPi);
151 const float yawStep = 2.f * kPi / float(planeCount);
152 int leaves = 0;
153 for (int index = 0; index < planeCount; ++index) {
154 const bool cap = index >= planes;
155
156 // Ring planes take an even share of the circle plus a jitter, so the
157 // bundle never reads as a regular polygon from any angle.
158 const float yaw =
159 cap ? randomRange(rng, 0.f, 2.f * kPi) : yawPhase + (float(index) + randomRange(rng, 0.2f, 0.8f)) * yawStep;
160 // Ring planes lean a little off vertical; the caps lean near-horizontal
161 // so the cluster's poles are not left hollow.
162 const float tilt = cap ? ((index & 1) ? 1.f : -1.f) * randomRange(rng, 62.f, 86.f) * kPi / 180.f
163 : randomRange(rng, -tiltMax, tiltMax);
164 const V3 planeNormal{std::sin(yaw) * std::cos(tilt), std::sin(tilt), std::cos(yaw) * std::cos(tilt)};
165
166 V3 right, forward;
167 basisFor(planeNormal, right, forward);
168
169 const float radialScale = 1.f - scaleVar * randomRange(rng, 0.f, 1.f) - (cap ? 0.35f : 0.f);
170 const float planeRadius = radius * std::max(0.2f, radialScale);
171 const float offsetAngle = randomRange(rng, 0.f, 2.f * kPi);
172 const float offsetR = planeOffset * std::sqrt(randomRange(rng, 0.f, 1.f));
173 const V3 origin = add(
174 center, add(mul(right, std::cos(offsetAngle) * offsetR), mul(forward, std::sin(offsetAngle) * offsetR)));
175
176 const std::vector<V2> centers = samplePlaneCentres(desc.seed ^ (std::uint32_t(index + 1) * 2654435761u),
177 planeRadius, minDistance, maxLeaves);
178 for (const V2 &point : centers) {
179 const float jitter = randomRange(rng, -1.f, 1.f) * leafJitter;
180 const V3 position =
181 add(origin, add(add(mul(right, point.x), mul(forward, point.y)), mul(planeNormal, jitter)));
182
183 // Point the petals outward from the cluster centre so the bundle
184 // reads as growth radiating from the branch rather than confetti.
185 float roll = randomRange(rng, 0.f, 2.f * kPi);
186 const V3 radial = sub(position, center);
187 const V3 tangent = sub(radial, mul(planeNormal, dot(radial, planeNormal)));
188 if (dot(tangent, tangent) > 1e-8f) {
189 const V3 outward = norm(tangent);
190 roll = std::atan2(dot(outward, forward), dot(outward, right)) - 0.5f * kPi +
191 randomRange(rng, -0.85f, 0.85f);
192 }
193
194 const float size = leafSize * (1.f + randomRange(rng, -sizeVar, sizeVar));
195 // One of six shared leaf-card panels (tex.tree_atlas / tex.foliage).
196 constexpr int kCols = 2;
197 constexpr int kRows = 3;
198 const int col = int(randomRange(rng, 0.f, float(kCols))) % kCols;
199 const int row = int(randomRange(rng, 0.f, float(kRows))) % kRows;
200 const float inset = 0.04f;
201 const float spanU = (uvMax - uvMin) / float(kCols);
202 const float spanV = 1.f / float(kRows);
203 const float u0 = uvMin + (float(col) + inset) * spanU;
204 const float u1 = uvMin + (float(col) + 1.f - inset) * spanU;
205 const float v0 = (float(row) + inset) * spanV;
206 const float v1 = (float(row) + 1.f - inset) * spanV;
207 const auto shadedNormal = [&](V3 p) { return normalAt(p, planeNormal); };
208 const float aspect = randomRange(rng, 0.65f, 1.05f);
209 const V3 leafRight = norm(add(right, mul(planeNormal, randomRange(rng, -0.25f, 0.25f))));
210 const V3 leafForward = norm(sub(forward, mul(leafRight, dot(forward, leafRight))));
211 addLeafCard(out, position, leafRight, leafForward, size, aspect, roll, u0, u1, v0, v1, desc.doubleSided,
212 shadedNormal);
213 ++leaves;
214 }
215 }
216 return leaves;
217}
218
219} // namespace eve::procgen
double z
double y
double x
const std::string & s
std::vector< V3 > centers
Definition BushMesh.cpp:100
float yaw
Definition BushMesh.cpp:37
float planes[6][4]
glm::vec4 p[6]
scene::NodeDesc desc
glm::vec3 n
Definition Grass.cpp:63
HexVec3 right
std::int32_t c
float length
JointPoint2D axis
std::array< float, 3 > position
MeleePoint3 b
Definition MeleeHit.cpp:41
double radius
Definition MeleeHit.cpp:42
MeleePoint3 a
Definition MeleeHit.cpp:40
Vec3 planeNormal
bool doubleSided
glm::dvec3 origin
glm::dvec2 center
glm::dvec3 dx
glm::dvec3 dz
std::shared_ptr< const std::vector< glm::vec2 > > points
float axisY
Definition RockMesh.cpp:22
float axisX
Definition RockMesh.cpp:22
ScriptStructure out
int spacing
uint32_t seed
Definition TreeMesh.cpp:276
float tilt
Definition TreeMesh.cpp:162
float size
Definition TreeMesh.cpp:158
double weight
uint32_t index
V forward
glm::vec3 point
CPU triangle mesh from procedural mesh recipes (e.g. marching cubes). Positions/normals are xyz-packe...
Definition MeshBuild.h:20
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
int addFoliageCluster(MeshBuild &out, const FoliageClusterDesc &desc)
Append one blue-noise leaf cluster to a mesh.
PointSet poissonDiskPoints(int width, int depth, float radius, uint32_t seed, int maxPoints)
Bridson blue-noise (Poisson disk) samples in a width x depth area (XZ, y=0).
Definition PointSet.cpp:506
Recipe for one stylized leaf cluster ("foliage card bundle").