Rendering a Planet in the Browser: MapLibre, WebGL, and Keeping a Globe at 60 FPS
You can render a live planet in the browser at 60 frames per second — even with tens of thousands of points on it — if you treat the map as a WebGL scene with a strict 16.6 ms frame budget, not as a pile of DOM elements. The trick is sending the GPU one big buffer, clustering aggressively at low zoom, and never doing layout work inside the render loop. I learned this building WorldEye , a…
Achieving a real-time, live rendering of a planet within a browser at a consistent 60 frames per second is made possible through the implementation of WebGL and a strategic approach to handling vast datasets. This technique is exemplified in the development of WorldEye, a real-time globe that integrates 74,229 live public cameras, flights, satellites, weather phenomena, earthquakes, and fires.
To accomplish this feat, the map must be viewed not as a collection of DOM elements, but as a WebGL scene, adhering to a strict frame budget of 16.6 milliseconds per frame. This budget encompasses all necessary computations, including JavaScript logic, layout adjustments, painting operations, and GPU rendering. Given that mid-range phones with a devicePixelRatio of 2 or 3 have a significantly reduced frame budget, optimization is crucial.
The dataset, consisting of 74,229 camera points, is not rendered as individual icons. Instead, it is clustered aggressively at lower zoom levels, meaning that features deemed significant at typical zoom levels (ranging from ~200 to 800) are displayed as distinct symbols. Anything beyond this threshold is either clustered or culled, reducing the rendering load.
MapLibre GL JS plays a pivotal role in this process by enabling the rendering of GeoJSON data sources directly in WebGL. This allows for batch processing of data, which is essential for efficient rendering. The challenge of rendering a globe is amplified due to three main factors: projection costs, the horizon culling requirement, and density variations.
Projection costs arise because vertices need to be transformed in real time as the globe rotates, a task that is more demanding in JavaScript when dealing with thousands of points. The horizon cull addresses the issue of rendering points that are not visible due to the curvature of the Earth, necessitating smart filtering to exclude such points from drawing operations.
Lastly, the density of points along world zoom levels creates clusters of overlapping icons, especially in populated areas, which can lead to overdraw—where pixels are drawn multiple times, consuming GPU resources.
The core solution involves using a single GeoJSON source for live layers, which is updated and styled declaratively. The implementation involves initializing a map with a globe projection, setting up a single clustered source for the cameras, and adding three distinct layers: one for clustered data, another for showing individual camera points with varying opacity based on their point count, and a third for rendering camera points as small circles.
By clustering points and offloading aggregation to MapLibre's worker thread, the main thread remains free to handle other tasks, thus significantly improving performance.
Key strategies include:
- Using one GeoJSON source per live layer and updating it as a whole rather than individually, to minimize the workload on the main thread.
- Leveraging cluster functionality within MapLibre GL JS to handle aggregation and reduce the number of draw calls.
- Designing a data-driven styling mechanism that utilizes step functions to manage symbol visibility based on point counts, allowing the GPU to efficiently process these decisions without the overhead of individual callbacks.
- Implementing a queue system for updating camera data to ensure that updates are processed in batches, rather than individually, preventing the browser from becoming overwhelmed.
By adhering to these principles—treating the map as a WebGL scene, utilizing clustering for large datasets, minimizing DOM interactions, and offloading complex computations to the GPU—developers can render detailed, live globes within browsers at high frame rates. This approach not only optimizes performance but also enhances the user experience by providing a smooth, interactive globe that remains responsive even under heavy data loads.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to