载入中...
搜索中...
未找到
RoadBakeProfiles.cpp
浏览该文件的文档.
1#include <algorithm>
2#include <cmath>
4
6namespace {
7bool sectionTriangleHasArea(const RoadProfile& profile, float scale, std::size_t a, std::size_t b, std::size_t c) {
8 const float asphaltHalf = profileAsphaltHalfWidth(profile);
9 const double originSide = scaledProfileSide(profile.points[a].side, asphaltHalf, scale);
10 const double ax = static_cast<double>(scaledProfileSide(profile.points[b].side, asphaltHalf, scale)) - originSide;
11 const double ay = static_cast<double>(profile.points[b].up) - profile.points[a].up;
12 const double bx = static_cast<double>(scaledProfileSide(profile.points[c].side, asphaltHalf, scale)) - originSide;
13 const double by = static_cast<double>(profile.points[c].up) - profile.points[a].up;
14 const double area = ax * by - ay * bx;
15 return area * area > 1e-12;
16}
17} // namespace
18Result<void> loftProfileClean(MeshBuild& mesh, const std::vector<SplineFrameSample>& frames,
19 const RoadProfile& sourceProfile, float pathLength, float uvMeters,
20 RoadMaterial asphaltMaterial, std::size_t budget, int subdivisions) {
21 if (frames.size() < 2 || sourceProfile.points.size() < 2 || subdivisions < 1 || subdivisions > 32)
22 return bakeFail<void>(DiagnosticCode::InvalidArgument, "invalid section subdivision input", "profile");
23 RoadProfile refined;
24 const RoadProfile* selected = &sourceProfile;
25 if (subdivisions > 1) {
26 const std::size_t originalSegments =
27 sourceProfile.closed ? sourceProfile.points.size() : sourceProfile.points.size() - 1;
28 const std::size_t work = (frames.size() - 1) * originalSegments * static_cast<std::size_t>(subdivisions) * 10u;
29 const auto existing =
30 static_cast<std::size_t>(mesh.getVertexCount()) + static_cast<std::size_t>(mesh.getIndexCount());
31 if (existing > budget || work > budget - existing)
32 return bakeFail<void>(DiagnosticCode::PreconditionViolation, "section subdivisions exceed mesh budget",
33 "mesh");
34 refined = sourceProfile;
35 refined.points.clear();
36 refined.explicitMaterials = true;
37 refined.points.reserve(originalSegments * static_cast<std::size_t>(subdivisions) + 1);
38 for (std::size_t i = 0; i < originalSegments; ++i) {
39 const auto& a = sourceProfile.points[i];
40 const auto& b = sourceProfile.points[(i + 1) % sourceProfile.points.size()];
41 auto material = a.material;
42 if (!sourceProfile.explicitMaterials &&
43 (a.material == RoadMaterial::Asphalt || b.material == RoadMaterial::Asphalt))
44 material = std::fabs(a.up - b.up) > 1e-3f ? RoadMaterial::Curb : RoadMaterial::Asphalt;
45 for (int j = 0; j < subdivisions; ++j) {
46 const float t = static_cast<float>(j) / static_cast<float>(subdivisions);
47 refined.points.push_back({std::lerp(a.side, b.side, t), std::lerp(a.up, b.up, t), material});
48 }
49 }
50 if (!sourceProfile.closed) refined.points.push_back(sourceProfile.points.back());
51 selected = &refined;
52 }
53 const auto& profile = *selected;
54 if (frames.size() < 2 || profile.points.size() < 2)
55 return bakeFail<void>(DiagnosticCode::InvalidArgument, "loft needs >=2 frames and profile points");
56
57 const std::size_t segments = profile.closed ? profile.points.size() : profile.points.size() - 1;
58 const std::size_t needed = (frames.size() - 1) * segments * 10u;
59 const auto existing =
60 static_cast<std::size_t>(mesh.getVertexCount()) + static_cast<std::size_t>(mesh.getIndexCount());
61 if (existing > budget || needed > budget - existing)
62 return bakeFail<void>(DiagnosticCode::PreconditionViolation, "profile loft exceeds mesh budget", "mesh");
63 const int ringCount = static_cast<int>(frames.size());
64 const int profileCount = static_cast<int>(profile.points.size());
65 std::vector<V3> ringPositions(static_cast<std::size_t>(ringCount * profileCount));
66
67 float profileArea = 0.f;
68 if (profile.closed)
69 for (std::size_t point = 0; point < profile.points.size(); ++point) {
70 const auto& a = profile.points[point];
71 const auto& b = profile.points[(point + 1) % profile.points.size()];
72 profileArea += a.side * b.up - a.up * b.side;
73 }
74 for (int ring = 0; ring < ringCount; ++ring) {
75 const auto& frame = frames[static_cast<std::size_t>(ring)];
76 const V3 origin{frame.sample.x, frame.sample.y, frame.sample.z};
77 const V3 side{frame.sideX, frame.sideY, frame.sideZ};
78 const V3 up{frame.upX, frame.upY, frame.upZ};
79 for (int i = 0; i < profileCount; ++i) {
80 const auto& pp = profile.points[static_cast<std::size_t>(i)];
81 ringPositions[static_cast<std::size_t>(ring * profileCount + i)] =
82 origin + side * scaledProfileSide(pp.side, profileAsphaltHalfWidth(profile), frame.sample.scaleX) +
83 up * pp.up;
84 }
85 }
86
87 for (int ring = 0; ring < ringCount - 1; ++ring) {
88 // Spline samples retain their distance along the untrimmed edge. Using
89 // that distance keeps texture phase stable when junction radii or bake
90 // tessellation change; uvMeters is metres per repeat, not repeat count.
91 const float u0 = frames[static_cast<std::size_t>(ring)].sample.normalizedDistance * pathLength / uvMeters;
92 const float u1 = frames[static_cast<std::size_t>(ring + 1)].sample.normalizedDistance * pathLength / uvMeters;
93 for (int i = 0; i < static_cast<int>(segments); ++i) {
94 const auto& a = profile.points[static_cast<std::size_t>(i)];
95 const int next = (i + 1) % profileCount;
96 const auto& b = profile.points[static_cast<std::size_t>(next)];
97 // Prefer asphalt for near-horizontal driving strips (endpoint material alone
98 // would tag the asphalt span as curb because the left corner is a curb drop).
99 RoadMaterial mat = a.material;
100 if (!profile.explicitMaterials && std::fabs(a.up - b.up) > 1e-3f &&
101 (a.material == RoadMaterial::Asphalt || b.material == RoadMaterial::Asphalt))
102 mat = RoadMaterial::Curb;
103 if (!profile.explicitMaterials && std::fabs(a.up - b.up) <= 1e-3f &&
104 (a.material == RoadMaterial::Asphalt || b.material == RoadMaterial::Asphalt)) {
106 }
107 if (mat == RoadMaterial::Asphalt) mat = asphaltMaterial;
108 const V3& p00 = ringPositions[static_cast<std::size_t>(ring * profileCount + i)];
109 const V3& p01 = ringPositions[static_cast<std::size_t>(ring * profileCount + next)];
110 const V3& p10 = ringPositions[static_cast<std::size_t>((ring + 1) * profileCount + i)];
111 const V3& p11 = ringPositions[static_cast<std::size_t>((ring + 1) * profileCount + next)];
112 V3 normal = normalize(cross(p10 - p00, p01 - p00));
113 if (profile.closed && profileArea > 0.f) normal = normal * -1.f;
114 const float v0 = a.side / uvMeters;
115 const float v1 = b.side / uvMeters;
116 appendOrientedQuad(mesh, p00, p10, p11, p01, normal, u0, u1, v0, v1, mat, QuadDiagonal::SecondToFourth);
117 }
118 }
119 return Result<void>::success();
120}
121
124 if (profile.points.size() < 3) return Result<void>::success();
125 const V3 origin{frame.sample.x, frame.sample.y, frame.sample.z};
126 const V3 side{frame.sideX, frame.sideY, frame.sideZ};
127 const V3 up{frame.upX, frame.upY, frame.upZ};
128 V3 fwd{frame.forwardX, frame.forwardY, frame.forwardZ};
129 if (!outward) fwd = fwd * -1.f;
130 const V3 nrm = normalize(fwd);
131 std::vector<V3> ring;
132 ring.reserve(profile.points.size());
133 for (const auto& pp : profile.points)
134 ring.push_back(origin +
135 side * scaledProfileSide(pp.side, profileAsphaltHalfWidth(profile), frame.sample.scaleX) +
136 up * pp.up);
137 if (profile.explicitMaterials) {
138 if (!profile.closed) return Result<void>::success();
139 std::vector<V3> polygon;
140 polygon.reserve(profile.points.size());
141 for (const auto& point : profile.points) polygon.push_back({point.side, 0.f, point.up});
142 std::vector<std::array<std::size_t, 3>> triangles;
145 return bakeFail<void>(DiagnosticCode::InvalidArgument, "section cap cannot be triangulated", "profile");
146 const auto base = static_cast<std::uint32_t>(mesh.getVertexCount());
147 mesh.setActiveGroup(roadMaterialGroup(profile.points.front().material == RoadMaterial::Asphalt
149 : profile.points.front().material));
150 for (const auto& p : ring) mesh.addVertex(p.x, p.y, p.z, nrm.x, nrm.y, nrm.z, 0.f, 0.f);
151 for (const auto& tri : triangles) {
152 auto a = tri[0], b = tri[1], c = tri[2];
153 if (dot(cross(ring[b] - ring[a], ring[c] - ring[a]), nrm) < 0.f) std::swap(b, c);
154 mesh.addTriangle(base + static_cast<std::uint32_t>(a), base + static_cast<std::uint32_t>(b),
155 base + static_cast<std::uint32_t>(c));
156 }
157 return Result<void>::success();
158 }
159 // Fan-fill the end polygon (closes the jersey-barrier U looking into the hub).
161 const auto base = static_cast<std::uint32_t>(mesh.getVertexCount());
162 for (const V3& p : ring) mesh.addVertex(p.x, p.y, p.z, nrm.x, nrm.y, nrm.z, 0.f, 0.f);
163 for (std::size_t i = 1; i + 1 < ring.size(); ++i) {
164 if (!sectionTriangleHasArea(profile, frame.sample.scaleX, 0, i, i + 1)) continue;
165 if (outward)
166 mesh.addTriangle(base, base + static_cast<std::uint32_t>(i), base + static_cast<std::uint32_t>(i + 1));
167 else
168 mesh.addTriangle(base, base + static_cast<std::uint32_t>(i + 1), base + static_cast<std::uint32_t>(i));
169 }
170 return Result<void>::success();
171}
172
180void capProfileShoulders(MeshBuild& mesh, const SplineFrameSample& frame, const RoadProfile& profile, bool outward) {
181 if (profile.points.size() < 6) return;
182 const V3 origin{frame.sample.x, frame.sample.y, frame.sample.z};
183 const V3 side{frame.sideX, frame.sideY, frame.sideZ};
184 const V3 up{frame.upX, frame.upY, frame.upZ};
185 V3 fwd{frame.forwardX, frame.forwardY, frame.forwardZ};
186 if (!outward) fwd = fwd * -1.f;
187 const V3 nrm = normalize(fwd);
188
189 // First Asphalt point marks the deck span start; shoulders are everything else.
190 std::size_t asphaltBegin = profile.points.size();
191 for (std::size_t i = 0; i < profile.points.size(); ++i) {
192 if (profile.points[i].material == RoadMaterial::Asphalt) {
193 asphaltBegin = i;
194 break;
195 }
196 }
197 if (asphaltBegin == 0 || asphaltBegin >= profile.points.size()) return;
198
199 auto emitFan = [&](std::size_t begin, std::size_t end, RoadMaterial mat) {
200 if (end < begin + 3) return;
201 mesh.setActiveGroup(roadMaterialGroup(mat));
202 const auto base = static_cast<std::uint32_t>(mesh.getVertexCount());
203 for (std::size_t i = begin; i < end; ++i) {
204 const auto& pp = profile.points[i];
205 const V3 p = origin +
206 side * scaledProfileSide(pp.side, profileAsphaltHalfWidth(profile), frame.sample.scaleX) +
207 up * pp.up;
208 mesh.addVertex(p.x, p.y, p.z, nrm.x, nrm.y, nrm.z, 0.f, 0.f);
209 }
210 const auto count = static_cast<std::uint32_t>(end - begin);
211 for (std::uint32_t i = 1; i + 1 < count; ++i) {
212 if (!sectionTriangleHasArea(profile, frame.sample.scaleX, begin, begin + i, begin + i + 1)) continue;
213 if (outward)
214 mesh.addTriangle(base, base + i, base + i + 1);
215 else
216 mesh.addTriangle(base, base + i + 1, base + i);
217 }
218 };
219
220 // Left shoulder: outer sidewalk → curb down to asphalt lip (excludes asphalt point).
221 emitFan(0, asphaltBegin, RoadMaterial::Sidewalk);
222 // Right shoulder: asphalt lip → curb → outer sidewalk.
223 emitFan(asphaltBegin, profile.points.size(), RoadMaterial::Sidewalk);
224}
225
226
227} // namespace eve::procgen::road::detail
double z
double y
double x
std::string selected
glm::vec4 p[6]
float area
Definition Grass.cpp:62
HexVec3 up
std::int32_t c
std::array< float, 3 > scale
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
std::vector< TriangleRef > triangles
Texture * normal
OceanMeshSettings mesh
glm::dvec3 origin
ssq::Object frame
std::array< uint32_t, 16 > material
float t
size_t begin
size_t end
int count
int segments
Definition TreeMesh.cpp:281
glm::vec3 point
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
double sample(const Heightmap &map, double u, double v)
Sample.
V3 cross(V3 a, V3 b)
Module-private road bake implementation helper.
Definition RoadBake.cpp:29
float scaledProfileSide(float side, float asphaltHalf, float scale)
Module-private transverse width transform preserving fixed-width shoulders.
Definition RoadBake.cpp:114
float profileAsphaltHalfWidth(const RoadProfile &profile)
Module-private driving-surface half-width from a generated profile.
Definition RoadBake.cpp:121
Result< void > capProfileRing(MeshBuild &mesh, const SplineFrameSample &frame, const RoadProfile &profile, bool outward)
Module-private road bake implementation helper.
void capProfileShoulders(MeshBuild &mesh, const SplineFrameSample &frame, const RoadProfile &profile, bool outward)
Module-private road bake implementation helper.
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.
TriangulationStatus triangulateBoundary(const std::vector< V3 > &polygon, std::vector< std::array< std::size_t, 3 > > &triangles, BoundaryPointPolicy boundaryPolicy)
Definition RoadBake.cpp:348
float dot(V3 a, V3 b)
Module-private road bake implementation helper.
Definition RoadBake.cpp:28
void appendOrientedQuad(MeshBuild &mesh, V3 a, V3 b, V3 c, V3 d, V3 normal, float u0, float u1, float v0, float v1, RoadMaterial material, QuadDiagonal diagonal)
Emit a quad whose winding matches normal (mesh3D expects object-space CCW).
Definition RoadBake.cpp:441
V3 normalize(V3 a)
Module-private road bake implementation helper.
Definition RoadBake.cpp:31
const char * roadMaterialGroup(RoadMaterial material) noexcept
Map a material tag to the MeshBuild group name used by the baker. @ownership borrowed — returns a poi...
RoadMaterial
Profile material tags used when lofting a road cross-section.
Definition RoadTypes.h:37
Owning section frame; sample position includes roll-pivot displacement, unlike the author centerline ...
Definition SplinePath.h:63
Owning open profile polyline for one edge style and lane layout.
Definition RoadTypes.h:347
std::vector< RoadProfilePoint > points
Definition RoadTypes.h:348