载入中...
搜索中...
未找到
Cabinet.cpp
浏览该文件的文档.
2
5
6#include <cmath>
7#include <limits>
8#include <string>
9#include <utility>
10
11namespace eve::procgen {
13 const float invalid = std::numeric_limits<float>::quiet_NaN();
14 auto number = [&](const char* key, float fallback) { return p.getFloat(key, p.has(key) ? invalid : fallback); };
15 const float w = number("cabinetWidth", 1.8f), h = number("cabinetHeight", 2.0f), d = number("depth", 0.55f);
16 const float t = number("thickness", 0.025f), leg = p.getBool("legs", true) ? number("legHeight", 0.15f) : 0.f;
17 const int columns = p.getInt("columns", p.has("columns") ? -1 : 2),
18 levels = p.getInt("levels", p.has("levels") ? -1 : 3);
19 const int doors = p.getInt("doors", p.has("doors") ? -1 : 2),
20 drawers = p.getInt("drawers", p.has("drawers") ? -1 : 2);
21 const std::string style = p.getString("style", "modern");
22 auto fail = [] {
25 "invalid cabinet dimensions, counts, style or insufficient clearance", "cabinet"));
26 };
27 if (!std::isfinite(w) || !std::isfinite(h) || !std::isfinite(d) || !std::isfinite(t) || !std::isfinite(leg) ||
28 w > 100 || h > 100 || d > 100 || t < 0.002f || leg < 0 || columns < 1 || columns > 16 || levels < 1 ||
29 levels > 16 || doors < 0 || doors > 16 || drawers < 0 || drawers > 16 ||
30 (style != "modern" && style != "minimal" && style != "european" && style != "chinese"))
31 return fail();
32 const bool hasCornice = style == "european" || style == "chinese";
33 const float bodyTop = hasCornice ? h - 2 * t : h;
34 const float inner = w - 2 * t, bottom = leg + t, top = bodyTop - t;
35 const float drawerH = drawers ? (top - bottom) * 0.35f / drawers : 0;
36 const float split = bottom + drawerH * drawers;
37 const float bay = (inner - (columns - 1) * t) / columns;
38 const float doorW = doors ? inner / doors : inner;
39 if (d < 8 * t || bay < 6 * t || top - bottom < 12 * t || (top - split) / levels < 4 * t ||
40 (drawers && drawerH < 4 * t) || (doors && doorW < 6 * t))
41 return fail();
43 auto box = [&](float x, float y, float z, float sx, float sy, float sz, const std::string& group) {
44 const int firstVertex = m.getVertexCount();
45 prototype_detail::addBox(m, {x, y, z}, {sx, sy, sz}, group.c_str());
46 // Drawer fronts and crown moulding have horizontal grain; doors remain vertical.
47 if ((group.starts_with("drawer.") && !group.ends_with(".interior") && !group.ends_with(".hardware")) ||
48 group == "cornice") {
49 for (int i = firstVertex; i < m.getVertexCount(); ++i)
50 std::swap(m.uvs()[size_t(i) * 2], m.uvs()[size_t(i) * 2 + 1]);
51 }
52 };
53 box(0, leg + t / 2, 0, w, t, d, "body");
54 box(0, bodyTop - t / 2, 0, w, t, d, "body");
55 for (float sign : {-1.f, 1.f}) box(sign * (w - t) / 2, (leg + bodyTop) / 2, 0, t, bodyTop - leg, d, "body");
56 box(0, (leg + bodyTop) / 2, -(d - t) / 2, inner, bodyTop - leg - 2 * t, t, "back");
57 for (int c = 1; c < columns; ++c)
58 box(-inner / 2 + c * (bay + t) - t / 2, (split + top) / 2, 0, t, top - split, d - 2 * t, "dividers");
59 for (int l = 1; l < levels; ++l) box(0, split + (top - split) * l / levels, 0, inner, t, d - 2 * t, "shelves");
60 if (drawers) box(0, split, 0, inner, t, d - 2 * t, "shelves");
61 if (leg > 0)
62 for (float x : {-1.f, 1.f})
63 for (float z : {-1.f, 1.f}) {
64 if (style == "european")
65 prototype_detail::addCylinder(m, {x * (w / 2 - 2 * t), leg / 2, z * (d / 2 - 2 * t)}, t, 1.5f * t,
66 leg, 12, "legs");
67 else
68 box(x * (w / 2 - 2 * t), leg / 2, z * (d / 2 - 2 * t), 2 * t, leg, 2 * t, "legs");
69 }
70 auto front = [&](float x, float y, float fw, float fh, const std::string& group, bool drawer, float handleSide) {
71 const float z = d / 2 - t / 2;
72 box(x, y, z, fw, fh, t, group);
73 if (style == "european" || style == "chinese") {
74 for (float sign : {-1.f, 1.f}) {
75 box(x + sign * (fw / 2 - t), y, z + t, t, fh - t, t, group + ".trim");
76 box(x, y + sign * (fh / 2 - t), z + t, fw - t, t, t, group + ".trim");
77 }
78 if (style == "chinese")
79 for (int i = -1; i <= 1; ++i) box(x + i * fw / 5, y, z + t, 0.4f * t, fh - 4 * t, t, group + ".trim");
80 }
81 if (style != "minimal") {
82 const float hx = drawer ? x : x + handleSide * (fw / 2 - 2 * t);
83 box(hx, y, z + 2 * t, drawer ? fw * 0.3f : t, drawer ? t : fh * 0.15f, t, group + ".hardware");
84 }
85 };
86 for (int i = 0; i < doors; ++i)
87 front(-inner / 2 + (i + 0.5f) * doorW, (split + top) / 2, doorW - t * 0.25f, top - split - t * 0.5f,
88 "door." + std::to_string(i), false, i % 2 == 0 ? 1.f : -1.f);
89 for (int i = 0; i < drawers; ++i) {
90 const std::string group = "drawer." + std::to_string(i);
91 const float y = bottom + (i + 0.5f) * drawerH;
92 front(0, y, inner - t * 0.5f, drawerH - t * 0.5f, group, true, 1.f);
93 box(0, y - drawerH / 2 + t, 0, inner - 2 * t, t, d - 4 * t, group + ".interior");
94 for (float s : {-1.f, 1.f}) box(s * (inner / 2 - t), y, -t, t, drawerH - 2 * t, d - 4 * t, group + ".interior");
95 box(0, y, -d / 2 + 2 * t, inner - 2 * t, drawerH - 2 * t, t, group + ".interior");
96 }
97 if (hasCornice) {
98 box(0, h - 1.5f * t, 0, w + 2 * t, t, d + 2 * t, "cornice");
99 box(0, h - t / 2, 0, w + 4 * t, t, d + 4 * t, "cornice");
100 }
101 for (const auto& group : m.groupNames()) {
102 std::string role = "exterior";
103 if (group.ends_with(".hardware"))
104 role = "hardware";
105 else if (group.ends_with(".trim") || group == "cornice")
106 role = "trim";
107 else if (group.ends_with(".interior") || group == "back" || group == "shelves" || group == "dividers")
108 role = "interior";
109 else if (group == "legs")
110 role = "legs";
111 m.setMeta("material." + group, role);
112 }
113 m.setMeta("recipe", "mesh.cabinet");
114 m.setMeta("style", style);
115 m.setMeta("origin", "ground-center");
116 return eve::Result<MeshBuild>::success(std::move(m));
117}
118
121 s.id = "mesh.cabinet";
122 s.displayName = "Cabinet";
123 s.category = "Furniture";
124 s.params = {ParamDescriptor::floating("cabinetWidth", "Width", 1.8f, 0.1f, 100, 0.05f),
125 ParamDescriptor::floating("cabinetHeight", "Height", 2, 0.1f, 100, 0.05f),
126 ParamDescriptor::floating("depth", "Depth", 0.55f, 0.1f, 100, 0.05f),
127 ParamDescriptor::floating("thickness", "Panel thickness", 0.025f, 0.002f, 1, 0.005f),
128 ParamDescriptor::integer("columns", "Compartments", 2, 1, 16),
129 ParamDescriptor::integer("levels", "Storage levels", 3, 1, 16),
130 ParamDescriptor::integer("doors", "Door leaves", 2, 0, 16),
131 ParamDescriptor::integer("drawers", "Drawers", 2, 0, 16),
132 ParamDescriptor::boolean("legs", "Raised legs", true),
133 ParamDescriptor::floating("legHeight", "Leg height", 0.15f, 0, 2, 0.01f),
134 ParamDescriptor::choice("style", "Style", "modern", {"european", "chinese", "minimal", "modern"})};
135 registry.registerRecipe(std::move(s), [](const Params& p, MeshBuild& out, std::string& error) {
136 auto result = generateCabinet(p);
137 if (!result.ok()) {
138 error = result.status().describe();
139 return false;
140 }
141 out = std::move(result).takeValue();
142 return true;
143 });
144}
145} // namespace eve::procgen
double z
double y
double m[16]
double x
float w
Definition AnimClip.cpp:738
const std::string & s
glm::vec4 p[6]
SQObjectPtr key
Definition ECS.cpp:155
std::uint32_t firstVertex
std::int32_t c
int h
Poco::JSON::Object::Ptr result
std::string error
Definition Package.cpp:174
const eve::script::BindingContractRegistry & registry
float d
float t
RenderSystem3D::ShadowExtraDrawer drawer
double number
ScriptStructure out
AbilityActivationGroup group
int columns
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
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 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.
Result< int > invalid(std::string message)
Invalid.
void registerCabinetRecipe(MeshRecipeRegistry &registry)
Register mesh.cabinet and its editor schema during single-threaded initialization.
Definition Cabinet.cpp:119
eve::Result< MeshBuild > generateCabinet(const Params &p)
Build a deterministic, Y-up cabinet with its base at Y=0 and front toward +Z.
Definition Cabinet.cpp:12
static ParamDescriptor boolean(std::string key, std::string label, bool defaultValue)
Construct a Boolean descriptor stored as zero or one in Params.
static ParamDescriptor choice(std::string key, std::string label, std::string defaultValue, std::vector< std::string > choices)
Construct a finite-choice string descriptor.
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.
Complete metadata and parameter schema shared by every procgen recipe family.
Definition ParamSchema.h:52