Docs
Guides

Put a basemap and terrain under a scene

Draw a scene over a basemap on the map, on the globe and in the 3D scene, turn terrain on under the globe and the 3D scene, hide place labels, and change any of it while the view runs.

A scene is drawn on its own ground: a plain colour on the map, a globe in one colour, a grid in the 3D scene. That needs no token and no one's tiles, and it is where every view starts. A basemap and terrain are the ground of the place itself, from a source your app is licensed to use. The readers name no provider: you give the address and the words the source asks to be shown, and you may change them while the view runs.

basemap.tsx
"use client";

import { useState } from "react";
import maplibregl from "maplibre-gl";
import "maplibre-gl/dist/maplibre-gl.css";
import type { GlobeImagery, GlobeTerrain } from "@klorad/engine-cesium";
import { CesiumView } from "@klorad/engine-cesium/react";
import type { GlLibrary, MapBasemap } from "@klorad/engine-mapbox";
import { MapboxView } from "@klorad/engine-mapbox/react";

/** The sources your app is licensed to use. These addresses are placeholders: put your own, with what each asks to be shown. */
const STREETS: MapBasemap = { style: "https://tiles.example.com/styles/streets/style.json" };
const AERIAL: MapBasemap = { tiles: ["https://tiles.example.com/aerial/{z}/{x}/{y}.jpg"], tileSize: 256, attribution: "© Example Imagery", maxzoom: 20 };
const IMAGERY: GlobeImagery = { url: "https://tiles.example.com/aerial/{z}/{x}/{y}.jpg", credit: "© Example Imagery" };
const TERRAIN: GlobeTerrain = { url: "https://tiles.example.com/terrain", credit: "© Example Terrain" };

/** A map whose basemap a person changes: the scene stays drawn, selected and where the camera was. */
export function MapWithBasemaps() {
  const [basemap, setBasemap] = useState<MapBasemap | null>(null);
  return (
    <>
      <button onClick={() => setBasemap(null)}>None</button>
      <button onClick={() => setBasemap(STREETS)}>Streets</button>
      <button onClick={() => setBasemap(AERIAL)}>Aerial</button>
      <div style={{ height: 420 }}>
        <MapboxView gl={maplibregl as unknown as GlLibrary} basemap={basemap} />
      </div>
    </>
  );
}

/** A globe with imagery, and terrain turned on and off: over terrain the scene's ground stands on the hill at its origin. */
export function GlobeWithTerrain() {
  const [terrain, setTerrain] = useState(false);
  return (
    <>
      <label>
        <input type="checkbox" checked={terrain} onChange={(e) => setTerrain(e.target.checked)} /> Terrain
      </label>
      <div style={{ height: 420 }}>
        <CesiumView imagery={IMAGERY} terrain={terrain ? TERRAIN : null} />
      </div>
    </>
  );
}

The addresses in the sample are placeholders. Put the ones of your own source, with its terms: a key in a tile address is read by every browser that opens your page, so use only a key your provider calls publishable and restricts to your domains.

On the map

MapboxView takes basemap, and createMapboxRenderer takes it as an option and changes it with setBasemap. A basemap is one of two things:

ShapeWhat it is
{ style }A GL style, as a URL or as the style itself: the source's own layers, labels and words.
{ tiles, tileSize?, attribution?, minzoom?, maxzoom? }Raster tiles as XYZ templates, drawn over the plain ground. attribution is shown by the map's attribution control.

null is no basemap: the style the map was made with, or the plain ground. Changing the basemap keeps the scene drawn, the selection and the camera; the reader puts the scene's layers and icons back on every style the map takes.

labels (and setLabels) hides the place names of a GL style, which are its symbol layers, and shows them again; the scene's own labels are never touched. Raster tiles have their names painted in: to offer a person the choice, give the tiles of the same basemap without names. The map draws no terrain: it is a plan from above.

On the globe

CesiumView takes imagery and terrain, and the renderer changes them with setImagery and setTerrain. Imagery is raster tiles as an XYZ template; a basemap that exists only as a GL style cannot be drawn on the globe. Terrain is the address of a tileset CesiumJS reads as terrain, one that answers layer.json.

In the 3D scene

