载入中...
搜索中...
未找到
GeneratedArtifactRecipes.cpp
浏览该文件的文档.
1#include <algorithm>
2#include <string>
3#include <utility>
8namespace eve::procgen {
9namespace {
10#include "procgen/GeneratedArtifactDiagnostics.inc"
11Bounds boundsForMesh(const MeshBuild &mesh) noexcept {
12 Bounds bounds;
13 for (int i = 0; i < mesh.getVertexCount(); ++i) {
14 bounds.include(mesh.getPositionX(i), mesh.getPositionY(i), mesh.getPositionZ(i));
15 }
16 return bounds;
17}
18
19Bounds boundsForGrid(const Grid2D &grid) noexcept {
20 Bounds bounds;
21 if (grid.getWidth() <= 0 || grid.getHeight() <= 0) return bounds;
22 bounds.include(0.f, 0.f, 0.f);
23 bounds.include(float(grid.getWidth()), 0.f, float(grid.getHeight()));
24 return bounds;
25}
26
27Bounds boundsForPoints(const PointSet &points) noexcept {
28 Bounds bounds;
29 for (const ProcgenPoint &point : points.points()) bounds.include(point.x, point.y, point.z);
30 return bounds;
31}
32
33Collider colliderForMesh(const MeshBuild &mesh) {
34 Collider collider;
35 collider.vertices = mesh.positions();
36 collider.indices = mesh.indices();
37 collider.bounds = boundsForMesh(mesh);
38 return collider;
39}
40
41Grid2D makeHexTopology(const Params &params) {
42 const int width = std::clamp(params.getInt("width", 32), 1, 256);
43 const int height = std::clamp(params.getInt("height", 24), 1, 256);
44 Grid2D topology;
45 topology.resize(width, height);
46 const std::uint32_t seed = params.getSeed();
47 for (int y = 0; y < height; ++y) {
48 for (int x = 0; x < width; ++x) {
49 // This is a compact cell-topology projection. Elevation and dense
50 // geometry remain in the mesh recipe; no per-cell ECS objects are
51 // created merely to publish topology.
52 const std::uint32_t value = seed ^ (std::uint32_t(x) * 0x9e3779b9u) ^ (std::uint32_t(y) * 0x85ebca6bu);
53 topology.setCell(x, y, int((value ^ (value >> 16u)) % 10u));
54 }
55 }
56 topology.setMeta("role", "topology");
57 topology.setMeta("algorithm", "mesh.hexterrain");
58 topology.setMeta("coordinateSpace", "hex_axial");
59 return topology;
60}
61
62PointSet makeHexAnchors(const Params &params) {
63 const float radius = params.getFloat("radius", 1.f);
64 const int width = std::max(2, params.getInt("width", 32));
65 const int height = std::max(2, params.getInt("height", 24));
66 const float xStep = radius * 1.5f;
67 const float zStep = radius * 1.7320508076f;
68 PointSet anchors;
69 anchors.add(0.f, 0.f, 0.f);
70 anchors.add(float(width - 1) * xStep, 0.f, 0.f);
71 anchors.add(0.f, 0.f, float(height - 1) * zStep);
72 anchors.add(float(width - 1) * xStep, 0.f, (float(height - 1) + float((width - 1) & 1) * .5f) * zStep);
73 anchors.setStringAttribute(0, "role", "spawn");
74 anchors.setStringAttribute(1, "role", "edge");
75 anchors.setStringAttribute(2, "role", "edge");
76 anchors.setStringAttribute(3, "role", "edge");
77 return anchors;
78}
79
80Grid2D makeCastleTopology(const Params &params) {
81 const int rings = std::clamp(params.getInt("rings", 2), 1, 4);
82 Grid2D topology;
83 topology.resize(rings, 1);
84 for (int ring = 0; ring < rings; ++ring) topology.setCell(ring, 0, ring + 1);
85 topology.setMeta("role", "topology");
86 topology.setMeta("algorithm", "mesh.castle");
87 topology.setMeta("coordinateSpace", "castle_ring");
88 return topology;
89}
90
91PointSet makeCastleAnchors(const Params &params) {
92 const float width = std::max(16.f, params.getFloat("width", 42.f));
93 const float depth = std::max(16.f, params.getFloat("depth", 36.f));
94 const float gateWidth = std::clamp(params.getFloat("gateWidth", 5.f), 2.f, width * .3f);
95 const float keepWidth = std::clamp(params.getFloat("keepWidth", width * .25f), 6.f, width * .55f);
96 const float keepDepth = std::clamp(params.getFloat("keepDepth", depth * .24f), 6.f, depth * .55f);
97 PointSet anchors;
98 anchors.add(0.f, 0.f, -depth * .5f - 1.f);
99 anchors.add(-width * .5f + gateWidth, 0.f, -depth * .5f);
100 anchors.add(width * .5f - gateWidth, 0.f, -depth * .5f);
101 anchors.add(-keepWidth * .5f, 0.f, -keepDepth * .5f);
102 anchors.add(keepWidth * .5f, 0.f, keepDepth * .5f);
103 anchors.setStringAttribute(0, "role", "gate");
104 anchors.setStringAttribute(1, "role", "outer_gate_left");
105 anchors.setStringAttribute(2, "role", "outer_gate_right");
106 anchors.setStringAttribute(3, "role", "keep_min");
107 anchors.setStringAttribute(4, "role", "keep_max");
108 return anchors;
109}
110
111eve::Result<ArtifactPart> partFromMesh(std::string role, ArtifactId parent, const BuildKey &parentKey, MeshBuild mesh,
112 eve::Value::Object metadata) {
113 auto childKey = BuildKey::fromCanonical(parentKey.format() + "/" + role);
114 if (!childKey)
115 return rejectedPart(eve::DiagnosticCode::InvalidArgument, "cannot derive child build key for " + role);
116 const ArtifactId childId = parent.child(role);
117 const Bounds bounds = boundsForMesh(mesh);
118 return makeArtifactPart(std::move(role), childId, ArtifactType::MeshData, eve::SchemaVersion(1),
119 std::move(*childKey), bounds, {}, std::move(metadata), std::move(mesh));
120}
121
122eve::Result<ArtifactPart> partFromCollider(std::string role, ArtifactId parent, const BuildKey &parentKey,
123 Collider collider, eve::Value::Object metadata) {
124 auto childKey = BuildKey::fromCanonical(parentKey.format() + "/" + role);
125 if (!childKey)
126 return rejectedPart(eve::DiagnosticCode::InvalidArgument, "cannot derive child build key for " + role);
127 const ArtifactId childId = parent.child(role);
128 const Bounds bounds = collider.bounds;
129 return makeArtifactPart(std::move(role), childId, ArtifactType::Collider, eve::SchemaVersion(1),
130 std::move(*childKey), bounds, {parent.child("mesh")}, std::move(metadata),
131 std::move(collider));
132}
133
134eve::Result<ArtifactPart> partFromGrid(std::string role, ArtifactId parent, const BuildKey &parentKey, Grid2D grid,
135 eve::Value::Object metadata) {
136 auto childKey = BuildKey::fromCanonical(parentKey.format() + "/" + role);
137 if (!childKey)
138 return rejectedPart(eve::DiagnosticCode::InvalidArgument, "cannot derive child build key for " + role);
139 const ArtifactId childId = parent.child(role);
140 const Bounds bounds = boundsForGrid(grid);
141 return makeArtifactPart(std::move(role), childId, ArtifactType::Grid, eve::SchemaVersion(1), std::move(*childKey),
142 bounds, {}, std::move(metadata), std::move(grid));
143}
144
145eve::Result<ArtifactPart> partFromPoints(std::string role, ArtifactId parent, const BuildKey &parentKey,
146 PointSet points, eve::Value::Object metadata) {
147 auto childKey = BuildKey::fromCanonical(parentKey.format() + "/" + role);
148 if (!childKey)
149 return rejectedPart(eve::DiagnosticCode::InvalidArgument, "cannot derive child build key for " + role);
150 const ArtifactId childId = parent.child(role);
151 const Bounds bounds = boundsForPoints(points);
152 return makeArtifactPart(std::move(role), childId, ArtifactType::PointSet, eve::SchemaVersion(1),
153 std::move(*childKey), bounds, {}, std::move(metadata), std::move(points));
154}
155
156template <class T>
157bool appendPart(eve::Result<ArtifactPart> &&result, std::vector<ArtifactPart> &parts, eve::Status &failure) {
158 if (!result.ok()) {
159 failure = result.status();
160 return false;
161 }
162 parts.emplace_back(std::move(result).takeValue());
163 return true;
164}
165
166eve::Result<GeneratedArtifact> generateComposite(const Params &params, ArtifactId id, std::string_view recipe,
167 bool (*generateMesh)(const Params &, MeshBuild &, std::string &),
168 Grid2D topology, PointSet anchors) {
169 if (id.isNil())
170 return rejectedArtifact(eve::DiagnosticCode::InvalidArgument, "top-level artifact identity must not be nil");
171 const auto key = BuildKey::forRecipe(recipe, params);
172 if (!key)
173 return rejectedArtifact(eve::DiagnosticCode::InvalidArgument, "recipe or parameters cannot form a build key");
174
175 MeshBuild mesh;
176 std::string error;
177 if (!generateMesh(params, mesh, error)) {
178 return rejectedArtifact(eve::DiagnosticCode::Failed,
179 error.empty() ? "procedural mesh generation failed" : error);
180 }
181 const Collider collider = colliderForMesh(mesh);
182 std::vector<ArtifactPart> parts;
183 parts.reserve(4);
184 eve::Status failure;
185 eve::Value::Object meshMeta;
186 meshMeta.emplace("role", eve::Value("mesh"));
187 meshMeta.emplace("densePayload", eve::Value(true));
188 auto meshPart = partFromMesh("mesh", id, *key, std::move(mesh), std::move(meshMeta));
189 if (!appendPart<ArtifactPart>(std::move(meshPart), parts, failure))
190 return eve::Result<GeneratedArtifact>::failure(std::move(failure));
191
192 eve::Value::Object colliderMeta;
193 colliderMeta.emplace("role", eve::Value("collider"));
194 colliderMeta.emplace("source", eve::Value("mesh"));
195 auto colliderPart = partFromCollider("collider", id, *key, collider, std::move(colliderMeta));
196 if (!appendPart<ArtifactPart>(std::move(colliderPart), parts, failure))
197 return eve::Result<GeneratedArtifact>::failure(std::move(failure));
198
199 eve::Value::Object topologyMeta;
200 topologyMeta.emplace("role", eve::Value("topology"));
201 auto topologyPart = partFromGrid("topology", id, *key, std::move(topology), std::move(topologyMeta));
202 if (!appendPart<ArtifactPart>(std::move(topologyPart), parts, failure))
203 return eve::Result<GeneratedArtifact>::failure(std::move(failure));
204
205 eve::Value::Object anchorsMeta;
206 anchorsMeta.emplace("role", eve::Value("anchors"));
207 anchorsMeta.emplace("coordinateSpace", eve::Value("world"));
208 auto anchorsPart = partFromPoints("anchors", id, *key, std::move(anchors), std::move(anchorsMeta));
209 if (!appendPart<ArtifactPart>(std::move(anchorsPart), parts, failure))
210 return eve::Result<GeneratedArtifact>::failure(std::move(failure));
211
212 CompositeArtifact composite;
213 composite.children = std::move(parts);
214 eve::Value::Object metadata;
215 metadata.emplace("algorithm", eve::Value(std::string(recipe)));
216 metadata.emplace("childCount", eve::Value(std::int64_t(composite.children.size())));
217 metadata.emplace("determinism", eve::Value("seeded_cpu"));
218 metadata.emplace("payloadPolicy", eve::Value("dense data stays typed"));
219 const Bounds bounds = composite.children.front().bounds;
220 return makeArtifact(id, ArtifactType::Composite, eve::SchemaVersion(1), *key, bounds, {}, std::move(metadata),
221 GeneratedArtifact::Payload(std::move(composite)));
222}
223
224
225} // namespace
227 return generateComposite(params, id, "mesh.hexterrain", generateHexTerrainMesh, makeHexTopology(params),
228 makeHexAnchors(params));
229}
230
232 return generateComposite(params, id, "mesh.castle", generateCastleMesh, makeCastleTopology(params),
233 makeCastleAnchors(params));
234}
235
237 if (recipeId == "mesh.hexterrain") return generateHexTerrainArtifact(params, id);
238 if (recipeId == "mesh.castle") return generateCastleArtifact(params, id);
239 if (recipeId == "mesh.roadNetwork") return generateRoadNetworkArtifact(params, id);
240 if (id.isNil())
241 return rejectedArtifact(eve::DiagnosticCode::InvalidArgument, "top-level artifact identity must not be nil");
242 const auto key = BuildKey::forRecipe(recipeId, params);
243 if (!key)
244 return rejectedArtifact(eve::DiagnosticCode::InvalidArgument, "recipe or parameters cannot form a build key");
247 std::string error;
248 if (!MeshRecipeRegistry::instance().generate(std::string(recipeId), params, mesh, error))
249 return rejectedArtifact(eve::DiagnosticCode::Failed,
250 error.empty() ? "procedural mesh generation failed" : error);
251 eve::Value::Object metadata;
252 metadata.emplace("algorithm", eve::Value(std::string(recipeId)));
253 metadata.emplace("determinism", eve::Value("seeded_cpu"));
254 const Bounds bounds = boundsForMesh(mesh);
255 return makeArtifact(id, ArtifactType::MeshData, eve::SchemaVersion(1), *key, bounds, {}, std::move(metadata),
256 std::move(mesh));
257}
258
259
260} // namespace eve::procgen
double z
double y
double x
std::vector< eve::artifact::PartView > parts
SQObjectPtr key
Definition ECS.cpp:155
Owning, backend-neutral products published by procedural generation.
std::uint32_t height
std::uint32_t width
const ColourScope * parent
Poco::JSON::Object::Ptr result
std::vector< RecipeParam > params
double radius
Definition MeleeHit.cpp:42
OceanMeshSettings mesh
std::string error
Definition Package.cpp:174
BakeRecipe recipe
Topology topology
std::shared_ptr< const std::vector< glm::vec2 > > points
Object::Ptr value
uint32_t seed
Definition TreeMesh.cpp:276
std::uint32_t depth
glm::vec3 point
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
Structured status and zero or more diagnostics for an operation.
Definition Status.h:70
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
std::map< std::string, Value > Object
Definition Value.h:34
Id128 public API.
Definition Identity.h:111
Id128 child(std::string_view role) const noexcept
Derives a deterministic child UUID for a hierarchical identity.
Definition Identity.h:191
static std::optional< BuildKey > fromCanonical(std::string_view canonical)
Construct a build key from canonical text.
Definition BuildKey.cpp:27
static std::optional< BuildKey > forRecipe(std::string_view recipeId, const Params &params)
Derive a compact deterministic key for a recipe and parameters.
Definition BuildKey.cpp:32
CPU triangle mesh from procedural mesh recipes (e.g. marching cubes). Positions/normals are xyz-packe...
Definition MeshBuild.h:20
void registerBuiltins()
Registers builtins.
static MeshRecipeRegistry & instance()
Access the process-wide mesh recipe registry.
Owning, typed generation parameters.
Definition Params.h:27
EVENGINE_API_DOMAINS eve::Result< GeneratedArtifact > generateRoadNetworkArtifact(const Params &params, ArtifactId id)
Generate a road composite containing mesh, collider, terrain footprint, navigation and placement poin...
bool generateHexTerrainMesh(const Params &params, MeshBuild &out, std::string &error)
Build a deterministic, flat-top hexagonal continent mesh.
bool generateCastleMesh(const Params &p, MeshBuild &out, std::string &error)
Build a complete, walkable, multi-ring medieval castle.
EVENGINE_API_DOMAINS eve::Result< GeneratedArtifact > generateCastleArtifact(const Params &params, ArtifactId id)
Generate a castle composite containing mesh, collider, ring topology and gate/keep anchors.
eve::Result< ArtifactPart > makeArtifactPart(std::string role, ArtifactId id, ArtifactType type, eve::SchemaVersion schemaVersion, BuildKey buildKey, Bounds bounds, std::vector< ArtifactId > dependencies, eve::Value::Object metadata, ArtifactLeafPayload payload)
Validate and construct a leaf part for a composite artifact.
eve::ArtifactId ArtifactId
Common strong UUID identity for one published procedural artifact.
EVENGINE_API_DOMAINS eve::Result< GeneratedArtifact > generateHexTerrainArtifact(const Params &params, ArtifactId id)
Generate a hex-terrain composite containing mesh, collider, topology and world anchors.
EVENGINE_API_DOMAINS eve::Result< GeneratedArtifact > generateMeshArtifact(std::string_view recipeId, const Params &params, ArtifactId id)
Generate any registered mesh recipe as a CPU artifact.
eve::Result< GeneratedArtifact > makeArtifact(ArtifactId id, ArtifactType type, eve::SchemaVersion schemaVersion, BuildKey buildKey, Bounds bounds, std::vector< ArtifactId > dependencies, eve::Value::Object metadata, GeneratedArtifact::Payload payload)
Validate and construct one artifact record.
WidgetDesc grid(int columns, std::vector< WidgetDesc > children, std::string id)
Fixed-column grid; children are placed in source order.
Definition Widget.cpp:878
Axis-aligned bounds in the artifact's declared world coordinate space.
std::variant< Grid2D, PointSet, MeshData, ImageData, Collider, CompositeArtifact > Payload
glm::vec4 bounds