diff --git a/docs/RENDERER-CONTRACT.md b/docs/RENDERER-CONTRACT.md index 04058271b..36b9e3594 100644 --- a/docs/RENDERER-CONTRACT.md +++ b/docs/RENDERER-CONTRACT.md @@ -149,7 +149,7 @@ target standing up). The Cesium port draws clamped shapes of every kind. A clamped outline is a draped line (a ground polyline a ring, under the shape's pick id; an outlined clamped shape is rebuilt on any change; a pulsing one pulses through its line's -material). What +material, as an image fill pulses through its image material's colour). What a ground primitive cannot drape stands on the surface unclamped, the surface read when the set is built: an extruded shape from the lowest surface height under it up to its `extrudeHeight`, a wall with its foot on the surface under diff --git a/src/render/cesium/draw.js b/src/render/cesium/draw.js index 4de3bbfb2..c1123fccb 100644 --- a/src/render/cesium/draw.js +++ b/src/render/cesium/draw.js @@ -278,12 +278,6 @@ function pulseOf(item) { throw new Error( 'the Cesium renderer pulses between finite alphas, over a positive period', ); - const plain = (value) => - value == null || typeof value === 'string' || Array.isArray(value); - if (!plain(item.fill) || !plain(item.outline)) - throw new Error( - 'the Cesium renderer pulses colour fills and outlines only', - ); return pulse; } @@ -4583,6 +4577,11 @@ export function createCesiumDraw({ !part.fill.isDestroyed() ) colourOn(part.fill, part.id, tint(look.fill, scratchColour)); + // An image fill pulses through its material's colour, white at rest. + if (part.image) + part.image.material.uniforms.color = Cesium.Color.WHITE.withAlpha( + alpha ?? 1, + ); if (look.outline) { if (part.outline?.ready && !part.outline.isDestroyed()) colourOn(part.outline, part.id, tint(look.outline, scratchColour)); @@ -4630,7 +4629,16 @@ export function createCesiumDraw({ for (const part of parts?.values() ?? []) { if (!part.pulse || !part.show) continue; const ground = part.ground; - if (ground && !ground.isDestroyed()) { + const imageFill = + typeof part.look.fill === 'object' && + !Array.isArray(part.look.fill); + if (ground && !ground.isDestroyed() && imageFill) { + // An image fill, a ground primitive of its own, pulses through + // its material's colour. + pulsing = true; + ground.appearance.material.uniforms.color = + Cesium.Color.WHITE.withAlpha(pulseAlpha(part.pulse, now)); + } else if (ground && !ground.isDestroyed()) { // One still building asks for frames, to pulse once it is ready. pulsing = true; if (ground.ready) diff --git a/src/render/cesium/draw.test.mjs b/src/render/cesium/draw.test.mjs index 430f0995e..30bb01670 100644 --- a/src/render/cesium/draw.test.mjs +++ b/src/render/cesium/draw.test.mjs @@ -1161,22 +1161,12 @@ test('a pulse the Cesium renderer cannot run is refused before anything changes' assert.throws(() => set.set([disc(radius)]), /pulses alpha only, forever/); assert.equal(scene.added[0], host, 'still drawn as it was'); assert.doesNotThrow(() => set.upsert([{ ...disc(undefined), id: 'c' }])); - // Nor numbers it cannot draw, nor a fill that is not a colour. + // Nor numbers it cannot draw. assert.throws( () => set.upsert([disc({ property: 'alpha', from: 0, to: 1, periodMs: 0 })]), /positive period/, ); - assert.throws( - () => - set.upsert([ - { - ...disc({ property: 'alpha', from: 0, to: 1, periodMs: 1000 }), - fill: { image: 'x.png' }, - }, - ]), - /colour fills and outlines only/, - ); set.dispose(); }); @@ -8135,3 +8125,47 @@ test('a draped line in the sky’s frame drapes where the sky is, again as its c Date.now = realNow; } }); + +test('image fills pulse their alpha through their material, clamped or not', () => + withImageClasses(() => { + const { scene, draw, frame } = drawOver(); + const set = draw.shapes({ id: 'pulsing-images' }); + const animate = { property: 'alpha', from: 0.3, to: 0.3, periodMs: 1000 }; + const rect = (west) => ({ + kind: 'rectangle', + rect: { west, south: 0, east: west + 1, north: 1 }, + }); + set.set([ + { + id: 'flat', + geometry: rect(0), + fill: { image: 'pad.png' }, + height: 'absolute', + animate, + }, + { + id: 'draped', + geometry: rect(4), + fill: { image: 'pad.png' }, + height: 'clampGround', + animate, + }, + ]); + frame(); + const materials = []; + const walk = (primitive) => { + if (primitive instanceof Cesium.PrimitiveCollection) + for (let i = 0; i < primitive.length; i++) walk(primitive.get(i)); + else if (primitive?.appearance?.material?.type === 'Image') + materials.push(primitive.appearance.material); + }; + scene.added.forEach(walk); + assert.equal(materials.length, 2, 'both drawn'); + for (const material of materials) + assert.equal( + Math.round(material.uniforms.color.alpha * 100) / 100, + 0.3, + 'pulsed', + ); + set.dispose(); + }));