载入中...
搜索中...
未找到
RoadRecipes.cpp
浏览该文件的文档.
4
5#include "image/ImageData.h"
9
10#include <algorithm>
11#include <cmath>
12
13namespace eve::procgen::road {
14
16 auto baked = bakeRoadNetworkRecipe(params);
17 if (!baked.ok()) {
18 error = baked.status().describe();
19 return false;
20 }
21 out = std::move(baked.value().mesh);
22 return true;
23}
24
26 auto baked = bakeRoadNetworkRecipe(params);
27 if (!baked.ok()) return Result<RoadOverlay>::failure(baked.status());
28 return Result<RoadOverlay>::success(std::move(baked.value().overlay));
29}
30
31std::unique_ptr<image::ImageData> generateRoadMarkingsTexture(const Params& params, std::string& error) {
32 const int width = std::clamp(params.getWidth() > 0 ? params.getWidth() : params.getInt("deckWidth", 256), 8, 2048);
33 const int height = std::clamp(params.getHeight() > 0 ? params.getHeight() : params.getInt("height", 64), 8, 2048);
34 const float dashPeriod = std::max(0.05f, params.getFloat("dashPeriod", 0.18f));
35 const float dashDuty = std::clamp(params.getFloat("dashDuty", 0.45f), 0.05f, 0.95f);
36 const bool zebra = params.getBool("zebra", true);
37
38 auto image = std::make_unique<image::ImageData>(width, height, "RGBA8");
39 for (int y = 0; y < height; ++y) {
40 const float v = (static_cast<float>(y) + 0.5f) / static_cast<float>(height);
41 for (int x = 0; x < width; ++x) {
42 const float u = (static_cast<float>(x) + 0.5f) / static_cast<float>(width);
43 float r = 0.18f, g = 0.18f, b = 0.20f;
44
45 // White edge lines near v=0 and v=1.
46 if (v < 0.08f || v > 0.92f) {
47 r = g = b = 0.92f;
48 }
49 // Yellow dashed center.
50 else if (std::fabs(v - 0.5f) < 0.035f) {
51 const float phase = std::fmod(u, dashPeriod) / dashPeriod;
52 if (phase < dashDuty) {
53 r = 0.95f;
54 g = 0.75f;
55 b = 0.12f;
56 }
57 }
58 // Lane divider dashes at 1/4 and 3/4.
59 else if (std::fabs(v - 0.25f) < 0.02f || std::fabs(v - 0.75f) < 0.02f) {
60 const float phase = std::fmod(u + 0.05f, dashPeriod) / dashPeriod;
61 if (phase < dashDuty) r = g = b = 0.88f;
62 }
63
64 // Zebra band near u ends (intersection approach).
65 if (zebra && (u < 0.08f || u > 0.92f)) {
66 const int stripe = static_cast<int>(v * 12.f);
67 if ((stripe & 1) == 0) r = g = b = 0.95f;
68 }
69
70 // Direction arrow glyph in the middle third.
71 const float au = (u - 0.42f) / 0.16f;
72 const float av = (v - 0.5f) / 0.18f;
73 if (au >= 0.f && au <= 1.f && std::fabs(av) <= (1.f - au) * 0.55f + 0.08f) {
74 r = 0.95f;
75 g = 0.45f;
76 b = 0.08f;
77 }
78
79 image->setPixel(x, y, image::ImageData::Colorf{r, g, b, 1.f});
80 }
81 }
82 (void)error;
83 return image;
84}
85
87 RecipeDescriptor schema{"mesh.roadNetwork", "Procedural Road Network", "Mesh", {}};
88 schema.params.push_back(ParamDescriptor::integer("seed", "Seed", 1, 0, 2147483647));
89 schema.params.push_back(
90 ParamDescriptor::choice("scene", "Scene", "straight",
91 {"straight", "curve", "bridge", "cross", "tee", "y", "fork", "skew", "t-junction",
92 "y-junction", "sloped-t", "curve-uphill", "tight-turn", "roundabout", "interchange"}));
93 schema.params.push_back(ParamDescriptor::choice("roadType", "Road Type", "highway", {"highway", "railway"}));
94 schema.params.push_back(ParamDescriptor::floating("span", "Span", 36.f, 16.f, 256.f, 1.f));
95 schema.params.push_back(ParamDescriptor::floating("bridgeHeight", "Bridge Height", 6.f, 1.f, 64.f, 0.5f));
96 schema.params.push_back(ParamDescriptor::integer("lanes", "Lanes / Tracks", 2, 1, 4));
97 schema.params.push_back(ParamDescriptor::integer("pathSegments", "Path Segments", 32, 8, 256));
98 schema.params.push_back(ParamDescriptor::boolean("piers", "Piers", true));
99 schema.params.push_back(ParamDescriptor::boolean("markings", "Markings", true));
100 schema.params.push_back(ParamDescriptor::boolean("navigation", "Navigation Overlay", true));
101 schema.params.push_back(ParamDescriptor::boolean("junctions", "Junctions", true));
102 schema.params.push_back(ParamDescriptor::boolean("placements", "Decoration Placements", true));
103 schema.params.push_back(
104 ParamDescriptor::floating("sideObjectSpacing", "Side Object Spacing", 8.f, 0.25f, 100.f, 0.25f));
105 schema.params.push_back(ParamDescriptor::floating("sideObjectOffset", "Side Object Offset", 0.5f, 0.f, 32.f, 0.1f));
106 schema.params.push_back(
107 ParamDescriptor::floating("sideObjectClearance", "Side Object Clearance", 1.5f, 0.f, 32.f, 0.1f));
108 schema.params.push_back(
109 ParamDescriptor::floating("junctionChordError", "Junction Curve Error", 0.10f, 0.01f, 1.f, 0.01f));
110 schema.params.push_back(
111 ParamDescriptor::floating("terrainBlendDistance", "Terrain Surface Blend", 2.f, 0.f, 64.f, 0.25f));
112 schema.params.push_back(ParamDescriptor::floating("chunkSize", "Render Chunk Size", 64.f, 4.f, 1024.f, 1.f));
113 schema.params.push_back(ParamDescriptor::integer("lodCount", "LOD Count", 3, 1, 4));
114 registry.registerRecipe(std::move(schema), generateRoadNetworkMesh);
115
116 RecipeDescriptor bridge{"mesh.autoBridge", "Automatic Bridge", "Mesh", {}};
118 const auto& defaults = reference.value();
119 for (const auto& axis : {"X", "Y", "Z"}) {
120 const std::string suffix(axis);
121 bridge.params.push_back(ParamDescriptor::floating(
122 "start" + suffix, "Start " + suffix,
123 suffix == "X" ? defaults.start.x : (suffix == "Y" ? defaults.start.y : defaults.start.z), -10000.f, 10000.f,
124 .5f));
125 bridge.params.push_back(ParamDescriptor::floating(
126 "end" + suffix, "End " + suffix,
127 suffix == "X" ? defaults.end.x : (suffix == "Y" ? defaults.end.y : defaults.end.z), -10000.f, 10000.f,
128 .5f));
129 }
130 bridge.params.push_back(ParamDescriptor::floating("deckWidth", "Driving Width", defaults.width, 1.f, 32.f, .5f));
131 bridge.params.push_back(ParamDescriptor::choice("bridgeType", "Bridge Type", "beam",
132 {"beam", "slab", "arch", "tiedArch", "throughTruss", "deckTruss",
133 "suspension", "cableStayed", "cantilever", "trestle"}));
134 bridge.params.push_back(ParamDescriptor::integer("spans", "Spans / Bents", defaults.spans, 1, 64));
135 bridge.params.push_back(ParamDescriptor::integer("panels", "Structural Panels", defaults.panels, 2, 64));
136 bridge.params.push_back(
137 ParamDescriptor::integer("curveSegments", "Curve Segments", defaults.curveSegments, 4, 128));
138 bridge.params.push_back(ParamDescriptor::integer("cableSides", "Cable Sides", defaults.cableSides, 3, 16));
139 bridge.params.push_back(
140 ParamDescriptor::floating("groundY", "Foundation Level", defaults.groundY, -10000.f, 10000.f, .5f));
141 bridge.params.push_back(
142 ParamDescriptor::floating("memberWidth", "Structural Member Width", defaults.memberWidth, .01f, 10.f, .05f));
143 bridge.params.push_back(ParamDescriptor::floating("archRise", "Arch Rise", defaults.archRise, .1f, 100.f, .5f));
144 bridge.params.push_back(
145 ParamDescriptor::floating("towerHeight", "Tower Height", defaults.towerHeight, 1.f, 100.f, .5f));
146 bridge.params.push_back(
147 ParamDescriptor::floating("trussHeight", "Truss Height", defaults.trussHeight, .1f, 100.f, .5f));
148 bridge.params.push_back(
149 ParamDescriptor::floating("cableRadius", "Cable Radius", defaults.cableRadius, .01f, 2.f, .01f));
150 bridge.params.push_back(
151 ParamDescriptor::floating("cableSag", "Cable Sag Ratio", defaults.cableSag, .05f, .95f, .05f));
152 bridge.params.push_back(
153 ParamDescriptor::floating("towerInset", "Tower Inset Ratio", defaults.towerInset, .15f, .4f, .01f));
154 bridge.params.push_back(ParamDescriptor::floating("towerTopWidthRatio", "Tower Top Width Ratio",
155 defaults.towerTopWidthRatio, .05f, 1.f, .05f));
156 bridge.params.push_back(ParamDescriptor::boolean("railings", "Railings", true));
157 bridge.params.push_back(ParamDescriptor::boolean("crossBracing", "Cross Bracing", true));
158 bridge.params.push_back(
159 ParamDescriptor::floating("deckThickness", "Deck Thickness", defaults.deckThickness, .1f, 5.f, .05f));
160 bridge.params.push_back(ParamDescriptor::floating("pierWidth", "Pier Width", defaults.pierWidth, .1f, 10.f, .1f));
161 bridge.params.push_back(ParamDescriptor::floating("pierDepth", "Pier Depth", defaults.pierDepth, .1f, 10.f, .1f));
162 bridge.params.push_back(
163 ParamDescriptor::floating("railingHeight", "Railing Height", defaults.railingHeight, .1f, 3.f, .1f));
164 bridge.params.push_back(
165 ParamDescriptor::floating("railingWidth", "Railing Width", defaults.railingWidth, .1f, 2.f, .05f));
166 bridge.params.push_back(
167 ParamDescriptor::floating("sidewalkWidth", "Sidewalk Width", defaults.sidewalkWidth, 0.f, 5.f, .1f));
168 bridge.params.push_back(ParamDescriptor::boolean("piers", "Piers", true));
169 bridge.params.push_back(ParamDescriptor::boolean("markings", "Markings", true));
170 bridge.params.push_back(ParamDescriptor::choice("railingType", "Railing Construction", "metal",
171 {"metal", "concrete", "masonry", "timber"}));
172 bridge.params.push_back(ParamDescriptor::boolean("connectionDetails", "Gusset Plates and Bolts", true));
173 bridge.params.push_back(ParamDescriptor::integer("railingBars", "Horizontal Rails", defaults.railingBars, 1, 8));
174 bridge.params.push_back(ParamDescriptor::integer("plankCount", "Timber Deck Planks", defaults.plankCount, 8, 2048));
175 bridge.params.push_back(
176 ParamDescriptor::floating("memberDepth", "Structural Section Depth", defaults.memberDepth, 0.01f, 10.f, 0.05f));
177 bridge.params.push_back(ParamDescriptor::floating("girderHeight", "Longitudinal Girder Height",
178 defaults.girderHeight, 0.1f, 10.f, 0.1f));
179 bridge.params.push_back(
180 ParamDescriptor::floating("archRibWidth", "Arch Rib Width", defaults.archRibWidth, 0.1f, 10.f, 0.1f));
181 bridge.params.push_back(
182 ParamDescriptor::floating("archRibDepth", "Arch Rib Depth", defaults.archRibDepth, 0.1f, 10.f, 0.1f));
183 bridge.params.push_back(ParamDescriptor::floating("foundationHeight", "Foundation Step Height",
184 defaults.foundationHeight, 0.1f, 5.f, 0.1f));
185 bridge.params.push_back(ParamDescriptor::floating("foundationScale", "Foundation Width Scale",
186 defaults.foundationScale, 1.f, 5.f, 0.1f));
187 bridge.params.push_back(
188 ParamDescriptor::floating("pierCapHeight", "Pier Capital Height", defaults.pierCapHeight, 0.1f, 5.f, 0.1f));
189 bridge.params.push_back(
190 ParamDescriptor::floating("abutmentDepth", "Abutment Depth", defaults.abutmentDepth, 0.1f, 10.f, 0.1f));
191 bridge.params.push_back(ParamDescriptor::floating("wingWallLength", "Retaining Wing Length",
192 defaults.wingWallLength, 0.1f, 20.f, 0.5f));
193 bridge.params.push_back(
194 ParamDescriptor::floating("uvMeters", "Texture Repeat Metres", defaults.uvMeters, 0.01f, 20.f, 0.1f));
195 bridge.params.push_back(
196 ParamDescriptor::floating("portalInset", "Sloped Truss End Ratio", defaults.portalInset, 0.f, 0.2f, 0.01f));
197 bridge.params.push_back(ParamDescriptor::floating("cantileverCenterRatio", "Suspended Center Depth Ratio",
198 defaults.cantileverCenterRatio, 0.1f, 0.7f, 0.05f));
199 bridge.params.push_back(
200 ParamDescriptor::floating("towerLegWidth", "Tower Leg Width", defaults.towerLegWidth, 0.1f, 10.f, 0.1f));
201 registry.registerRecipe(std::move(bridge), [](const Params& params, MeshBuild& out, std::string& error) {
202 if (params.has("networkSnapshot")) {
203 error = "mesh.autoBridge accepts endpoints, not networkSnapshot";
204 return false;
205 }
206 auto decoded = bridgeParametersFromParams(params);
207 if (!decoded.ok()) {
208 error = decoded.status().describe();
209 return false;
210 }
211 auto plan = planBridge(decoded.value());
212 if (!plan.ok()) {
213 error = plan.status().describe();
214 return false;
215 }
216 auto mesh = bakeBridge(plan.value());
217 if (!mesh.ok()) {
218 error = mesh.status().describe();
219 return false;
220 }
221 out = std::move(mesh).takeValue();
222 return true;
223 });
224}
225
227 RecipeDescriptor schema = RecipeDescriptor::grid("tex.roadMarkings", "Road Markings Logic", "Road", 8, 8);
228 schema.params.push_back(ParamDescriptor::floating("dashPeriod", "Dash Period", 0.18f, 0.02f, 1.f, 0.01f));
229 schema.params.push_back(ParamDescriptor::floating("dashDuty", "Dash Duty", 0.45f, 0.05f, 0.95f, 0.01f));
230 schema.params.push_back(ParamDescriptor::boolean("zebra", "Zebra Ends", true));
231 registry.registerRecipe(std::move(schema), generateRoadMarkingsTexture);
232}
233
234} // namespace eve::procgen::road
double y
double x
AuthorityStoreHandleRef reference
Definition Authority.cpp:24
std::vector< Value > defaults
Definition ECS.cpp:49
tensor::Graph g
Definition GpuGraph.cpp:7
vk::UniqueImage image
float u
Definition Grass.cpp:233
double r
float v
std::uint32_t height
std::uint32_t width
JointPoint2D axis
std::vector< RecipeParam > params
MeleePoint3 b
Definition MeleeHit.cpp:41
OceanMeshSettings mesh
std::string error
Definition Package.cpp:174
const eve::script::BindingContractRegistry & registry
ScriptStructure out
float phase
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
medialoader::Colorf Colorf
Definition ImageData.h:41
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
EVENGINE_API_DOMAINS public API.
Result< BridgePlan > planBridge(const BridgeParameters &p)
Plan the selected topology without renderer or terrain providers.
Result< RoadOverlay > generateRoadNetworkOverlay(const Params &params)
Bake navigation overlay for the same Params used by mesh.roadNetwork.
std::unique_ptr< image::ImageData > generateRoadMarkingsTexture(const Params &params, std::string &error)
Pure-function road marking atlas (asphalt + edge + dashed center + zebra).
Result< BridgeParameters > bridgePreset(BridgeKind kind)
Return an owning reference-aligned preset; unsupported kinds fail without side effects.
Result< MeshBuild > bakeBridge(const BridgePlan &plan)
Bake a validated member plan into independent CPU geometry; upload separately.
Result< BridgeParameters > bridgeParametersFromParams(const Params &params)
Decode recipe parameters into an owning typed authoring value.
bool generateRoadNetworkMesh(const Params &params, MeshBuild &out, std::string &error)
Generate a multi-level interchange mesh from Params (span/height/lanes/seed).
Result< RoadBakeResult > bakeRoadNetworkRecipe(const Params &params)
Build the shared road recipe once for mesh, navigation and artifact projections.
void registerRoadMeshRecipes(MeshRecipeRegistry &registry)
Registers road mesh recipes.
void registerRoadTextureRecipes(TextureRecipeRegistry &registry)
Registers road texture recipes.
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
static RecipeDescriptor grid(std::string id, std::string displayName, std::string category, int minimumWidth, int minimumHeight, int maximumWidth=4096, int maximumHeight=4096)
Create a schema with common seed, width and height parameters.
std::vector< ParamDescriptor > params
Definition ParamSchema.h:56