Illustrates how transparency in maps work.

100% © IGN, © iTowns

Transparency in maps (and any other 3D object) is a tricky problem. Due to limitations in how 3D renderers work, it is not generally possible to correctly display overlapping transparent objects. For example, set the opacity of at least 2 maps to less than 100%, rotate the camera around, and observe various rendering issues, such as missing map tiles.

index.js
import colormap from 'colormap';
import { Color } from 'three';
import { MapControls } from 'three/examples/jsm/controls/MapControls.js';
import { Fill, Stroke, Style } from 'ol/style.js';
import TileWMS from 'ol/source/TileWMS.js';
import GeoJSON from 'ol/format/GeoJSON.js';

import BilFormat from '@giro3d/giro3d/formats/BilFormat.js';
import Extent from '@giro3d/giro3d/core/geographic/Extent.js';
import Instance from '@giro3d/giro3d/core/Instance.js';
import TiledImageSource from '@giro3d/giro3d/sources/TiledImageSource.js';
import ColorLayer from '@giro3d/giro3d/core/layer/ColorLayer.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 VectorSource from '@giro3d/giro3d/sources/VectorSource.js';
import ColorMap from '@giro3d/giro3d/core/layer/ColorMap.js';



// # Planar (EPSG:3946) viewer

// Defines projection that we will use (taken from https://epsg.io/3946, Proj4js section)
Instance.registerCRS('EPSG:3946',
    '+proj=lcc +lat_1=45.25 +lat_2=46.75 +lat_0=46 +lon_0=3 +x_0=1700000 +y_0=5200000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs');

// Defines geographic extent: CRS, min/max X, min/max Y
const extent = new Extent(
    'EPSG:3946',
    1837816.94334, 1847692.32501,
    5170036.4587, 5178412.82698,
);

// `viewerDiv` will contain giro3d' rendering area (the canvas element)
const viewerDiv = document.getElementById('viewerDiv');

// Creates the giro3d instance
const instance = new Instance(viewerDiv, {
    crs: 'EPSG:3946',
    renderer: {
        clearColor: false,
    },
});

const terrainMap = new Map('terrain', { extent, doubleSided: true, hillshading: true });
instance.add(terrainMap);

const min = 100;
const max = 300;

const values = colormap({ colormap: 'viridis', nshades: 256 });
const colors = values.map(v => new Color(v));
const colorMap = new ColorMap(colors, min, max);

const elevationLayer = new ElevationLayer({
    name: 'terrain',
    extent,
    colorMap,
    minmax: { min, max },
    source: new TiledImageSource({
        source: new TileWMS({
            url: 'https://data.geopf.fr/wms-r',
            projection: 'EPSG:3946',
            crossOrigin: 'anonymous',
            params: {
                LAYERS: ['ELEVATION.ELEVATIONGRIDCOVERAGE.HIGHRES'],
                FORMAT: 'image/x-bil;bits=32',
            },
        }),
        format: new BilFormat(),
        noDataValue: -1000,
    }),
});

terrainMap.addLayer(elevationLayer);

const orthophotoMap = new Map('orthophoto', { extent, doubleSided: true });
instance.add(orthophotoMap);

const orthophotoLayer = new ColorLayer({
    name: 'orthophoto',
    extent,
    source: new TiledImageSource({
        source: new TileWMS({
            url: 'https://data.geopf.fr/wms-r',
            projection: 'EPSG:3946',
            params: {
                LAYERS: ['HR.ORTHOIMAGERY.ORTHOPHOTOS'],
                FORMAT: 'image/jpeg',
            },
        }),
    }),
});
orthophotoMap.addLayer(orthophotoLayer);

const vectorMap = new Map('geojson', { extent, doubleSided: true, backgroundOpacity: 0 });
instance.add(vectorMap);

const geoJsonLayer = new ColorLayer({
    name: 'geojson',
    extent,
    source: new VectorSource({
        data: 'https://raw.githubusercontent.com/iTowns/iTowns2-sample-data/master/lyon.geojson',
        format: new GeoJSON(),
        dataProjection: 'EPSG:3946',
        style: new Style({
            fill: new Fill({
                color: 'rgba(255, 165, 0, 0.6)',
            }),
            stroke: new Stroke({
                color: 'white',
            }),
        }),
    }),
});

vectorMap.addLayer(geoJsonLayer);

orthophotoMap.object3d.translateZ(+1500);
orthophotoMap.object3d.updateMatrixWorld();
vectorMap.object3d.translateZ(+2500);
vectorMap.object3d.updateMatrixWorld();

// Sets the camera position
instance.camera.camera3D.position.set(1832816, 5163527, 6121);

// Creates controls
const controls = new MapControls(instance.camera.camera3D, instance.domElement);
// Then looks at extent's center
controls.target = extent.centerAsVector3();
controls.saveState();

controls.enableDamping = true;
controls.dampingFactor = 0.2;

instance.useTHREEControls(controls);

