diff --git a/src/data/iconAtlas.js b/src/data/iconAtlas.js index 379f60d42..d8e18044b 100644 --- a/src/data/iconAtlas.js +++ b/src/data/iconAtlas.js @@ -9,6 +9,29 @@ * each slot's texture rectangle from `rects`. */ +/** + * Drawn width in device pixels (the drawing buffer's) past which an icon + * row with no switch of its own draws its near image: the aircraft layers' + * switch. + */ +export const NEAR_IMAGE_DEVICE_PX = 76; + +/** + * `NEAR_IMAGE_DEVICE_PX` in CSS pixels, for an icon item's + * `imageLod.switchPx`, which the draw contract measures in CSS pixels: the + * device switch divided by the drawing buffer's pixels per CSS pixel. That + * is the scene's pixel ratio, which the quality port's frame signals report + * as `pixelRatio`: on the default renderer its `pixelRatio` setting, or 1 + * while that is 0 (browser recommended). `devicePixelRatio` does not enter + * it. A layer drawing through the port passes the ratio its frames report + * and rewrites its rows' switch when that changes. + * @param {number} [pixelRatio] Drawing-buffer pixels per CSS pixel (1). + */ +export function nearImageSwitchPx(pixelRatio = 1) { + const ratio = Number.isFinite(pixelRatio) && pixelRatio > 0 ? pixelRatio : 1; + return NEAR_IMAGE_DEVICE_PX / ratio; +} + /** Slots a renderer can address (one vec4 uniform each). */ export const MAX_ICON_SLOTS = 64; diff --git a/src/data/iconSetPrimitive.js b/src/data/iconSetPrimitive.js index f64302ad4..a80eb0320 100644 --- a/src/data/iconSetPrimitive.js +++ b/src/data/iconSetPrimitive.js @@ -17,15 +17,16 @@ import * as Cesium from 'cesium'; import { ICON_SEGMENT_GLSL, SEGMENT_STRIDE } from './iconSegment.js'; import { ICON_CULL_HEIGHT_M, ICON_CULL_LIFT, STYLE_STRIDE } from './iconSet.js'; -import { iconAtlas, MAX_ICON_SLOTS } from './iconAtlas.js'; +import { + iconAtlas, + MAX_ICON_SLOTS, + NEAR_IMAGE_DEVICE_PX, +} from './iconAtlas.js'; import { cameraLimbDistanceM, getAircraftRecessionParams, } from './aircraftRecession.js'; -/** On-screen device pixels above which a row draws its near (large) image. */ -const NEAR_IMAGE_PX = 76; - const ATTRIBUTE_LOCATIONS = Object.freeze({ corner: 0, seg0: 1, @@ -139,7 +140,7 @@ void main() { float glyphPx = style0.x * scale * distanceScale * czm_pixelRatio; bool nearImage = style3.x > 0.0 ? glyphPx / czm_pixelRatio > style3.x - : glyphPx > ${NEAR_IMAGE_PX.toFixed(1)}; + : glyphPx > ${NEAR_IMAGE_DEVICE_PX.toFixed(1)}; float slot = nearImage ? style1.y : style1.x; if (slot < 0.0) return; // no image set vec4 rect = u_slots[int(slot)]; diff --git a/src/render/cesium/gpuIcons.test.mjs b/src/render/cesium/gpuIcons.test.mjs index cb6bcf534..166253e15 100644 --- a/src/render/cesium/gpuIcons.test.mjs +++ b/src/render/cesium/gpuIcons.test.mjs @@ -749,3 +749,35 @@ test('an SVG takes the atlas at its own size, other images at the item’s', asy 'an svg ref at its own', ); }); + +test('an image switch in CSS pixels stands for the aircraft layers’ 76 device pixels', async () => { + const { NEAR_IMAGE_DEVICE_PX, nearImageSwitchPx } = + await import('../../data/iconAtlas.js'); + assert.equal(NEAR_IMAGE_DEVICE_PX, 76); + assert.equal(nearImageSwitchPx(2), 38); + const display = globalThis.devicePixelRatio; + globalThis.devicePixelRatio = 2; + try { + assert.equal( + nearImageSwitchPx(), + 76, + 'the scene’s ratio, not the display’s', + ); + } finally { + globalThis.devicePixelRatio = display; + } + assert.equal(nearImageSwitchPx(0), 76, 'no ratio: 1'); + assert.equal(nearImageSwitchPx(NaN), 76); + const { part, draw } = drawOver(); + const set = draw.icons({ id: 'gpu:switch-px' }); + set.set([ + icon('a', { + motion: { kind: 'hold' }, + imageLod: { near: 'near.png', switchPx: nearImageSwitchPx(2) }, + }), + ]); + const { set: rowsOf } = rows(part, 'gpu:switch-px'); + // Stored as given: the shader (not run here) switches past it in CSS + // pixels, the drawn width over the scene's pixel ratio. + assert.equal(rowsOf.imageSwitch(rowsOf.row('a')), 38); +});