载入中...
搜索中...
未找到
RoadBakeStructures.cpp
浏览该文件的文档.
2
3#include <algorithm>
4#include <cmath>
5
7namespace {
8SplineFrameSample interpolate(const SplineFrameSample& a, const SplineFrameSample& b, float fraction) {
9 const float t =
10 (fraction - a.sample.normalizedDistance) / (b.sample.normalizedDistance - a.sample.normalizedDistance);
11 auto out = a;
12 out.authoredMarker = false;
13 out.sample.x = std::lerp(a.sample.x, b.sample.x, t);
14 out.sample.y = std::lerp(a.sample.y, b.sample.y, t);
15 out.sample.z = std::lerp(a.sample.z, b.sample.z, t);
16 out.sample.scaleX = std::lerp(a.sample.scaleX, b.sample.scaleX, t);
17 out.sample.rollDegrees = std::lerp(a.sample.rollDegrees, b.sample.rollDegrees, t);
18 out.sample.normalizedDistance = fraction;
19 const auto forward = normalize(V3{std::lerp(a.forwardX, b.forwardX, t), std::lerp(a.forwardY, b.forwardY, t),
20 std::lerp(a.forwardZ, b.forwardZ, t)});
21 const auto upHint =
22 normalize(V3{std::lerp(a.upX, b.upX, t), std::lerp(a.upY, b.upY, t), std::lerp(a.upZ, b.upZ, t)});
23 const auto side = normalize(cross(upHint, forward)), up = normalize(cross(forward, side));
24 out.forwardX = forward.x;
25 out.forwardY = forward.y;
26 out.forwardZ = forward.z;
27 out.sideX = side.x;
28 out.sideY = side.y;
29 out.sideZ = side.z;
30 out.upX = up.x;
31 out.upY = up.y;
32 out.upZ = up.z;
33 return out;
34}
35std::vector<SplineFrameSample> clipFrames(const std::vector<SplineFrameSample>& frames, float start, float end) {
36 std::vector<SplineFrameSample> result;
37 start = std::max(start, frames.front().sample.normalizedDistance);
38 end = std::min(end, frames.back().sample.normalizedDistance);
39 if (end - start <= 1e-6f) return result;
40 auto sample = [&](float fraction) {
41 for (std::size_t i = 1; i < frames.size(); ++i) {
42 if (fraction <= frames[i].sample.normalizedDistance + 1e-7f) {
43 if (std::fabs(fraction - frames[i].sample.normalizedDistance) < 1e-7f) return frames[i];
44 if (fraction <= frames[i - 1].sample.normalizedDistance) return frames[i - 1];
45 return interpolate(frames[i - 1], frames[i], fraction);
46 }
47 }
48 return frames.back();
49 };
50 result.push_back(sample(start));
51 for (const auto& f : frames)
52 if (f.sample.normalizedDistance > start + 1e-7f && f.sample.normalizedDistance < end - 1e-7f)
53 result.push_back(f);
54 result.push_back(sample(end));
55 return result;
56}
57} // namespace
58Result<void> bakeStructuralGeometry(MeshBuild& mesh, const std::vector<SplineFrameSample>& frames, const RoadEdge& edge,
59 const RoadProfile& profile, float pathLength, const RoadBakeOptions& options) {
60 std::vector<float> boundaries{frames.front().sample.normalizedDistance, frames.back().sample.normalizedDistance};
61 for (const auto& interval : edge.style.structures) {
62 if (interval.start > boundaries.front() && interval.start < frames.back().sample.normalizedDistance)
63 boundaries.push_back(interval.start);
64 if (interval.end > boundaries.front() && interval.end < frames.back().sample.normalizedDistance)
65 boundaries.push_back(interval.end);
66 }
67 std::sort(boundaries.begin(), boundaries.end());
68 boundaries.erase(std::unique(boundaries.begin(), boundaries.end()), boundaries.end());
69 for (std::size_t i = 1; i < boundaries.size(); ++i) {
70 auto chunk = clipFrames(frames, boundaries[i - 1], boundaries[i]);
71 if (chunk.size() < 2) continue;
72 const float middle = (boundaries[i - 1] + boundaries[i]) * 0.5f;
73 const RoadStructureInterval* structure = nullptr;
74 for (const auto& item : edge.style.structures)
75 if (middle >= item.start && middle < item.end) {
76 structure = &item;
77 break;
78 }
79 const auto material = structure ? (structure->kind == RoadStructureKind::Bridge ? RoadMaterial::BridgeAsphalt
82 if (structure && structure->kind == RoadStructureKind::Tunnel) {
83 const float startEnd =
84 std::min(structure->end, structure->start + structure->portalStartLength / pathLength);
85 const float endStart = std::max(structure->start, structure->end - structure->portalEndLength / pathLength);
86 std::vector<float> cuts{chunk.front().sample.normalizedDistance, chunk.back().sample.normalizedDistance};
87 for (float cut : {startEnd, endStart})
88 if (cut > cuts.front() && cut < chunk.back().sample.normalizedDistance) cuts.push_back(cut);
89 std::sort(cuts.begin(), cuts.end());
90 cuts.erase(std::unique(cuts.begin(), cuts.end()), cuts.end());
91 for (std::size_t j = 1; j < cuts.size(); ++j) {
92 const float center = (cuts[j - 1] + cuts[j]) * .5f;
93 const auto tag = center < startEnd || center > endStart ? RoadMaterial::TunnelPortalAsphalt
95 auto loft = loftProfileClean(mesh, clipFrames(chunk, cuts[j - 1], cuts[j]), profile, pathLength,
96 edge.style.uvMeters, tag, options.maximumMeshElements,
97 edge.style.crossSectionSubdivisions);
98 if (!loft.ok()) return loft;
99 }
100 } else {
101 auto loft = loftProfileClean(mesh, chunk, profile, pathLength, edge.style.uvMeters, material,
102 options.maximumMeshElements, edge.style.crossSectionSubdivisions);
103 if (!loft.ok()) return loft;
104 }
105 if ((!structure && edge.style.structures.empty() && edge.style.automaticStructures) ||
106 (structure && structure->kind == RoadStructureKind::Bridge)) {
107 auto deck = addDeckAndPiers(mesh, chunk, edge.style, profile.halfWidth, asphaltHalfWidth(edge),
108 options.includePiers, options.maximumMeshElements);
109 if (!deck.ok()) return deck;
110 }
111 if (structure && structure->kind == RoadStructureKind::Tunnel) {
112 const float w = profile.halfWidth, h = structure->tunnelHeight, t = structure->wallThickness;
113 RoadProfile shell;
114 shell.closed = true;
115 shell.explicitMaterials = true;
116 shell.halfWidth = w + t;
117 shell.points = {{-w, 0.f, RoadMaterial::Tunnel}, {-w, h, RoadMaterial::Tunnel},
119 {w + t, 0.f, RoadMaterial::Tunnel}, {w + t, h + t, RoadMaterial::Tunnel},
120 {-w - t, h + t, RoadMaterial::Tunnel}, {-w - t, 0.f, RoadMaterial::Tunnel}};
121 auto tunnel = loftProfileClean(mesh, chunk, shell, pathLength, edge.style.uvMeters, RoadMaterial::Tunnel,
122 options.maximumMeshElements);
123 if (!tunnel.ok()) return tunnel;
124 {
125 auto capped = capProfileRing(mesh, chunk.front(), shell, false);
126 if (!capped.ok()) return capped;
127 }
128 {
129 auto capped = capProfileRing(mesh, chunk.back(), shell, true);
130 if (!capped.ok()) return capped;
131 }
132 }
134 return bakeFail<void>(DiagnosticCode::PreconditionViolation, "structures exceed mesh budget", "mesh");
135 }
136 return Result<void>::success();
137}
138Result<void> bakeContinuousFacilities(MeshBuild& mesh, const std::vector<SplineFrameSample>& frames,
139 const RoadNetwork& network, const RoadEdge& edge, float pathLength,
141 const std::unordered_set<std::uint32_t>& activeNodes) {
142 for (const auto& facility : edge.style.facilities) {
143 auto chunk = clipFrames(frames, facility.start, facility.end);
144 if (chunk.size() < 2) continue;
145 for (auto& frame : chunk) {
146 const float lateral =
147 scaledProfileSide(facility.lateralOffset, asphaltHalfWidth(edge), frame.sample.scaleX);
148 frame.sample.x += frame.sideX * lateral + frame.upX * facility.verticalOffset;
149 frame.sample.y += frame.sideY * lateral + frame.upY * facility.verticalOffset;
150 frame.sample.z += frame.sideZ * lateral + frame.upZ * facility.verticalOffset;
151 frame.sample.scaleX = 1.f;
152 }
153 RoadProfile section;
154 section.points = facility.profile;
155 section.closed = true;
156 section.explicitMaterials = true;
157 if (!facility.prefab.vertices.empty()) {
158 const float low =
159 std::lerp(facility.prefab.sourceStart, facility.prefab.sourceEnd,
160 (chunk.front().sample.normalizedDistance - facility.start) / (facility.end - facility.start));
161 const float high =
162 std::lerp(facility.prefab.sourceStart, facility.prefab.sourceEnd,
163 (chunk.back().sample.normalizedDistance - facility.start) / (facility.end - facility.start));
164 auto deformed = bakeFacilityPrefab(mesh, facility.prefab, chunk, low, high,
165 pathLength * (facility.end - facility.start) /
166 (facility.prefab.sourceEnd - facility.prefab.sourceStart),
167 options.maximumMeshElements);
168 if (!deformed.ok()) return deformed;
169 continue;
170 }
171 auto loft = loftProfileClean(mesh, chunk, section, pathLength, facility.uvMeters, RoadMaterial::Curb,
172 options.maximumMeshElements);
173 if (!loft.ok()) return loft;
174 if (facility.capStart &&
175 !(activeNodes.contains(edge.from) && isFacilityContinued(network, edge.from, edge, facility))) {
176 auto capped = capProfileRing(mesh, chunk.front(), section, false);
177 if (!capped.ok()) return capped;
178 }
179 if (facility.capEnd &&
180 !(activeNodes.contains(edge.to) && isFacilityContinued(network, edge.to, edge, facility))) {
181 auto capped = capProfileRing(mesh, chunk.back(), section, true);
182 if (!capped.ok()) return capped;
183 }
185 return bakeFail<void>(DiagnosticCode::PreconditionViolation, "facilities exceed mesh budget", "mesh");
186 }
187 return Result<void>::success();
188}
189} // namespace eve::procgen::road::detail
Duration start
float w
Definition AnimClip.cpp:738
double lateral
HexVec3 up
int h
Poco::JSON::Object::Ptr result
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
const std::string * tag
OceanMeshSettings mesh
ssq::Object frame
glm::dvec2 center
std::array< uint32_t, 16 > material
float f
float t
Heightmap tunnel
int cuts
Definition RockMesh.cpp:25
ScriptStructure out
size_t end
const SquirrelValueOptions & options
float fraction
V forward
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
CPU triangle mesh from procedural mesh recipes (e.g. marching cubes). Positions/normals are xyz-packe...
Definition MeshBuild.h:20
Owning directed road graph with lane connectivity.
Definition RoadNetwork.h:19
Vec3 interpolate(Vec3 start, Vec3 end, float amount) noexcept
Interpolate.
double sample(const Heightmap &map, double u, double v)
Sample.
Result< void > bakeStructuralGeometry(MeshBuild &mesh, const std::vector< SplineFrameSample > &frames, const RoadEdge &edge, const RoadProfile &profile, float pathLength, const RoadBakeOptions &options)
Module-private road bake implementation helper.
Result< void > bakeFacilityPrefab(MeshBuild &mesh, const RoadFacilityPrefab &prefab, const std::vector< SplineFrameSample > &frames, float sourceLow, float sourceHigh, float fullSourceLength, std::size_t budget)
Clip source triangles into frame slabs and deform their positions and inverse-transpose normals.
V3 cross(V3 a, V3 b)
Module-private road bake implementation helper.
Definition RoadBake.cpp:29
float asphaltHalfWidth(const RoadEdge &edge)
Module-private road bake implementation helper.
Definition RoadBake.cpp:100
float scaledProfileSide(float side, float asphaltHalf, float scale)
Module-private transverse width transform preserving fixed-width shoulders.
Definition RoadBake.cpp:114
Result< void > capProfileRing(MeshBuild &mesh, const SplineFrameSample &frame, const RoadProfile &profile, bool outward)
Module-private road bake implementation helper.
bool isMeshBudgetExceeded(const MeshBuild &mesh, const RoadBakeOptions &options)
Module-private road bake implementation helper.
Definition RoadBake.cpp:37
Result< void > loftProfileClean(MeshBuild &mesh, const std::vector< SplineFrameSample > &frames, const RoadProfile &profile, float pathLength, float uvMeters, RoadMaterial asphaltMaterial, std::size_t budget, int subdivisions=1)
Module-private road bake implementation helper.
bool isFacilityContinued(const RoadNetwork &network, std::uint32_t nodeId, const RoadEdge &edge, const RoadContinuousFacility &facility)
Query compatible degree-two facility continuation; no retained peer pointer.
Result< void > bakeContinuousFacilities(MeshBuild &mesh, const std::vector< SplineFrameSample > &frames, const RoadNetwork &network, const RoadEdge &edge, float pathLength, const RoadBakeOptions &options, const std::unordered_set< std::uint32_t > &activeNodes)
Sweep continuous facilities and clip them to exact interval/socket boundaries.
V3 normalize(V3 a)
Module-private road bake implementation helper.
Definition RoadBake.cpp:31
Result< void > addDeckAndPiers(MeshBuild &mesh, const std::vector< SplineFrameSample > &frames, const RoadStyle &style, float halfWidth, float asphaltHalf, bool includePiers, std::size_t budget)
Module-private road bake implementation helper.
Definition RoadBake.cpp:471
@ TunnelPortalAsphalt
Driving surface inside an authored tunnel portal footprint.
Options for baking a road network into mesh + navigation overlays.
Definition RoadBake.h:50
Directed centerline edge with optional reverse lanes.
Definition RoadTypes.h:286
Owning open profile polyline for one edge style and lane layout.
Definition RoadTypes.h:347
std::vector< RoadProfilePoint > points
Definition RoadTypes.h:348
Normalized arc-distance interval; nonoverlapping, sorted, owned by RoadNetwork.
Definition RoadTypes.h:63
float portalEndLength
Hole footprint depths in metres; zero omits a portal.
Definition RoadTypes.h:68
float tunnelHeight
Interior roof height above the centerline.
Definition RoadTypes.h:66
float wallThickness
Positive tunnel wall/roof thickness in metres.
Definition RoadTypes.h:67