Refactor junction mesh generation to use profile strips and improve seam handling

This commit is contained in:
2026-05-13 18:27:46 +02:00
parent 43084723a0
commit a8c62adfe6

View File

@@ -135,32 +135,6 @@ function getOutwardSegmentNormal(
}; };
} }
function getMiteredOutwardOffset(options: {
footprint: SplineCorridorJunctionFootprint;
pointIndex: number;
edgeWidth: number;
}): { x: number; z: number } {
const points = options.footprint.points;
const previous = points[(options.pointIndex - 1 + points.length) % points.length]!;
const current = points[options.pointIndex]!;
const next = points[(options.pointIndex + 1) % points.length]!;
const previousNormal = getOutwardSegmentNormal(previous, current);
const nextNormal = getOutwardSegmentNormal(current, next);
const miter = normalizeXZ({
x: previousNormal.x + nextNormal.x,
z: previousNormal.z + nextNormal.z
});
const scale = Math.min(
3,
Math.max(1, 1 / Math.max(0.25, miter.x * nextNormal.x + miter.z * nextNormal.z))
);
return {
x: miter.x * options.edgeWidth * scale,
z: miter.z * options.edgeWidth * scale
};
}
function getPerimeterDistances( function getPerimeterDistances(
points: readonly SplineCorridorJunctionFootprintPoint[] points: readonly SplineCorridorJunctionFootprintPoint[]
): number[] { ): number[] {
@@ -207,6 +181,26 @@ function addJunctionEdgeProfileCap(options: {
} }
} }
function addProfileStrip(options: {
indices: number[];
startRow: number;
endRow: number;
profileVertexCount: number;
}) {
for (
let profileIndex = 0;
profileIndex < options.profileVertexCount - 1;
profileIndex += 1
) {
const current = options.startRow + profileIndex;
const currentNext = current + 1;
const next = options.endRow + profileIndex;
const nextNext = next + 1;
options.indices.push(current, currentNext, next, next, currentNext, nextNext);
}
}
function isRoadMouthSegment( function isRoadMouthSegment(
points: readonly SplineCorridorJunctionFootprintPoint[], points: readonly SplineCorridorJunctionFootprintPoint[],
index: number index: number
@@ -272,24 +266,33 @@ export function buildSplineCorridorJunctionEdgeMeshData(options: {
: terrainWorldY + point.heightOffset; : terrainWorldY + point.heightOffset;
} }
for (let pointIndex = 0; pointIndex <= footprint.points.length; pointIndex += 1) { function pushProfileRow(optionsForRow: {
const wrappedPointIndex = pointIndex % footprint.points.length; point: SplineCorridorJunctionFootprintPoint;
const point = footprint.points[wrappedPointIndex]!; outwardNormal: { x: number; z: number };
const outwardOffset = getMiteredOutwardOffset({ perimeterDistance: number;
footprint, }): number {
pointIndex: wrappedPointIndex, const rowOffset = positions.length / 3;
edgeWidth: options.edge.width const outwardNormal = normalizeXZ(optionsForRow.outwardNormal);
});
for (let profileIndex = 0; profileIndex < profile.length; profileIndex += 1) { for (let profileIndex = 0; profileIndex < profile.length; profileIndex += 1) {
const profilePoint = profile[profileIndex]!; const profilePoint = profile[profileIndex]!;
const x = point.x + outwardOffset.x * profilePoint.offsetRatio; const x =
const z = point.z + outwardOffset.z * profilePoint.offsetRatio; optionsForRow.point.x +
const y = resolveBaseY(point, x, z) + profilePoint.heightOffset; outwardNormal.x * options.edge.width * profilePoint.offsetRatio;
const z =
optionsForRow.point.z +
outwardNormal.z * options.edge.width * profilePoint.offsetRatio;
const y =
resolveBaseY(optionsForRow.point, x, z) + profilePoint.heightOffset;
positions.push(x, y, z); positions.push(x, y, z);
uvs.push(perimeterDistances[pointIndex]!, profileIndex / profileLastIndex); uvs.push(
optionsForRow.perimeterDistance,
profileIndex / profileLastIndex
);
} }
return rowOffset;
} }
for (let pointIndex = 0; pointIndex < footprint.points.length; pointIndex += 1) { for (let pointIndex = 0; pointIndex < footprint.points.length; pointIndex += 1) {
@@ -297,38 +300,76 @@ export function buildSplineCorridorJunctionEdgeMeshData(options: {
continue; continue;
} }
const currentRow = pointIndex * profile.length; const currentPoint = footprint.points[pointIndex]!;
const nextRow = currentRow + profile.length; const nextPoint = footprint.points[(pointIndex + 1) % footprint.points.length]!;
const segmentOutwardNormal = getOutwardSegmentNormal(currentPoint, nextPoint);
for (let profileIndex = 0; profileIndex < profile.length - 1; profileIndex += 1) { const currentRow = pushProfileRow({
const current = currentRow + profileIndex; point: currentPoint,
const currentNext = current + 1; outwardNormal: segmentOutwardNormal,
const next = nextRow + profileIndex; perimeterDistance: perimeterDistances[pointIndex]!
const nextNext = next + 1; });
const nextRow = pushProfileRow({
indices.push(current, currentNext, next, next, currentNext, nextNext); point: nextPoint,
} outwardNormal: segmentOutwardNormal,
perimeterDistance: perimeterDistances[pointIndex + 1]!
});
const previousSegmentIndex = const previousSegmentIndex =
(pointIndex - 1 + footprint.points.length) % footprint.points.length; (pointIndex - 1 + footprint.points.length) % footprint.points.length;
const nextSegmentIndex = (pointIndex + 1) % footprint.points.length; const nextSegmentIndex = (pointIndex + 1) % footprint.points.length;
addProfileStrip({
indices,
startRow: currentRow,
endRow: nextRow,
profileVertexCount: profile.length
});
if (isRoadMouthSegment(footprint.points, previousSegmentIndex)) { if (isRoadMouthSegment(footprint.points, previousSegmentIndex)) {
addJunctionEdgeProfileCap({ if (currentPoint.connectionOutwardAxis !== undefined) {
indices, const seamRow = pushProfileRow({
rowOffset: currentRow, point: currentPoint,
profileVertexCount: profile.length, outwardNormal: currentPoint.connectionOutwardAxis,
reverse: true perimeterDistance: perimeterDistances[pointIndex]!
}); });
addProfileStrip({
indices,
startRow: seamRow,
endRow: currentRow,
profileVertexCount: profile.length
});
} else {
addJunctionEdgeProfileCap({
indices,
rowOffset: currentRow,
profileVertexCount: profile.length,
reverse: true
});
}
} }
if (isRoadMouthSegment(footprint.points, nextSegmentIndex)) { if (isRoadMouthSegment(footprint.points, nextSegmentIndex)) {
addJunctionEdgeProfileCap({ if (nextPoint.connectionOutwardAxis !== undefined) {
indices, const seamRow = pushProfileRow({
rowOffset: nextRow, point: nextPoint,
profileVertexCount: profile.length, outwardNormal: nextPoint.connectionOutwardAxis,
reverse: false perimeterDistance: perimeterDistances[pointIndex + 1]!
}); });
addProfileStrip({
indices,
startRow: nextRow,
endRow: seamRow,
profileVertexCount: profile.length
});
} else {
addJunctionEdgeProfileCap({
indices,
rowOffset: nextRow,
profileVertexCount: profile.length,
reverse: false
});
}
} }
} }