载入中...
搜索中...
未找到
MeshBuild.cpp
浏览该文件的文档.
1#include "procgen/MeshBuild.h"
2
3#include <utility>
4
5#include <algorithm>
6#include <cmath>
7
8namespace eve::procgen {
9
11 positions_.clear();
12 normals_.clear();
13 uvs_.clear();
14 colors_.clear();
15 indices_.clear();
16 triangleGroups_.clear();
17 groupNames_.clear();
18 activeGroup_ = -1;
19 meta_.clear();
20}
21
23 if (vertexCount > 0) {
24 positions_.reserve(size_t(vertexCount) * 3u);
25 normals_.reserve(size_t(vertexCount) * 3u);
26 uvs_.reserve(size_t(vertexCount) * 2u);
27 if (!colors_.empty()) colors_.reserve(size_t(vertexCount) * 4u);
28 }
29 if (indexCount > 0) {
30 indices_.reserve(size_t(indexCount));
31 triangleGroups_.reserve(size_t(indexCount) / 3u);
32 }
33}
34
35void MeshBuild::addVertex(float px, float py, float pz, float nx, float ny, float nz, float u,
36 float v) {
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);
43 uvs_.push_back(u);
44 uvs_.push_back(v);
45 if (!colors_.empty()) colors_.insert(colors_.end(), {1.f, 1.f, 1.f, 1.f});
46}
47
48void MeshBuild::addTriangle(uint32_t i0, uint32_t i1, uint32_t i2) {
49 indices_.push_back(i0);
50 indices_.push_back(i1);
51 indices_.push_back(i2);
52 triangleGroups_.push_back(activeGroup_);
53}
54
55int MeshBuild::setActiveGroup(const std::string &name) {
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; }
59 }
60 groupNames_.push_back(name);
61 activeGroup_ = int(groupNames_.size()) - 1;
62 return activeGroup_;
63}
64
65int MeshBuild::getGroupCount() const { return int(groupNames_.size()); }
66
67std::string MeshBuild::getGroupName(int groupIndex) const {
68 if (groupIndex < 0 || groupIndex >= getGroupCount()) return {};
69 return groupNames_[size_t(groupIndex)];
70}
71
72int MeshBuild::getTriangleGroup(int triangleIndex) const {
73 if (triangleIndex < 0 || triangleIndex >= int(triangleGroups_.size())) return -1;
74 return triangleGroups_[size_t(triangleIndex)];
75}
76
77eve::Result<void> MeshBuild::restoreGroupData(std::vector<std::string> names, std::vector<int> assignments,
78 int activeGroup) {
79 const auto invalid = [&](std::string message, std::string path) {
81 eve::DiagnosticCode::ProcgenGroupDataInvalid, std::move(message), std::move(path), {}, "procgen.mesh"));
82 };
83 if (assignments.size() != indices_.size() / 3u || activeGroup < -1 ||
84 activeGroup >= static_cast<int>(names.size()) ||
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);
91 activeGroup_ = activeGroup;
93}
94
95std::unique_ptr<MeshBuild> MeshBuild::copyGroup(int groupIndex) const {
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;
103 uint32_t dst[3];
104 for (int corner = 0; corner < 3; ++corner) {
105 const uint32_t src = indices_[size_t(t) * 3u + size_t(corner)];
106 auto found = remap.find(src);
107 if (found == remap.end()) {
108 const uint32_t next = uint32_t(result->getVertexCount());
109 result->addVertex(getPositionX(int(src)), getPositionY(int(src)), getPositionZ(int(src)),
110 getNormalX(int(src)), getNormalY(int(src)), getNormalZ(int(src)),
111 getUvU(int(src)), getUvV(int(src)));
112 if (hasVertexColors())
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;
117 }
118 result->addTriangle(dst[0], dst[1], dst[2]);
119 }
120 if (result->empty()) return {};
121 if (!copiedColors.empty()) result->setVertexColors(std::move(copiedColors)).ignore("validated copied color stream");
122 result->meta_ = meta_;
123 result->setMeta("group", groupNames_[size_t(groupIndex)]);
124 return result;
125}
126
127bool MeshBuild::appendTransformed(const MeshBuild *other, float tx, float ty, float tz,
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)
131 return false;
132 const uint32_t base = uint32_t(getVertexCount());
133 if (other->hasVertexColors() && !hasVertexColors()) colors_.assign(std::size_t(base) * 4u, 1.f);
134 std::vector<int> groupMap(size_t(other->getGroupCount()), -1);
135 const int previousGroup = activeGroup_;
136 for (int i = 0; i < other->getGroupCount(); ++i)
137 groupMap[size_t(i)] = setActiveGroup(other->getGroupName(i));
139 const float radians = yawDegrees * 0.01745329251994329577f;
140 const float c = std::cos(radians), s = std::sin(radians);
141 for (int i = 0; i < other->getVertexCount(); ++i) {
142 const float px = other->getPositionX(i) * sx;
143 const float py = other->getPositionY(i) * sy;
144 const float pz = other->getPositionZ(i) * sz;
145 const float ix = other->getNormalX(i) / sx;
146 const float iy = other->getNormalY(i) / sy;
147 const float iz = other->getNormalZ(i) / sz;
148 float nx = c * ix + s * iz;
149 float ny = iy;
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; }
153 addVertex(c * px + s * pz + tx, py + ty, -s * px + c * pz + tz,
154 nx, ny, nz, other->getUvU(i), other->getUvV(i));
155 if (other->hasVertexColors())
156 for (int component = 0; component < 4; ++component)
157 colors_[colors_.size() - 4u + std::size_t(component)] = other->getColor(i, component);
158 }
159 const bool mirrored = (sx * sy * sz) < 0.f;
160 for (int i = 0; i + 2 < other->getIndexCount(); i += 3) {
161 const int sourceGroup = other->getTriangleGroup(i / 3);
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));
166 if (mirrored) addTriangle(a, cidx, b); else addTriangle(a, b, cidx);
167 }
168 activeGroup_ = previousGroup;
169 return true;
170}
171
172int MeshBuild::getVertexCount() const { return int(positions_.size() / 3u); }
173int MeshBuild::getIndexCount() const { return int(indices_.size()); }
174bool MeshBuild::empty() const { return positions_.empty() || indices_.empty(); }
175
176float MeshBuild::getPositionX(int i) const {
177 if (i < 0 || i >= getVertexCount()) return 0.f;
178 return positions_[size_t(i) * 3u];
179}
180float MeshBuild::getPositionY(int i) const {
181 if (i < 0 || i >= getVertexCount()) return 0.f;
182 return positions_[size_t(i) * 3u + 1u];
183}
184float MeshBuild::getPositionZ(int i) const {
185 if (i < 0 || i >= getVertexCount()) return 0.f;
186 return positions_[size_t(i) * 3u + 2u];
187}
188float MeshBuild::getNormalX(int i) const {
189 if (i < 0 || i >= getVertexCount()) return 0.f;
190 return normals_[size_t(i) * 3u];
191}
192float MeshBuild::getNormalY(int i) const {
193 if (i < 0 || i >= getVertexCount()) return 0.f;
194 return normals_[size_t(i) * 3u + 1u];
195}
196float MeshBuild::getNormalZ(int i) const {
197 if (i < 0 || i >= getVertexCount()) return 0.f;
198 return normals_[size_t(i) * 3u + 2u];
199}
200float MeshBuild::getUvU(int i) const {
201 if (i < 0 || i >= getVertexCount()) return 0.f;
202 return uvs_[size_t(i) * 2u];
203}
204float MeshBuild::getUvV(int i) const {
205 if (i < 0 || i >= getVertexCount()) return 0.f;
206 return uvs_[size_t(i) * 2u + 1u];
207}
208bool MeshBuild::hasVertexColors() const noexcept {
209 return !colors_.empty() && colors_.size() == std::size_t(getVertexCount()) * 4u;
210}
211float MeshBuild::getColor(int vertexIndex, int component) const noexcept {
212 if (!hasVertexColors() || vertexIndex < 0 || vertexIndex >= getVertexCount() || component < 0 || component > 3)
213 return 1.f;
214 return colors_[std::size_t(vertexIndex) * 4u + std::size_t(component)];
215}
217 if (!colors.empty() && colors.size() != std::size_t(getVertexCount()) * 4u)
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);
225}
226eve::Result<void> MeshBuild::setColor(int vertexIndex, float r, float g, float b, float a) {
227 if (vertexIndex < 0 || vertexIndex >= getVertexCount())
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"));
233 if (!hasVertexColors()) colors_.assign(std::size_t(getVertexCount()) * 4u, 1.f);
234 const auto base = std::size_t(vertexIndex) * 4u;
235 colors_[base] = r;
236 colors_[base + 1u] = g;
237 colors_[base + 2u] = b;
238 colors_[base + 3u] = a;
240}
241int MeshBuild::getIndex(int i) const {
242 if (i < 0 || i >= getIndexCount()) return 0;
243 return int(indices_[size_t(i)]);
244}
245
246void MeshBuild::setMeta(const std::string &key, const std::string &value) { meta_[key] = value; }
247
248std::string MeshBuild::getMeta(const std::string &key, const std::string &defaultValue) const {
249 auto it = meta_.find(key);
250 return it == meta_.end() ? defaultValue : it->second;
251}
252
253} // namespace eve::procgen
const std::string & s
SQObjectPtr key
Definition ECS.cpp:155
std::string name
Definition ECS.cpp:154
std::string message
std::uint32_t vertexCount
std::uint32_t indexCount
tensor::Graph g
Definition GpuGraph.cpp:7
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
double r
float v
std::int32_t c
std::vector< Colorf > px
Poco::JSON::Object::Ptr result
double defaultValue
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
ScriptMotionSinkStore< ScriptMotionSink< MotionColorTraits > > colors
std::string path
Definition PlayHost.cpp:111
Object::Ptr value
float t
std::unordered_map< std::uint32_t, std::uint32_t > remap
bool found
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.
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
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.cpp:19
CPU triangle mesh from procedural mesh recipes (e.g. marching cubes). Positions/normals are xyz-packe...
Definition MeshBuild.h:20
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.
Definition MeshBuild.cpp:35
void addTriangle(uint32_t i0, uint32_t i1, uint32_t i2)
Adds triangle.
Definition MeshBuild.cpp:48
std::unique_ptr< MeshBuild > copyGroup(int groupIndex) const
Copies one named group into a standalone compact mesh.
Definition MeshBuild.cpp:95
float getNormalZ(int i) const
Returns the normal z.
int getGroupCount() const
Number of named triangle groups.
Definition MeshBuild.cpp:65
void reserve(int vertexCount, int indexCount)
Reserve.
Definition MeshBuild.cpp:22
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.
Definition MeshBuild.cpp:55
const std::vector< float > & colors() const
Colors.
Definition MeshBuild.h:132
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.
Definition MeshBuild.cpp:77
int getTriangleGroup(int triangleIndex) const
Group index for a triangle (not an index-buffer element), or -1.
Definition MeshBuild.cpp:72
float getUvU(int i) const
Returns the uv u.
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 empty() const
Empty.
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.
void clear()
Clears .
Definition MeshBuild.cpp:10
int activeGroup() const
Return the currently selected group, or -1 when none is active.
Definition MeshBuild.h:65
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.
Definition MeshBuild.cpp:67
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.