跪拜 Guibai
← All articles
Cesium · WebGL · Vue.js

The Other Cesium Mask: Darkening a District Interior While Keeping the Map Clear

By Tony费 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Internal masking is the natural complement to hollow-highlight masking for geospatial dashboards. Knowing that Cesium's `holes` API cannot produce this effect saves a developer from a dead-end approach and points straight to the simpler polygon-fill solution.

Summary

The previous approach used a global rectangle with holes to produce a dark-outside, bright-inside highlight. This technique flips the effect: the base map remains clear everywhere except inside the target district, which gets a dark overlay and a blue boundary line.

GeoJSON coordinates are flattened recursively and converted to Cartesian3 positions, then drawn as a single polygon entity with `clampToGround: true`. A separate polyline entity traces the same boundary for a sharp outline. The result is a clean internal mask with no dependency on Cesium's `holes` mechanism, which cannot invert its logic.

A complete Vue 3 + Cesium component is provided, including Amap tile layers, a vector/imagery toggle, and a camera fly-to preset over Shanghai's Songjiang District.

Takeaways
Flatten multi-dimensional GeoJSON coordinate arrays recursively before passing them to `Cesium.Cartesian3.fromDegreesArray`.
Draw the mask as a single polygon entity with `fill: true`, `clampToGround: true`, and a semi-transparent `Color.fromBytes(0, 0, 0, 200)` material.
Add a separate polyline entity with the same positions, a wider stroke, and a solid color to keep the district boundary visible.
Cesium's `PolygonHierarchy.holes` always cuts out the holes array from the outer polygon; there is no built-in parameter to invert the mask.
The component cleans up the Viewer instance in `onUnmounted` to prevent WebGL memory leaks.
Amap tile URLs used in the demo are for learning only; production deployments require a licensed key from the map provider.
Conclusions

The two-mask pattern — external darkening via holes and internal darkening via direct polygon fill — covers nearly all administrative-district highlighting needs in Cesium without third-party plugins.

Flattening GeoJSON coordinates is a recurring friction point in Cesium that many tutorials skip; the recursive reducer shown here handles arbitrary nesting depths.

Using a separate polyline for the boundary rather than a polygon outline property gives finer control over stroke width and color, which matters when the mask fill is dark.

Concepts & terms
PolygonHierarchy.holes
A Cesium polygon property where the outer coordinates define the filled area and the holes array defines cutout regions. It cannot be inverted to fill only the holes.
clampToGround
A Cesium entity option that forces a polygon or polyline to conform to the 3D terrain surface instead of floating at a fixed altitude.
Cartesian3.fromDegreesArray
A Cesium utility that converts a flat array of longitude-latitude pairs into an array of Cartesian3 world coordinates for entity positions.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