Illustrates the use of the AxisGrid entity.

100% © U.S. Geological Survey

The AxisGrid is useful to get a grasp of a dataset's volume in 3D space, including is height. The relative origin mode is useful to get a grasp of the dataset's size, with the absolute origin mode displays coordinates in the local CRS.

index.js
import colormap from 'colormap';
import { Color } from 'three';
import { MapControls } from 'three/examples/jsm/controls/MapControls.js';
import XYZ from 'ol/source/XYZ.js';

import Extent from '@giro3d/giro3d/core/geographic/Extent.js';
import Instance from '@giro3d/giro3d/core/Instance.js';
import ElevationLayer from '@giro3d/giro3d/core/layer/ElevationLayer.js';
import TiledImageSource from '@giro3d/giro3d/sources/TiledImageSource.js';
import Map from '@giro3d/giro3d/entities/Map.js';
import AxisGrid, { TickOrigin } from '@giro3d/giro3d/entities/AxisGrid.js';
import Inspector from '@giro3d/giro3d/gui/Inspector.js';
import Interpretation from '@giro3d/giro3d/core/layer/Interpretation.js';
import GeoTIFFFormat from '@giro3d/giro3d/formats/GeoTIFFFormat.js';
import ColorMap, { ColorMapMode } from '@giro3d/giro3d/core/layer/ColorMap.js';

const x = -13602000;
const y = 5812000;
const halfWidth = 2500;

// Defines geographic extent: CRS, min/max X, min/max Y
const extent = new Extent('EPSG:3857', x - halfWidth, x + halfWidth, y - halfWidth, y + halfWidth);

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

// Creates a Giro3D instance
const instance = new Instance(viewerDiv, {
    crs: extent.crs(),
    renderer: {
        clearColor: 0x0a3b59,
    },
});

// Creates a map that will contain the layer
const map = new Map('planar', {
    extent,
    hillshading: true,
    discardNoData: true,
    doubleSided: true,
    backgroundColor: 'white',
});

instance.add(map);

const source = new TiledImageSource({
    source: new XYZ({
        minZoom: 10,
        maxZoom: 16,
        url: 'https://3d.oslandia.com/dem/MtStHelens-tiles/{z}/{x}/{y}.tif',
    }),
    format: new GeoTIFFFormat(),
});

const floor = 1100;
const ceiling = 2500;

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

const dem = new ElevationLayer({
    name: 'dem',
    extent,
    interpretation: Interpretation.Raw,
    source,
    colorMap: new ColorMap(colors, floor, ceiling, ColorMapMode.Elevation),
});

map.addLayer(dem);

// Create an axis grid that encompasses the Map.
const axisGrid = new AxisGrid('axis-grid', {
    volume: {
        extent: extent.withRelativeMargin(0.1),
        floor,
        ceiling,
    },
    ticks: {
        x: 1000,
        y: 1000,
        z: 200,
    },
});

instance.add(axisGrid);

instance.camera.camera3D.position.set(-13594700, 5819700, 7300);

// Instanciates controls
const controls = new MapControls(instance.camera.camera3D, instance.domElement);

controls.target.set(-13603000, 5811000, 0);

instance.useTHREEControls(controls);

// Manage GUI

function bindAxisStep(axis) {
    const slider = document.getElementById(`${axis}-axis-step`);
    slider.oninput = () => {
        axisGrid.ticks[axis] = parseInt(slider.value, 10);
        axisGrid.refresh();
        instance.notifyChange(axisGrid);
    };
}

function bindToggle(name, rebuild, action) {
    const toggle = document.getElementById(`toggle-${name}`);
    toggle.oninput = () => {
        const state = toggle.checked;
        action(state);
        if (rebuild) {
            axisGrid.refresh();
        }
        instance.notifyChange(axisGrid);
    };
}

bindAxisStep('x');
bindAxisStep('y');
bindAxisStep('z');

bindToggle('entity', false, v => {
    axisGrid.visible = v;
});
bindToggle('origin', true, v => {
    axisGrid.origin = v ? TickOrigin.Relative : TickOrigin.Absolute;
});
bindToggle('ceiling', false, v => {
    axisGrid.showCeilingGrid = v;
});
bindToggle('floor', false, v => {
    axisGrid.showFloorGrid = v;
});
bindToggle('sides', false, v => {
    axisGrid.showSideGrids = v;
});

document.getElementById('randomize-position').onclick = () => {
    const current = axisGrid.volume.extent;
    const dims = current.dimensions();
    const center = current.centerAsVector3();
    const range = 5000;
    center.set(
        center.x + (Math.random() - 0.5) * range,
        center.y + (Math.random() - 0.5) * range,
        0,
    );
    const newExtent = new Extent(
        current.crs(),
        center.x - dims.x / 2,
        center.x + dims.x / 2,
        center.y - dims.y / 2,
        center.y + dims.y / 2,
    );

    axisGrid.volume.extent = newExtent;
    axisGrid.refresh();
    instance.notifyChange(axisGrid);
};

