Display a video on a Map with a VideoSource.

100% © NOAA.

The VideoSource can display a video at an arbitrary extent. You can either pass a URL to the remote video, or provide a VideoTexture or an HTMLVideoElement to display.

index.js
import GeoJSON from "ol/format/GeoJSON.js";
import { Stroke, Style } from "ol/style.js";
import { Vector3 } from "three";

import ColorMap from "@giro3d/giro3d/core/ColorMap.js";
import CoordinateSystem from "@giro3d/giro3d/core/geographic/CoordinateSystem.js";
import Extent from "@giro3d/giro3d/core/geographic/Extent.js";
import Instance from "@giro3d/giro3d/core/Instance.js";
import ColorLayer from "@giro3d/giro3d/core/layer/ColorLayer.js";
import Globe from "@giro3d/giro3d/entities/Globe.js";
import Inspector from "@giro3d/giro3d/gui/Inspector.js";
import VectorSource from "@giro3d/giro3d/sources/VectorSource.js";
import VideoSource from "@giro3d/giro3d/sources/VideoSource.js";

function makeColorRamp(
  preset,
  discrete = false,
  invert = false,
  mirror = false,
) {
  let nshades = discrete ? 10 : 256;

  const values = colormap({ colormap: preset, nshades });

  const colors = values.map((v) => new Color(v));

  if (invert) {
    colors.reverse();
  }

  if (mirror) {
    const mirrored = [...colors, ...colors.reverse()];
    return mirrored;
  }

  return colors;
}

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

const globe = new Globe({});

instance.add(globe);

const source = new VideoSource({
  extent: new Extent(CoordinateSystem.epsg4326, {
    west: -180,
    east: +180,
    south: -90,
    north: +90,
  }),
  source: "https://3d.oslandia.com/giro3d/videos/humidity.webm",
});

const video = new ColorLayer({
  name: "video",
  source,
  colorMap: new ColorMap({
    colors: makeColorRamp("jet", false, true),
    min: 0,
    max: 1,
  }),
});

source.addEventListener("loaded", () => {
  source.video.loop = true;
  source.video.play();
});

globe.addLayer(video).catch(console.error);

const outlineStyle = new Style({
  stroke: new Stroke({ color: "black", width: 2 }),
});

// Display the countries boundaries.
const boundaries = new ColorLayer({
  name: "boundaries",
  source: new VectorSource({
    data: {
      url: "https://3d.oslandia.com/giro3d/vectors/countries.geojson",
      format: new GeoJSON(),
    },
    style: outlineStyle,
    dataProjection: CoordinateSystem.epsg4326,
  }),
});

globe.addLayer(boundaries).catch(console.error);

const position = globe.ellipsoid.toCartesian(0, 0, 25_000_000);
instance.view.camera.position.copy(position);
instance.view.camera.lookAt(new Vector3(0, 0, 0));

Inspector.attach("inspector", instance);
index.html
<!doctype html>
<html lang="en">
  <head>
    <title>Video source</title>
    <meta charset="UTF-8" />
    <meta name="name" content="video-source" />
    <meta
      name="description"
      content="Display a video on a Map with a &lt;code&gt;VideoSource&lt;/code&gt;."
    />
    <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>

    <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": "video-source",
    "dependencies": {
        "@giro3d/giro3d": "1.0.0"
    },
    "devDependencies": {
        "vite": "^3.2.3"
    },
    "scripts": {
        "start": "vite",
        "build": "vite build"
    }
}
vite.config.js
import { defineConfig } from "vite";

export default defineConfig({
  build: {
    target: 'esnext',
  },
  optimizeDeps: {
    esbuildOptions: {
      target: 'esnext',
    },
  },
})