Turn draped rectangles by an optional rotation, and drape image fills in the Cesium port

A rectangle shape gains an optional rotation: radians counter-clockwise,
turning it about its centre in longitude and latitude, as a KML ground
overlay turns; an image fill turns with it. A renderer that predates it
draws the rectangle unturned. The Cesium draw port passes it as the
rectangle's rotation and texture rotation, and now drapes image fills on
clamped shapes, each on a ground primitive of its own that owns its
material, where it refused them before. Hidden image fills are not built
until they show. The stub picks draped rectangles, turned and across the
antimeridian, and a conformance check picks a quarter-turned overlay on
both renderers. The KML importer still draws its overlays itself.
This commit is contained in:
Sameh Khamis committed 2026-11-07 21:49:34 -08:00
1 parent 7ad65b2e87
commit 6286bcfb07
9 files changed
+331 -14

No files matched your search

+78 -12
View File
@@ -1854,6 +1854,9 @@ export function createCesiumDraw({
),
height: g.height ?? 0,
extrudedHeight,
// An image fill turns with the rectangle, as a KML overlay's does.
rotation: g.rotation ?? 0,
stRotation: g.rotation ?? 0,
}),
};
case 'box': {
@@ -1902,6 +1905,8 @@ export function createCesiumDraw({
g.rect.east,
g.rect.north,
),
rotation: g.rotation ?? 0,
stRotation: g.rotation ?? 0,
});
default:
throw new Error(`the Cesium renderer cannot clamp ${g.kind} shapes`);
@@ -1926,13 +1931,24 @@ export function createCesiumDraw({
parts.set(item.id, { look: lookOf(item), id: null, ground: null });
continue;
}
if (typeof item.fill === 'object' && !Array.isArray(item.fill))
const imageFill =
typeof item.fill === 'object' && !Array.isArray(item.fill)
? item.fill
: null;
if (imageFill && !imageFill.image)
throw new Error('the Cesium renderer fills shapes with colours only');
if (item.extrudeHeight !== undefined)
throw new Error('the Cesium renderer cannot clamp extruded shapes');
// Hidden ones are built too, hidden, so showing them is a change of
// their show, not a rebuild of every fill; a hidden one it cannot
// build stays out, as before, and showing it rebuilds.
// Draping a material needs depth textures (WebGL 2 always has them).
if (imageFill && scene.context?.depthTexture === false)
throw new Error('the Cesium renderer cannot drape images here');
// Refused before anything is made, so nothing is left half built.
const image = imageFill ? toImage(imageFill.image) : null;
// Hidden colour fills are built too, hidden, so showing them is a
// change of their show, not a rebuild of every fill; a hidden one it
// cannot build stays out, as before, and showing it rebuilds. Hidden
// image fills are not passed in (`hiddenFillBuilds`): their textures
// load when they show.
let geometry;
try {
geometry = groundGeometry(item);
@@ -1946,20 +1962,41 @@ export function createCesiumDraw({
ground: null,
};
parts.set(item.id, part);
const show = new Cesium.ShowGeometryInstanceAttribute(
item.show !== false,
);
const instance = new Cesium.GeometryInstance({
geometry,
attributes: {
color: Cesium.ColorGeometryInstanceAttribute.fromColor(
toColor(item.fill),
),
show: new Cesium.ShowGeometryInstanceAttribute(item.show !== false),
},
attributes: imageFill
? { show }
: {
color: Cesium.ColorGeometryInstanceAttribute.fromColor(
toColor(item.fill),
),
show,
},
id: part.id,
});
const clampTo = item.clampTo ?? record.spec.clampTo ?? 'both';
if (imageFill) {
// An image fill is a ground primitive of its own, with its material.
// (Cesium turns its texture wrongly across the antimeridian: its
// rotation points are taken against a centre wrapped to ±180°.)
batches.push({
clampTo,
instances: [instance],
parts: [part],
image,
imageFill,
});
continue;
}
const rectangle = instance.geometry.rectangle;
let batch = batches.find(
(b) => b.clampTo === clampTo && !b.rectangles.collides(rectangle),
(b) =>
b.rectangles &&
b.clampTo === clampTo &&
!b.rectangles.collides(rectangle),
);
if (!batch) {
batch = {
@@ -1979,11 +2016,40 @@ export function createCesiumDraw({
? Cesium.GroundPrimitive
: UnpickableGroundPrimitive;
const grounds = batches.map((batch) => {
const material = batch.imageFill
? Cesium.Material.fromType('Image', {
image: batch.image,
repeat: new Cesium.Cartesian2(
...(batch.imageFill.repeat ?? [1, 1]),
),
})
: null;
const ground = new Ground({
geometryInstances: batch.instances,
classificationType: classificationOf(batch.clampTo),
allowPicking: pickable,
...(material
? {
appearance: new Cesium.MaterialAppearance({
material,
materialSupport:
Cesium.MaterialAppearance.MaterialSupport.TEXTURED,
translucent: true,
flat: true,
}),
}
: {}),
});
// Its material holds the texture, which the primitive leaves alone:
// it goes with the primitive, which may outlive the host that built it
// while a rebuild's ground primitives get ready.
if (material) {
const destroyGround = ground.destroy.bind(ground);
ground.destroy = () => {
if (!material.isDestroyed()) material.destroy();
return destroyGround();
};
}
for (const part of batch.parts) part.ground = ground;
return ground;
});
@@ -2096,7 +2162,7 @@ export function createCesiumDraw({
};
for (const item of record.items.values()) {
// Clamped ones are built hidden as well (`groundFills`), when they
// are a fill it can draw: others hidden stay out, as before.
// are a colour fill it can draw: others hidden stay out, as before.
if (
item.height === 'clampGround' &&
(item.show !== false || hiddenFillBuilds(item))
+95
View File
@@ -2590,6 +2590,101 @@ test('an unclamped shape filled with an image draws a textured primitive and fre
);
}));
test('a draped rectangle turns, its image fill with it, on a ground primitive that owns its material', () =>
withImageClasses(() => {
const scene = fakeScene();
const draw = createCesiumDraw({
scene,
report: (error) => assert.fail(error),
}).port;
const set = draw.shapes({ id: 'overlays' });
const overlay = (extra = {}) => ({
id: 'overlay',
geometry: {
kind: 'rectangle',
rect: { west: 2, south: 1, east: 2.1, north: 1.1 },
rotation: 0.4,
},
fill: { image: 'overlay.png' },
height: 'clampGround',
...extra,
});
const tinted = {
id: 'tinted',
geometry: {
kind: 'rectangle',
rect: { west: 2, south: 1, east: 2.1, north: 1.1 },
rotation: -0.2,
},
fill: [1, 0, 0, 0.5],
height: 'clampGround',
};
set.set([overlay(), tinted]);
const grounds = () => {
const host = scene.added[0];
const out = [];
for (let i = 0; i < host.length; i++)
if (host.get(i) instanceof Cesium.GroundPrimitive)
out.push(host.get(i));
return out;
};
const [image, colour] = grounds();
assert.equal(
grounds().length,
2,
'the image on a ground primitive of its own',
);
const { appearance, geometryInstances } = image;
assert.ok(appearance instanceof Cesium.MaterialAppearance);
assert.equal(appearance.material.type, 'Image');
assert.equal(appearance.material.uniforms.image, 'overlay.png');
const [instance] = geometryInstances;
assert.equal(instance.id.id, 'overlay');
assert.equal(instance.geometry._rotation, 0.4);
assert.equal(instance.geometry._stRotation, 0.4, 'the image turns with it');
assert.equal(instance.attributes.color, undefined);
assert.equal(colour.geometryInstances[0].geometry._rotation, -0.2);
// Hiding it is a change of its show, not a rebuild.
const host = scene.added[0];
set.upsert([overlay({ show: false })]);
assert.equal(scene.added[0], host, 'not rebuilt');
const material = appearance.material;
scene.primitives.remove = (primitive) => {
const at = scene.added.indexOf(primitive);
if (at >= 0) scene.added.splice(at, 1);
primitive.destroy();
return at >= 0;
};
set.dispose();
assert.equal(material.isDestroyed(), true, 'its material goes with it');
// Not rotated, it is as before.
const plain = draw.shapes({ id: 'plain' });
plain.set([
overlay({
geometry: {
kind: 'rectangle',
rect: { west: 2, south: 1, east: 2.1, north: 1.1 },
},
}),
]);
const [flat] = grounds();
assert.equal(flat.geometryInstances[0].geometry._rotation, 0);
plain.dispose();
// An image it cannot take is refused before anything is built.
const made = scene.added.length;
assert.throws(
() =>
draw
.shapes({ id: 'atlas' })
.set([
overlay(),
overlay({ id: 'b', fill: { image: { atlas: 'a', slot: 's' } } }),
]),
/URL, SVG and canvas images only/,
);
assert.equal(scene.added.length, made, 'nothing drawn');
}));
test('scene data packs draw their geometry and media marker through the Cesium draw port on a viewer of its own', async (t) => {
const names = [
'HTMLCanvasElement',
+1
View File
@@ -59,6 +59,7 @@ export const CESIUM_CHECKS = new Set([
'draw.points.update',
'draw.lodGroup',
'draw.sets',
'draw.shapes.drapedRotation',
'effects.crossfade',
'effects.defineStyle',
'effects.styles',