载入中...
搜索中...
未找到
RoadTerrainPlan.cpp
浏览该文件的文档.
2#include <limits>
3#include <unordered_map>
8namespace eve::procgen::road {
9namespace {
10template <class T>
11Result<T> fail(const char* message) {
13}
14RoadTerrainWriteOptions optionsFor(const RoadTerrainPolicy& p, std::size_t budget) {
15 RoadTerrainWriteOptions o;
16 o.blendDistance = p.blendDistance;
17 o.maxVerticalDelta = p.maxVerticalDelta;
18 o.splatLayer = p.splatLayer;
19 o.splatOpacity = p.splatOpacity;
20 o.clearDetails = p.clearDetails;
21 o.clearTrees = p.clearTrees;
22 o.cutTunnelHoles = p.cutTunnelHoles;
23 o.tunnelHolesAtPortalsOnly = p.tunnelHolesAtPortalsOnly;
24 o.maximumWork = budget;
25 return o;
26}
27Result<MeshBuild> compact(const MeshBuild& source, const RoadGeometrySourceSpan& span, std::size_t& used,
28 std::size_t cap) {
29 MeshBuild mesh;
30 std::unordered_map<std::uint32_t, int> vertices;
31 std::vector<float> colors;
32 const auto& indices = source.indices();
33 const std::size_t end = static_cast<std::size_t>(span.firstTriangle) + span.triangleCount;
34 for (std::size_t t = span.firstTriangle; t < end; ++t) {
35 if (cap - used < 3) return fail<MeshBuild>("prepared index budget exhausted");
36 used += 3;
37 mesh.setActiveGroup(source.getGroupName(source.getTriangleGroup(static_cast<int>(t))));
38 int out[3];
39 for (int j = 0; j < 3; ++j) {
40 auto v = indices[t * 3 + j];
41 auto found = vertices.find(v);
42 if (found != vertices.end()) {
43 out[j] = found->second;
44 continue;
45 }
46 const std::size_t cost = source.hasVertexColors() ? 12 : 8;
47 if (cost > cap - used) return fail<MeshBuild>("prepared vertex budget exhausted");
48 used += cost;
49 auto i = static_cast<std::size_t>(v);
50 int index = mesh.getVertexCount();
51 mesh.addVertex(source.positions()[i * 3], source.positions()[i * 3 + 1], source.positions()[i * 3 + 2],
52 source.normals()[i * 3], source.normals()[i * 3 + 1], source.normals()[i * 3 + 2],
53 source.uvs()[i * 2], source.uvs()[i * 2 + 1]);
54 vertices.emplace(v, index);
55 out[j] = index;
56 if (source.hasVertexColors())
57 colors.insert(colors.end(), source.colors().begin() + i * 4, source.colors().begin() + i * 4 + 4);
58 }
59 mesh.addTriangle(out[0], out[1], out[2]);
60 }
61 auto colored = mesh.setVertexColors(std::move(colors));
62 if (!colored.ok()) return Result<MeshBuild>::failure(colored.status());
63 return Result<MeshBuild>::success(std::move(mesh));
64}
65} // namespace
71 std::vector<Item> items;
72 std::vector<RoadTerrainWrite> writes(std::size_t budget) const {
73 std::vector<RoadTerrainWrite> out;
74 out.reserve(items.size());
75 for (const auto& item : items) out.push_back({std::cref(item.mesh), optionsFor(item.policy, budget)});
76 return out;
77 }
78};
79RoadTerrainPlan::RoadTerrainPlan(std::unique_ptr<Impl> impl) : impl_(std::move(impl)) {}
81RoadTerrainPlan::RoadTerrainPlan(RoadTerrainPlan&&) noexcept = default;
82RoadTerrainPlan& RoadTerrainPlan::operator=(RoadTerrainPlan&&) noexcept = default;
84 const RoadTerrainPolicy& sharedPolicy,
85 std::size_t maximumPreparedElements) {
86 if (maximumPreparedElements == 0) return fail<RoadTerrainPlan>("prepared element budget must be positive");
87 RoadStyle sharedStyle;
88 sharedStyle.terrainPolicy = sharedPolicy;
89 auto validShared = detail::validateRoadAuthoring(sharedStyle);
90 if (!validShared.ok()) return Result<RoadTerrainPlan>::failure(validShared.status());
91 auto baked = bakeRoadNetwork(network, options);
92 if (!baked.ok()) return Result<RoadTerrainPlan>::failure(baked.status());
93 auto sources = PreparedRoadGeometrySources::prepare(baked.value().mesh);
94 if (!sources.ok()) return Result<RoadTerrainPlan>::failure(sources.status());
95 std::unordered_map<std::uint32_t, RoadTerrainPolicy> policies;
96 for (const auto& edge : network.edges()) policies.emplace(edge.id, edge.style.terrainPolicy);
97 auto impl = std::make_unique<Impl>();
98 std::size_t used = 0;
99 for (const auto& span : sources.value().spans()) {
100 RoadTerrainPolicy policy = sharedPolicy;
101 if (span.kind == RoadGeometrySourceKind::Edge) {
102 auto edge = policies.find(span.id);
103 if (edge == policies.end()) return fail<RoadTerrainPlan>("source edge has no author policy");
104 policy = edge->second;
105 }
106 if (!policy.enabled) continue;
107 if (impl->items.size() >= 4096) return fail<RoadTerrainPlan>("prepared write count exceeds batch limit");
108 auto mesh = compact(baked.value().mesh, span, used, maximumPreparedElements);
109 if (!mesh.ok()) return Result<RoadTerrainPlan>::failure(mesh.status());
110 impl->items.push_back({std::move(mesh).takeValue(), policy});
111 }
113}
114Result<int> RoadTerrainPlan::apply(RoadTerrainWorkspace& workspace, std::size_t maximumWork) const {
115 if (!impl_ || maximumWork == 0) return fail<int>("missing plan or invalid execution budget");
116 if (impl_->items.empty()) return Result<int>::success(0, Status::success(StatusCode::NoOp));
117 auto writes = impl_->writes(maximumWork);
118 return workspace.applyBatch(writes, maximumWork);
119}
120Result<int> RoadTerrainPlan::applyWorld(TerrainWorldWorkspace& world, std::size_t maximumWork) const {
121 if (!impl_ || maximumWork == 0) return fail<int>("missing plan or invalid execution budget");
122 if (impl_->items.empty()) return Result<int>::success(0, Status::success(StatusCode::NoOp));
123 auto writes = impl_->writes(maximumWork);
124 return world.conformRoadBatch(writes, maximumWork);
125}
126} // namespace eve::procgen::road
void * impl
glm::vec4 p[6]
eve::resource::CostSpec cost
std::string message
std::vector< std::uint32_t > indices
float v
uint64_t used
ScriptMotionSinkStore< ScriptMotionSink< MotionColorTraits > > colors
OceanMeshSettings mesh
std::vector< std::pair< std::uint64_t, OceanBoatReceipt > > sources
float t
bool found
ScriptStructure out
std::string_view source
size_t end
const SquirrelValueOptions & options
std::array< double, 2 > vertices[3]
uint32_t index
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
Own a complete Pcg-style terrain grid and its height, splat, detail, tree, object and probe state....
static Result< PreparedRoadGeometrySources > prepare(const MeshBuild &mesh)
Validate mesh layout, fingerprint and complete ordered source partition before returning an owned tok...
Owning directed road graph with lane connectivity.
Definition RoadNetwork.h:19
Immutable owning per-source terrain write plan prepared from one authoritative network snapshot....
Result< int > apply(RoadTerrainWorkspace &workspace, std::size_t maximumWork) const
Apply the immutable plan in one workspace transaction; disabled-only plans are NoOp.
RoadTerrainPlan(RoadTerrainPlan &&) noexcept
Transfer the plan.
Result< int > applyWorld(TerrainWorldWorkspace &world, std::size_t maximumWork) const
Apply through the native World complete-channel transaction.
~RoadTerrainPlan()
Destroy the owned meshes and copied policies.
static Result< RoadTerrainPlan > prepare(const RoadNetwork &network, const RoadBakeOptions &options, const RoadTerrainPolicy &sharedPolicy, std::size_t maximumPreparedElements)
Bake the borrowed network and compact source intervals into independently owned write meshes.
Single mutable owner of imported terrain tiles and their complete-channel history....
Result< int > applyBatch(std::span< const RoadTerrainWrite > writes, std::size_t maximumWork)
Apply ordered per-road policies as one complete-channel history frame.
Result< T > fail(std::string message, DiagnosticCode code=DiagnosticCode::InvalidArgument)
Result< void > validateRoadAuthoring(const RoadStyle &style)
Validate structural intervals and continuous-facility authoring.
Result< RoadBakeResult > bakeRoadNetwork(const RoadNetwork &network, const RoadBakeOptions &options)
Bake every edge, junction platform, pier, marking and navigation overlay.
Definition RoadBake.cpp:729
Options for baking a road network into mesh + navigation overlays.
Definition RoadBake.h:50
Owning cross-section, width policy and structural authoring for one road edge.
Definition RoadTypes.h:182
RoadTerrainPolicy terrainPolicy
Authoritative ground-channel policy; execution remains explicit.
Definition RoadTypes.h:228
std::vector< RoadTerrainWrite > writes(std::size_t budget) const
Owning per-road terrain author policy; copied with styles, presets and history. Owner-thread value wi...