https://github.com/tsherif/mercator-gl
Tiny library for GLSL web mercator projections
https://github.com/tsherif/mercator-gl
3d geospatial mercator visualization webgl
Last synced: 2 months ago
JSON representation
Tiny library for GLSL web mercator projections
- Host: GitHub
- URL: https://github.com/tsherif/mercator-gl
- Owner: tsherif
- License: mit
- Created: 2019-09-03T12:07:38.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-07T09:32:50.000Z (over 2 years ago)
- Last Synced: 2025-02-27T16:17:33.891Z (3 months ago)
- Topics: 3d, geospatial, mercator, visualization, webgl
- Language: JavaScript
- Homepage: https://tsherif.github.io/mercator-gl/
- Size: 4.8 MB
- Stars: 45
- Watchers: 5
- Forks: 9
- Open Issues: 15
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
MercatorGL
==========[](https://travis-ci.com/tsherif/mercator-gl) [](https://coveralls.io/github/tsherif/mercator-gl?branch=master) [](https://github.com/tsherif/mercator-gl/blob/master/build/mercator-gl.min.js) [](https://github.com/tsherif/mercator-gl/blob/master/LICENSE) [](https://www.npmjs.com/package/mercator-gl)
MercatorGL is a minimal library for calculating web mercator projections on a GPU using WebGL. It provides utilities to inject GLSL code for projecting longitude/latitude coordinates into already exisiting vertex shader code and calculate the uniforms it requires. MercatorGL focuses on numerical stability by performing most calculations at 64-bit precision, and switching to an "offset mode" at higher zoom levels (using a technique borrowed from [deck.gl](https://medium.com/vis-gl/how-sometimes-assuming-the-earth-is-flat-helps-speed-up-rendering-in-deck-gl-c43b72fd6db4)).
Following [Mapbox conventions](https://blog.mapbox.com/512-map-tiles-cb5bfd6e72ba) input coordinates are transformed to a 512x512 Mercator space, with (0, 0) at the top-left and (512, 512) at the bottom-right. Z-coordinates, if provided, are interpreted as meter elevations. The application must provide a projection matrix (via `updateMercatorUniforms`) to map from Mercator space into clip space.
An example of usage with [MapboxGL](https://docs.mapbox.com/mapbox-gl-js/api/) is shown below.
```JavaScript
let map = new mapboxgl.Map({
container: mapboxContainer,
style: "mapbox://styles/mapbox/streets-v9",
center: [-73.982130, 40.762896],
zoom: 15
});// NOTE: MercatorGL works with both GLSL 1 and 3 shaders
let vs = `
#version 300 es
layout(location=0) in vec2 lngLatPosition;
void main() {
// mercator_gl_lngLatToClip function injected by injectMercatorGLSL().
// mercator_gl_lngLatToMercator and mercator_gl_mercatorToClip also available to do
// projection in multiple steps.
gl_Position = mercator_gl_lngLatToClip(position);
}
`;let fs = `
#version 300 es
precision highp float;
uniform vec4 color;
out vec4 fragColor;
void main() {
fragColor = color;
}
`;// Insert projection functions into vertex shader
let vertexShaderSource = injectMercatorGLSL(vs);
let fragmentShaderSource = fs;
// Create WebGL program from vertexShaderSource and fragmentShaderSourcelet uniforms = {
// An application uniform, not used by MercatorGL
color: new Float32Array(1.0, 0.0, 0.0, 1.0);
};// Uniforms used by MercatorGL are added to the map.
allocateMercatorUniforms(uniforms);map.on("render", (e) => {
let center = map.getCenter().toArray();
let zoom = map.getZoom();// Update the values of MercatorGL uniforms in the map (including projection matrix provided by Mapbox).
// The application must use the map to update program uniforms used by MercatorGL.
updateMercatorUniforms(uniforms, center, zoom, map.transform.projMatrix);// Draw to canvas
});
```