3D relief like OsmAnd: the 3D button raises the terrain on its own DEM source (exaggeration 1.5), no longer needing Terrain shading or waiting for isStyleLoaded (false while tiles load, so it never came on); color-relief uses ["elevation"] (MapLibre 6), which had broken the whole Terrain shading overlay
This commit is contained in:
1 parent
dbe742321a
commit
a3e5fefaf1
2 files changed
+45
-38
No files matched your search
+3
-4
@@ -229,10 +229,9 @@ class ZoomLevelControl {
|
||||
|
||||
// Tilts the camera between a flat top-down view and a fixed 3D-perspective
|
||||
// pitch — bearing stays untouched, so this is purely "look at it from an
|
||||
// angle", not a rotate/compass control. Independent of the "Terrain
|
||||
// shading" overlay toggle (overlays.js) — that adds a raster-dem hillshade/
|
||||
// color-relief layer, this just moves the camera; combining both gives a
|
||||
// real sense of relief but neither depends on the other.
|
||||
// angle", not a rotate/compass control. Tilted, the ground is raised into
|
||||
// real 3D relief (OverlaysControl.syncTerrain, overlays.js), with or
|
||||
// without the "Terrain shading" overlay.
|
||||
const PITCH_3D = 60;
|
||||
|
||||
class PitchToggleControl {
|
||||
|
||||
+42
-34
@@ -18,6 +18,13 @@ const WIKI_ICON_ID = "wiki-icon";
|
||||
const WIKI_BADGE_COLOR = "#795548";
|
||||
|
||||
const HILLSHADE_ID = "hillshade-dem";
|
||||
// Raised 3D relief (map.setTerrain) has a DEM source of its own, so the 3D
|
||||
// button shows it with or without the Terrain shading overlay (MapLibre
|
||||
// also advises against sharing one source between hillshade and terrain).
|
||||
const TERRAIN_ID = "terrain-dem";
|
||||
// Real heights look flat from a car-navigation tilt; 3× (before) turned
|
||||
// the Tatras into needles.
|
||||
const TERRAIN_EXAGGERATION = 1.5;
|
||||
const COLOR_RELIEF_ID = "elevation-tint";
|
||||
// MapLibre's own default light ({anchor:"viewport", intensity:0.5,
|
||||
// position:[1.15,210,30]}) sits the sun almost overhead (polar 30°) — fine
|
||||
@@ -37,7 +44,7 @@ const DEFAULT_LIGHT = { anchor: "viewport", color: "#fff", intensity: 0.5, posit
|
||||
const ELEVATION_COLOR_RAMP = [
|
||||
"interpolate",
|
||||
["linear"],
|
||||
["raster-value"],
|
||||
["elevation"],
|
||||
0, "#c3e8ab",
|
||||
400, "#dcd08f",
|
||||
900, "#c9a66b",
|
||||
@@ -87,34 +94,31 @@ class HillshadeControl {
|
||||
|
||||
_toggle(active) {
|
||||
this._active = active;
|
||||
// Clear first: setTerrain may still be pointing at HILLSHADE_ID from
|
||||
// before, and removing that source out from under it is worth avoiding
|
||||
// even for the instant before syncTerrain below re-evaluates it.
|
||||
if (this._map.getTerrain()) this._map.setTerrain(null);
|
||||
if (active) this._addLayer();
|
||||
else this._removeLayer();
|
||||
this.syncTerrain(this._map.getPitch() > 0);
|
||||
}
|
||||
|
||||
// Real terrain displacement (map.setTerrain), not just the flat
|
||||
// hillshade/color-relief paint above — called both when this checkbox
|
||||
// changes and when the 3D pitch toggle changes (map.js's
|
||||
// PitchToggleControl, via OverlaysControl.syncTerrain below), since
|
||||
// showing raised terrain needs both "tilted enough to see it" and "the
|
||||
// DEM source this checkbox controls is actually loaded".
|
||||
// hillshade/color-relief paint above — whenever the map is tilted (the
|
||||
// 3D button, map.js's PitchToggleControl, via OverlaysControl.syncTerrain
|
||||
// below), like OsmAnd's 3D relief.
|
||||
syncTerrain(pitched) {
|
||||
// setLight/setTerrain throw if the style isn't loaded yet — called
|
||||
// synchronously from map.js's setOverlaysControl during page init,
|
||||
// before the first style.load; that listener re-invokes this once it
|
||||
// actually is loaded, so bailing here just skips the premature call.
|
||||
if (!this._map.isStyleLoaded()) return;
|
||||
const terrainOn = pitched && this._active && this._map.getSource(HILLSHADE_ID);
|
||||
if (terrainOn) {
|
||||
this._map.setTerrain({ source: HILLSHADE_ID, exaggeration: 3 });
|
||||
} else if (this._map.getTerrain()) {
|
||||
this._map.setTerrain(null);
|
||||
// Not isStyleLoaded(): that's false while any tile is still loading —
|
||||
// always the case right after tilting — so the terrain never came on.
|
||||
// Before the first style.load these throw; that listener calls again.
|
||||
try {
|
||||
if (pitched) {
|
||||
if (!this._map.getSource(TERRAIN_ID)) this._map.addSource(TERRAIN_ID, this._demSource());
|
||||
this._map.setTerrain({ source: TERRAIN_ID, exaggeration: TERRAIN_EXAGGERATION });
|
||||
} else if (this._map.getTerrain()) {
|
||||
this._map.setTerrain(null);
|
||||
}
|
||||
this._map.setLight(pitched ? TERRAIN_LIGHT : DEFAULT_LIGHT);
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
this._map.setLight(terrainOn ? TERRAIN_LIGHT : DEFAULT_LIGHT);
|
||||
const terrainOn = pitched;
|
||||
// The raster hillshade layer fakes relief by shading a flat texture —
|
||||
// once real 3D geometry is doing that job (with its own lit shading
|
||||
// for free), the raster version just looks like a blurry, low-res
|
||||
@@ -135,7 +139,23 @@ class HillshadeControl {
|
||||
// (docs/development.adoc), so this credits them regardless of how it
|
||||
// got there — their own site lists the full per-region source
|
||||
// provenance (CC-BY-4.0 and similar), not practical to reproduce here.
|
||||
const source = this._offlineAvailable
|
||||
this._map.addSource(HILLSHADE_ID, this._demSource());
|
||||
// Elevation tint under, hillshade on top: the tint sets the base color
|
||||
// per elevation band, hillshade multiplies slope-shading over it — the
|
||||
// combined "characteristic relief look" the TODO asked for, not two
|
||||
// independent layers.
|
||||
this._map.addLayer({
|
||||
id: COLOR_RELIEF_ID,
|
||||
type: "color-relief",
|
||||
source: HILLSHADE_ID,
|
||||
paint: { "color-relief-color": ELEVATION_COLOR_RAMP, "color-relief-opacity": 0.7 },
|
||||
});
|
||||
this._map.addLayer({ id: HILLSHADE_ID, type: "hillshade", source: HILLSHADE_ID, paint: { "hillshade-exaggeration": 0.4 } });
|
||||
}
|
||||
|
||||
// The offline terrain.pmtiles when there is one, else the public tiles.
|
||||
_demSource() {
|
||||
return this._offlineAvailable
|
||||
? {
|
||||
type: "raster-dem",
|
||||
url: `pmtiles://${window.location.origin}/tiles/terrain.pmtiles`,
|
||||
@@ -150,18 +170,6 @@ class HillshadeControl {
|
||||
maxzoom: 15,
|
||||
attribution: "Terrain tiles: Mapzen, SRTM, ArcticDEM",
|
||||
};
|
||||
this._map.addSource(HILLSHADE_ID, source);
|
||||
// Elevation tint under, hillshade on top: the tint sets the base color
|
||||
// per elevation band, hillshade multiplies slope-shading over it — the
|
||||
// combined "characteristic relief look" the TODO asked for, not two
|
||||
// independent layers.
|
||||
this._map.addLayer({
|
||||
id: COLOR_RELIEF_ID,
|
||||
type: "color-relief",
|
||||
source: HILLSHADE_ID,
|
||||
paint: { "color-relief-color": ELEVATION_COLOR_RAMP, "color-relief-opacity": 0.7 },
|
||||
});
|
||||
this._map.addLayer({ id: HILLSHADE_ID, type: "hillshade", source: HILLSHADE_ID, paint: { "hillshade-exaggeration": 0.4 } });
|
||||
}
|
||||
|
||||
_removeLayer() {
|
||||
|
||||
Reference in new issue
Block a user