Inspector.attach(document.getElementById('panelDiv'), instance);

// Bind events


function bindToggle(id, callback) {
    const toggle = document.getElementById(id);
    toggle.oninput = () => {
        const state = toggle.checked;
        callback(state);
        instance.notifyChange();
    };
}

function bindSlider(id, callback) {
    const slider = document.getElementById(id);
    slider.oninput = function oninput() {
        callback(slider.valueAsNumber);
        instance.notifyChange();
    };
}

bindToggle('show-terrain', v => { terrainMap.visible = v; });
bindToggle('show-orthophoto', v => { orthophotoMap.visible = v; });
bindToggle('show-vector', v => { vectorMap.visible = v; });

bindSlider('terrain-opacity', o => { terrainMap.opacity = o; });
bindSlider('orthophoto-opacity', o => { orthophotoMap.opacity = o; });
bindSlider('vector-opacity', o => { vectorMap.opacity = o; });
bindSlider('vector-bg-opacity', o => {
    vectorMap.materialOptions.backgroundOpacity = o;
    instance.notifyChange(vectorMap);
});
index.html
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Giro3D - Stacking transparent maps</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
  <style>
    body {
      padding: 0;
      margin: 0;
      width: 100vw;
      height: 100vh;
    }

    #viewerDiv {
      width: 100%;
      height: 100%;
    }

    #panelDiv {
      position: absolute;
      top: 0;
      left: 0;
    }
    #viewerDiv canvas {
  background-image: linear-gradient(45deg, #aaaaaa 25%, transparent 25%), linear-gradient(-45deg, #aaaaaa 25%, transparent 25%), linear-gradient(45deg, transparent 75%, #aaaaaa 75%), linear-gradient(-45deg, transparent 75%, #aaaaaa 75%);
  background-size: 20px 20px;
  background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
}

  </style>
</head>

<body>
  <div id="viewerDiv"></div>
  <div id="panelDiv"></div>
  <div class="m-2 position-absolute top-0 end-0">

    <!-- Vector map -->
    <div class="card m-2">
        <div class="card-header">
            <div class="form-check form-switch">
                <input class="form-check-input" checked type="checkbox" role="switch" id="show-vector">
                <label class="form-check-label" for="show-vector">Vector map</label>
            </div>
        </div>

        <fieldset class="container m-1" id="vector-options">

            <!-- Opacity -->
            <label for="vector-opacity" class="form-label">Map opacity</label>
            <div class="input-group">
                <input type="range" min="0" step="0.01" max="1" value="1" class="slider" id="vector-opacity">
            </div>

            <!-- Opacity -->
            <label for="vector-bg-opacity" class="form-label">Background opacity</label>
            <div class="input-group">
                <input type="range" min="0" step="0.01" max="1" value="0" class="slider" id="vector-bg-opacity">
            </div>

        </fieldset>
    </div>

    <!-- Orthophoto map -->
    <div class="card m-2">
        <div class="card-header">
            <div class="form-check form-switch">
                <input class="form-check-input" checked type="checkbox" role="switch" id="show-orthophoto">
                <label class="form-check-label" for="show-orthophoto">Orthophoto map</label>
            </div>
        </div>

        <fieldset class="container m-1" id="orthophoto-options">

            <!-- Opacity -->
            <label for="orthophoto-opacity" class="form-label">Map opacity</label>
            <div class="input-group">
                <input type="range" min="0" step="0.01" max="1" value="1" class="slider" id="orthophoto-opacity">
            </div>

        </fieldset>
    </div>

    <!-- Terrain map -->
    <div class="card m-2">
        <div class="card-header">
            <div class="form-check form-switch">
                <input class="form-check-input" checked type="checkbox" role="switch" id="show-terrain">
                <label class="form-check-label" for="show-terrain">Terrain map</label>
            </div>
        </div>

        <fieldset class="container m-1" id="terrain-options">

            <!-- Opacity -->
            <label for="terrain-opacity" class="form-label">Map opacity</label>
            <div class="input-group">
                <input type="range" min="0" step="0.01" max="1" value="1" class="slider" id="terrain-opacity">
            </div>

        </fieldset>
    </div>

</div>

  <script type="module" src="index.js"></script>
  <script>
    /* activate popovers */
    const popoverTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="popover"]'));
    popoverTriggerList.map(
      // bootstrap is used as script in the template, disable warning about undef
      // eslint-disable-next-line no-undef
      popoverTriggerEl => new bootstrap.Popover(popoverTriggerEl, {
        trigger: 'hover',
        placement: 'left',
        content: document.getElementById(popoverTriggerEl.getAttribute('data-bs-content')).innerHTML,
        html: true,
      }),
    );
  </script>
</body>

</html>
package.json
{
  "name": "map_transparency_stack",
  "dependencies": {
    "@giro3d/giro3d": "0.33.1"
  },
  "devDependencies": {
    "vite": "^3.2.3"
  },
  "scripts": {
    "start": "vite",
    "build": "vite build"
  }
}