Illustrates the use of the AxisGrid entity.

Parameters
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, DoubleSide } 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";

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 bindColorPicker(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() {
    // Let's change the classification color with the color picker value
    const hexColor = element.value;
    onChange(new Color(hexColor));
  };

  const externalFunction = (v) => {
    element.value = `#${new Color(v).getHexString()}`;
    onChange(element.value);
  };

  return [externalFunction, new Color(element.value), element];
}

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

const extent = new Extent(
  "EPSG:3857",
  x - halfWidth,
  x + halfWidth,
  y - halfWidth,
  y + halfWidth,
);

const instance = new Instance({
  target: "view",
  crs: extent.crs,
  backgroundColor: 0x0a3b59,
});

const map = new Map({
  extent,
  hillshading: true,
  discardNoData: true,
  side: DoubleSide,
  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({
  volume: {
    extent: extent.withRelativeMargin(0.1),
    floor,
    ceiling,
  },
  ticks: {
    x: 1000,
    y: 1000,
    z: 200,
  },
});

instance.add(axisGrid);

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

const controls = new MapControls(instance.view.camera, instance.domElement);
controls.target.set(-13603000, 5811000, 0);
instance.view.setControls(controls);

function bindAxisStep(axis) {
  bindSlider(`${axis}-axis-step`, (v) => {
    axisGrid.ticks[axis] = v;
    axisGrid.refresh();
    instance.notifyChange(axisGrid);
  });
}

bindAxisStep("x");
bindAxisStep("y");
bindAxisStep("z");

bindColorPicker("color", (color) => {
  axisGrid.color = color;
  instance.notifyChange(axisGrid);
});

bindToggle("entity", (v) => {
  axisGrid.visible = v;
  instance.notifyChange(axisGrid);
});
bindToggle("origin", (v) => {
  axisGrid.origin = v ? TickOrigin.Relative : TickOrigin.Absolute;
  axisGrid.refresh();
  instance.notifyChange(axisGrid);
});
bindToggle("ceiling", (v) => {
  axisGrid.showCeilingGrid = v;
  instance.notifyChange(axisGrid);
});
bindToggle("floor", (v) => {
  axisGrid.showFloorGrid = v;
  instance.notifyChange(axisGrid);
});
bindToggle("sides", (v) => {
  axisGrid.showSideGrids = v;
  instance.notifyChange(axisGrid);
});

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("inspector", 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/examples/css/example.css"
    />
  </head>

  <body>
    <div id="view" class="m-0 p-0 w-100 h-100"></div>
    <div
      id="inspector"
      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-header">Parameters</div>
        <div class="card-body">
          <!-- Color -->
          <label class="form-check-label w-100 mb-2" for="color">
            <div class="row">
              <div class="col">Color</div>
              <div class="col">
                <input
                  type="color"
                  class="form-control form-control-color float-end h-100 w-100"
                  id="color"
                  value="#ffffff"
                  title="color"
                  autocomplete="off"
                />
              </div>
            </div>
          </label>

          <!-- Show/Hide AxisGrid -->
          <div class="form-check form-switch">
            <input
              class="form-check-input"
              type="checkbox"
              checked="true"
              role="switch"
              id="entity"
              autocomplete="off"
            />
            <label class="form-check-label" for="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="origin"
              autocomplete="off"
            />
            <label class="form-check-label" for="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="ceiling"
              autocomplete="off"
            />
            <label class="form-check-label" for="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="floor"
              autocomplete="off"
            />
            <label class="form-check-label" for="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="sides"
              autocomplete="off"
            />
            <label class="form-check-label" for="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.40.0"
    },
    "devDependencies": {
        "vite": "^3.2.3"
    },
    "scripts": {
        "start": "vite",
        "build": "vite build"
    }
}