Draw a one pixel wide raster shell image

The shell's material reads an image width of 1 as Cesium's 1x1 default
texture, nothing arrived yet, so a one pixel wide image never drew while
'drawn' was still told. Such an image is drawn as a copy two pixels wide,
which samples the same.
This commit is contained in:
Sameh Khamis committed 2026-11-10 09:07:51 -08:00
1 parent 149f8aad4d
commit 46d44eed89
2 files changed
+81 -6

No files matched your search

+33 -6
View File
@@ -151,9 +151,12 @@ export function createShellSurface({
primitives.add(primitive); primitives.add(primitive);
orderShells(primitives, primitive, height); orderShells(primitives, primitive, height);
let lift = 0; let lift = 0;
// What it was given, and what the material draws (`widened`).
let imageSource = null;
let image = null; let image = null;
let frames = 0; let frames = 0;
// The detail image, its own upload count and a window waiting for it to draw. // The detail image, its own upload count and a window waiting for it to draw.
let detailSource = null;
let detail = null; let detail = null;
let detailFrames = 0; let detailFrames = 0;
let pendingWindow = null; let pendingWindow = null;
@@ -229,9 +232,10 @@ export function createShellSurface({
return { return {
setImage(next) { setImage(next) {
if (destroyed || next === image) return; if (destroyed || next === imageSource) return;
image = next; imageSource = next;
material.uniforms.image = next; image = widened(next);
material.uniforms.image = image;
frames = UPLOAD_FRAMES; frames = UPLOAD_FRAMES;
wake(); wake();
}, },
@@ -259,6 +263,7 @@ export function createShellSurface({
if (destroyed) return; if (destroyed) return;
if (!next) { if (!next) {
if (detail === null) return; if (detail === null) return;
detailSource = null;
detail = null; detail = null;
detailFrames = 0; detailFrames = 0;
pendingWindow = null; pendingWindow = null;
@@ -267,9 +272,10 @@ export function createShellSurface({
ask(); ask();
return; return;
} }
if (next !== detail) { if (next !== detailSource) {
detail = next; detailSource = next;
material.uniforms.detail = next; detail = widened(next);
material.uniforms.detail = detail;
detailFrames = UPLOAD_FRAMES; detailFrames = UPLOAD_FRAMES;
} }
const applied = appliedWindow(); const applied = appliedWindow();
@@ -319,7 +325,9 @@ export function createShellSurface({
if (!primitive.isDestroyed()) primitive.destroy(); if (!primitive.isDestroyed()) primitive.destroy();
// Primitive.destroy leaves the material and its texture alive. // Primitive.destroy leaves the material and its texture alive.
if (!material.isDestroyed()) material.destroy(); if (!material.isDestroyed()) material.destroy();
imageSource = null;
image = null; image = null;
detailSource = null;
detail = null; detail = null;
pendingWindow = null; pendingWindow = null;
ask(); ask();
@@ -339,6 +347,25 @@ function placeIn(outer, inner) {
}; };
} }
/**
* `source`, or a copy two pixels wide when it is one. The material reads a
* width of 1 as Cesium's 1×1 default (nothing has arrived yet), so a one
* pixel wide image would never draw. Doubling the column samples the same.
*/
function widened(source) {
if (!(source?.width < 2)) return source;
const canvas =
typeof document !== 'undefined'
? document.createElement('canvas')
: new OffscreenCanvas(2, source.height);
canvas.width = 2;
canvas.height = source.height;
const context = canvas.getContext('2d');
context.imageSmoothingEnabled = false;
context.drawImage(source, 0, 0, 2, source.height);
return canvas;
}
/** Whether `ref` is drawn as it is, with no decode first. */ /** Whether `ref` is drawn as it is, with no decode first. */
const immediate = (ref) => Boolean(ref?.canvas); const immediate = (ref) => Boolean(ref?.canvas);
+48
View File
@@ -186,6 +186,54 @@ test('a surface applies a new detail window only once its image has drawn; the s
assert.equal(scene.postRender.size, 0); assert.equal(scene.postRender.size, 0);
}); });
test('a surface draws a one pixel wide image as a copy two pixels wide', (t) => {
// The material reads a width of 1 as the 1×1 default, nothing arrived.
const drawn = [];
class FakeCanvas {
constructor(width, height) {
this.width = width;
this.height = height;
}
getContext() {
return { drawImage: (...args) => drawn.push(args) };
}
}
const had = Object.getOwnPropertyDescriptor(globalThis, 'OffscreenCanvas');
globalThis.OffscreenCanvas = FakeCanvas;
t.after(() => {
if (had) Object.defineProperty(globalThis, 'OffscreenCanvas', had);
else delete globalThis.OffscreenCanvas;
});
const cesium = createShellCesium();
const scene = createShellScene();
const surface = createShellSurface({
scene,
cesium,
rectangle: Cesium.Rectangle.fromDegrees(-100, 28, -95, 32),
height: 10_000,
});
const [primitive] = scene.primitives.items;
const { uniforms } = primitive.appearance.material;
const pixel = { width: 1, height: 1 };
surface.setImage(pixel);
assert.ok(uniforms.image instanceof FakeCanvas);
assert.deepEqual([uniforms.image.width, uniforms.image.height], [2, 1]);
assert.deepEqual(drawn, [[pixel, 0, 0, 2, 1]]);
const copy = uniforms.image;
surface.setImage(pixel);
assert.equal(uniforms.image, copy, 'the same image is not copied again');
renderShells(cesium, scene, 3);
assert.equal(surface.isDrawn(), true, 'drawn once the copy has uploaded');
const wide = { width: 4, height: 1 };
surface.setImage(wide);
assert.equal(uniforms.image, wide, 'a wider image is drawn as it is');
surface.setDetail(
{ width: 1, height: 8 },
{ west: 0, south: 0, east: 1, north: 1 },
);
assert.deepEqual([uniforms.detail.width, uniforms.detail.height], [2, 8]);
});
test("a surface rises by a uniform scale about the Earth's centre", () => { test("a surface rises by a uniform scale about the Earth's centre", () => {
const scene = { const scene = {
primitives: new Cesium.PrimitiveCollection(), primitives: new Cesium.PrimitiveCollection(),