How to extract the terrain information for use in fragment shader?

I would like to extract the terrain information from our quantized mesh, so that I can use those values, say the height of the terrain, or the depth of the terrain from the camera, in custom shaders.

As an example of what I would like to do with this information:

  1. Extract all the height data from the current tile
  2. Create a buffer and pass it to a custom fragment shader for visualization

Hi @jonathansull99, welcome to the community!

I don’t know of a way to access an entire tile’s worth of data within the fragment shader. But you can compute the depth of the terrain at the current pixel, and use that information in the shader.

If you want an actual height above the ellipsoid, it may take a few steps. Some things you may find useful:

  1. czm_windowToEyeCoordinates is a function to compute eye coordinates at the current pixel
  2. czm_inverseModelView transforms eye coordinates to model coordinates
  3. czm_modelToEnu transforms model coordinates to an east-north-up coordinate system at the position on the ellipsoid below the camera.

The z coordinate of the result will then be a good approximation of height above the ellipsoid, for points not too far away from the camera.

You could then use the glsl methods dFdx and dFdy if you want to incorporate information from neighboring pixels.

1 Like

Appreciate the advice!

1 Like