How to decrease sampleTerrain requests and improve its performance?

Hi Dear Community,

I am trying to calculate distance between a point and terrain height dynamically (every second). To do this, I write this code:

const FindIntersection = (raisedPositionsCartographic) => {
   
    for (let i  = 0; i < cartographicReal.length; i++){
      // do something
      if(raisedPositionsCartographic[i].height - cartographicReal[i].height > 0.1){
        break;
      }
    }
  };

var result = await Cesium['sampleTerrain'](Application.getMap()['viewer']['terrainProvider'], 11, cartographic);

return FindIntersection(result);

But it has really bad performance with too much request on same terrain tile:

Any thoughts to improve this performance or prevent making requests to the same tile?

Thank you for your time!

Are you doing the sampling every second (like through setInterval()) or in real-time (with the render cycle)? But more importantly, what kind of TerrainProvider have you got there? It looks like a WMS (or web service) instead of, for example, a DTM. I guess a bit more info about your actual data would help.

But I’m also curious as to why you write calls to the API this way and what the getMap() function does;

Cesium['sampleTerrain'](Application.getMap()['viewer']['terrainProvider']

Why the [‘’] notation, any particular reason? Also, could there be extra time spent in .getMap()? The sampling is a bit slow, and I guess you could have a stab at using slightly different methods (for example creating your own ray and test intersection), however I doubt that will get you much. I think firstly you need to tell us about the terrain provider and the data itself (what is it, where does it come from, etc.)

Cheers,

Alex

I put together a little Sandcastle example that I think roughly mirrors what you’re trying to do.

On my browser, each call takes 30-100ms, which does seem a bit high, and creates a network request to the terrain service, though the devtools say they resolve in a couple ms because the file is cached. Maybe the service you’re using for terrain doesn’t set the correct headers to ensure tiles are cached by the browser? (I do agree that Cesium could probably do some application-level caching here…)