diff --git a/src/render/cesium/draw.js b/src/render/cesium/draw.js index bcaf14e2f..5f3da9385 100644 --- a/src/render/cesium/draw.js +++ b/src/render/cesium/draw.js @@ -935,6 +935,100 @@ export function createCesiumDraw({ return look; } + /** + * What a billboard draws of an icon item but its position, image, `meta` + * and `lodGroup`, as plain values: the course rotation follows the + * position, so it is written with it. + */ + function iconStyleOf(record, item) { + const color = item.color; + const curve = (c) => + c ? [c.near, c.nearValue, c.far, c.farValue] : undefined; + return { + width: item.size[0], + height: item.size[1], + scale: item.scale, + color: + Array.isArray(color) || ArrayBuffer.isView(color) ? [...color] : color, + alpha: item.alpha, + focus: item.focus, + anchor: item.anchor, + disableDepthTest: item.disableDepthTest, + heightMode: item.height, + clampTo: record.spec.clampTo, + show: item.show, + displayRange: item.displayRange ? [...item.displayRange] : undefined, + scaleByDistance: curve(item.scaleByDistance), + fadeByDistance: curve(item.fadeByDistance), + rotationMode: item.rotationMode, + rotation: item.rotation, + course: item.course, + }; + } + + /** Whether `item` draws as `style` (`iconStyleOf`) says, but its position. */ + function sameIconStyle(style, record, item) { + const sameList = (a, b) => { + if (!a || !b) return a === b; + if (a.length !== b.length) return false; + for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false; + return true; + }; + const sameCurve = (a, c) => + !a || !c + ? !a && !c + : a[0] === c.near && + a[1] === c.nearValue && + a[2] === c.far && + a[3] === c.farValue; + const color = item.color; + return ( + style.width === item.size[0] && + style.height === item.size[1] && + style.scale === item.scale && + (Array.isArray(color) || ArrayBuffer.isView(color) + ? Array.isArray(style.color) && sameList(style.color, color) + : style.color === color) && + style.alpha === item.alpha && + style.focus === item.focus && + style.anchor === item.anchor && + style.disableDepthTest === item.disableDepthTest && + style.heightMode === item.height && + style.clampTo === record.spec.clampTo && + style.show === item.show && + sameList(style.displayRange, item.displayRange) && + sameCurve(style.scaleByDistance, item.scaleByDistance) && + sameCurve(style.fadeByDistance, item.fadeByDistance) && + style.rotationMode === item.rotationMode && + style.rotation === item.rotation && + style.course === item.course + ); + } + + const movedEnu = new Cesium.Matrix4(); + const movedAxis = new Cesium.Cartesian3(); + /** + * Move a drawn icon whose look is unchanged (`sameIconStyle`): its + * position, the course rotation that follows it, and its pick record. + */ + function moveIcon(record, entry, item) { + const { billboard, placed } = entry; + const position = toCartesianInto(item.pos, placed.position); + billboard.position = position; + if (item.rotationMode === 'course') { + Cesium.Transforms.eastNorthUpToFixedFrame(position, undefined, movedEnu); + // The billboard copies the axis it is given. + billboard.alignedAxis = Cesium.Matrix4.multiplyByPointAsVector( + movedEnu, + Cesium.Cartesian3.UNIT_Y, + movedAxis, + ); + } + billboard.id.meta = item.meta; + billboard.id.lodGroup = item.lodGroup; + billboard.show = shownNow(record, placed); + } + /** Give a billboard its image: a generated one under its key. */ function setIconImage(record, billboard, image) { if (isKeyed(image)) billboard.setImage(image.key, image.canvas); @@ -957,6 +1051,8 @@ export function createCesiumDraw({ placed, image: imageKeyOf(image), canvas: image?.canvas, + // What it is drawn as, for `patchIcon`'s quick path. + style: iconStyleOf(record, item), }); } @@ -968,6 +1064,17 @@ export function createCesiumDraw({ */ function patchIcon(record, entry, item, image) { const { billboard, placed } = entry; + // Drawn as it was but for where it is: only that is written, so a set + // whose icons move every frame costs little more than the moves. + if ( + entry.style && + imageKeyOf(image) === entry.image && + sameIconStyle(entry.style, record, item) + ) { + moveIcon(record, entry, item); + return; + } + entry.style = iconStyleOf(record, item); const position = toCartesian(item.pos); const look = iconLook(record, item, position); // Sized first: a new SVG image rasterises at the billboard's size. @@ -986,6 +1093,42 @@ export function createCesiumDraw({ billboard.show = shownNow(record, placed); } + /** + * `syncIcons`' quick case: every item is drawn, every touched one keeps + * the image it is drawn with, and nothing drawn has gone, so each touched + * one is changed where it is and nothing is added, removed or reordered. + * False, changing nothing, otherwise. + * @param {object} record + * @param {readonly string[]} touched + */ + function patchDrawnIcons(record, touched) { + const drawn = record.billboards; + // Nothing to remove: every item drawn, and nothing drawn but items. + if ( + !Array.isArray(touched) || + touched.length === 0 || + drawn.size !== record.items.size + ) + return false; + for (const id of touched) { + const entry = drawn.get(id); + const item = record.items.get(id); + if (!entry || !item) return false; + const image = iconImage(record, item.image); + if ( + image === undefined || + imageKeyOf(image) !== entry.image || + (isKeyed(image) && image.canvas !== entry.canvas) + ) + return false; + } + for (const id of touched) { + const item = record.items.get(id); + patchIcon(record, drawn.get(id), item, iconImage(record, item.image)); + } + return true; + } + /** * Bring an icon set's billboards in line with its items without a * rebuild: those of items gone (or whose image is gone) are removed, the @@ -1015,6 +1158,7 @@ export function createCesiumDraw({ host.isDestroyed() ) return false; + if (!reordered && patchDrawnIcons(record, touched)) return ICONS_KEPT; const images = new Map(); for (const id of touched) { const item = record.items.get(id); diff --git a/src/render/cesium/draw.test.mjs b/src/render/cesium/draw.test.mjs index 3eef17d77..dc575d4e9 100644 --- a/src/render/cesium/draw.test.mjs +++ b/src/render/cesium/draw.test.mjs @@ -2823,6 +2823,91 @@ test('icon sets change their billboards in place: restyled, moved, re-imaged, ad assert.equal(scene.added.length, 0); }); +test('icons that only move are moved on the billboards they have, and still restyle', () => { + const scene = fakeScene(); + const part = createCesiumDraw({ + scene, + report: (error) => assert.fail(error), + }); + const set = part.port.icons({ id: 'icons:moving' }); + const icon = (lon, extra = {}) => ({ + id: 'a', + pos: Cesium.Cartesian3.fromDegrees(lon, 10, 20), + image: 'a.png', + size: [20, 20], + color: [1, 0, 0, 1], + rotationMode: 'screen', + rotation: 0.25, + disableDepthTest: Infinity, + meta: { lon }, + ...extra, + }); + set.set([icon(0), { ...icon(5), id: 'b' }]); + const [host] = scene.added; + const a = host.get(0); + const version = part.version(); + // Only moved: its look is not written again. + let restyles = 0; + const width = Object.getOwnPropertyDescriptor( + Object.getPrototypeOf(a), + 'width', + ); + Object.defineProperty(a, 'width', { + get: width.get, + set(value) { + restyles += 1; + width.set.call(this, value); + }, + configurable: true, + }); + for (const lon of [1, 2, 3]) set.upsert([icon(lon)]); + assert.equal(restyles, 0, 'moved, not restyled'); + assert.equal(scene.added[0], host, 'the same collection'); + assert.equal(host.get(0), a, 'the same billboard'); + const at = Cesium.Cartographic.fromCartesian(a.position); + assert.ok(Math.abs(Cesium.Math.toDegrees(at.longitude) - 3) < 1e-9); + assert.ok(Math.abs(at.height - 20) < 1e-6); + assert.equal(a.rotation, 0.25); + assert.ok(Cesium.Color.equals(a.color, Cesium.Color.RED)); + assert.deepEqual(a.id.meta, { lon: 3 }, 'picks read the latest meta'); + assert.equal(part.version(), version, 'nothing added or removed'); + + // A look that changes after moves is drawn, then moves go on from it. + set.upsert([icon(3, { color: [0, 1, 0, 1], rotation: 1, size: [30, 30] })]); + assert.equal(restyles, 1, 'restyled once'); + assert.ok(Cesium.Color.equals(a.color, Cesium.Color.LIME)); + assert.equal(a.rotation, 1); + assert.equal(a.width, 30); + set.upsert([icon(4, { color: [0, 1, 0, 1], rotation: 1, size: [30, 30] })]); + assert.ok(Cesium.Color.equals(a.color, Cesium.Color.LIME)); + assert.equal(a.width, 30); + const moved = Cesium.Cartographic.fromCartesian(a.position); + assert.ok(Math.abs(Cesium.Math.toDegrees(moved.longitude) - 4) < 1e-9); + + // A course-turned icon's axis follows where it moves. + const course = (lon) => + icon(lon, { rotationMode: 'course', course: 90, rotation: undefined }); + set.upsert([course(4)]); + set.upsert([course(60)]); + const north = Cesium.Matrix4.multiplyByPointAsVector( + Cesium.Transforms.eastNorthUpToFixedFrame( + Cesium.Cartesian3.fromDegrees(60, 10, 20), + ), + Cesium.Cartesian3.UNIT_Y, + new Cesium.Cartesian3(), + ); + assert.ok(Cesium.Cartesian3.equalsEpsilon(a.alignedAxis, north, 1e-12)); + assert.equal(a.rotation, -Cesium.Math.toRadians(90)); + // Hidden in place, while it moves. + set.upsert([{ ...course(61), show: false }]); + assert.equal(a.show, false); + set.upsert([{ ...course(62), show: false }]); + assert.equal(a.show, false); + set.upsert([course(63)]); + assert.equal(a.show, true); + set.dispose(); +}); + test('an icon drawn from an atlas slot takes a re-registered slot in place', () => { const scene = fakeScene(); const part = createCesiumDraw({