From 06516819da1cccdc5f0d8c64f8b48cd622fdf3e8 Mon Sep 17 00:00:00 2001 From: Sameh Khamis Date: Thu, 12 Nov 2026 12:10:27 -0800 Subject: [PATCH] fix(render): translucent icon sets on the GPU blend as Cesium's translucent billboards A GPU icon set whose blend is translucent draws every row in one alpha-blended pass in row order (LEQUAL, depth written), as BlendOption.TRANSLUCENT billboards do, in place of the opaque and translucent passes the set's blend only re-blended: overlapping icons kept the first row's fill and lost the outlines of those over it. The vessels, shipwrecks and FIRMS sets ask for that blend again, as their billboard collections did. --- src/layers/firms/rendering.js | 2 + src/layers/shipwrecks/index.js | 3 ++ src/layers/vessels/rendering.js | 3 ++ src/render/cesium/gpuIcons.js | 1 + src/render/cesium/iconSetPrimitive.js | 54 ++++++++++++++++++++++++--- src/render/cesium/setBlend.js | 4 +- 6 files changed, 60 insertions(+), 7 deletions(-) diff --git a/src/layers/firms/rendering.js b/src/layers/firms/rendering.js index eb85e03ac..55d93dae4 100644 --- a/src/layers/firms/rendering.js +++ b/src/layers/firms/rendering.js @@ -466,6 +466,8 @@ export function createRendering({ if (!layerState._viewer || layerState._icons) return; const set = drawOf(layerState._viewer).icons({ id: `firms:${++iconSets}:icons`, + // Blended in one pass, in order, as the detections overlap. + blend: 'translucent', horizonCull: true, show: layerState._enabled && !(layerState._handoffHeat >= 1), drawOrder: spriteDrawOrder?.('firms') ?? 0, diff --git a/src/layers/shipwrecks/index.js b/src/layers/shipwrecks/index.js index 866e5cfd6..51b37ecfe 100644 --- a/src/layers/shipwrecks/index.js +++ b/src/layers/shipwrecks/index.js @@ -380,6 +380,9 @@ export function createShipwrecksLayer({ // sprite layers (`SPRITE_LAYER_ORDER`). _markers = drawOf(viewer).icons({ id: 'shipwrecks:markers', + // Blended in one pass, in order: the wrecks added last (the + // largest) on top. + blend: 'translucent', drawOrder: spriteDrawOrder(SHIPWRECKS_LAYER_ID), show: false, }); diff --git a/src/layers/vessels/rendering.js b/src/layers/vessels/rendering.js index 75bc2b673..6e114985f 100644 --- a/src/layers/vessels/rendering.js +++ b/src/layers/vessels/rendering.js @@ -145,6 +145,9 @@ export function createRendering({ if (!viewer || state.icons) return; const set = drawOf(viewer).icons({ id: `ais-vessels:${++instances}`, + // Blended in one pass, in order, each chevron keeping its outline + // over the ones under it. + blend: 'translucent', horizonCull: true, drawOrder: spriteDrawOrder?.('ais') ?? 0, }); diff --git a/src/render/cesium/gpuIcons.js b/src/render/cesium/gpuIcons.js index 01cc358c6..53289ff9b 100644 --- a/src/render/cesium/gpuIcons.js +++ b/src/render/cesium/gpuIcons.js @@ -307,6 +307,7 @@ export function createGpuIcons({ clockTime(frameState, new Cesium.JulianDate()), requestRender: () => requestRender(`icons:${spec.id}`), cull: () => spec.horizonCull === true, + translucent: () => spec.blend === 'translucent', // Picked as the draw port's other items are: `{set, id, meta}` as // the item now is, hidden row by row for drill picks. pickObject: (id, _set, primitive) => ({ diff --git a/src/render/cesium/iconSetPrimitive.js b/src/render/cesium/iconSetPrimitive.js index c754dbb2e..4d62243af 100644 --- a/src/render/cesium/iconSetPrimitive.js +++ b/src/render/cesium/iconSetPrimitive.js @@ -285,7 +285,7 @@ void main() { if (color.a < 0.005) discard; #ifdef OPAQUE if (color.a < 0.995) discard; -#else +#elif defined(TRANSLUCENT) if (color.a >= 0.995) discard; #endif out_FragColor = color; @@ -320,20 +320,32 @@ const EARTH_BOUNDS = new Cesium.BoundingSphere( /** * @param {{ set: object, renderTime: (frameState: object) => Cesium.JulianDate, * requestRender?: () => void, cull?: () => boolean, + * translucent?: () => boolean, * pickObject?: (id: string, set: object, primitive: object) => object }} options * `renderTime` gives the instant to draw at (the layer's delayed clock), from * the frame being drawn. `requestRender` asks for a frame when the atlas packs * a newly loaded image, for a layer that does not render continuously. * `cull` says whether rows past the ellipsoid's horizon are hidden (by - * default always). `pickObject` builds a row's pick object around the + * default always). `translucent` says whether every row is drawn in one + * alpha-blended pass, in row order, the last on top (Cesium's + * `BlendOption.TRANSLUCENT`), in place of an opaque pass and a translucent + * one (by default never). `pickObject` builds a row's pick object around the * `primitive` drill picks hide it by (by default `{ id, iconSet, meta, * primitive }`). */ export class IconSetPrimitive { - constructor({ set, renderTime, requestRender, cull, pickObject }) { + constructor({ + set, + renderTime, + requestRender, + cull, + translucent, + pickObject, + }) { this.set = set; this.renderTime = renderTime; this._cull = cull ?? (() => true); + this._translucent = translucent ?? (() => false); this._pickObject = pickObject ?? null; this._pickPass = 0; this._stopAtlasListener = requestRender @@ -418,12 +430,23 @@ export class IconSetPrimitive { `[iconSetPrimitive] This context addresses ${this._slotCount} icon images; the atlas holds ${iconAtlas.size}, and rows drawing the rest draw nothing`, ); } - for (const command of this._commands) { + const commands = this._translucent() + ? [this._singleCommand] + : this._commands; + for (const command of commands) { command.instanceCount = set.count; frameState.commandList.push(command); } } + /** + * Whether this host draws `blend` itself, so the set's blend leaves its + * commands as they are (`setBlend.js`). + */ + drawsBlend(blend) { + return blend === 'translucent' && this._translucent(); + } + _syncAtlas(context) { if (iconAtlas.version === this._atlasVersion || !iconAtlas.canvas) return; this._atlasVersion = iconAtlas.version; @@ -546,7 +569,7 @@ export class IconSetPrimitive { this._generation = set.generation; for (const rows of Object.values(set.dirty)) rows.markAll(); if (this._commands) - for (const command of this._commands) + for (const command of [...this._commands, this._singleCommand]) command.vertexArray = this._vertexArray; } @@ -726,7 +749,12 @@ export class IconSetPrimitive { sources: [VERTEX_SHADER], }), fragmentShaderSource: new Cesium.ShaderSource({ - defines: pass === 'opaque' ? ['OPAQUE'] : ['TRANSLUCENT'], + defines: + pass === 'opaque' + ? ['OPAQUE'] + : pass === 'translucent' + ? ['TRANSLUCENT'] + : [], sources: [FRAGMENT_SHADER], }), attributeLocations: ATTRIBUTE_LOCATIONS, @@ -734,6 +762,7 @@ export class IconSetPrimitive { this._programs = { opaque: programFor('opaque'), translucent: programFor('translucent'), + single: programFor('single'), }; const depthTest = { enabled: true, func: Cesium.DepthFunction.LESS }; const command = (pass, program, renderState) => @@ -766,6 +795,18 @@ export class IconSetPrimitive { }), ), ]; + // Every row in one pass, blended in row order: equal depths pass, and + // depth is written in place of sorting, as Cesium's translucent + // billboards do. + this._singleCommand = command( + Cesium.Pass.TRANSLUCENT, + this._programs.single, + Cesium.RenderState.fromCache({ + depthTest: { enabled: true, func: Cesium.DepthFunction.LEQUAL }, + depthMask: true, + blending: Cesium.BlendingState.ALPHA_BLEND, + }), + ); } _destroyBuffers() { @@ -785,6 +826,7 @@ export class IconSetPrimitive { this._destroyBuffers(); this._programs?.opaque.destroy(); this._programs?.translucent.destroy(); + this._programs?.single.destroy(); this._texture?.destroy(); // The set outlives its renderer: release only the pick ids this one made. const set = this.set; diff --git a/src/render/cesium/setBlend.js b/src/render/cesium/setBlend.js index 131649620..0df57cbc0 100644 --- a/src/render/cesium/setBlend.js +++ b/src/render/cesium/setBlend.js @@ -129,7 +129,9 @@ export function blendHost(host, blendOf) { const start = list.length; const result = update.call(this, frameState); if (!frameState.passes?.render) return result; - const blend = blendOf(); + // A host that draws this blend itself keeps its commands as made. + const asked = blendOf(); + const blend = this.drawsBlend?.(asked) ? undefined : asked; if (blend === undefined && !turnedAny) return result; if (blend !== undefined) turnedAny = true; blendCommands(list, start, blend);