载入中...
搜索中...
未找到
Procedural.cpp
浏览该文件的文档.
2#include <algorithm>
3#include <cmath>
4#include <glm/glm.hpp>
5#include <vector>
6#include "common/Diagnostic.h"
8
9// Procedural.cpp
10namespace eve::graphics::hair {
11namespace {
12
13struct Triangle {
14 uint32_t i0 = 0, i1 = 0, i2 = 0;
15 float area = 0.f;
16 glm::vec3 n{0.f, 1.f, 0.f};
17};
18
19glm::vec3 readPos(const float *pos, int i) { return glm::vec3(pos[i * 3], pos[i * 3 + 1], pos[i * 3 + 2]); }
20
21glm::vec3 readNrm(const float *nrm, int i) {
22 if (!nrm) return glm::vec3(0.f, 1.f, 0.f);
23 return glm::vec3(nrm[i * 3], nrm[i * 3 + 1], nrm[i * 3 + 2]);
24}
25
26uint32_t mixSeed(uint32_t seed, uint32_t i) {
27 seed ^= 0x9e3779b9u + (i << 6) + (i >> 2);
28 seed *= 0x85ebca6bu;
29 return seed ^ (seed >> 13);
30}
31
32float hash01(uint32_t s) {
33 s ^= s >> 16;
34 s *= 0x7feb352du;
35 s ^= s >> 15;
36 s *= 0x846ca68bu;
37 s ^= s >> 16;
38 return float(s >> 8) * (1.f / 16777215.f);
39}
40
41bool buildTriangles(const float *posXYZ, const float *nrmXYZ, int vertexCount, const uint32_t *indices, int indexCount,
42 float minSlopeDot, std::vector<Triangle> &tris, std::vector<float> &cdf) {
43 tris.clear();
44 cdf.clear();
45 if (!posXYZ || !indices || vertexCount < 3 || indexCount < 3 || (indexCount % 3) != 0) return false;
46
47 float total = 0.f;
48 for (int t = 0; t + 2 < indexCount; t += 3) {
49 const uint32_t i0 = indices[t];
50 const uint32_t i1 = indices[t + 1];
51 const uint32_t i2 = indices[t + 2];
52 if (int(i0) >= vertexCount || int(i1) >= vertexCount || int(i2) >= vertexCount) continue;
53 const glm::vec3 a = readPos(posXYZ, int(i0));
54 const glm::vec3 b = readPos(posXYZ, int(i1));
55 const glm::vec3 c = readPos(posXYZ, int(i2));
56 glm::vec3 n = glm::cross(b - a, c - a);
57 const float twice = glm::length(n);
58 if (twice < 1e-10f) continue;
59 n /= twice;
60 if (nrmXYZ) {
61 glm::vec3 ns = readNrm(nrmXYZ, int(i0)) + readNrm(nrmXYZ, int(i1)) + readNrm(nrmXYZ, int(i2));
62 if (glm::dot(ns, ns) > 1e-8f) n = glm::normalize(ns);
63 }
64 if (n.y < minSlopeDot) continue;
65 Triangle tri;
66 tri.i0 = i0;
67 tri.i1 = i1;
68 tri.i2 = i2;
69 tri.area = 0.5f * twice;
70 tri.n = n;
71 total += tri.area;
72 tris.push_back(tri);
73 cdf.push_back(total);
74 }
75 return total > 1e-12f && !tris.empty();
76}
77
78int pickTriangle(const std::vector<float> &cdf, float u) {
79 const float target = u * cdf.back();
80 auto it = std::lower_bound(cdf.begin(), cdf.end(), target);
81 int idx = int(it - cdf.begin());
82 if (idx >= int(cdf.size())) idx = int(cdf.size()) - 1;
83 return idx;
84}
85
86glm::vec3 sampleOnTriangle(const float *posXYZ, const Triangle &tri, float u, float v) {
87 if (u + v > 1.f) {
88 u = 1.f - u;
89 v = 1.f - v;
90 }
91 const glm::vec3 a = readPos(posXYZ, int(tri.i0));
92 const glm::vec3 b = readPos(posXYZ, int(tri.i1));
93 const glm::vec3 c = readPos(posXYZ, int(tri.i2));
94 return a + u * (b - a) + v * (c - a);
95}
96
97} // namespace
98
99Result<StrandsDatas> generateOnMesh(const float *posXYZ, const float *nrmXYZ, int vertexCount, const uint32_t *indices,
100 int indexCount, const ProceduralParams &params) {
101 if (!posXYZ || !indices) {
103 Diagnostic::error(DiagnosticCode::InvalidArgument, "generateOnMesh: null mesh buffers", "hair.procedural"));
104 }
105 if (params.strandCount <= 0 || params.pointsPerStrand < 2) {
107 DiagnosticCode::InvalidArgument, "generateOnMesh: strandCount/pointsPerStrand invalid", "hair.procedural"));
108 }
109 if (!(params.length > 0.f) || !(params.rootRadius >= 0.f) || !(params.tipRadius >= 0.f)) {
111 DiagnosticCode::InvalidArgument, "generateOnMesh: non-positive length/radius", "hair.procedural"));
112 }
113
114 std::vector<Triangle> tris;
115 std::vector<float> cdf;
116 if (!buildTriangles(posXYZ, nrmXYZ, vertexCount, indices, indexCount, params.minSlopeDot, tris, cdf)) {
118 DiagnosticCode::InvalidArgument, "generateOnMesh: no usable triangles", "hair.procedural.mesh"));
119 }
120
122 std::vector<StrandPoint> points;
123 std::vector<StrandCurve> curves;
124 points.reserve(size_t(params.strandCount) * size_t(params.pointsPerStrand));
125 curves.reserve(size_t(params.strandCount));
126
127 for (int s = 0; s < params.strandCount; ++s) {
128 const uint32_t seed = mixSeed(params.seed, uint32_t(s));
129 const float uPick = hash01(seed);
130 const float uBary = hash01(mixSeed(seed, 1u));
131 const float vBary = hash01(mixSeed(seed, 2u));
132 const Triangle &tri = tris[size_t(pickTriangle(cdf, uPick))];
133
134 float bu = uBary;
135 float bv = vBary;
136 if (bu + bv > 1.f) {
137 bu = 1.f - bu;
138 bv = 1.f - bv;
139 }
140 const float bw = 1.f - bu - bv;
141 const glm::vec3 root = sampleOnTriangle(posXYZ, tri, bu, bv);
142
143 glm::vec3 n = tri.n;
144 if (nrmXYZ) {
145 n = readNrm(nrmXYZ, int(tri.i0)) * bw + readNrm(nrmXYZ, int(tri.i1)) * bu +
146 readNrm(nrmXYZ, int(tri.i2)) * bv;
147 if (glm::dot(n, n) > 1e-8f)
148 n = glm::normalize(n);
149 else
150 n = tri.n;
151 }
152
153 glm::vec3 tangent = glm::cross(n, glm::vec3(0.f, 0.f, 1.f));
154 if (glm::dot(tangent, tangent) < 1e-8f) tangent = glm::cross(n, glm::vec3(1.f, 0.f, 0.f));
155 tangent = glm::normalize(tangent);
156 const glm::vec3 bitangent = glm::normalize(glm::cross(n, tangent));
157
158 const float len = params.length + (hash01(mixSeed(seed, 3u)) * 2.f - 1.f) * params.lengthJitter;
159 const float safeLen = std::max(len, params.length * 0.25f);
160 const float curlPhase = hash01(mixSeed(seed, 4u)) * 6.2831853f;
161 const float curlAmp = params.curlStrength * (0.5f + hash01(mixSeed(seed, 5u)));
162
164 curve.pointOffset = uint32_t(points.size());
165 curve.pointCount = uint32_t(params.pointsPerStrand);
166 float accum = 0.f;
167 glm::vec3 prev = root;
168 for (int p = 0; p < params.pointsPerStrand; ++p) {
169 const float t = float(p) / float(params.pointsPerStrand - 1);
170 const float along = t * safeLen;
171 const float curl = curlAmp * std::sin(curlPhase + t * 6.2831853f * 1.5f);
172 const glm::vec3 pos = root + n * along + tangent * (curl * t) + bitangent * (curl * 0.35f * t);
173 if (p > 0) accum += glm::length(pos - prev);
174 prev = pos;
175 StrandPoint sp;
176 sp.position = pos;
177 sp.u = t;
178 sp.radius = params.rootRadius * (1.f - t) + params.tipRadius * t;
179 points.push_back(sp);
180 }
181 curve.length = accum;
182 curves.push_back(curve);
183 }
184
185 out.setPoints(std::move(points));
186 out.setCurves(std::move(curves));
187 auto check = out.validate();
188 if (!check.ok()) return Result<StrandsDatas>::failure(check.status());
189 return Result<StrandsDatas>::success(std::move(out));
190}
191
193 if (!(sizeX > 0.f) || !(sizeZ > 0.f)) {
195 DiagnosticCode::InvalidArgument, "generateOnPlane: non-positive size", "hair.procedural.plane"));
196 }
197 const float hx = sizeX * 0.5f;
198 const float hz = sizeZ * 0.5f;
199 const float pos[12] = {-hx, 0.f, -hz, hx, 0.f, -hz, hx, 0.f, hz, -hx, 0.f, hz};
200 const float nrm[12] = {0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0};
201 const uint32_t idx[6] = {0, 1, 2, 0, 2, 3};
202 return generateOnMesh(pos, nrm, 4, idx, 6, params);
203}
204
205} // namespace eve::graphics::hair
206
207// RibbonBuilder.cpp
208namespace eve::graphics::hair {
209namespace {
210
211glm::vec3 safeNormalize(const glm::vec3 &v, const glm::vec3 &fallback) {
212 const float len2 = glm::dot(v, v);
213 if (len2 < 1e-12f) return fallback;
214 return v * (1.f / std::sqrt(len2));
215}
216
217} // namespace
218
220 auto validated = strands.validate();
221 if (!validated.ok()) return Result<RibbonMesh>::failure(validated.status());
222
223 if (params.widthScale <= 0.f || !std::isfinite(params.widthScale)) {
225 DiagnosticCode::InvalidArgument, "buildRibbons: widthScale must be positive", "hair.ribbon.widthScale"));
226 }
227
228 const glm::vec3 sideHint = safeNormalize(params.sideHint, glm::vec3(1.f, 0.f, 0.f));
229
231 const size_t curveN = strands.curveCount();
232 mesh.posXYZ.reserve(curveN * 8u * 3u);
233 mesh.nrmXYZ.reserve(curveN * 8u * 3u);
234 mesh.uvST.reserve(curveN * 8u * 2u);
235 mesh.indices.reserve(curveN * 7u * 6u);
236
237 for (size_t ci = 0; ci < curveN; ++ci) {
238 const auto pts = strands.curvePoints(ci);
239 if (pts.size() < 2) continue;
240
241 const uint32_t baseVert = uint32_t(mesh.posXYZ.size() / 3u);
242 for (size_t pi = 0; pi < pts.size(); ++pi) {
243 const StrandPoint &p = pts[pi];
244 glm::vec3 tangent(0.f, 1.f, 0.f);
245 if (pi + 1 < pts.size()) {
246 tangent = pts[pi + 1].position - p.position;
247 } else {
248 tangent = p.position - pts[pi - 1].position;
249 }
250 tangent = safeNormalize(tangent, glm::vec3(0.f, 1.f, 0.f));
251
252 glm::vec3 side = glm::cross(tangent, sideHint);
253 if (glm::dot(side, side) < 1e-10f) {
254 side = glm::cross(tangent, glm::vec3(0.f, 0.f, 1.f));
255 }
256 side = safeNormalize(side, glm::vec3(1.f, 0.f, 0.f));
257
258 const float halfW = std::max(p.radius * params.widthScale, params.minWidth);
259 const glm::vec3 left = p.position - side * halfW;
260 const glm::vec3 right = p.position + side * halfW;
261
262 auto pushVert = [&](const glm::vec3 &pos, float vSide) {
263 mesh.posXYZ.push_back(pos.x);
264 mesh.posXYZ.push_back(pos.y);
265 mesh.posXYZ.push_back(pos.z);
266 mesh.nrmXYZ.push_back(tangent.x);
267 mesh.nrmXYZ.push_back(tangent.y);
268 mesh.nrmXYZ.push_back(tangent.z);
269 mesh.uvST.push_back(vSide);
270 mesh.uvST.push_back(p.u);
271 };
272 pushVert(left, 0.f);
273 pushVert(right, 1.f);
274 }
275
276 const uint32_t segs = uint32_t(pts.size() - 1u);
277 for (uint32_t s = 0; s < segs; ++s) {
278 const uint32_t i0 = baseVert + s * 2u;
279 const uint32_t i1 = i0 + 1u;
280 const uint32_t i2 = i0 + 2u;
281 const uint32_t i3 = i0 + 3u;
282 mesh.indices.push_back(i0);
283 mesh.indices.push_back(i2);
284 mesh.indices.push_back(i1);
285 mesh.indices.push_back(i1);
286 mesh.indices.push_back(i2);
287 mesh.indices.push_back(i3);
288 }
289 }
290
291 if (mesh.indices.empty()) {
293 Diagnostic::error(DiagnosticCode::Failed, "buildRibbons: produced empty mesh", "hair.ribbon"));
294 }
295 return Result<RibbonMesh>::success(std::move(mesh));
296}
297
298void appendRibbonMesh(RibbonMesh &dst, const RibbonMesh &src) {
299 const uint32_t base = uint32_t(dst.vertexCount());
300 dst.posXYZ.insert(dst.posXYZ.end(), src.posXYZ.begin(), src.posXYZ.end());
301 dst.nrmXYZ.insert(dst.nrmXYZ.end(), src.nrmXYZ.begin(), src.nrmXYZ.end());
302 dst.uvST.insert(dst.uvST.end(), src.uvST.begin(), src.uvST.end());
303 dst.indices.reserve(dst.indices.size() + src.indices.size());
304 for (uint32_t idx : src.indices) dst.indices.push_back(base + idx);
305}
306
307} // namespace eve::graphics::hair
LogicalId target
int root
Definition AnimSmr.cpp:119
const std::string & s
float halfW
int strands
glm::vec4 p[6]
Stable, structured diagnostics shared by engine modules.
std::uint32_t vertexCount
std::uint32_t indexCount
uint32_t i1
Definition Grass.cpp:61
uint32_t i2
Definition Grass.cpp:61
uint32_t i0
Definition Grass.cpp:61
float u
Definition Grass.cpp:233
float area
Definition Grass.cpp:62
glm::vec3 n
Definition Grass.cpp:63
std::vector< std::uint32_t > indices
float v
HexVec3 left
HexVec3 right
std::int32_t c
std::vector< RecipeParam > params
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
OceanMeshSettings mesh
int idx
std::shared_ptr< const std::vector< glm::vec2 > > points
float t
ScriptStructure out
Heightmap curve
uint32_t seed
Definition TreeMesh.cpp:276
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
CPU-authoritative strand buffer (UE FHairStrandsDatas analogue).
Procedural hair/fur card meshes + material/LOD helpers (no strand simulation).
Definition Graphics.h:64
Result< RibbonMesh > buildRibbons(const StrandsDatas &strands, const RibbonParams &params)
Expand validated strands into stable (non-view-facing) ribbons.
Result< StrandsDatas > generateOnPlane(float sizeX, float sizeZ, const ProceduralParams &params)
Grow strands on a Y-up XZ plane centered at the origin.
Result< StrandsDatas > generateOnMesh(const float *posXYZ, const float *nrmXYZ, int vertexCount, const uint32_t *indices, int indexCount, const ProceduralParams &params)
Grow strands from area-weighted samples on an indexed triangle mesh.
void appendRibbonMesh(RibbonMesh &dst, const RibbonMesh &src)
Append src into dst, rebasing triangle indices by dst vertex count. @ownership Both meshes remain cal...
Parameters for growing strands on a triangle mesh (scalp / body).
Definition Procedural.h:16
Triangle-list ribbon mesh expanded from strand curves.
std::vector< float > posXYZ
std::vector< float > nrmXYZ
int vertexCount() const
Vertex count.
std::vector< uint32_t > indices
RibbonParams public API.
One strand as a contiguous point range. @ownership Value type owned by StrandsDatas.
One control point on a hair curve (UE FHairStrandsDatas point analogue). @ownership Value type owned ...
float u
Root-to-tip parameter in [0, 1].