Give sensor picks plain screen positions and the canvas from their caller

sensorPickPosition returns {x, y} rather than a Cesium Cartesian2 and is
handed the canvas it sizes by, so it no longer reaches Cesium or the
viewer and leaves the Cesium allow-list. The style records stay keyed by
the scene object.
This commit is contained in:
Sameh Khamis committed 2026-11-08 14:49:27 -08:00
1 parent 75ad7baf13
commit afd8dba67b
4 files changed
+22 -14

No files matched your search

+7 -3
View File
@@ -126,7 +126,7 @@ const dispatchers = new WeakMap();
* method is memoized, so subscribers share the passes. * method is memoized, so subscribers share the passes.
* @typedef {object} ClickEvent * @typedef {object} ClickEvent
* @property {{x: number, y: number}} screenPosition Where the user clicked. * @property {{x: number, y: number}} screenPosition Where the user clicked.
* @property {Cesium.Cartesian2} position Where that lands in the pick buffer * @property {{x: number, y: number}} position Where that lands in the pick buffer
* (corrected for sensor styles; see sensorPick.js). * (corrected for sensor styles; see sensorPick.js).
* @property {ClickGesture} gesture * @property {ClickGesture} gesture
* @property {object|undefined} pick `scene.pick` at `position`. * @property {object|undefined} pick `scene.pick` at `position`.
@@ -270,7 +270,11 @@ function createDispatcher(viewer, { createHandler, now, document } = {}) {
// Where the pointer lands in the pick buffer, as a click's // Where the pointer lands in the pick buffer, as a click's
// `position` is (corrected for sensor styles); made on first read. // `position` is (corrected for sensor styles); made on first read.
get pickPosition() { get pickPosition() {
pickPosition ??= sensorPickPosition(viewer.scene, endPosition); pickPosition ??= sensorPickPosition(
viewer.scene,
endPosition,
viewer.scene.canvas,
);
return pickPosition; return pickPosition;
}, },
}); });
@@ -370,7 +374,7 @@ function createDispatcher(viewer, { createHandler, now, document } = {}) {
*/ */
function createClickEvent(viewer, click, gesture) { function createClickEvent(viewer, click, gesture) {
const scene = viewer.scene; const scene = viewer.scene;
const position = sensorPickPosition(scene, click.position); const position = sensorPickPosition(scene, click.position, scene.canvas);
const memo = new Map(); const memo = new Map();
const once = (key, compute) => { const once = (key, compute) => {
if (!memo.has(key)) memo.set(key, compute()); if (!memo.has(key)) memo.set(key, compute());
+7 -3
View File
@@ -418,11 +418,11 @@ test('sensor styles move the pick to where the picture came from', () => {
}; };
registerStyleStages(scene, { surveillance: stage }); registerStyleStages(scene, { surveillance: stage });
const click = { x: 300, y: 50 }; const click = { x: 300, y: 50 };
const mapped = sensorPickPosition(scene, click); const mapped = sensorPickPosition(scene, click, scene.canvas);
assert.notDeepEqual([mapped.x, mapped.y], [300, 50]); assert.notDeepEqual([mapped.x, mapped.y], [300, 50]);
assert.deepEqual(click, { x: 300, y: 50 }, 'the click is left alone'); assert.deepEqual(click, { x: 300, y: 50 }, 'the click is left alone');
stage.enabled = false; stage.enabled = false;
const plain = sensorPickPosition(scene, click); const plain = sensorPickPosition(scene, click, scene.canvas);
assert.deepEqual([plain.x, plain.y], [300, 50]); assert.deepEqual([plain.x, plain.y], [300, 50]);
}); });
@@ -440,7 +440,11 @@ test('a move carries its pick-buffer position, sensor-corrected as a click is',
const seen = []; const seen = [];
h.input.onPointer('move', (event) => seen.push(event)); h.input.onPointer('move', (event) => seen.push(event));
h.handler.fire(TYPES.MOUSE_MOVE, { endPosition: { x: 300, y: 50 } }); h.handler.fire(TYPES.MOUSE_MOVE, { endPosition: { x: 300, y: 50 } });
const expected = sensorPickPosition(h.viewer.scene, { x: 300, y: 50 }); const expected = sensorPickPosition(
h.viewer.scene,
{ x: 300, y: 50 },
h.viewer.scene.canvas,
);
assert.deepEqual( assert.deepEqual(
[seen[0].pickPosition.x, seen[0].pickPosition.y], [seen[0].pickPosition.x, seen[0].pickPosition.y],
[expected.x, expected.y], [expected.x, expected.y],
+8 -7
View File
@@ -6,27 +6,28 @@
* different spot of the scene: map it back through the shader's barrel * different spot of the scene: map it back through the shader's barrel
* distortion and quantization first. * distortion and quantization first.
*/ */
import * as Cesium from 'cesium';
import { styleStagesOf } from '../styles/styleStages.js'; import { styleStagesOf } from '../styles/styleStages.js';
const SENSOR_STYLES = ['thermal', 'surveillance']; const SENSOR_STYLES = ['thermal', 'surveillance'];
/** /**
* The pick-buffer position for a screen click. * The pick-buffer position for a screen click.
* @param {Cesium.Scene} scene * @param {object} scene The scene object the styles are recorded on.
* @param {{x: number, y: number}} position CSS pixels on the canvas. * @param {{x: number, y: number}} position CSS pixels on the canvas.
* @returns {Cesium.Cartesian2} A new position; the click's own is left alone. * @param {{width: number, height: number, clientWidth: number,
* clientHeight: number}} canvas The canvas the scene draws in.
* @returns {{x: number, y: number}} A new position; the click's own is left
* alone.
*/ */
export function sensorPickPosition(scene, position) { export function sensorPickPosition(scene, position, canvas) {
const out = new Cesium.Cartesian2(position.x, position.y); const out = { x: position.x, y: position.y };
const stages = styleStagesOf(scene); const stages = styleStagesOf(scene);
if (!stages) return out; if (!stages) return out;
for (const style of SENSOR_STYLES) { for (const style of SENSOR_STYLES) {
const stage = stages[style]; const stage = stages[style];
if (!stage?.enabled) continue; if (!stage?.enabled) continue;
const intensity = stage.uniforms.intensity; const intensity = stage.uniforms.intensity;
const canvas = scene.canvas, const w = canvas.clientWidth,
w = canvas.clientWidth,
h = canvas.clientHeight; h = canvas.clientHeight;
let u = out.x / w, let u = out.x / w,
v = 1 - out.y / h; v = 1 - out.y / h;
-1
View File
@@ -8,7 +8,6 @@
"src/cyberSonarScene.js", "src/cyberSonarScene.js",
"src/data/geoImportPins.js", "src/data/geoImportPins.js",
"src/input/dispatcher.js", "src/input/dispatcher.js",
"src/input/sensorPick.js",
"src/layers/installations/rendering.js", "src/layers/installations/rendering.js",
"src/layers/recentImagery/rendering.js", "src/layers/recentImagery/rendering.js",
"src/layers/satellites/rendering.js", "src/layers/satellites/rendering.js",