载入中...
搜索中...
未找到
VesselRecipes.cpp
浏览该文件的文档.
1#include <array>
2#include <cmath>
3#include <limits>
4#include <utility>
7namespace eve::procgen {
8namespace {
9struct Shape {
10 const char* name;
11 float height;
12 std::array<float, 4> radii;
13};
14constexpr Shape shapes[] = {
15 {"planter", 1.2f, {0.55f, 0.65f, 0.85f, 0.9f}}, {"jar", 1.6f, {0.6f, 1.3f, 1.3f, 0.65f}},
16 {"bowl", 0.65f, {0.35f, 0.45f, 0.9f, 1.f}}, {"plate", 0.18f, {0.65f, 0.7f, 0.95f, 1.1f}},
17 {"saucer", 0.1f, {0.5f, 0.55f, 0.8f, 0.85f}}, {"cylinder", 1.f, {0.8f, 0.8f, 0.8f, 0.8f}}};
18eve::Result<MeshBuild> build(const Params& params) {
19 auto invalid = [] {
22 "VesselParametersInvalid: Unknown shape/material or invalid curve, thickness, height or resolution"));
23 };
24 const auto name = params.getString("shape", "planter");
25 const Shape* shape = nullptr;
26 for (const auto& candidate : shapes)
27 if (name == candidate.name) shape = &candidate;
28 if (!shape) return invalid();
29 const float missing = std::numeric_limits<float>::quiet_NaN();
30 auto read = [&](const char* key, float fallback) {
31 return params.getFloat(key, params.has(key) ? missing : fallback);
32 };
33 const float requestedHeight = read("vesselHeight", 0.f);
34 const float height = requestedHeight == 0.f ? shape->height : requestedHeight;
35 const float thickness = read("wallThickness", 0.045f);
36 const float bottom = read("bottomThickness", 0.05f);
37 const int samples = params.getInt("curveSegments", params.has("curveSegments") ? -1 : 48);
38 const int radial = params.getInt("radialSegments", params.has("radialSegments") ? -1 : 96);
39 auto material = params.getString("material", "auto");
40 if (material == "auto") material = name == "planter" ? "terracotta" : "porcelain";
41 if (material != "terracotta" && material != "glazed_ceramic" && material != "porcelain" && material != "celadon" &&
42 material != "stoneware")
43 return invalid();
44 if (!std::isfinite(height) || !std::isfinite(thickness) || !std::isfinite(bottom) || height <= 0.f ||
45 height > 1000.f || thickness < 0.0001f || bottom < 0.0001f || bottom >= height || samples < 4 ||
46 samples > 256 || radial < 3 || radial > 512)
47 return invalid();
48 std::array<float, 4> r{};
49 const char* keys[] = {"radius0", "radius1", "radius2", "radius3"};
50 for (size_t i = 0; i < r.size(); ++i) {
51 r[i] = read(keys[i], 0.f);
52 if (r[i] == 0.f) r[i] = shape->radii[i];
53 if (!std::isfinite(r[i]) || r[i] <= thickness + 0.0001f || r[i] > 1000.f) return invalid();
54 }
55 auto radius = [&](float t) {
56 const float s = 1.f - t;
57 return s * s * s * r[0] + 3.f * s * s * t * r[1] + 3.f * s * t * t * r[2] + t * t * t * r[3];
58 };
59 std::vector<VesselSectionPoint> section{{0.f, 0.f}};
60 for (int i = 0; i <= samples; ++i) {
61 const float t = float(i) / samples;
62 section.push_back({radius(t), height * t});
63 }
64 // A semicircular lip joins the outer and inner surfaces without a zero-width top.
65 for (int i = 1; i <= 8; ++i) {
66 const float angle = 3.141592653589793f * float(i) / 8.f;
67 section.push_back({r[3] - thickness * 0.5f + thickness * 0.5f * std::cos(angle),
68 height + thickness * 0.5f * std::sin(angle)});
69 }
70 for (int i = samples - 1; i >= 0; --i) {
71 const float t = float(i) / samples;
72 section.push_back({radius(t) - thickness, bottom + (height - bottom) * t});
73 }
74 section.push_back({0.f, bottom});
75 auto result = revolveVesselSection(section, radial);
76 if (!result.ok()) return result;
77 result.value().setMeta("shape", name);
78 result.value().setMeta("materialRecipe", "pbr." + material);
79 return result;
80}
81} // namespace
83 RecipeDescriptor schema{"mesh.vessel", "Revolved pottery / vessels", "Pottery", {}};
84 schema.params.push_back(ParamDescriptor::choice("shape", "Shape", "planter",
85 {"planter", "jar", "bowl", "plate", "saucer", "cylinder"}));
86 schema.params.push_back(ParamDescriptor::choice(
87 "material", "Material", "auto", {"auto", "terracotta", "glazed_ceramic", "porcelain", "celadon", "stoneware"}));
88 schema.params.push_back(ParamDescriptor::integer("curveSegments", "Curve samples", 48, 4, 256));
89 schema.params.push_back(ParamDescriptor::integer("radialSegments", "Radial segments", 96, 3, 512));
90 schema.params.push_back(
91 ParamDescriptor::floating("wallThickness", "Radial wall thickness", 0.045f, 0.0001f, 100.f, 0.001f));
92 schema.params.push_back(
93 ParamDescriptor::floating("bottomThickness", "Bottom thickness", 0.05f, 0.0001f, 100.f, 0.001f));
94 schema.params.push_back(ParamDescriptor::floating("vesselHeight", "Height (0 = preset)", 0.f, 0.f, 1000.f, 0.01f));
95 for (int i = 0; i < 4; ++i)
96 schema.params.push_back(ParamDescriptor::floating("radius" + std::to_string(i),
97 "Bezier radius " + std::to_string(i) + " (0 = preset)", 0.f,
98 0.f, 1000.f, 0.01f));
99 registry.registerRecipe(std::move(schema), [](const Params& params, MeshBuild& output, std::string& error) {
100 auto result = build(params);
101 if (!result.ok()) {
102 error = "VesselParametersInvalid: invalid or intersecting vessel section";
103 return false;
104 }
105 output = std::move(result.value());
106 return true;
107 });
108}
109} // namespace eve::procgen
std::string output
const std::string & s
SQObjectPtr key
Definition ECS.cpp:155
std::string name
Definition ECS.cpp:154
ShaderImageInput shape
double r
std::uint32_t height
std::function< Numbers(b3JointId)> read
float thickness
Poco::JSON::Object::Ptr result
std::vector< RecipeParam > params
double radius
Definition MeleeHit.cpp:42
std::string error
Definition Package.cpp:174
std::array< uint32_t, 16 > material
const eve::script::BindingContractRegistry & registry
float t
std::string build
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 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
void keys(const Value &value, std::initializer_list< const char * > names)
Result< int > invalid(std::string message)
Invalid.
eve::Result< MeshBuild > revolveVesselSection(const std::vector< VesselSectionPoint > &section, int radialSegments)
Revolve a solid section: underside axis, outside, rim, inside, floor axis.
EVENGINE_API_DOMAINS void registerVesselRecipes(MeshRecipeRegistry &registry)
Register pottery recipes at initialization on the registry owner thread.
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
std::vector< ParamDescriptor > params
Definition ParamSchema.h:56