Inspector.attach(document.getElementById('panelDiv'), instance);
index.html
<!doctype html>
<html lang="en">
    <head>
        <title>AxisGrid</title>
        <meta charset="UTF-8" />
        <meta name="name" content="axisgrid" />
        <meta name="description" content="Illustrates the use of the AxisGrid entity." />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />

        <link rel="icon" href="https://giro3d.org/images/favicon.svg" />
        <link href="https://giro3d.org/assets/bootstrap-custom.css" rel="stylesheet" />
        <script src="https://giro3d.org/assets/bootstrap.bundle.min.js"></script>
        <link
            rel="stylesheet"
            type="text/css"
            href="https://giro3d.org/latest/css/example.css"
        />

        
    </head>

    <body>
        <div id="viewerDiv" class="m-0 p-0 w-100 h-100"></div>
        <div id="panelDiv" class="position-absolute top-0 start-0 mh-100 overflow-auto"></div>

        <div class="side-pane-with-status-bar">
            <div class="card">
                <div class="card-body">
                    <!-- Show/Hide AxisGrid -->
                    <div class="form-check form-switch">
                        <input
                            class="form-check-input"
                            type="checkbox"
                            checked="true"
                            role="switch"
                            id="toggle-entity"
                            autocomplete="off"
                        />
                        <label class="form-check-label" for="toggle-entity">Show axis grid</label>
                    </div>

                    <!-- Absolute/Relative -->
                    <div class="form-check form-switch">
                        <input
                            class="form-check-input"
                            type="checkbox"
                            checked="true"
                            role="switch"
                            id="toggle-origin"
                            autocomplete="off"
                        />
                        <label class="form-check-label" for="toggle-origin">Relative origin</label>
                    </div>

                    <!-- Show/Hide ceiling -->
                    <div class="form-check form-switch">
                        <input
                            class="form-check-input"
                            type="checkbox"
                            checked="true"
                            role="switch"
                            id="toggle-ceiling"
                            autocomplete="off"
                        />
                        <label class="form-check-label" for="toggle-ceiling">Show ceiling</label>
                    </div>

                    <!-- Show/Hide floor -->
                    <div class="form-check form-switch">
                        <input
                            class="form-check-input"
                            type="checkbox"
                            checked="true"
                            role="switch"
                            id="toggle-floor"
                            autocomplete="off"
                        />
                        <label class="form-check-label" for="toggle-floor">Show floor</label>
                    </div>

                    <!-- Show/Hide sides -->
                    <div class="form-check form-switch">
                        <input
                            class="form-check-input"
                            type="checkbox"
                            checked="true"
                            role="switch"
                            id="toggle-sides"
                            autocomplete="off"
                        />
                        <label class="form-check-label" for="toggle-sides">Show sides</label>
                    </div>

                    <div class="my-2"></div>

                    <!-- X step -->
                    <label for="x-axis-step" class="form-label">x-axis step</label>
                    <div class="input-group">
                        <input
                            type="range"
                            min="100"
                            max="3000"
                            value="1000"
                            step="100"
                            class="form-range"
                            id="x-axis-step"
                            autocomplete="off"
                        />
                    </div>

                    <div class="my-2"></div>

                    <!-- Y step -->
                    <label for="y-axis-step" class="form-label">y-axis step</label>
                    <div class="input-group">
                        <input
                            type="range"
                            min="100"
                            max="3000"
                            value="1000"
                            step="100"
                            class="form-range"
                            id="y-axis-step"
                            autocomplete="off"
                        />
                    </div>

                    <div class="my-2"></div>

                    <!-- Z step -->
                    <label for="z-axis-step" class="form-label">z-axis step</label>
                    <div class="input-group">
                        <input
                            type="range"
                            min="100"
                            max="3000"
                            value="200"
                            step="100"
                            class="form-range"
                            id="z-axis-step"
                            autocomplete="off"
                        />
                    </div>

                    <div class="my-4"></div>

                    <!-- Randomize grid position -->
                    <button type="button" class="btn btn-primary" id="randomize-position">
                        Randomize position
                    </button>
                </div>
            </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": "axisgrid",
    "dependencies": {
        "colormap": "^2.3.2",
        "@giro3d/giro3d": "0.37.2"
    },
    "devDependencies": {
        "vite": "^3.2.3"
    },
    "scripts": {
        "start": "vite",
        "build": "vite build"
    }
}