lunes, 8 de enero de 2018

Fast Cesium terrain rendering with the new quantized-mesh output of the opensource CesiumTerrainBuilder tool

Cesium is an open-source JavaScript library for creating 3D globes and 2D maps in a web browser without a plugin. It uses WebGL for hardware-accelerated graphics, and is cross-platform, cross-browser, and tuned for dynamic-data visualization.
Cesium is developed by an open-source community, with support from the Cesium Consortium, founded by Analytical Graphics, Inc (AGI). and Bentley Systems.
Terrain support
Cesium supports streaming and visualizing global high-resolution terrain and water effects for oceans, lakes, and rivers. Mountain peaks, valleys, and other terrain features really show the benefit of a 3D globe compared to a 2D map.
Cesium supports several methods for requesting terrain using terrain providers. Most terrain providers use a REST interface over HTTP to request terrain tiles as height maps. Terrain providers differ based on how requests are formatted and how terrain data is organized. You can read details in this tutorial.
How can you add your own terrain data to Cesium? 
First, you need to check if you really need it. You have the option to stream high-resolution terrain data directly from the servers at AGI. It's free to use on public sites under the terms of use. If you want to host the terrain data on your own servers, AGI provides a commercial product - the STK Terrain Server.
But if you want to do it yourself, you have to follow reading next :-).
Cesium supports two terrain formats:
  1. heightmap
  2. quantized-mesh
Read this great article here for more details. About formats, it says:
  • The quantized-mesh format follows the same tile structure as heightmap tiles, but each tile is better optimised for large-scale terrain rendering. Instead of creating a dense, uniform triangle mesh in the browser, an irregular triangle mesh is pre-rendered for each tile. It's a better representation of the landscape, having less detail in flat areas while increasing the density in steep terrain. The mesh terrain is also more memory efficient and renders faster.
You can generate heightmap tiles with Cesium Terrain Builder, a great opensource command-line utility by Homme Zwaagstra at the GeoData Institute, University of Southampton.
Here you can see how much dense are the tiles using the heightmap format:
At the present time, this tool can't create tiles in quantized-mesh format. So I have added the quantized-mesh support to "ctb-tile" tool to create tiles in this lightweight format :-)
Briefly, ctb-tile tool provides two new features:
  • New quantized-mesh format ouput.
  • New metadata file output.
The format parameter ("-f" or "--output-format") provides the new "Mesh" option in order to output quantized-mesh tiles. It reads from a Digital Elevation Model (DEM) as usual, and applies the Chuncked LOD strategy by Thatcher Ulrich to convert the regular grid of each tile in an equivalent and simplified irregular mesh.
The process takes care of an input geometric error to safely merge each pair of triangles with its parent one if there is not a visible loss of quality. Also, you can define a factor of this geometric error using the new "--mesh-qfactor" parameter (1.0 by default) to apply your own error value. Larger values should mean minor quality.
How does ctb-tile calculate the geometric error of each tile? It uses a similar algorithm as Cesium chooses what level of tiles has to render. The error for level zero of a geodetic quadtree is around 77,067.34 meters:
This errors table is applied to decide if a triangle has to be written to output mesh or it has to be splitted to its next child ones. To do not collapse all tiles of lower levels, the process adds more triangles than geometric error indicates, otherwise we would get a squared globe.
And to finish, the new "-l" (or "--layer") option will allow you to create the "layer.json" file with all metadata information that describes the tileset. It fills the "available" json-attribute too, it seems mandatory to successfully load the terrain in Cesium.
Do you like this? You can freely test my github fork!. I proposed a pull request with this to CTB repository too, If owners agree, I would happy they merge it.