Illustrates adjusting brightness, constrast, and saturation on color layers and a maps.

Colorimetry
  • Map
  • Satellite layer
  • GeoJSON layer

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

import Instance from '@giro3d/giro3d/core/Instance.js';
import Extent from '@giro3d/giro3d/core/geographic/Extent.js';
import Map from '@giro3d/giro3d/entities/Map.js';
import ColorLayer from '@giro3d/giro3d/core/layer/ColorLayer.js';
import Inspector from '@giro3d/giro3d/gui/Inspector.js';
import WmsSource from '@giro3d/giro3d/sources/WmsSource.js';
import VectorSource from '@giro3d/giro3d/sources/VectorSource.js';



const viewer = document.getElementById('viewerDiv');

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',
);
Instance.registerCRS('EPSG:4171', '+proj=longlat +ellps=GRS80 +no_defs +type=crs');

const instance = new Instance(viewer, { crs: 'EPSG:3946' });

const xmin = 1837816.94334;
const xmax = 1847692.32501;
const ymin = 5170036.4587;
const ymax = 5178412.82698;

const extent = new Extent('EPSG:3946', xmin, xmax, ymin, ymax);

const map = new Map('city-of-lyon', { extent });
instance.add(map);

const satelliteSource = new WmsSource({
    url: 'https://data.geopf.fr/wms-r',
    projection: 'EPSG:3946',
    layer: 'HR.ORTHOIMAGERY.ORTHOPHOTOS',
    imageFormat: 'image/jpeg',
});

const colorLayer = new ColorLayer({
    name: 'satellite',
    source: satelliteSource,
    extent: map.extent,
});
map.addLayer(colorLayer);

// Adds our first layer from a geojson file
// Initial source: https://data.grandlyon.com/jeux-de-donnees/parcs-places-jardins-indice-canopee-metropole-lyon/info
const geoJsonLayer = new ColorLayer({
    name: 'geojson',
    source: new VectorSource({
        data: 'https://3d.oslandia.com/lyon/evg_esp_veg.evgparcindiccanope_latest.geojson',
        // Defines the dataProjection to reproject the data,
        // GeoJSON specifications say that the crs should be EPSG:4326 but
        // here we are using a different one.
        dataProjection: 'EPSG:4171',
        format: new GeoJSON(),
        style: feature =>
            new Style({
                fill: new Fill({
                    color: `rgba(0, 128, 0, ${feature.get('indiccanop')})`,
                }),
                stroke: new Stroke({
                    color: 'white',
                }),
            }),
    }),
});
map.addLayer(geoJsonLayer);

const camera = instance.camera.camera3D;
const cameraAltitude = 2000;

const cameraPosition = new Vector3(extent.west(), extent.south(), cameraAltitude);
camera.position.copy(cameraPosition);

const controls = new MapControls(camera, instance.domElement);
controls.target = extent.centerAsVector3();

controls.enableDamping = true;
controls.dampingFactor = 0.2;
controls.maxPolarAngle = Math.PI / 2.3;

controls.saveState();

instance.useTHREEControls(controls);

function bindSlider(name, fn) {
    const slider = document.getElementById(name);
    slider.oninput = function oninput() {
        fn(slider.value);
        instance.notifyChange(map);
    };
}

function bindLayerSliders(id, layer) {
    document.getElementById(`${id}-reset`).onclick = function onclick() {
        layer.brightness = 0;
        layer.saturation = 1;
        layer.contrast = 1;
        instance.notifyChange(map);
    };
    bindSlider(`${id}-brightness`, v => {
        layer.brightness = v;
    });
    bindSlider(`${id}-contrast`, v => {
        layer.contrast = v;
    });
    bindSlider(`${id}-saturation`, v => {
        layer.saturation = v;
    });
}

bindLayerSliders('satellite', colorLayer);
bindLayerSliders('vector', geoJsonLayer);

const mapParams = map.materialOptions.colorimetry;
bindSlider('map-brightness', v => {
    mapParams.brightness = v;
});
bindSlider('map-contrast', v => {
    mapParams.contrast = v;
});
bindSlider('map-saturation', v => {
    mapParams.saturation = v;
});

