16 triangleGroups_.clear();
27 if (!colors_.empty()) colors_.reserve(
size_t(
vertexCount) * 4u);
31 triangleGroups_.reserve(
size_t(
indexCount) / 3u);
37 positions_.push_back(
px);
38 positions_.push_back(
py);
39 positions_.push_back(
pz);
40 normals_.push_back(
nx);
41 normals_.push_back(
ny);
42 normals_.push_back(
nz);
45 if (!colors_.empty()) colors_.insert(colors_.end(), {1.f, 1.f, 1.f, 1.f});
49 indices_.push_back(
i0);
50 indices_.push_back(
i1);
51 indices_.push_back(
i2);
52 triangleGroups_.push_back(activeGroup_);
56 if (
name.empty()) { activeGroup_ = -1;
return -1; }
57 for (
int i = 0; i < int(groupNames_.size()); ++i) {
58 if (groupNames_[
size_t(i)] ==
name) { activeGroup_ = i;
return i; }
60 groupNames_.push_back(
name);
61 activeGroup_ = int(groupNames_.size()) - 1;
68 if (groupIndex < 0 || groupIndex >=
getGroupCount())
return {};
69 return groupNames_[size_t(groupIndex)];
73 if (triangleIndex < 0 || triangleIndex >=
int(triangleGroups_.size()))
return -1;
74 return triangleGroups_[size_t(triangleIndex)];
83 if (assignments.size() != indices_.size() / 3u ||
activeGroup < -1 ||
85 std::any_of(names.begin(), names.end(), [](
const std::string &
name) { return name.empty(); }) ||
86 std::any_of(assignments.begin(), assignments.end(),
87 [&](
int group) { return group < -1 || group >= static_cast<int>(names.size()); }))
88 return invalid(
"mesh group sidecar is inconsistent with dense mesh streams",
"groups");
89 groupNames_ = std::move(names);
90 triangleGroups_ = std::move(assignments);
96 if (groupIndex < 0 || groupIndex >=
getGroupCount())
return {};
97 auto result = std::make_unique<MeshBuild>();
98 result->setActiveGroup(groupNames_[
size_t(groupIndex)]);
99 std::unordered_map<uint32_t, uint32_t>
remap;
100 std::vector<float> copiedColors;
101 for (
int t = 0;
t < int(triangleGroups_.size()); ++
t) {
102 if (triangleGroups_[
size_t(
t)] != groupIndex)
continue;
104 for (
int corner = 0; corner < 3; ++corner) {
105 const uint32_t src = indices_[size_t(
t) * 3u + size_t(corner)];
108 const uint32_t next = uint32_t(
result->getVertexCount());
113 for (
int component = 0; component < 4; ++component)
114 copiedColors.push_back(
getColor(
int(src), component));
115 remap.emplace(src, next); dst[corner] = next;
116 }
else dst[corner] =
found->second;
118 result->addTriangle(dst[0], dst[1], dst[2]);
120 if (
result->empty())
return {};
121 if (!copiedColors.empty())
result->setVertexColors(std::move(copiedColors)).ignore(
"validated copied color stream");
123 result->setMeta(
"group", groupNames_[
size_t(groupIndex)]);
128 float yawDegrees,
float sx,
float sy,
float sz) {
129 if (!other || other ==
this || other->
empty() || std::fabs(
sx) < 1e-8f ||
130 std::fabs(
sy) < 1e-8f || std::fabs(
sz) < 1e-8f)
134 std::vector<int> groupMap(
size_t(other->
getGroupCount()), -1);
135 const int previousGroup = activeGroup_;
139 const float radians = yawDegrees * 0.01745329251994329577f;
140 const float c = std::cos(radians),
s = std::sin(radians);
148 float nx =
c * ix +
s * iz;
150 float nz = -
s * ix +
c * iz;
151 const float nl = std::sqrt(
nx *
nx +
ny *
ny +
nz *
nz);
152 if (nl > 1e-8f) {
nx /= nl;
ny /= nl;
nz /= nl; }
156 for (
int component = 0; component < 4; ++component)
157 colors_[colors_.size() - 4u + std::size_t(component)] = other->
getColor(i, component);
159 const bool mirrored = (
sx *
sy *
sz) < 0.f;
162 activeGroup_ = sourceGroup >= 0 ? groupMap[size_t(sourceGroup)] : -1;
163 const uint32_t
a = base + uint32_t(other->
getIndex(i));
164 const uint32_t
b = base + uint32_t(other->
getIndex(i + 1));
165 const uint32_t cidx = base + uint32_t(other->
getIndex(i + 2));
168 activeGroup_ = previousGroup;
178 return positions_[size_t(i) * 3u];
182 return positions_[size_t(i) * 3u + 1u];
186 return positions_[size_t(i) * 3u + 2u];
190 return normals_[size_t(i) * 3u];
194 return normals_[size_t(i) * 3u + 1u];
198 return normals_[size_t(i) * 3u + 2u];
202 return uvs_[size_t(i) * 2u];
206 return uvs_[size_t(i) * 2u + 1u];
209 return !colors_.empty() && colors_.size() == std::size_t(
getVertexCount()) * 4u;
212 if (!hasVertexColors() || vertexIndex < 0 || vertexIndex >= getVertexCount() || component < 0 || component > 3)
214 return colors_[std::size_t(vertexIndex) * 4u + std::size_t(component)];
219 "mesh vertex color count must match the vertex count",
"colors", {},
"procgen.mesh"));
220 if (std::any_of(
colors.begin(),
colors.end(), [](
float value) { return !std::isfinite(value); }))
222 "mesh vertex colors must be finite",
"colors", {},
"procgen.mesh"));
223 colors_ = std::move(
colors);
229 "mesh vertex index is out of range",
"vertexIndex", {},
"procgen.mesh"));
230 if (!std::isfinite(
r) || !std::isfinite(
g) || !std::isfinite(
b) || !std::isfinite(
a))
232 "mesh vertex color components must be finite",
"color", {},
"procgen.mesh"));
234 const auto base = std::size_t(vertexIndex) * 4u;
236 colors_[base + 1u] =
g;
237 colors_[base + 2u] =
b;
238 colors_[base + 3u] =
a;
243 return int(indices_[
size_t(i)]);
249 auto it = meta_.find(
key);
std::uint32_t vertexCount
ScriptMotionSinkStore< ScriptMotionSink< MotionColorTraits > > colors
std::unordered_map< std::uint32_t, std::uint32_t > remap
AbilityActivationGroup group
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.
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
CPU triangle mesh from procedural mesh recipes (e.g. marching cubes). Positions/normals are xyz-packe...
float getPositionZ(int i) const
Returns the position z.
eve::Result< void > setColor(int vertexIndex, float r, float g, float b, float a)
Set one vertex RGBA (allocates a default-white stream if absent).
float getPositionY(int i) const
Returns the position y.
std::string getMeta(const std::string &key, const std::string &defaultValue) const
Returns the meta.
void addVertex(float px, float py, float pz, float nx, float ny, float nz, float u, float v)
Adds vertex.
void addTriangle(uint32_t i0, uint32_t i1, uint32_t i2)
Adds triangle.
std::unique_ptr< MeshBuild > copyGroup(int groupIndex) const
Copies one named group into a standalone compact mesh.
float getNormalZ(int i) const
Returns the normal z.
int getGroupCount() const
Number of named triangle groups.
void reserve(int vertexCount, int indexCount)
Reserve.
void setMeta(const std::string &key, const std::string &value)
Sets the meta.
int setActiveGroup(const std::string &name)
Select/create the named group assigned to subsequently added triangles.
const std::vector< float > & colors() const
Colors.
float getNormalY(int i) const
Returns the normal y.
int getIndexCount() const
Returns the index count.
eve::Result< void > setVertexColors(std::vector< float > colors)
Atomically replace the optional packed RGBA stream; empty removes it.
eve::Result< void > restoreGroupData(std::vector< std::string > names, std::vector< int > assignments, int activeGroup)
Restore the complete group sidecar after dense streams are loaded.
int getTriangleGroup(int triangleIndex) const
Group index for a triangle (not an index-buffer element), or -1.
float getUvU(int i) const
Returns the uv u.
int getVertexCount() const
float getColor(int vertexIndex, int component) const noexcept
Return one color component, or 1 for an invalid vertex/component or an absent color stream.
bool appendTransformed(const MeshBuild *other, float tx, float ty, float tz, float yawDegrees, float sx, float sy, float sz)
Append another procedural mesh after an affine transform.
int activeGroup() const
Return the currently selected group, or -1 when none is active.
float getUvV(int i) const
Returns the uv v.
float getNormalX(int i) const
Returns the normal x.
int getIndex(int i) const
std::string getGroupName(int groupIndex) const
Group name, or an empty string for an invalid index.
bool hasVertexColors() const noexcept
Return whether this mesh owns one packed RGBA color per vertex.
float getPositionX(int i) const
Returns the position x.
Result< int > invalid(std::string message)
Invalid.
@ ProcgenGroupDataInvalid
Mesh group sidecar data is inconsistent with its dense streams.