Batch infrastructure and cable stems through engine.lines

Add copied, committed segment batches to the contract, Cesium, Navara and
the fake engine. Keep each segment's colour, width and pick identity while
drawing one primitive or overlay mesh per display. Preserve Cesium's stem
sizes, ground sampling, visible cohorts, update cadence and click targets.
Navara draws the batch after tone mapping through the existing overlay.

Cover disconnected geometry, picks, hidden replacement, empty batches and
cleanup in the contract suite and engine tests. Keep the infrastructure
and cable display assertions on fake and headless Cesium engines, and
read batch counts and pick identities in the browser checks.

No coupling count fell or grew: all eleven metrics are unchanged, including
891 viewer lines, three external Cesium import files and 189 layer Navara
render lines.

Checked in the browser, keyless: the contract suite passes on both engines,
with the new segment picking and pixel checks; the infrastructure and
submarine-cable engine switches, the cable overlay and infrastructure LOD
scripts pass.
This commit is contained in:
German Khokhlov committed 2026-11-07 22:47:24 +04:00
1 parent fd65288101
commit fbadf792d8
19 files changed
+677 -141

No files matched your search

+3
View File
@@ -43,6 +43,9 @@ const NO_MOVERS = Object.freeze({
list: () => [],
});
const NO_LINES = Object.freeze({
createSegments() {
throw new Error("Navara lines need the view's three runtime (lines/)");
},
createLine() {
throw new Error("Navara lines need the view's three runtime (lines/)");
},
+26 -3
View File
@@ -42,6 +42,13 @@ export function createNavaraLines(view, { overlay }) {
line.setPositions(state.flat);
request();
},
setSegments(items) {
state.items = items;
const flat = new Float64Array(items.length * 6);
items.forEach((item, index) => flat.set(item.positions, index * 6));
line.setPositions(flat, items);
request();
},
setColor(rgba) {
line.setColor(rgba);
request();
@@ -68,9 +75,25 @@ export function createNavaraLines(view, { overlay }) {
lines: registry.lines,
/** Shown lines with a pickId: options, drawn positions, matrix. */
pickables: () =>
[...drawn.values()].filter(
(state) => state.shown && state.options.pickId !== undefined,
),
[...drawn.values()]
.filter((state) => state.shown)
.flatMap((state) =>
state.options.segments
? (state.items ?? [])
.filter((item) => item.pickId !== undefined)
.map((item) => ({
options: {
...state.options,
widthPx: item.widthPx,
pickId: item.pickId,
},
flat: item.positions,
modelMatrix: null,
}))
: state.options.pickId !== undefined
? [state]
: [],
),
destroy: () => registry.destroyAll(),
};
}
+36 -7
View File
@@ -30,6 +30,11 @@ const VERTEX_HEAD = /* glsl */ `
attribute vec3 gevEnd;
attribute vec2 gevSide;
attribute float gevS;
#ifdef GEV_SEGMENTS
attribute vec4 gevColor;
attribute float gevWidth;
varying vec4 vGevColor;
#endif
uniform vec2 uGevViewport;
uniform float uGevWidthPx;
varying float vGevS;
@@ -51,7 +56,12 @@ vec2 gevDirection = gevScreenB - gevScreenA;
gevDirection = length(gevDirection) > 1e-6 ? normalize(gevDirection) : vec2(1.0, 0.0);
vec2 gevNormal = vec2(-gevDirection.y, gevDirection.x);
gl_Position = gevSide.x < 0.5 ? gevA : gevB;
#ifdef GEV_SEGMENTS
vGevColor = gevColor;
gl_Position.xy += gevNormal * gevSide.y * gevWidth / uGevViewport * gl_Position.w;
#else
gl_Position.xy += gevNormal * gevSide.y * uGevWidthPx / uGevViewport * gl_Position.w;
#endif
vec4 mvPosition = modelViewMatrix * vec4(gevSide.x < 0.5 ? position : gevEnd, 1.0);
vGevS = gevS;
vGevT = gevSide.y * 0.5 + 0.5;
@@ -61,6 +71,9 @@ vGevDepthFree = 0.0;
const FRAGMENT_HEAD = /* glsl */ `
uniform vec4 uGevColor;
#ifdef GEV_SEGMENTS
varying vec4 vGevColor;
#endif
uniform float uGevAlphaScale;
uniform float uGevPixelRatio;
${OVERLAY_DEPTH_FRAGMENT}
@@ -92,7 +105,11 @@ const FRAGMENT_COLOR = /* glsl */ `
#include <color_fragment>
if (gevOverlayHidden()) discard;
// sRGB colours (as Cesium takes them) into the linear output.
#ifdef GEV_SEGMENTS
diffuseColor = sRGBTransferEOTF(vGevColor);
#else
diffuseColor = sRGBTransferEOTF(uGevColor);
#endif
diffuseColor.a *= uGevAlphaScale;
if (uGevArrow) {
float head = max(fwidth(vGevS) * 10.0 * uGevPixelRatio, 1e-6);
@@ -142,14 +159,16 @@ export function patchLineShader(shader, uniforms) {
* segment 4 vertices (start/end × side) and 2 triangles; `gevS` is the
* fraction of the line's length at the vertex's end.
*/
export function buildRibbon(flat, origin) {
export function buildRibbon(flat, origin, items = null) {
const points = flat.length / 3;
const segments = Math.max(0, points - 1);
const segments = items ? items.length : Math.max(0, points - 1);
const start = new Float32Array(segments * 12);
const end = new Float32Array(segments * 12);
const side = new Float32Array(segments * 8);
const along = new Float32Array(segments * 4);
const index = new Uint32Array(segments * 6);
const colors = items ? new Float32Array(segments * 16) : null;
const widths = items ? new Float32Array(segments * 4) : null;
const cumulative = new Float64Array(points);
for (let p = 1; p < points; p++)
cumulative[p] =
@@ -161,7 +180,7 @@ export function buildRibbon(flat, origin) {
);
const total = cumulative[points - 1] || 1;
for (let s = 0; s < segments; s++) {
const a = s * 3;
const a = s * (items ? 6 : 3);
const b = a + 3;
for (let v = 0; v < 4; v++) {
const vertex = s * 4 + v;
@@ -172,7 +191,11 @@ export function buildRibbon(flat, origin) {
const atEnd = v >= 2 ? 1 : 0;
side[vertex * 2] = atEnd;
side[vertex * 2 + 1] = v % 2 === 0 ? 1 : -1;
along[vertex] = cumulative[s + atEnd] / total;
along[vertex] = items ? atEnd : cumulative[s + atEnd] / total;
if (items) {
colors.set(items[s].color, vertex * 4);
widths[vertex] = items[s].widthPx;
}
}
index.set(
[s * 4, s * 4 + 1, s * 4 + 2, s * 4 + 2, s * 4 + 1, s * 4 + 3],
@@ -184,6 +207,10 @@ export function buildRibbon(flat, origin) {
geometry.setAttribute('gevEnd', new BufferAttribute(end, 3));
geometry.setAttribute('gevSide', new BufferAttribute(side, 2));
geometry.setAttribute('gevS', new BufferAttribute(along, 1));
if (items) {
geometry.setAttribute('gevColor', new BufferAttribute(colors, 4));
geometry.setAttribute('gevWidth', new BufferAttribute(widths, 1));
}
geometry.setIndex(new BufferAttribute(index, 1));
return geometry;
}
@@ -228,9 +255,11 @@ export function createLineMesh(
uGevAlphaScale: { value: alphaScale },
uGevDepthMode: { value: depthMode },
};
if (options.segments) value.defines = { GEV_SEGMENTS: 1 };
value.userData.gevDepthMode = depthMode;
value.onBeforeCompile = (shader) => patchLineShader(shader, uniforms);
value.customProgramCacheKey = () => 'gev-line';
value.customProgramCacheKey = () =>
options.segments ? 'gev-segments' : 'gev-line';
return value;
};
const group = new Group();
@@ -267,10 +296,10 @@ export function createLineMesh(
group,
uniforms: shared,
meshes: () => (behind ? [main, behind] : [main]),
setPositions(flat) {
setPositions(flat, items = null) {
const previous = main.geometry;
origin = flat.length >= 3 ? [flat[0], flat[1], flat[2]] : [0, 0, 0];
const geometry = buildRibbon(flat, origin);
const geometry = buildRibbon(flat, origin, items);
main.geometry = geometry;
if (behind) behind.geometry = geometry;
previous.dispose();
+79
View File
@@ -7,6 +7,7 @@ import {
UniformsUtils,
Vector3,
} from 'three';
import { createNavaraLines } from './index.js';
import { normalizeLineOptions } from '../../lines.js';
import { buildRibbon, createLineMesh, patchLineShader } from './lineMesh.js';
@@ -87,3 +88,81 @@ test('a line places its group at the first point under its model matrix', () =>
assert.equal(line.group.visible, false);
line.dispose();
});
test('segment batches share an overlay mesh without connecting the endpoints', () => {
const groups = [];
let requests = 0,
disposed = 0;
const overlay = {
uniforms: {
tGevSceneDepth: { value: null },
uGevResolution: { value: [0, 0] },
},
add: (group) => groups.push(group),
remove: (group) => groups.splice(groups.indexOf(group), 1),
};
const part = createNavaraLines(
{
forceUpdate() {
requests++;
},
canvas: { clientWidth: 800, clientHeight: 600 },
},
{ overlay },
);
const batch = part.lines.createSegments({
id: 'stems',
widthPx: 2,
color: [1, 0, 0, 1],
});
const items = [
{
positions: new Float64Array([6378137, 0, 0, 6378137, 10, 0]),
pickId: 'first',
},
{
positions: new Float64Array([6378137, 100, 0, 6378137, 110, 0]),
widthPx: 4,
color: [0, 1, 0, 0.5],
pickId: 'second',
},
];
batch.replace(items);
batch.commit();
assert.equal(groups.length, 1);
assert.equal(groups[0].children.length, 1);
const mesh = groups[0].children[0];
const geometry = mesh.geometry;
assert.equal(geometry.index.count, 12, 'two quads, no bridge');
assert.deepEqual(
[...geometry.getAttribute('position').array.slice(12, 15)],
[0, 100, 0],
);
assert.deepEqual(
[...geometry.getAttribute('gevWidth').array],
[2, 2, 2, 2, 4, 4, 4, 4],
);
assert.deepEqual(
[...geometry.getAttribute('gevColor').array.slice(16, 20)],
[0, 1, 0, 0.5],
);
assert.equal(mesh.material.defines.GEV_SEGMENTS, 1);
assert.deepEqual(
part.pickables().map((item) => item.options.pickId),
['first', 'second'],
);
geometry.addEventListener('dispose', () => disposed++);
batch.show(false);
batch.replace(items.slice(1));
batch.commit();
assert.equal(disposed, 1);
assert.equal(groups[0].visible, false);
assert.deepEqual(part.pickables(), []);
batch.show(true);
assert.equal(part.pickables()[0].options.pickId, 'second');
assert.equal(part.pickables()[0].options.id, 'stems');
part.destroy();
assert.deepEqual(groups, []);
assert.deepEqual(part.lines.list(), []);
assert.ok(requests >= 5);
});