Draw vectorBase: a MapLibre style's vector tiles painted as imagery

src/render/vectorBase/ is a Cesium-free painter: loadVectorStyle loads
a style and its TileJSON sources and compiles its layers (skipping, with a
reason, what it does not draw), and paintVectorTile paints a Web Mercator
tile on a canvas: background, fill, line and circle layers with filters in
both syntaxes, legacy functions and a listed subset of expressions, and
symbol text labels placed clear of each other inside the tile. Sources are
decoded with pbf and @mapbox/vector-tile, already dependencies. The Cesium
port loads the style before changing the base, draws it as Web Mercator
imagery under the base's other imagery, credits its sources, and claims
vectorBase; the stub loads the style. globe.base.vector serves a style and
tile from data URLs and reads the drawn fill.
This commit is contained in:
Sameh Khamis committed 2026-11-11 18:24:49 -08:00
1 parent d9ad7da9d5
commit 5fae3cf485
17 files changed
+2032 -15

No files matched your search

+20 -1
View File
@@ -20,6 +20,8 @@ import { provideUnboundGlobe } from '../rendererOf.js';
import { isBaseTileset, markBaseTileset } from '../baseTilesets.js';
import { listenerList, uncaught } from './common.js';
import { createCesiumImagery } from './imagery.js';
import { attributionText } from './vectorBase.js';
import { loadVectorStyle } from '../vectorBase/index.js';
import { createTerrainRetryPolicy } from '../../maps/terrainRetry.js';
// The base marks live outside the adapter, so code that is not Cesium's can
@@ -134,6 +136,8 @@ export function createCesiumGlobe(
let photorealGen = 0;
let terrainGen = 0;
let baseCredit = null;
// The vector base's sources' credits.
const vectorCredits = [];
/** Every 3D tileset the scene holds. */
function tilesets() {
@@ -234,10 +238,24 @@ export function createCesiumGlobe(
}
async function setBase(base) {
if (base.vector) throw new TypeError('This renderer draws no vector base');
if (base.vector && !can('vectorBase'))
throw new TypeError('This renderer draws no vector base');
// The style loads before the base changes: one that fails changes
// nothing.
const style = base.vector
? await loadVectorStyle(base.vector.styleUrl)
: null;
for (const handle of imagery.list()) handle.remove();
baseCredit?.();
baseCredit = base.credit && credits ? credits.addStatic(base.credit) : null;
for (const off of vectorCredits.splice(0)) off();
if (style) {
// Under any imagery the base also gives.
imagery.add({ kind: 'vectorStyle', style }, undefined, { base: true });
if (credits)
for (const text of style.attribution.map(attributionText))
if (text) vectorCredits.push(credits.addStatic({ text }));
}
const loads = [];
if (base.photoreal !== undefined) loads.push(setPhotoreal(base.photoreal));
if (base.terrain !== undefined) loads.push(setTerrain(base.terrain));
@@ -569,6 +587,7 @@ export function createCesiumGlobe(
imagery.dispose();
baseCredit?.();
baseCredit = null;
for (const off of vectorCredits.splice(0)) off();
},
};
}
+1
View File
@@ -90,6 +90,7 @@ export const CESIUM_CHECKS = new Set([
'frame.renderOnce',
'frame.suspend',
'frame.time',
'globe.base.vector',
'globe.imagery',
'globe.imagery.frames',
'globe.imagery.video',
+4
View File
@@ -20,6 +20,7 @@
* imagery (`imageryFrames.js`).
*/
import * as Cesium from 'cesium';
import { vectorBaseProvider } from './vectorBase.js';
import { creditMarkup } from './credits.js';
import { listenerList } from './common.js';
import {
@@ -146,6 +147,9 @@ export function providerFor(spec, { worldDataToken } = {}) {
{ accessToken },
);
}
// The app's own: the vector base's tiles, painted from its style.
case 'vectorStyle':
return vectorBaseProvider(spec.style);
case 'canvas':
if (typeof spec.drawTile !== 'function')
throw new TypeError('Canvas imagery needs drawTile');
+3 -1
View File
@@ -50,6 +50,8 @@ const GLOBE_CAPABILITIES = [
'imageryFrames',
'surfaceIdentity',
'surfaceShading',
// A MapLibre style's vector tiles, painted as imagery (`vectorBase.js`).
'vectorBase',
'globeVisibility',
];
@@ -83,7 +85,7 @@ export function cesiumCapabilities(scene, only) {
? ['keepEarthFramed']
: []),
...GPU_ICON_CAPABILITIES,
// Icon sets on billboards cluster on screen (`draw.js`).
// Icon sets cluster on screen, on billboards and the GPU (`draw.js`).
'cluster',
'styles',
'bloom',
+1
View File
@@ -30,6 +30,7 @@ test('capabilities follow what the scene supports', () => {
'imageryFrames',
'surfaceIdentity',
'surfaceShading',
'vectorBase',
];
const globe = { show: true };
const draw = ['models', 'modelAtmosphere', 'quads', 'meshes', 'fields'];
+49
View File
@@ -0,0 +1,49 @@
/**
* @module render/cesium/vectorBase
* @description `vectorBase` on Cesium: an imagery provider over the vector
* base painter (`src/render/vectorBase/`), each Web Mercator tile painted
* from the style at 256 px.
*/
import * as Cesium from 'cesium';
import { paintVectorTile } from '../vectorBase/index.js';
/** The tile size vector base tiles are painted at, pixels. */
export const VECTOR_TILE_PX = 256;
/** The deepest level painted; deeper, Cesium draws this level's tiles. */
export const VECTOR_MAX_LEVEL = 20;
/**
* A Cesium imagery provider painting a loaded style's tiles.
* @param {Awaited<ReturnType<typeof import('../vectorBase/index.js').loadVectorStyle>>} style
*/
export function vectorBaseProvider(style) {
const tilingScheme = new Cesium.WebMercatorTilingScheme();
return {
tilingScheme,
tileWidth: VECTOR_TILE_PX,
tileHeight: VECTOR_TILE_PX,
minimumLevel: 0,
maximumLevel: VECTOR_MAX_LEVEL,
rectangle: tilingScheme.rectangle,
credit: undefined,
errorEvent: new Cesium.Event(),
hasAlphaChannel: false,
tileDiscardPolicy: undefined,
proxy: undefined,
getTileCredits: () => undefined,
pickFeatures: () => undefined,
requestImage: (x, y, level) =>
paintVectorTile(style, level, x, y, { size: VECTOR_TILE_PX }),
};
}
/** A TileJSON attribution (markup) as plain text. */
export function attributionText(html) {
return String(html)
.replace(/<[^>]*>/g, ' ')
.replace(/&copy;/gi, '©')
.replace(/&amp;/gi, '&')
.replace(/\s+/g, ' ')
.trim();
}
+34
View File
@@ -0,0 +1,34 @@
import assert from 'node:assert/strict';
import * as Cesium from 'cesium';
import test from 'node:test';
import {
attributionText,
VECTOR_TILE_PX,
vectorBaseProvider,
} from './vectorBase.js';
import { cesiumCapabilities } from './ports.js';
test('the vector base is Web Mercator imagery painted from its style', () => {
const provider = vectorBaseProvider({
layers: [],
sources: {},
tiles: new Map(),
});
assert.ok(provider.tilingScheme instanceof Cesium.WebMercatorTilingScheme);
assert.equal(provider.tileWidth, VECTOR_TILE_PX);
assert.equal(provider.hasAlphaChannel, false);
assert.equal(typeof provider.requestImage, 'function');
});
test('a source’s attribution is credited as plain text', () => {
assert.equal(
attributionText(
'<a href="https://x.test">&copy; OpenMapTiles</a> <a href="y">Data &amp; more</a>',
),
'© OpenMapTiles Data & more',
);
});
test('a Cesium globe claims vectorBase', () => {
assert.ok(cesiumCapabilities({}).includes('vectorBase'));
});