← Back to other projects

Skopje Air 3D

A live 3D map of air pollution in Skopje, using real-time rendering techniques from open-world games. A personal project, built in the browser with three.js.

Skopje Air 3D: a 3D map of Skopje with pollution clouds in red and orange over the valley and sensor pins

Overview

Skopje Air 3D shows live air pollution as localised clouds over each neighbourhood of Skopje, on a 3D map built from real terrain and OpenStreetMap buildings. It has a timelapse of the last 24 hours and of past days, a street-level walk mode with haze based on the local PM2.5 reading, and community reports. The site is available in English, Macedonian and Albanian.

The idea came from how open-world games draw large maps. The project applies the same techniques to a map of a real city, in the browser.

Real-time techniques

  • Frustum culling: the terrain is split into square tiles, so only the tiles in view are drawn. Buildings are merged into chunks, each with a bounding sphere, for the same reason.
  • Level of detail: small buildings and minor streets are separate meshes that are hidden beyond a set distance. The distances are checked about five times a second, not every frame.
  • Draw distance tied to fog: the fog and the camera's far plane follow the camera's distance and angle, so geometry that is fully fogged is never drawn.
  • Adaptive resolution: the render resolution drops when the frame rate falls and rises again when there is headroom, with different thresholds in each direction to avoid flicker.
  • Idle throttling: when nothing is moving, the map renders at about 30 fps instead of 60 to save GPU work and battery.
  • Physical haze: in street view, visibility distance comes from the Koschmieder relation and the fog uses the Beer–Lambert law instead of three.js's default formula.

Technical breakdown

Selected excerpts from the project's JavaScript. The full repository is private.

Level of detail

Each detail mesh stores its position and a range. The update loop shows it when the camera is closer than the range and hides it otherwise. It only writes the visible flag when the value changes.

export const LOD = { smallBuildings: Q.smallBuildings, minorRoads: Q.minorRoads };
const v = new THREE.Vector3();
export function updateLod(groups, camPos) {
  for (const g of groups) for (const l of g.userData.lod) {
    const show = v.set(l.x, l.y, l.z).distanceTo(camPos) < l.range;
    if (l.mesh.visible !== show) l.mesh.visible = show;
  }
}

Adaptive resolution

A smoothed frame-rate average drives the render pixel ratio. It steps down by 0.25 below 50 fps and back up above 58 fps, with delays between changes. Frames while the app is idle-throttled, and long pauses such as a hidden tab, are ignored so they don't give false readings.

function adaptResolution(now) {
  const dt = now - lastFrame;
  lastFrame = now;
  if (now - lastActive > 600 || dt > 250) return;
  fpsAvg += (1000 / dt - fpsAvg) * 0.05;
  if (now - lastScale < 1500) return;
  let next = pixelRatio;
  if (fpsAvg < 50) next = Math.max(PR_MIN, pixelRatio - 0.25);
  else if (fpsAvg > 58 && now - lastScale > 5000) next = Math.min(PR_MAX, pixelRatio + 0.25);
  if (next !== pixelRatio) { pixelRatio = next; renderer.setPixelRatio(next); resize(); lastScale = now; fpsAvg = 50; }
}

Physical haze

Light extinction is estimated from the local PM2.5 value and converted to a visibility distance. three.js's default exponential fog squares the distance, which looks too clear up close, so the fog shader line is replaced with the physical form for street view.

const extinction = pm25 => (13 + 10 * Math.max(0, pm25)) / 1000;      // per km
export const visibilityKm = pm25 => 3.912 / extinction(pm25);

THREE.ShaderChunk.fog_fragment = THREE.ShaderChunk.fog_fragment.replace(
  '1.0 - exp( - fogDensity * fogDensity * vFogDepth * vFogDepth )', '1.0 - exp( - fogDensity * vFogDepth )');