land.tsx
"use client";

import { useState } from "react";
import { environmentOf } from "@klorad/api/world";
import type { Land, LandRelief } from "@klorad/engine-three";
import { ThreeView } from "@klorad/engine-three/react";

/** The sources your app is licensed to use. These addresses are placeholders: put your own, with what each asks to be shown. */
const IMAGERY = { url: "https://tiles.example.com/aerial/{z}/{x}/{y}.jpg", credit: "© Example Imagery", maximumLevel: 20 };
const TERRAIN = { url: "https://tiles.example.com/terrain/", credit: "© Example Terrain" };

const SAID: Record<LandRelief, string> = { off: "", loading: "Loading elevation data.", on: "", failed: "Elevation data did not load. The land is drawn flat." };

/** A 3D scene over a basemap, with terrain turned on and off: no object moves, and the view says where its terrain stands. */
export function SceneOnLand() {
  const [terrain, setTerrain] = useState(false);
  const [relief, setRelief] = useState<LandRelief>("off");
  const land: Land = { imagery: IMAGERY, ...(terrain ? { terrain: TERRAIN } : {}) };
  return (
    <>
      <label>
        <input type="checkbox" checked={terrain} onChange={(e) => setTerrain(e.target.checked)} /> 3D terrain
      </label>
      <p role="status">{SAID[relief]}</p>
      <div style={{ height: 420 }}>
        <ThreeView environment={environmentOf("day")} land={land} onLand={setRelief} />
      </div>
    </>
  );
}

ThreeView takes land, and the renderer changes it with setLand. Land is a basemap as raster tiles (imagery, an XYZ template), terrain as a quantized mesh tileset (terrain, the address its layer.json is under, the same one the globe reads), or both. The tiles are placed by the scene's coordinate system, so a hill stands where it stands on Earth and a metre is a metre; they are read as the camera needs them and given back when out of sight. The credits of what is drawn are shown on the view.

Terrain is elevation data, not a picture of hills, and the view says where it stands through onLand (and landRelief()): "loading" until elevation stands under the scene, "on" once it is drawn, and "failed" when the source did not answer, in which case the land is drawn flat with its basemap and nothing else changes. Say it to the person as the sample does: flat land must not pass for terrain.

Where the scene stands over terrain

A scene's ground is flat, at its origin's height. Terrain is not. With terrain on, both the globe reader and the three.js reader make the terrain's surface at the scene's origin the scene's ground: where the scene stands it is on the hill, neither under it nor above it. No object's position or height is read or changed by it. Away from the origin the terrain rises and falls and the scene does not follow it, so on a slope an object far from the origin may stand over the land drawn there, or under it. On the globe, if your scene stores true heights above the ellipsoid, make the view with heights="ellipsoid" and they are drawn as stored.

Terrain is solid

Terrain hides what is behind it and what is under it, in both readers: a hill in front of an object covers it, and so does land that stands higher than the object. A basemap with no terrain is a flat picture under the scene's ground and hides nothing.

The pointer meets the land that is drawn. Over terrain a hit's local.up is the height of the land under the pointer, a drag keeps an object's height over the land under it, and land in front of an object takes the pick. To stand something on the land yourself, ask the view how high its ground is at a place:

const up = view.groundHeight({ east: 120, north: -80 });
scene.update(id, { position: { east: 120, north: -80, up } });

groundHeight is the land as it is drawn at that moment, finest near the camera, and 0 where no terrain is drawn: on flat ground, on a plain globe and always on the map. Nothing is stood on the land for you, so objects placed on flat ground keep their heights when terrain comes on.

Letting a person choose

Keep the choice where your app keeps it, and pass what was chosen as the prop. A scene's environment has a place for it: map, a SceneMap with the basemap by name, terrain and labels. It names what is drawn and never where the tiles come from, so it is saved, versioned and published with the rest of the scene's look. In Klorad One the creator sets the Project's map in Look and publishes it with the Project, and each person, a visitor included, may change it for themselves in Display, from the sources the deployment names.

Try it in your browserExample: Draw a 3D scene over a basemap and terrainA basemap and real elevation under a 3D scene, placed by the scene's own location on Earth.

Something wrong or unclear? Tell us.