载入中...
搜索中...
未找到
PrototypeKit.cpp
浏览该文件的文档.
2#include <algorithm>
3#include <array>
4#include <cmath>
5#include <cstdint>
6#include <string>
10
11// PrototypeKit.cpp
12namespace eve::procgen {
13namespace {
14
20using prototype_detail::Vec3;
21
22constexpr std::array<PrototypePieceDescriptor, 75> kPieces{{
23 {"arrow", "Arrow", 1.0f, 0.18f, 2.0f},
24 {"box", "Open Box", 1.5f, 1.0f, 1.5f},
25 {"coin", "Coin", 0.8f, 0.12f, 0.8f},
26 {"cone", "Cone", 1.0f, 1.5f, 1.0f},
27 {"cone1", "Cone Low", 1.2f, 0.8f, 1.2f},
28 {"cone2", "Cone Tall", 0.8f, 2.0f, 0.8f},
29 {"cone3", "Frustum", 1.0f, 1.5f, 1.0f},
30 {"cone4", "Pyramid", 1.2f, 1.5f, 1.2f},
31 {"cube", "Cube", 1.0f, 1.0f, 1.0f},
32 {"cube4", "Low Block", 1.0f, 0.5f, 1.0f},
33 {"cube5", "Tall Block", 1.0f, 2.0f, 1.0f},
34 {"cube6", "Wide Block", 2.0f, 1.0f, 1.0f},
35 {"cube7", "Platform Block", 2.0f, 0.35f, 2.0f},
36 {"cube8", "Beam Block", 3.0f, 0.4f, 0.4f},
37 {"cube9", "Inset Block", 1.2f, 1.2f, 1.2f},
38 {"cylinder", "Cylinder", 1.0f, 1.0f, 1.0f},
39 {"cylinder1", "Cylinder Detailed", 1.0f, 1.0f, 1.0f},
40 {"cylinder2", "Cylinder Low", 1.3f, 0.5f, 1.3f},
41 {"cylinder3", "Cylinder Tall", 0.8f, 2.0f, 0.8f},
42 {"cylinder4", "Hex Cylinder", 1.0f, 1.0f, 1.0f},
43 {"cylinder5", "Octagonal Column", 1.0f, 2.0f, 1.0f},
44 {"door", "Door", 1.0f, 2.1f, 0.15f},
45 {"door1", "Door Framed", 1.2f, 2.2f, 0.2f},
46 {"door2", "Door Double", 1.8f, 2.2f, 0.18f},
47 {"door3", "Door Reinforced", 1.2f, 2.2f, 0.22f},
48 {"fence-edge", "Fence Edge", 0.4f, 1.2f, 0.4f},
49 {"fence-wood", "Wood Fence", 2.5f, 1.2f, 0.2f},
50 {"fence", "Fence", 2.5f, 1.2f, 0.16f},
51 {"fence2", "Fence Dense", 2.5f, 1.4f, 0.18f},
52 {"fence3", "Fence Low", 2.5f, 0.8f, 0.18f},
53 {"ground-corner", "Ground Corner", 2.0f, 0.15f, 2.0f},
54 {"ground", "Ground Tile", 2.0f, 0.12f, 2.0f},
55 {"ground1", "Ground Slab", 2.0f, 0.3f, 2.0f},
56 {"key", "Key", 1.8f, 0.12f, 0.7f},
57 {"ladder", "Ladder", 1.0f, 2.5f, 0.16f},
58 {"ladder1", "Wide Ladder", 1.4f, 3.0f, 0.18f},
59 {"pillar", "Pillar", 0.8f, 2.5f, 0.8f},
60 {"pillar1", "Pillar Square", 0.8f, 2.5f, 0.8f},
61 {"pillar2", "Pillar Round", 0.8f, 2.5f, 0.8f},
62 {"pillar3", "Pillar Heavy", 1.0f, 3.0f, 1.0f},
63 {"pillar4", "Pillar Tapered", 0.9f, 2.5f, 0.9f},
64 {"railing-edge", "Railing Edge", 0.35f, 1.0f, 0.35f},
65 {"railing", "Railing", 2.5f, 1.0f, 0.12f},
66 {"ramp", "Ramp", 2.0f, 1.0f, 2.0f},
67 {"ramp1", "Ramp Wide", 3.0f, 1.2f, 2.0f},
68 {"sphere", "Sphere", 1.0f, 1.0f, 1.0f},
69 {"sphere1", "Sphere Detailed", 1.0f, 1.0f, 1.0f},
70 {"sphere2", "Ellipsoid", 1.2f, 1.8f, 1.2f},
71 {"spike", "Spike", 0.5f, 1.2f, 0.5f},
72 {"spikes-big", "Spikes Big", 2.0f, 1.2f, 2.0f},
73 {"spikes-small", "Spikes Small", 2.0f, 0.6f, 2.0f},
74 {"stairs-corner", "Stairs Corner", 2.0f, 1.5f, 2.0f},
75 {"stairs-corner1", "Stairs Corner Wide", 3.0f, 1.5f, 3.0f},
76 {"stairs", "Stairs", 2.0f, 1.5f, 2.5f},
77 {"stairs1", "Stairs Solid", 2.0f, 1.5f, 2.5f},
78 {"stairs2", "Stairs Low", 2.0f, 0.8f, 2.5f},
79 {"toggle-switch", "Toggle Switch", 0.8f, 0.6f, 0.5f},
80 {"torus", "Torus", 1.4f, 0.35f, 1.4f},
81 {"wall-corner-bottom", "Wall Corner Bottom", 2.0f, 1.0f, 2.0f},
82 {"wall-corner-bottom1", "Wall Corner Bottom Thin", 2.0f, 0.6f, 2.0f},
83 {"wall-corner-bottom2", "Wall Corner Bottom Heavy", 2.0f, 1.2f, 2.0f},
84 {"wall-corner-top", "Wall Corner Top", 2.0f, 1.0f, 2.0f},
85 {"wall-corner-top1", "Wall Corner Top Thin", 2.0f, 0.6f, 2.0f},
86 {"wall-corner", "Wall Corner", 2.0f, 3.0f, 2.0f},
87 {"wall-corner1", "Wall Corner Short", 2.0f, 2.0f, 2.0f},
88 {"wall-corner4", "Wall Corner Heavy", 2.0f, 3.0f, 2.0f},
89 {"wall-door", "Wall Door", 3.0f, 3.0f, 0.25f},
90 {"wall-door1", "Wall Door Wide", 4.0f, 3.0f, 0.25f},
91 {"wall-door2", "Wall Door Tall", 3.0f, 4.0f, 0.25f},
92 {"wall-window", "Wall Window", 3.0f, 3.0f, 0.25f},
93 {"wall-window1", "Wall Window Wide", 4.0f, 3.0f, 0.25f},
94 {"wall", "Wall", 3.0f, 3.0f, 0.25f},
95 {"wall1", "Wall Half", 3.0f, 1.5f, 0.25f},
96 {"window", "Window", 1.2f, 1.2f, 0.15f},
97 {"window1", "Window Wide", 1.8f, 1.2f, 0.15f},
98}};
99
100const PrototypePieceDescriptor* findPiece(std::string_view id) {
101 const auto it = std::find_if(kPieces.begin(), kPieces.end(), [id](const auto& piece) { return piece.id == id; });
102 return it == kPieces.end() ? nullptr : &*it;
103}
104
105struct BuildContext {
106 MeshBuild mesh;
107 float w;
108 float h;
109 float d;
110 float t;
112 int steps;
113};
114
115float positiveParam(const Params& params, const char* name, float fallback) {
116 return params.has(name) ? params.getFloat(name, fallback) : fallback;
117}
118
119void addFrame(BuildContext& c, float openingWidth, float openingHeight, float sill, const char* group) {
120 const float side = std::max(c.t, (c.w - openingWidth) * 0.5f);
121 addBox(c.mesh, {-c.w * 0.5f + side * 0.5f, c.h * 0.5f, 0}, {side, c.h, c.d}, group);
122 addBox(c.mesh, {c.w * 0.5f - side * 0.5f, c.h * 0.5f, 0}, {side, c.h, c.d}, group);
123 if (sill > 0.0f) addBox(c.mesh, {0, sill * 0.5f, 0}, {openingWidth, sill, c.d}, group);
124 const float top = std::max(c.t, c.h - sill - openingHeight);
125 addBox(c.mesh, {0, c.h - top * 0.5f, 0}, {openingWidth, top, c.d}, group);
126}
127
128void addCorner(BuildContext& c, const char* group) {
129 addBox(c.mesh, {0, c.h * 0.5f, -c.d * 0.5f + c.t * 0.5f}, {c.w, c.h, c.t}, group);
130 addBox(c.mesh, {-c.w * 0.5f + c.t * 0.5f, c.h * 0.5f, 0}, {c.t, c.h, c.d}, group);
131}
132
133void addFence(BuildContext& c, int posts, int rails, bool pickets) {
134 posts = std::max(2, posts);
135 for (int i = 0; i < posts; ++i) {
136 const float x = -c.w * 0.5f + c.w * float(i) / float(posts - 1);
137 addBox(c.mesh, {x, c.h * 0.5f, 0}, {c.t, c.h, c.d}, "posts");
138 }
139 for (int i = 0; i < rails; ++i) {
140 const float y = c.h * float(i + 1) / float(rails + 1);
141 addBox(c.mesh, {0, y, 0}, {c.w, c.t, c.d * 0.75f}, "rails");
142 }
143 if (pickets) {
144 const int count = std::max(3, posts * 2 - 1);
145 for (int i = 1; i < count - 1; ++i) {
146 const float x = -c.w * 0.5f + c.w * float(i) / float(count - 1);
147 addBox(c.mesh, {x, c.h * 0.45f, 0}, {c.t * 0.55f, c.h * 0.9f, c.d * 0.6f}, "pickets");
148 }
149 }
150}
151
152void addLadder(BuildContext& c) {
153 addBox(c.mesh, {-c.w * 0.5f + c.t * 0.5f, c.h * 0.5f, 0}, {c.t, c.h, c.d}, "rails");
154 addBox(c.mesh, {c.w * 0.5f - c.t * 0.5f, c.h * 0.5f, 0}, {c.t, c.h, c.d}, "rails");
155 for (int i = 1; i < c.steps; ++i) {
156 addBox(c.mesh, {0, c.h * float(i) / float(c.steps), 0}, {c.w, c.t * 0.75f, c.d * 0.75f}, "rungs");
157 }
158}
159
160void addStairs(BuildContext& c, bool corner) {
161 for (int i = 0; i < c.steps; ++i) {
162 const float fraction = float(i + 1) / float(c.steps);
163 const float stepDepth = c.d / float(c.steps);
164 const float z = c.d * 0.5f - stepDepth * (float(i) + 0.5f);
165 addBox(c.mesh, {0, c.h * fraction * 0.5f, z}, {c.w, c.h * fraction, stepDepth}, "steps");
166 if (corner) {
167 const float x = c.w * 0.5f - stepDepth * (float(i) + 0.5f);
168 addBox(c.mesh, {x, c.h * fraction * 0.5f, 0}, {stepDepth, c.h * fraction, c.d}, "corner-steps");
169 }
170 }
171}
172
173void buildPiece(std::string_view id, BuildContext& c) {
174 if (id == "arrow") {
175 addBox(c.mesh, {0, c.h * 0.5f, -c.d * 0.15f}, {c.w * 0.22f, c.h, c.d * 0.7f}, "shaft");
176 addWedge(c.mesh, {0, c.h * 0.5f, c.d * 0.35f}, {c.w, c.h, c.d * 0.3f}, false, "head");
177 } else if (id == "box") {
178 addBox(c.mesh, {0, c.t * 0.5f, 0}, {c.w, c.t, c.d}, "base");
179 addBox(c.mesh, {-c.w * 0.5f + c.t * 0.5f, c.h * 0.5f, 0}, {c.t, c.h, c.d}, "walls");
180 addBox(c.mesh, {c.w * 0.5f - c.t * 0.5f, c.h * 0.5f, 0}, {c.t, c.h, c.d}, "walls");
181 addBox(c.mesh, {0, c.h * 0.5f, -c.d * 0.5f + c.t * 0.5f}, {c.w, c.h, c.t}, "walls");
182 addBox(c.mesh, {0, c.h * 0.5f, c.d * 0.5f - c.t * 0.5f}, {c.w, c.h, c.t}, "walls");
183 } else if (id == "coin") {
184 addCylinder(c.mesh, {0, c.h * 0.5f, 0}, c.w * 0.5f, c.w * 0.5f, c.h, c.detail, "coin");
185 } else if (id.starts_with("cone")) {
186 int variant = id == "cone" ? 0 : id.back() - '0';
187 const int sides = variant == 4 ? 4 : c.detail;
188 const float top = variant == 3 ? c.w * 0.22f : 0.0f;
189 addCylinder(c.mesh, {0, c.h * 0.5f, 0}, c.w * 0.5f, top, c.h, sides, "body");
190 } else if (id.starts_with("cube")) {
191 addBox(c.mesh, {0, c.h * 0.5f, 0}, {c.w, c.h, c.d}, "body");
192 if (id == "cube9") addBox(c.mesh, {0, c.h + c.t * 0.25f, 0}, {c.w * 0.72f, c.t * 0.5f, c.d * 0.72f}, "inset");
193 } else if (id.starts_with("cylinder")) {
194 const int variant = id == "cylinder" ? 0 : id.back() - '0';
195 const int sides = variant == 4 ? 6 : (variant == 5 ? 8 : c.detail + (variant == 1 ? 8 : 0));
196 addCylinder(c.mesh, {0, c.h * 0.5f, 0}, c.w * 0.5f, c.w * 0.5f, c.h, sides, "body");
197 } else if (id.starts_with("door")) {
198 addBox(c.mesh, {0, c.h * 0.5f, 0}, {c.w, c.h, c.d}, "panel");
199 if (id != "door") {
200 addFrame(c, c.w - 2.0f * c.t, c.h - 2.0f * c.t, 0.0f, "frame");
201 if (id == "door3") addBox(c.mesh, {0, c.h * 0.55f, c.d}, {c.w * 0.9f, c.t, c.t}, "brace");
202 }
203 } else if (id.starts_with("fence")) {
204 const bool edge = id == "fence-edge";
205 if (edge)
206 addBox(c.mesh, {0, c.h * 0.5f, 0}, {c.w, c.h, c.d}, "post");
207 else
208 addFence(c, id == "fence2" ? 7 : 5, id == "fence3" ? 1 : 2, id == "fence-wood");
209 } else if (id.starts_with("ground")) {
210 if (id == "ground-corner") {
211 addBox(c.mesh, {0, c.h * 0.5f, -c.d * 0.25f}, {c.w, c.h, c.d * 0.5f}, "ground");
212 addBox(c.mesh, {-c.w * 0.25f, c.h * 0.5f, c.d * 0.25f}, {c.w * 0.5f, c.h, c.d * 0.5f}, "ground");
213 } else
214 addBox(c.mesh, {0, c.h * 0.5f, 0}, {c.w, c.h, c.d}, "ground");
215 } else if (id == "key") {
216 addTorus(c.mesh, {-c.w * 0.35f, c.h * 0.5f, 0}, c.d * 0.28f, c.t * 0.45f, c.detail, 6, "ring");
217 addBox(c.mesh, {c.w * 0.1f, c.h * 0.5f, 0}, {c.w * 0.65f, c.t, c.t}, "shaft");
218 addBox(c.mesh, {c.w * 0.38f, c.h * 0.5f, c.d * 0.13f}, {c.t, c.t, c.d * 0.35f}, "teeth");
219 } else if (id.starts_with("ladder")) {
220 addLadder(c);
221 } else if (id.starts_with("pillar")) {
222 const bool square = id == "pillar1";
223 if (square)
224 addBox(c.mesh, {0, c.h * 0.5f, 0}, {c.w * 0.65f, c.h, c.d * 0.65f}, "shaft");
225 else
226 addCylinder(c.mesh, {0, c.h * 0.5f, 0}, c.w * 0.3f, id == "pillar4" ? c.w * 0.22f : c.w * 0.3f, c.h,
227 id == "pillar2" ? c.detail : 8, "shaft");
228 addBox(c.mesh, {0, c.t * 0.5f, 0}, {c.w, c.t, c.d}, "base");
229 addBox(c.mesh, {0, c.h - c.t * 0.5f, 0}, {c.w, c.t, c.d}, "capital");
230 } else if (id.starts_with("railing")) {
231 if (id == "railing-edge")
232 addBox(c.mesh, {0, c.h * 0.5f, 0}, {c.w, c.h, c.d}, "post");
233 else
234 addFence(c, 5, 1, false);
235 } else if (id.starts_with("ramp")) {
236 addWedge(c.mesh, {0, c.h * 0.5f, 0}, {c.w, c.h, c.d}, false, "ramp");
237 } else if (id.starts_with("sphere")) {
238 addSphere(c.mesh, {0, c.h * 0.5f, 0}, {c.w * 0.5f, c.h * 0.5f, c.d * 0.5f},
239 id == "sphere1" ? std::max(4, c.detail / 2) : std::max(3, c.detail / 3),
240 id == "sphere1" ? c.detail * 2 : c.detail, "body");
241 } else if (id == "spike") {
242 addCylinder(c.mesh, {0, c.h * 0.5f, 0}, c.w * 0.5f, 0.0f, c.h, 4, "spike");
243 } else if (id.starts_with("spikes")) {
244 const int count = id == "spikes-big" ? 3 : 5;
245 for (int z = 0; z < count; ++z)
246 for (int x = 0; x < count; ++x) {
247 const float px = -c.w * 0.5f + c.w * (float(x) + 0.5f) / float(count);
248 const float pz = -c.d * 0.5f + c.d * (float(z) + 0.5f) / float(count);
249 addCylinder(c.mesh, {px, c.h * 0.5f, pz}, std::min(c.w, c.d) * 0.35f / float(count), 0, c.h, 4,
250 "spikes");
251 }
252 } else if (id.starts_with("stairs")) {
253 addStairs(c, id.starts_with("stairs-corner"));
254 } else if (id == "toggle-switch") {
255 addBox(c.mesh, {0, c.t * 0.5f, 0}, {c.w, c.t, c.d}, "base");
256 addWedge(c.mesh, {0, c.t + (c.h - c.t) * 0.5f, 0}, {c.w * 0.3f, c.h - c.t, c.d * 0.3f}, false, "lever");
257 } else if (id == "torus") {
258 addTorus(c.mesh, {0, c.h * 0.5f, 0}, c.w * 0.36f, c.h * 0.5f, c.detail * 2, std::max(4, c.detail / 2), "ring");
259 } else if (id.starts_with("wall-corner")) {
260 addCorner(c, "wall");
261 } else if (id.starts_with("wall-door")) {
262 addFrame(c, c.w * 0.42f, c.h * 0.72f, 0.0f, "wall");
263 } else if (id.starts_with("wall-window")) {
264 addFrame(c, c.w * 0.5f, c.h * 0.42f, c.h * 0.28f, "wall");
265 } else if (id.starts_with("wall")) {
266 addBox(c.mesh, {0, c.h * 0.5f, 0}, {c.w, c.h, c.d}, "wall");
267 } else if (id.starts_with("window")) {
268 addFrame(c, c.w - 2.0f * c.t, c.h - 2.0f * c.t, c.t, "frame");
269 addBox(c.mesh, {0, c.h * 0.5f, 0}, {c.t * 0.6f, c.h - 2.0f * c.t, c.d * 0.55f}, "mullion");
270 }
271}
272
273RecipeDescriptor recipeDescriptor(const PrototypePieceDescriptor& piece) {
274 RecipeDescriptor schema{"prototype." + std::string(piece.id), std::string(piece.displayName), "Prototype 3D", {}};
275 schema.params.push_back(ParamDescriptor::floating("scale", "Uniform Scale", 1.0f, 0.01f, 100.0f, 0.05f));
276 schema.params.push_back(ParamDescriptor::floating("width", "Width", piece.defaultWidth, 0.01f, 1000.0f, 0.05f));
277 schema.params.push_back(ParamDescriptor::floating("height", "Height", piece.defaultHeight, 0.01f, 1000.0f, 0.05f));
278 schema.params.push_back(ParamDescriptor::floating("depth", "Depth", piece.defaultDepth, 0.01f, 1000.0f, 0.05f));
279 schema.params.push_back(ParamDescriptor::floating("thickness", "Member Thickness", 0.12f, 0.005f, 100.0f, 0.01f));
280 schema.params.push_back(ParamDescriptor::integer("detail", "Radial Detail", 12, 3, 64));
281 schema.params.push_back(ParamDescriptor::integer("steps", "Steps / Rungs", 6, 2, 64));
282 schema.params.push_back(
283 ParamDescriptor::floating("uvScale", "UV Tiles per World Unit", 0.5f, 0.01f, 100.0f, 0.05f));
284 return schema;
285}
286
287} // namespace
288
289std::span<const PrototypePieceDescriptor> prototypePieceDescriptors() noexcept { return kPieces; }
290
292 const PrototypePieceDescriptor* piece = findPiece(pieceId);
293 if (!piece) {
295 eve::Diagnostic::error(eve::DiagnosticCode::NotFound, "unknown prototype piece", "pieceId"));
296 }
297 const float scale = positiveParam(params, "scale", 1.0f);
298 const float uvScale = positiveParam(params, "uvScale", 0.5f);
299 BuildContext context{{},
300 positiveParam(params, "width", piece->defaultWidth) * scale,
301 positiveParam(params, "height", piece->defaultHeight) * scale,
302 positiveParam(params, "depth", piece->defaultDepth) * scale,
303 positiveParam(params, "thickness", 0.12f) * scale,
304 std::clamp(params.getInt("detail", 12), 3, 64),
305 std::clamp(params.getInt("steps", 6), 2, 64)};
306 if (!std::isfinite(scale) || scale <= 0.0f || !std::isfinite(context.w) || !std::isfinite(context.h) ||
307 !std::isfinite(uvScale) || !std::isfinite(context.d) || !std::isfinite(context.t) || context.w <= 0.0f ||
308 context.h <= 0.0f || context.d <= 0.0f || context.t <= 0.0f) {
311 "prototype dimensions and scale must be finite and positive", "params"));
312 }
313 context.t = std::min({context.t, context.w * 0.45f, context.h * 0.45f, context.d * 0.45f});
314 buildPiece(pieceId, context);
315 if (context.mesh.empty()) {
317 eve::DiagnosticCode::Failed, "prototype generator produced an empty mesh", "pieceId"));
318 }
319 for (float& uv : context.mesh.uvs()) uv *= uvScale;
320 context.mesh.setMeta("recipe", "prototype." + std::string(pieceId));
321 context.mesh.setMeta("origin", "ground-center");
322 context.mesh.setMeta("source", "procedural");
324}
325
327 for (const auto& piece : kPieces) {
328 const std::string id(piece.id);
329 registry.registerRecipe(recipeDescriptor(piece),
330 [id](const Params& params, MeshBuild& out, std::string& error) {
331 auto generated = generatePrototypePiece(id, params);
332 if (!generated.ok()) {
333 error = generated.status().describe();
334 return false;
335 }
336 out = std::move(generated).takeValue();
337 return true;
338 });
339 }
340}
341
342} // namespace eve::procgen
343
344// PrototypeKitPrimitives.cpp
346namespace {
347
348constexpr float kPi = 3.14159265358979323846f;
349
350uint32_t addVertex(MeshBuild& mesh, Vec3 p, Vec3 n, float u, float v) {
351 const auto index = static_cast<uint32_t>(mesh.getVertexCount());
352 mesh.addVertex(p.x, p.y, p.z, n.x, n.y, n.z, u, v);
353 return index;
354}
355
356struct UV {
357 float u;
358 float v;
359};
360
361UV projectUv(Vec3 p, Vec3 normal) {
362 const float ax = std::abs(normal.x);
363 const float ay = std::abs(normal.y);
364 const float az = std::abs(normal.z);
365 if (ay >= ax && ay >= az) return {p.x, p.z};
366 if (ax >= az) return {p.z, p.y};
367 return {p.x, p.y};
368}
369
370void addQuad(MeshBuild& mesh, Vec3 a, Vec3 b, Vec3 c, Vec3 d, Vec3 normal) {
371 const uint32_t base = static_cast<uint32_t>(mesh.getVertexCount());
372 const UV uvA = projectUv(a, normal);
373 const UV uvB = projectUv(b, normal);
374 const UV uvC = projectUv(c, normal);
375 const UV uvD = projectUv(d, normal);
376 addVertex(mesh, a, normal, uvA.u, uvA.v);
377 addVertex(mesh, b, normal, uvB.u, uvB.v);
378 addVertex(mesh, c, normal, uvC.u, uvC.v);
379 addVertex(mesh, d, normal, uvD.u, uvD.v);
380 mesh.addTriangle(base, base + 1, base + 2);
381 mesh.addTriangle(base, base + 2, base + 3);
382}
383
384Vec3 normalOf(Vec3 a, Vec3 b, Vec3 c) {
385 const Vec3 u{b.x - a.x, b.y - a.y, b.z - a.z};
386 const Vec3 v{c.x - a.x, c.y - a.y, c.z - a.z};
387 Vec3 n{u.y * v.z - u.z * v.y, u.z * v.x - u.x * v.z, u.x * v.y - u.y * v.x};
388 const float length = std::sqrt(n.x * n.x + n.y * n.y + n.z * n.z);
389 if (length > 1.0e-6f) {
390 n.x /= length;
391 n.y /= length;
392 n.z /= length;
393 }
394 return n;
395}
396
397} // namespace
398
399void addBox(MeshBuild& mesh, Vec3 center, Vec3 size, const char* group) {
400 mesh.setActiveGroup(group);
401 const float x0 = center.x - size.x * 0.5f;
402 const float x1 = center.x + size.x * 0.5f;
403 const float y0 = center.y - size.y * 0.5f;
404 const float y1 = center.y + size.y * 0.5f;
405 const float z0 = center.z - size.z * 0.5f;
406 const float z1 = center.z + size.z * 0.5f;
407 addQuad(mesh, {x0, y0, z1}, {x1, y0, z1}, {x1, y1, z1}, {x0, y1, z1}, {0, 0, 1});
408 addQuad(mesh, {x1, y0, z0}, {x0, y0, z0}, {x0, y1, z0}, {x1, y1, z0}, {0, 0, -1});
409 addQuad(mesh, {x0, y0, z0}, {x0, y0, z1}, {x0, y1, z1}, {x0, y1, z0}, {-1, 0, 0});
410 addQuad(mesh, {x1, y0, z1}, {x1, y0, z0}, {x1, y1, z0}, {x1, y1, z1}, {1, 0, 0});
411 addQuad(mesh, {x0, y1, z1}, {x1, y1, z1}, {x1, y1, z0}, {x0, y1, z0}, {0, 1, 0});
412 addQuad(mesh, {x0, y0, z0}, {x1, y0, z0}, {x1, y0, z1}, {x0, y0, z1}, {0, -1, 0});
413}
414
415void addWedge(MeshBuild& mesh, Vec3 center, Vec3 size, bool riseAlongX, const char* group) {
416 mesh.setActiveGroup(group);
417 const float x0 = center.x - size.x * 0.5f;
418 const float x1 = center.x + size.x * 0.5f;
419 const float y0 = center.y - size.y * 0.5f;
420 const float y1 = center.y + size.y * 0.5f;
421 const float z0 = center.z - size.z * 0.5f;
422 const float z1 = center.z + size.z * 0.5f;
423 Vec3 a{x0, y0, z0}, b{x1, y0, z0}, c{x1, y1, z0};
424 Vec3 d{x0, y0, z1}, e{x1, y0, z1}, f{x1, y1, z1};
425 if (!riseAlongX) {
426 a = {x1, y0, z0};
427 b = {x1, y0, z1};
428 c = {x1, y1, z1};
429 d = {x0, y0, z0};
430 e = {x0, y0, z1};
431 f = {x0, y1, z1};
432 }
433 const uint32_t first = static_cast<uint32_t>(mesh.getVertexCount());
434 const Vec3 n0 = normalOf(a, c, b);
435 const UV uvA = projectUv(a, n0);
436 const UV uvC = projectUv(c, n0);
437 const UV uvB = projectUv(b, n0);
438 addVertex(mesh, a, n0, uvA.u, uvA.v);
439 addVertex(mesh, c, n0, uvC.u, uvC.v);
440 addVertex(mesh, b, n0, uvB.u, uvB.v);
441 mesh.addTriangle(first, first + 1, first + 2);
442 const uint32_t second = static_cast<uint32_t>(mesh.getVertexCount());
443 const Vec3 n1 = normalOf(d, e, f);
444 const UV uvD = projectUv(d, n1);
445 const UV uvE = projectUv(e, n1);
446 const UV uvF = projectUv(f, n1);
447 addVertex(mesh, d, n1, uvD.u, uvD.v);
448 addVertex(mesh, e, n1, uvE.u, uvE.v);
449 addVertex(mesh, f, n1, uvF.u, uvF.v);
450 mesh.addTriangle(second, second + 1, second + 2);
451 addQuad(mesh, a, b, e, d, {0, -1, 0});
452 const Vec3 slopeNormal = normalOf(a, d, f);
453 addQuad(mesh, a, d, f, c, slopeNormal);
454 const Vec3 highNormal = normalOf(b, c, f);
455 addQuad(mesh, b, c, f, e, highNormal);
456}
457
458void addCylinder(MeshBuild& mesh, Vec3 center, float radiusBottom, float radiusTop, float height, int sides,
459 const char* group) {
460 mesh.setActiveGroup(group);
461 sides = std::clamp(sides, 3, 64);
462 const float y0 = center.y - height * 0.5f;
463 const float y1 = center.y + height * 0.5f;
464 const float radius = (radiusBottom + radiusTop) * 0.5f;
465 for (int i = 0; i < sides; ++i) {
466 const float a0 = 2.0f * kPi * float(i) / float(sides);
467 const float a1 = 2.0f * kPi * float(i + 1) / float(sides);
468 const Vec3 p0{center.x + std::cos(a0) * radiusBottom, y0, center.z + std::sin(a0) * radiusBottom};
469 const Vec3 p1{center.x + std::cos(a1) * radiusBottom, y0, center.z + std::sin(a1) * radiusBottom};
470 const Vec3 p2{center.x + std::cos(a1) * radiusTop, y1, center.z + std::sin(a1) * radiusTop};
471 const Vec3 p3{center.x + std::cos(a0) * radiusTop, y1, center.z + std::sin(a0) * radiusTop};
472 const Vec3 normal = normalOf(p0, p2, p1);
473 const uint32_t side = static_cast<uint32_t>(mesh.getVertexCount());
474 addVertex(mesh, p0, normal, a0 * radius, y0);
475 addVertex(mesh, p1, normal, a1 * radius, y0);
476 addVertex(mesh, p2, normal, a1 * radius, y1);
477 addVertex(mesh, p3, normal, a0 * radius, y1);
478 mesh.addTriangle(side, side + 2, side + 1);
479 mesh.addTriangle(side, side + 3, side + 2);
480 const uint32_t bottom = static_cast<uint32_t>(mesh.getVertexCount());
481 addVertex(mesh, {center.x, y0, center.z}, {0, -1, 0}, center.x, center.z);
482 addVertex(mesh, p1, {0, -1, 0}, p1.x, p1.z);
483 addVertex(mesh, p0, {0, -1, 0}, p0.x, p0.z);
484 mesh.addTriangle(bottom, bottom + 2, bottom + 1);
485 const uint32_t top = static_cast<uint32_t>(mesh.getVertexCount());
486 addVertex(mesh, {center.x, y1, center.z}, {0, 1, 0}, center.x, center.z);
487 addVertex(mesh, p3, {0, 1, 0}, p3.x, p3.z);
488 addVertex(mesh, p2, {0, 1, 0}, p2.x, p2.z);
489 mesh.addTriangle(top, top + 2, top + 1);
490 }
491}
492
493void addSphere(MeshBuild& mesh, Vec3 center, Vec3 radii, int rings, int sides, const char* group) {
494 mesh.setActiveGroup(group);
495 rings = std::clamp(rings, 2, 32);
496 sides = std::clamp(sides, 3, 64);
497 const uint32_t base = static_cast<uint32_t>(mesh.getVertexCount());
498 for (int ring = 0; ring <= rings; ++ring) {
499 const float v = float(ring) / float(rings);
500 const float phi = v * kPi;
501 for (int side = 0; side <= sides; ++side) {
502 const float u = float(side) / float(sides);
503 const float theta = u * 2.0f * kPi;
504 const Vec3 n{std::sin(phi) * std::cos(theta), std::cos(phi), std::sin(phi) * std::sin(theta)};
505 const float horizontalRadius = (radii.x + radii.z) * 0.5f;
506 addVertex(mesh, {center.x + n.x * radii.x, center.y + n.y * radii.y, center.z + n.z * radii.z}, n,
507 theta * horizontalRadius, phi * radii.y);
508 }
509 }
510 for (int ring = 0; ring < rings; ++ring) {
511 for (int side = 0; side < sides; ++side) {
512 const uint32_t a = base + uint32_t(ring * (sides + 1) + side);
513 const uint32_t b = a + uint32_t(sides + 1);
514 mesh.addTriangle(a, a + 1, b);
515 mesh.addTriangle(a + 1, b + 1, b);
516 }
517 }
518}
519
520void addTorus(MeshBuild& mesh, Vec3 center, float majorRadius, float minorRadius, int majorSegments, int minorSegments,
521 const char* group) {
522 mesh.setActiveGroup(group);
523 majorSegments = std::clamp(majorSegments, 3, 64);
524 minorSegments = std::clamp(minorSegments, 3, 32);
525 const uint32_t base = static_cast<uint32_t>(mesh.getVertexCount());
526 for (int major = 0; major <= majorSegments; ++major) {
527 const float u = float(major) / float(majorSegments);
528 const float a = 2.0f * kPi * u;
529 for (int minor = 0; minor <= minorSegments; ++minor) {
530 const float v = float(minor) / float(minorSegments);
531 const float b = 2.0f * kPi * v;
532 const Vec3 n{std::cos(a) * std::cos(b), std::sin(b), std::sin(a) * std::cos(b)};
533 const float radial = majorRadius + minorRadius * std::cos(b);
534 addVertex(mesh,
535 {center.x + radial * std::cos(a), center.y + minorRadius * std::sin(b),
536 center.z + radial * std::sin(a)},
537 n, a * majorRadius, b * minorRadius);
538 }
539 }
540 for (int major = 0; major < majorSegments; ++major) {
541 for (int minor = 0; minor < minorSegments; ++minor) {
542 const uint32_t a = base + uint32_t(major * (minorSegments + 1) + minor);
543 const uint32_t b = a + uint32_t(minorSegments + 1);
544 mesh.addTriangle(a, a + 1, b);
545 mesh.addTriangle(a + 1, b + 1, b);
546 }
547 }
548}
549
550} // namespace eve::procgen::prototype_detail
double z
double y
double x
float w
Definition AnimClip.cpp:738
std::string variant
float uv
glm::vec4 p[6]
std::string name
Definition ECS.cpp:154
float u
Definition Grass.cpp:233
glm::vec3 n
Definition Grass.cpp:63
float v
std::int32_t second
std::int32_t c
std::int32_t first
int h
std::vector< Colorf > px
std::uint32_t height
float length
std::array< float, 3 > scale
std::string id
std::vector< RecipeParam > params
MeleePoint3 b
Definition MeleeHit.cpp:41
double radius
Definition MeleeHit.cpp:42
MeleePoint3 a
Definition MeleeHit.cpp:40
Texture * normal
OceanMeshSettings mesh
glm::dvec2 center
std::string error
Definition Package.cpp:174
float f
const eve::script::BindingContractRegistry & registry
float d
int steps
int detail
float t
ScriptStructure out
int count
AbilityActivationGroup group
int sides
Definition TreeMesh.cpp:459
float size
Definition TreeMesh.cpp:158
uint32_t index
std::vector< double > phi
const VegetationPresetContext & context
float fraction
std::array< float, 4 > radii
float bottom
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
EVENGINE_API_DOMAINS public API.
Owning, typed generation parameters.
Definition Params.h:27
SQInteger top
Definition Snapshot.cpp:115
void addWedge(MeshBuild &mesh, Vec3 center, Vec3 size, bool riseAlongX, const char *group)
Adds wedge.
void addTorus(MeshBuild &mesh, Vec3 center, float majorRadius, float minorRadius, int majorSegments, int minorSegments, const char *group)
Adds torus.
void addCylinder(MeshBuild &mesh, Vec3 center, float radiusBottom, float radiusTop, float height, int sides, const char *group)
Adds cylinder.
void addBox(MeshBuild &mesh, Vec3 center, Vec3 size, const char *group)
Adds box.
void addSphere(MeshBuild &mesh, Vec3 center, Vec3 radii, int rings, int sides, const char *group)
Adds sphere.
eve::Result< MeshBuild > generatePrototypePiece(std::string_view pieceId, const Params &params)
Build one prototype module as an owning CPU triangle mesh.
void registerPrototypePieceRecipes(MeshRecipeRegistry &registry)
Register all prototype modules as prototype.* mesh recipes.
std::span< const PrototypePieceDescriptor > prototypePieceDescriptors() noexcept
Return the stable catalogue of 75 procedural prototype modules.
static ParamDescriptor floating(std::string key, std::string label, float defaultValue, float minimum, float maximum, float step)
Construct a bounded floating-point descriptor.
static ParamDescriptor integer(std::string key, std::string label, int defaultValue, int minimum, int maximum, int step=1)
Construct a bounded integer descriptor.
Metadata for one centered, Y-up module in the built-in prototype construction kit.
float defaultWidth
Default X extent in world units.
float defaultHeight
Default Y extent in world units.
float defaultDepth
Default Z extent in world units.