liveBook · Manning

CesiumJS

As explained in Digital Twins in Action

CesiumJS is an open-source JavaScript library for creating interactive three-dimensional geospatial visualizations in web applications.

CesiumJS is an open-source JavaScript library for rendering geospatial data and 3D content on an interactive globe. It places virtual objects at real-world locations, adding geographic context to a browser-based 3D model. section 7.2.2

A model can be located using WGS84 latitude and longitude, which Cesium converts to its internal Earth-Centered, Earth-Fixed coordinates (ECEF, EPSG:4978). A transformation matrix positions and rotates the model, including its offset from true north. section 7.2.2

A practical workflow for a custom model is:

  • Convert an OBJ model to 3D Tiles using Cesium ion or Obj2Tiles. The format organizes content into a spatial hierarchy with levels of detail. section 7.2.2
  • Load the tileset in a CesiumJS scene and specify its geographic location and orientation. Cesium ion can also host the converted content. section 7.2.2
  • Add surrounding context, such as an OpenStreetMap 3D building layer or a Google photorealistic 3D tileset. section 7.2.2

A house model in Perth

A house model is placed in a city park using geographic coordinates and a transformation matrix. An OpenStreetMap 3D building layer supplies surrounding context. For a small model, tiling may offer little performance benefit, but it still streamlines CesiumJS integration. section 7.2.2

Listing 7.3 Rendering and geolocating a 3D home model in CesiumJS
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Digitial Twins in Action</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/cesium/1.113.0/
    Cesium.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/cesium/1.113.0/
    Widgets/widgets.css" rel="stylesheet">
    <style>
      body, #cesiumContainer { margin: 0; padding: 0; overflow: hidden;
      width: 100vw; height: 100vh; }
    </style>
  </head>
  <body>
    <div id="cesiumContainer"></div>
    <script>
      Cesium.Ion.defaultAccessToken = YOUR_ION_TOKEN_HERE;  #1

      const [lat, lon, height, rotation] = [
        -31.961505, 115.868492, -31, -20];  #2
      const matrix = () =>
        Cesium.Matrix4.multiply(
          Cesium.Transforms.eastNorthUpToFixedFrame(Cesium.Cartesian3.
          fromDegrees(lon, lat, height)),
          Cesium.Matrix4.multiply(
            Cesium.Matrix4.fromScale(new Cesium.Cartesian3(
            0.01, 0.01, 0.01)),
            Cesium.Matrix4.fromRotation(Cesium.Matrix3.fromRotationZ(
            rotation * Math.PI / 180)),
            new Cesium.Matrix4()
          ),
          new Cesium.Matrix4()
        );

      (async () => {
        const viewer = new Cesium.Viewer("cesiumContainer", {
          terrain: Cesium.Terrain.fromWorldTerrain(),
        });

        const [osmBuildingsTileset, customTileset] = await Promise.all([
          Cesium.createOsmBuildingsAsync(),  #3
          Cesium.Cesium3DTileset.fromUrl("https://3d.dtia.site/tiles/
          tileset.json")
        ]);

        customTileset.modelMatrix = matrix();
        viewer.scene.primitives.add(osmBuildingsTileset);
        viewer.scene.primitives.add(customTileset);

        viewer.scene.camera.flyTo({
          destination: Cesium.Cartesian3.fromDegrees(lon, lat, 200)
        });
      })();
    </script>
  </body>
</html>

The listing sets up a CesiumJS viewer and demonstrates the model’s location and transformation. section 7.2.2

The same scene can be configured for a split-screen VR view. Enable the viewer’s VR button and invoke its command; on a phone placed in a low-cost cardboard viewer, the result gives a sense of immersion without dedicated VR hardware. section 7.5.1

const viewer = new Cesium.Viewer("cesiumContainer", {
  terrain: Cesium.Terrain.fromWorldTerrain(),
  vrButton: true  #1
});

if (viewer.vrButton) {
  viewer.vrButton.viewModel.command();  #2
}

This snippet enables the VR button and invokes its command when available. section 7.5.1

How Digital Twins in Action covers it

How deep it goes
Works through model conversion, coordinate transformation, scene loading, and a VR viewer configuration.
Examples
Uses a house model placed in a Perth park and viewed with surrounding buildings or a cardboard VR viewer.
What you can do after
Apply it via CesiumJS to geolocate a 3D model and enable a split-screen VR view.
How it connects
Connects 3D Tiles, WGS84, ECEF coordinates, transformation matrices, and virtual reality.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Digital Twins in Action. The section links point to the parts of the book it draws on.