Implement support for clipping road mesh geometry using visible station runs

This commit is contained in:
2026-05-13 13:18:39 +02:00
parent 1898298472
commit 5a0f9f1245

View File

@@ -415,6 +415,7 @@ function addEdgeProfileCaps(options: {
export function buildSplineRoadMeshData(options: {
path: SplineRoadPathLike;
terrains?: readonly Terrain[];
clipIntervals?: readonly SplineCorridorPathClipInterval[];
}): SplineRoadMeshData | null {
const { path } = options;
const terrains = options.terrains ?? [];
@@ -428,10 +429,18 @@ export function buildSplineRoadMeshData(options: {
const uvs: number[] = [];
const indices: number[] = [];
const halfWidth = path.road.width * 0.5;
const stationRuns = buildVisibleRoadStationRuns(
stations,
options.clipIntervals ?? []
);
let stationCount = 0;
for (let index = 0; index < stations.length; index += 1) {
const station = stations[index]!;
const tangent = resolveStationTangent(stations, index);
for (const run of stationRuns) {
const runVertexOffset = positions.length / 3;
for (let index = 0; index < run.length; index += 1) {
const station = run[index]!;
const tangent = resolveStationTangent(run, index);
if (tangent === null) {
return null;
@@ -459,8 +468,8 @@ export function buildSplineRoadMeshData(options: {
uvs.push(0, station.distance, 1, station.distance);
}
for (let index = 0; index < stations.length - 1; index += 1) {
const left = index * 2;
for (let index = 0; index < run.length - 1; index += 1) {
const left = runVertexOffset + index * 2;
const right = left + 1;
const nextLeft = left + 2;
const nextRight = left + 3;
@@ -468,12 +477,19 @@ export function buildSplineRoadMeshData(options: {
indices.push(left, right, nextLeft, nextLeft, right, nextRight);
}
stationCount += run.length;
}
if (positions.length === 0) {
return null;
}
return {
pathId: path.id,
positions: new Float32Array(positions),
uvs: new Float32Array(uvs),
indices: new Uint32Array(indices),
stationCount: stations.length,
stationCount,
totalLength: stations[stations.length - 1]?.distance ?? 0
};
}
@@ -481,6 +497,7 @@ export function buildSplineRoadMeshData(options: {
export function buildSplineRoadMeshGeometry(options: {
path: SplineRoadPathLike;
terrains?: readonly Terrain[];
clipIntervals?: readonly SplineCorridorPathClipInterval[];
}): BufferGeometry | null {
const meshData = buildSplineRoadMeshData(options);