Illustrates the use of the AxisGrid entity.
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.
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,
segments: 128,
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);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Giro3D - AxisGrid</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">
<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">
<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">
<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">
<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">
<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">
<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="slider" id="x-axis-step">
</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="slider" id="y-axis-step">
</div>
<div class="my-2"></div>
<!-- Z step -->
<label for="z-axis-step" class="form-label">zaxis step</label>
<div class="input-group">
<input type="range" min="100" max="3000" value="200" step="100" class="slider" id="z-axis-step">
</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>
{
"name": "axisgrid",
"dependencies": {
"@giro3d/giro3d": "0.34.2"
},
"devDependencies": {
"vite": "^3.2.3"
},
"scripts": {
"start": "vite",
"build": "vite build"
}
}