By passing the elevationRange option to the ColorLayer and/or Map constructor, you can limit the visibility of this layer/map within this range. A possible use case is to limit the display of a satellite layer above the sea level, then limit the display of a bathymetry dataset below the sea level.
index.js
import colormap from "colormap";import XYZ from "ol/source/XYZ.js";import { Color, Vector3 } from "three";import { MapControls } from "three/examples/jsm/controls/MapControls.js";import Extent from "@giro3d/giro3d/core/geographic/Extent.js";import Instance from "@giro3d/giro3d/core/Instance.js";import ColorLayer from "@giro3d/giro3d/core/layer/ColorLayer.js";import TiledImageSource from "@giro3d/giro3d/sources/TiledImageSource.js";import ElevationLayer from "@giro3d/giro3d/core/layer/ElevationLayer.js";import Map from "@giro3d/giro3d/entities/Map.js";import Inspector from "@giro3d/giro3d/gui/Inspector.js";import ColorMap from "@giro3d/giro3d/core/layer/ColorMap.js";import MapboxTerrainFormat from "@giro3d/giro3d/formats/MapboxTerrainFormat.js";function bindSlider(id, onChange) { const element = document.getElementById(id); if (!(element instanceof HTMLInputElement)) { throw new Error( "invalid binding element: expected HTMLInputElement, got: " + element.constructor.name, ); } element.oninput = function oninput() { onChange(element.valueAsNumber); }; const setValue = (v, min, max, step) => { if (min != null && max != null) { element.min = min.toString(); element.max = max.toString(); if (step != null) { element.step = step; } } element.valueAsNumber = v; onChange(element.valueAsNumber); }; const initialValue = element.valueAsNumber; return [setValue, initialValue, element];}function bindToggle(id, onChange) { const element = document.getElementById(id); if (!(element instanceof HTMLInputElement)) { throw new Error( "invalid binding element: expected HTMLButtonElement, got: " + element.constructor.name, ); } element.oninput = function oninput() { onChange(element.checked); }; const callback = (v) => { element.checked = v; onChange(element.checked); }; return [callback, element.checked, element];}function makeColorRamp( preset, discrete = false, invert = false, mirror = false,) { let nshades = discrete ? 10 : 256; const values = colormap({ colormap: preset, nshades }); const colors = values.map((v) => new Color(v)); if (invert) { colors.reverse(); } if (mirror) { const mirrored = [...colors, ...colors.reverse()]; return mirrored; } return colors;}const center = { x: -13601505, y: 5812315 };const extent = Extent.fromCenterAndSize("EPSG:3857", center, 20000, 20000);const instance = new Instance({ target: "view", crs: extent.crs, backgroundColor: null,});const map = new Map({ extent, elevationRange: { min: 500, max: 3000 },});instance.add(map);const colorRamp = makeColorRamp("viridis");const key = "pk.eyJ1IjoidG11Z3VldCIsImEiOiJjbGJ4dTNkOW0wYWx4M25ybWZ5YnpicHV6In0.KhDJ7W5N3d1z3ArrsDjX_A";// Adds a XYZ elevation layer with MapBox terrain RGB tilesetconst elevationLayer = new ElevationLayer({ name: "xyz_elevation", extent, source: new TiledImageSource({ format: new MapboxTerrainFormat(), source: new XYZ({ url: `https://api.mapbox.com/v4/mapbox.terrain-rgb/{z}/{x}/{y}.pngraw?access_token=${key}`, projection: extent.crs, crossOrigin: "anonymous", }), }), colorMap: new ColorMap(colorRamp, 700, 2500),});map.addLayer(elevationLayer);// Adds a XYZ color layer with MapBox satellite tilesetconst colorLayer = new ColorLayer({ name: "xyz_color", extent, source: new TiledImageSource({ source: new XYZ({ url: `https://api.mapbox.com/v4/mapbox.satellite/{z}/{x}/{y}.webp?access_token=${key}`, projection: extent.crs, crossOrigin: "anonymous", }), }), elevationRange: { min: 500, max: 3000 },});map.addLayer(colorLayer);// Sets the camera positioninstance.view.camera.position.set(-13615016, 5835706, 14797);const controls = new MapControls(instance.view.camera, instance.domElement);controls.target = new Vector3(-13603869, 5814829, 0);controls.saveState();controls.enableDamping = true;controls.dampingFactor = 0.2;controls.maxPolarAngle = Math.PI / 2.3;instance.view.setControls(controls);Inspector.attach("inspector", instance);let colorLayerRange = colorLayer.elevationRange;bindToggle("toggle-colorlayer-range", (enabled) => { if (enabled) { colorLayer.elevationRange = colorLayerRange; } else { colorLayer.elevationRange = null; } document.getElementById("layerMin").disabled = !enabled; document.getElementById("layerMax").disabled = !enabled; instance.notifyChange(map);});bindSlider("mapMin", (v) => { map.elevationRange.min = v; instance.notifyChange(map);});bindSlider("mapMax", (v) => { map.elevationRange.max = v; instance.notifyChange(map);});bindSlider("layerMin", (v) => { colorLayer.elevationRange = { min: v, max: colorLayer.elevationRange.max }; colorLayerRange = colorLayer.elevationRange; instance.notifyChange(map);});bindSlider("layerMax", (v) => { colorLayer.elevationRange = { min: colorLayer.elevationRange.min, max: v }; colorLayerRange = colorLayer.elevationRange; instance.notifyChange(map);});