Terrain height in Cesium

I’m currently looking to get the terrain height on mouse click. Going through the documentation I found two methods to suit my use case. The first one is Globe.getHeight and the second one is Cesium.sampleTerrainMostDetailed.

What’s the difference between these two methods? The values returned by these two are different.

Test code:
Globe.getHeight

<Viewer
      ref={ref}
      onClick={(click) => {
        if (click.position && ref.current?.cesiumElement) {
          var ellipsoid = ref.current?.cesiumElement.scene.globe.ellipsoid;
          var cartesian = ref.current?.cesiumElement.camera.pickEllipsoid(
            click.position,
            ellipsoid
          );
          if (cartesian) {
            var cartographic = ellipsoid.cartesianToCartographic(cartesian);

            var longitudeString = Cesium.Math.toDegrees(cartographic.longitude);
            var latitudeString = Cesium.Math.toDegrees(cartographic.latitude);
            var altitudeString =
              ref.current?.cesiumElement.scene.globe.getHeight(
                Cesium.Cartographic.fromDegrees(
                  cartographic.longitude,
                  cartographic.latitude
                )
              );
            window.alert(
              latitudeString + "," + longitudeString + "," + altitudeString
            );
          }
        }
      }}
    />

Cesium.sampleTerrainMostDetailed

<Viewer
      ref={ref}
      onClick={(click) => {
        if (click.position && ref.current?.cesiumElement) {
          var ellipsoid = ref.current?.cesiumElement.scene.globe.ellipsoid;
          var cartesian = ref.current?.cesiumElement.camera.pickEllipsoid(
            click.position,
            ellipsoid
          );
          if (cartesian) {
            var cartographic = ellipsoid.cartesianToCartographic(cartesian);

            var longitudeString = Cesium.Math.toDegrees(cartographic.longitude);
            var latitudeString = Cesium.Math.toDegrees(cartographic.latitude);
            Cesium.sampleTerrainMostDetailed(Cesium.createWorldTerrain(), [
              cartographic,
            ]).then(function (updatedPositions) {
              window.alert(
                longitudeString +
                  "," +
                  latitudeString +
                  "," +
                  updatedPositions[0].height
              );
            });
          }
        }
      }}
    />

Hi @Saran_Rathinavelu,

Globe.getHeight depends on the relative position of the camera (distance to picked coordinates / “zoom level”), because it get’s the height information from the rendered terrain.

However sampleTerrainMostDetailed looks for the most detailed height information stored in your terrain data, regardless the rendering / distance / “zoom level”.

The returned values should be the same if you zoom in close enough.

Best, Lennart