How to Build a Free Flight Sim

Part 1: Creating the 3D World

What if you could build your own flight simulator that runs entirely in a web browser, without expensive mapping APIs, commercial terrain services or a dedicated game engine?

In this tutorial series, we're going to explore how to do just that using a combination of free mapping technologies and open-source JavaScript libraries.

The first step is to build the world we are going to fly through. In Part 1, we'll create a detailed 3D terrain map of the Matterhorn in the Swiss Alps. In future instalments, we'll start taking to the skies and turn this 3D map into an interactive flying experience.

You can explore the finished map from Part 1 in the Live Demo and view the complete source code on GitHub.

Free 3D Terrain with MapLibre

Creating interactive 3D terrain maps used to be relatively expensive or technically complicated. You needed access to elevation data, a service to host terrain tiles, and a mapping library capable of rendering the results in three dimensions.

Fortunately, that's no longer the case. By combining MapLibre GL JS, OpenFreeMap and Mapterhorn, you can build an impressive 3D terrain map directly in your browser without paying for mapping APIs or running your own tile server.

The Three Free Mapping Components

Our 3D map brings together three different components:

  1. MapLibre GL JS - An open-source JavaScript library that renders interactive maps in the browser. It includes native support for 3D terrain, allowing elevation data to be turned into a three-dimensional landscape.

  2. OpenFreeMap - A free vector tile service providing the basemap, including roads, place names and landcover, using OpenStreetMap data.

  3. Mapterhorn - A free source of global Digital Elevation Model (DEM) tiles. These provide the elevation measurements that MapLibre uses to construct the 3D landscape.

Each component has a distinct job: OpenFreeMap supplies the map, Mapterhorn supplies the elevation data, and MapLibre brings everything together.

Building the 3D Map

Here are the main steps involved in creating our Matterhorn landscape.

1. Initialise the Map with OpenFreeMap

First, we initialise MapLibre GL JS and point it towards OpenFreeMap's Liberty vector basemap style.

const map = new maplibregl.Map({
    container: 'map',
    style: 'https://tiles.openfreemap.org/styles/liberty',
    center: matterhornCoords,
    zoom: 11.5,
    pitch: 50,
    bearing: 0,
    maxPitch: 85
});

A couple of properties are particularly important when creating a 3D map:

  • pitch controls the camera's viewing angle. A value of 0 gives you a conventional overhead map, while increasing it tilts the camera to reveal the landscape in perspective.

  • bearing rotates the map clockwise from north, allowing you to change the direction from which you view the terrain.

The pitch setting is especially important because even the most detailed elevation data won't look particularly three-dimensional if you're looking straight down at it!

2. Add Mapterhorn Elevation Data

Next, we add Mapterhorn's elevation tiles as a raster-dem source.

Once the map has finished loading, we can add the elevation source:

map.addSource('terrain-dem', {
    type: 'raster-dem',
    url: 'https://tiles.mapterhorn.com/tilejson.json',
    tileSize: 512,
    maxzoom: 12
});

The raster-dem source type tells MapLibre that these tiles contain elevation information rather than ordinary map imagery.

We use Mapterhorn's TileJSON endpoint to retrieve the tile configuration, rather than having to construct individual tile URLs ourselves.

The maxzoom property specifies the maximum zoom level at which the source provides tiles. When zooming beyond this level, MapLibre can reuse the available tiles at higher zoom levels, although this doesn't provide additional terrain detail.

3. Enable 3D Terrain

With the elevation source available, we can now activate MapLibre's native 3D terrain rendering.

map.setTerrain({
    source: 'terrain-dem',
    exaggeration: 1.4
});

The exaggeration property controls the vertical scale of the terrain.

A value of 1 represents the elevation data without additional vertical exaggeration. Increasing it makes mountains and valleys more pronounced, while reducing it produces a flatter landscape.

For our Matterhorn map, we've used 1.4 to make the surrounding Alpine terrain more visually dramatic.

You can experiment with this value to find the appearance that works best for your landscape.

4. Add Mountain Hillshading

Our map now has genuine 3D terrain, but we can improve its appearance further by adding hillshading.

Hillshading simulates the effect of sunlight falling across the landscape. Slopes facing away from the light become darker, while illuminated slopes become lighter. This helps emphasise ridges, valleys and changes in elevation.

For the hillshading, we'll add a second elevation source pointing to the same Mapterhorn tiles. MapLibre recommends keeping the terrain and hillshade sources separate to improve rendering quality.

map.addSource('hillshade-dem', {
    type: 'raster-dem',
    url: 'https://tiles.mapterhorn.com/tilejson.json',
    tileSize: 512,
    maxzoom: 12
});

We can then add a hillshade layer using this second source:

const firstSymbolLayer = map.getStyle().layers.find(
    layer => layer.type === 'symbol'
)?.id;

map.addLayer({
    id: 'hillshading',
    type: 'hillshade',
    source: 'hillshade-dem',
    paint: {
        'hillshade-shadow-color': '#473d3d',
        'hillshade-exaggeration': 0.5
    }
}, firstSymbolLayer);

The firstSymbolLayer finds the first symbol layer in the existing map style. By inserting our hillshade layer before it, we place the terrain shadows underneath the map labels, helping keep place names readable.

The hillshade-exaggeration property controls the strength of the shading. You can also experiment with different shadow and highlight colours to give your terrain a different appearance.

Experimenting with 3D Terrain

Once everything is working, there are several properties worth experimenting with.

  • Pitch: Increase the camera angle to explore the landscape from a more dramatic perspective.

  • Bearing: Rotate the map to view mountain ranges from different directions.

  • Terrain exaggeration: Increase or decrease the vertical scale to change how dramatic the landscape appears.

  • Hillshade: Experiment with different shadow colours and shading strengths to emphasise different landscape features.

These settings make it easy to create very different-looking terrain maps using exactly the same elevation data.

Preparing for Take-off

We've now built the world that will eventually become our flight simulator. We have a 3D landscape, realistic mountain relief and an interactive camera that lets us explore the Matterhorn from different perspectives.

The interesting thing about this combination is that none of the individual components is particularly complicated to use. MapLibre handles the rendering, OpenFreeMap supplies the basemap and Mapterhorn provides the elevation data.

Put them together and you have a surprisingly powerful 3D mapping platform that costs nothing to experiment with and requires no API keys or credit card.

You can host your finished map on GitHub Pages and start exploring the possibilities of free 3D terrain mapping.

In Part 2, we'll introduce an aircraft and start working on the controls that will allow us to fly around our mountainous landscape.


Comments

Popular Posts