document.getElementById('map-reset').onclick = function onclick() {
    mapParams.brightness = 0;
    mapParams.contrast = 1;
    mapParams.saturation = 1;

    instance.notifyChange(map);
};

Inspector.attach(document.getElementById('panelDiv'), instance);
index.html
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Giro3D - Color Adjustment</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;
    }
    
  </style>
</head>

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

        <ul class="list-group list-group-flush">
            <li class="list-group-item">
                <!-- Map parameters -->
                <h6 class="card-subtitle mb-2 text-body-secondary">Map</h6>
                <form class="container m-1">
                    <label for="map-brightness" class="form-label">Brightness</label>
                    <div class="input-group">
                        <input
                            type="range"
                            min="-1"
                            max="1"
                            value="0"
                            class="slider"
                            step="0.05"
                            id="map-brightness"
                        />
                    </div>

                    <label for="map-contrast" class="form-label">Contrast</label>
                    <div class="input-group">
                        <input
                            type="range"
                            min="0"
                            max="3"
                            value="1"
                            class="slider"
                            step="0.05"
                            id="map-contrast"
                        />
                    </div>

                    <label for="map-saturation" class="form-label">Saturation</label>
                    <div class="input-group">
                        <input
                            type="range"
                            min="0"
                            max="3"
                            value="1"
                            class="slider"
                            step="0.05"
                            id="map-saturation"
                        />
                    </div>

                    <div class="input-group my-2">
                        <input id="map-reset" type="reset" class="btn btn-primary" value="Reset" />
                    </div>
                </form>
            </li>
            <li class="list-group-item">
                <!-- Satellite layer parameters -->
                <h6 class="card-subtitle mb-2 text-body-secondary">Satellite layer</h6>
                <form class="container m-1">
                    <label for="satellite-brightness" class="form-label">Brightness</label>
                    <div class="input-group">
                        <input
                            type="range"
                            min="-1"
                            max="1"
                            value="0"
                            class="slider"
                            step="0.05"
                            id="satellite-brightness"
                        />
                    </div>

                    <label for="satellite-contrast" class="form-label">Contrast</label>
                    <div class="input-group">
                        <input
                            type="range"
                            min="0"
                            max="3"
                            value="1"
                            class="slider"
                            step="0.05"
                            id="satellite-contrast"
                        />
                    </div>

                    <label for="satellite-saturation" class="form-label">Saturation</label>
                    <div class="input-group">
                        <input
                            type="range"
                            min="0"
                            max="3"
                            value="1"
                            class="slider"
                            step="0.05"
                            id="satellite-saturation"
                        />
                    </div>

                    <div class="input-group my-2">
                        <input
                            id="satellite-reset"
                            type="reset"
                            class="btn btn-primary"
                            value="Reset"
                        />
                    </div>
                </form>
            </li>
            <li class="list-group-item">
                <!-- Vector layer parameters -->
                <h6 class="card-subtitle mb-2 text-body-secondary">GeoJSON layer</h6>
                <form class="container m-1">
                    <label for="vector-brightness" class="form-label">Brightness</label>
                    <div class="input-group">
                        <input
                            type="range"
                            min="-1"
                            max="1"
                            value="0"
                            class="slider"
                            step="0.05"
                            id="vector-brightness"
                        />
                    </div>

                    <label for="vector-contrast" class="form-label">Contrast</label>
                    <div class="input-group">
                        <input
                            type="range"
                            min="0"
                            max="3"
                            value="1"
                            class="slider"
                            step="0.05"
                            id="vector-contrast"
                        />
                    </div>

                    <label for="vector-saturation" class="form-label">Saturation</label>
                    <div class="input-group">
                        <input
                            type="range"
                            min="0"
                            max="3"
                            value="1"
                            class="slider"
                            step="0.05"
                            id="vector-saturation"
                        />
                    </div>

                    <div class="input-group my-2">
                        <input
                            id="vector-reset"
                            type="reset"
                            class="btn btn-primary"
                            value="Reset"
                        />
                    </div>
                </form>
            </li>
        </ul>
    </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": "color_adjustments",
  "dependencies": {
    "@giro3d/giro3d": "0.35.0"
  },
  "devDependencies": {
    "vite": "^3.2.3"
  },
  "scripts": {
    "start": "vite",
    "build": "vite build"
  }
}