Access latitude and longitude in fragment shader


I need to access the latitude and longitude of a pixel in a fragment shader (a code shader, not shadergraph). After an initial research it seem this info is available but to speed up development I would really appreciate some help on where to start.

Thanks and regards

You could use the event OnNewTileGameObjectCreated, iterate through the mesh vertices, calculate the Lat/Lon and store it in a vertex attribute.

Thats per-vertex. Maybe the pixel value can be interpolated?

If the tileset is a quad tree, you could calculate the latitude/pixel longitude/pixel.

I agree but I had the feeling this was already done somewhere. By looking at the shader I thought the UV of the mesh contained something usable to retrieve the coordinates. If not how do the map overlay work ? I don’t need to have exactly the lat/long in the UV. Something that could allow me to recreate in the fragment shader is good too

If you have a raster overlay, then the UV coordinates used to sample it will indeed be generated either from longitude/latitude (if the raster overlay uses a geographic project), or from Web Mercator coordinates (if the raster overlay uses that project, like Bing does). You’d also need to know the bounds of the tile in the projected coordinate system, and then you should be able to simply do a mix/lerp with the texture coordinates. And unproject the Web Mercator coordinates to longitude/latitude, if necessary (straightforward math, see WebMercatorProjection in cesium-native, but it requires the atan and exp functions).

The bounds aren’t passed into the material in any way, though, so that’s the first obstacle. And the next obstacle is that doing this in single precision on the GPU will cause problems. For small, zoomed-in tiles, there will not be sufficient precision to represent a different longitude/latitude for each pixel.

So it’s probably worth taking a step back and looking at why you want to do this, and consider if there’s a better way.

Thank you both for your replies

I don’t need to be very precise so I will try the road you mentioned @Kevin_Ring to start.


In the end I went with your solution @joseph.kaile . Thanks again :smiley: