16 glm::vec3
n{0.f, 1.f, 0.f};
19glm::vec3 readPos(
const float *
pos,
int i) {
return glm::vec3(
pos[i * 3],
pos[i * 3 + 1],
pos[i * 3 + 2]); }
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]);
26uint32_t mixSeed(uint32_t
seed, uint32_t i) {
27 seed ^= 0x9e3779b9u + (i << 6) + (i >> 2);
32float hash01(uint32_t
s) {
38 return float(
s >> 8) * (1.f / 16777215.f);
42 float minSlopeDot, std::vector<Triangle> &tris, std::vector<float> &cdf) {
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;
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);
64 if (
n.y < minSlopeDot)
continue;
69 tri.area = 0.5f * twice;
75 return total > 1e-12f && !tris.empty();
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;
86glm::vec3 sampleOnTriangle(
const float *posXYZ,
const Triangle &tri,
float u,
float v) {
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);
105 if (
params.strandCount <= 0 ||
params.pointsPerStrand < 2) {
109 if (!(
params.length > 0.f) || !(
params.rootRadius >= 0.f) || !(
params.tipRadius >= 0.f)) {
114 std::vector<Triangle> tris;
115 std::vector<float> cdf;
122 std::vector<StrandPoint>
points;
123 std::vector<StrandCurve> curves;
125 curves.reserve(
size_t(
params.strandCount));
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))];
140 const float bw = 1.f - bu - bv;
141 const glm::vec3
root = sampleOnTriangle(posXYZ, tri, bu, bv);
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);
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));
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)));
165 curve.pointCount = uint32_t(
params.pointsPerStrand);
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);
181 curve.length = accum;
182 curves.push_back(
curve);
186 out.setCurves(std::move(curves));
187 auto check =
out.validate();
193 if (!(sizeX > 0.f) || !(sizeZ > 0.f)) {
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};
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));
220 auto validated =
strands.validate();
223 if (
params.widthScale <= 0.f || !std::isfinite(
params.widthScale)) {
228 const glm::vec3 sideHint = safeNormalize(
params.sideHint, glm::vec3(1.f, 0.f, 0.f));
231 const size_t curveN =
strands.curveCount();
233 mesh.nrmXYZ.reserve(curveN * 8u * 3u);
234 mesh.uvST.reserve(curveN * 8u * 2u);
235 mesh.indices.reserve(curveN * 7u * 6u);
237 for (
size_t ci = 0; ci < curveN; ++ci) {
238 const auto pts =
strands.curvePoints(ci);
239 if (pts.size() < 2)
continue;
241 const uint32_t baseVert = uint32_t(
mesh.posXYZ.size() / 3u);
242 for (
size_t pi = 0; pi < pts.size(); ++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;
248 tangent =
p.position - pts[pi - 1].position;
250 tangent = safeNormalize(tangent, glm::vec3(0.f, 1.f, 0.f));
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));
256 side = safeNormalize(side, glm::vec3(1.f, 0.f, 0.f));
259 const glm::vec3
left =
p.position - side *
halfW;
262 auto pushVert = [&](
const glm::vec3 &
pos,
float vSide) {
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);
273 pushVert(
right, 1.f);
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);
291 if (
mesh.indices.empty()) {
Stable, structured diagnostics shared by engine modules.
std::uint32_t vertexCount
std::vector< std::uint32_t > indices
std::shared_ptr< const std::vector< glm::vec2 > > points
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.
static Result success(T value)
Construct a successful result owning value.
static Result failure(Status status)
Construct a failed result from a structured status.
CPU-authoritative strand buffer (UE FHairStrandsDatas analogue).
Procedural hair/fur card meshes + material/LOD helpers (no strand simulation).
Result< RibbonMesh > buildRibbons(const StrandsDatas &strands, const RibbonParams ¶ms)
Expand validated strands into stable (non-view-facing) ribbons.
Result< StrandsDatas > generateOnPlane(float sizeX, float sizeZ, const ProceduralParams ¶ms)
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 ¶ms)
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).
Triangle-list ribbon mesh expanded from strand curves.
std::vector< float > uvST
std::vector< float > posXYZ
std::vector< float > nrmXYZ
int vertexCount() const
Vertex count.
std::vector< uint32_t > indices
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].