Creating Realistic Terrain
with WebGL

Then and Now

The first version of Flight Simulator shipped in 1980 for the Apple II and, amazingly, it was in 3D! That was a remarkable achievement. It’s even more amazing when you consider that all of the 3D was done by hand, the result of meticulous calculations and low-level pixel commands. When Bruce Atwick tackled the early versions of Flight Simulator, not only were there no 3D frameworks, but there were no frameworks at all! Those versions of the game were mostly written in assembly, just a single step away from ones and zeroes that flow through a CPU.

When we set out to reimagine Flight Simulator for the web, we couldn’t help but think about the contrast of creating 3D then and now. Modern coding seems almost luxurious as we sculpt 3D worlds in WebGL with great frameworks like Babylon.js. It lets us focus on very high level problems. In this article, we’ll share our approach to one of these fun challenges: a simple way to create realistic looking large scale terrain.

Modeling and 3D Terrain

Most 3D objects are created with modeling tools and for good reason. Creating complex objects (like an airplane or even a building) is hard to do in code. Modeling tools almost always make sense, but there are exceptions! One of those might be cases like the rolling hills of the Flight Arcade island. We ended up using a technique that we found to be simpler and possibly even more intuitive: a heightmap.

A heightmap is a way to use a regular two-dimensional image to describe the elevation relief of a surface like an island or other terrain. It’s a pretty common way to work with elevation data, not only in games but also in geographic information systems (GIS) used by cartographers and geologists.

To help you get an idea for how this works, check out the interactive heightmap below. Try drawing in the image editor and then check out the resulting terrain.

Texture:
30

The concept behind a heightmap is pretty straightforward. In an image like the one above, pure black is the “floor” and pure white is the tallest peak. The grayscale colors in-between represent corresponding elevations. This gives us 256 levels of elevation which is plenty of detail for our game. Real-life applications might use the full color spectrum to store significantly more levels of detail (2564 = 4,294,967,296 levels of detail if you include an alpha channel).

A heightmap has a few advantages over a traditional polygonal mesh:

First, heightmaps are a lot more compact. Only the most signficant data (the elevation) gets stored. It will need to be turned into a 3D object programatically, but this is the classic trade: you save space now and pay later with computation. By storing the data as an image you get another space advantageL you can leverage standard image compression techniques and make the data tiny (by comparison)!

Second, heightmaps are a convenient way to generate, visualize and edit terrain. It's pretty intuitive when you see one. It feels a little like looking at a map. This proved to be particularly useful for Flight Arcade. We designed and edited our island right in Photoshop! This made it very simple to make small adjustments as needed. When, for example, we wanted to make sure that the runway was completely flat, we just made sure to paint over that area in a single color.

You can see the heightmap for Flight Arcade below. See if you can spot the “flat” areas we created for the runway and the village.

The heightmap for the Flight Arcade island. It was created in Photoshop and it's based on the "big island" in a famous Pacific Ocean island chain. Any guesses?
A texture that gets mapped onto the resulting 3D mesh after the heightmap is decoded. More on that below.

Decoding the Heightmap

We built Flight Arcade with Babylon.js and Babylon gave us a pretty straightforward path from heightmap to 3D. Babylon provides an API to generate a mesh geometry from a heightmap image:


var ground = BABYLON.Mesh.CreateGroundFromHeightMap(
    'your-mesh-name',
    '/path/to/heightmap.png',
    100, // width of the ground mesh (x axis)
    100, // depth of the ground mesh (z axis)
    40,  // number of subdivisions
    0,   // min height
    50,  // max height
    scene,
    false, // updateable?
    null // callback when mesh is ready
);

The amount of detail is determined byt that subdividisons propety and it's important to note that the parameter refers to the number of subdivisions on each side of the heighmap image, not the total number of cells. So increasing this number slightly can have a big effect on the total number of vertices in your mesh.

20 subdivisons=400 cells
50 subdivisons=2,500 cells
100 subdivisons=10,000 cells
500 subdivisons=250,000 cells
1000 subdivisons=1,000,000 cells

In the next section we'll learn how to texture the ground, but when experimenting with heightmap creation, it's useful to see the wireframe. Here is the code to apply a simple wireframe texture so its easy to see how the heightmap data is converted into the vertices of our mesh:


// simple wireframe material
var material = new BABYLON.StandardMaterial('ground-material', scene);
material.wireframe = true;
ground.material = material;

Creating Texture Detail

Once we had a model, mapping a texture was relatively straightforward. For Flight Arcade, we simply created a very large image that matched the island in our heightmap. The image gets stretched over the countours of the terrain so the texture and the height map remain correlated. This was really easy to visualize and, once again, all of the production work was done in Photoshop.

The original texture image was created at 4096x4096. That's pretty big! (We eventually reduced reduced the the size by a level to 2048x2048 in order to keep the download reasonable, but all of the development was done with the full size image). Here's a full-pixel sample from the original texture.

A full-pixel sample of the original island texture. The entire town is only around 300px square.

Those rectangles represent the buildings in the town on the island. We quickly noticed a discrepancy in the level of texturing detail that we could achieve between the terrain and the other 3D models. Even with our giant island texture, the difference was distractingly apparent!

To fix this, we “blended” additional detail into the terrain texture in the form of random noise. You can see the before and after below. Notice how the additional noise enhances the apperance of detail in the terrain.

Comparison of the airport rendered with and without the "noise" shader. Notice the discrepancy in detail between the buildings and the ground before the additional noise was added.

We created a custom shader to add the noise. Shaders give you an incredible amount of control over the rendering of a WebGL 3D scene and this is a great example of how a shader can be useful.

A WebGL shader consists of two major pieces: the vertex and fragment shaders. The principal goal of the vertex shader is to map vertices to a position in the rendered frame. The fragment (or pixel) shader controls the resulting color of the pixels.

Shaders are written in a high-level language called GLSL (Graphics Library Shader Language) which resembles c. This code is executed on the GPU. For an in-depth look at how shaders work, see this tutorial on how to create your own custom shader for Babylon.js

The Vertex Shader

We're not changing how our texture is mapped on to the ground mesh, so our vertex shader is quite simple. It just computes the standard mapping and assigns the target location.


precision mediump float;

// Attributes
attribute vec3 position;
attribute vec3 normal;
attribute vec2 uv;

// Uniforms
uniform mat4 worldViewProjection;

// Varying
varying vec4 vPosition;
varying vec3 vNormal;
varying vec2 vUV;

void main() {

    vec4 p = vec4( position, 1.0 );
    vPosition = p;
    vNormal = normal;
    vUV = uv;
    gl_Position = worldViewProjection * p;
}

The Fragment Shader

Our fragment shader is a little more complicated. It combines two different images: the base and blend images. The base image is mapped across the entire ground mesh. In Flight Arcade, this is the color image of the island. The blend image is the small noise image used to give the ground some texture and detail at close distances. The shader combines the values from each image to create a combined texture across the island.

The final lesson in Flight Arcade takes place on a foggy day so the other task our pixel shader has is to adjust the color to simulate fog. The adjustment is based on how far the vertex is from the camera, with distant pixels being more heavily "obscured" by the fog. You'll see this distance calculation in the calcFogFactor function above the main shader code.


#ifdef GL_ES
precision highp float;
#endif

uniform mat4 worldView;
varying vec4 vPosition;
varying vec3 vNormal;
varying vec2 vUV;

// Refs
uniform sampler2D baseSampler;
uniform sampler2D blendSampler;
uniform float blendScaleU;
uniform float blendScaleV;

#define FOGMODE_NONE 0.
#define FOGMODE_EXP 1.
#define FOGMODE_EXP2 2.
#define FOGMODE_LINEAR 3.
#define E 2.71828

uniform vec4 vFogInfos;
uniform vec3 vFogColor;

float calcFogFactor() {

    // gets distance from camera to vertex
    float fogDistance = gl_FragCoord.z / gl_FragCoord.w;

    float fogCoeff = 1.0;
    float fogStart = vFogInfos.y;
    float fogEnd = vFogInfos.z;
    float fogDensity = vFogInfos.w;

    if (FOGMODE_LINEAR == vFogInfos.x) {
        fogCoeff = (fogEnd - fogDistance) / (fogEnd - fogStart);
    }
    else if (FOGMODE_EXP == vFogInfos.x) {
        fogCoeff = 1.0 / pow(E, fogDistance * fogDensity);
    }
    else if (FOGMODE_EXP2 == vFogInfos.x) {
        fogCoeff = 1.0 / pow(E, fogDistance * fogDistance * fogDensity * fogDensity);
    }

    return clamp(fogCoeff, 0.0, 1.0);
}

void main(void) {

    vec4 baseColor = texture2D(baseSampler, vUV);

    vec2 blendUV = vec2(vUV.x * blendScaleU, vUV.y * blendScaleV);
    vec4 blendColor = texture2D(blendSampler, blendUV);

    // multiply type blending mode
    vec4 color = baseColor * blendColor;

    // factor in fog color
    float fog = calcFogFactor();
    color.rgb = fog * color.rgb + (1.0 - fog) * vFogColor;

    gl_FragColor = color;
}

The final piece for our custom Blend shader is the JavaScript code used by Babylon. The primary purpose of this code is to prepare the parameters passed to our vertex and pixel shaders.


function BlendMaterial(name, scene, options) {
    this.name = name;
    this.id = name;

    this.options = options;
    this.blendScaleU = options.blendScaleU || 1;
    this.blendScaleV = options.blendScaleV || 1;

    this._scene = scene;
    scene.materials.push(this);

    var assets = options.assetManager;
    var textureTask = assets.addTextureTask('blend-material-base-task', options.baseImage);
    textureTask.onSuccess = _.bind(function(task) {
        
        this.baseTexture = task.texture;
        this.baseTexture.uScale = 1;
        this.baseTexture.vScale = 1;

        if (options.baseHasAlpha) {
            this.baseTexture.hasAlpha = true;
        }

    }, this);

    textureTask = assets.addTextureTask('blend-material-blend-task', options.blendImage);
    textureTask.onSuccess = _.bind(function(task) {
        this.blendTexture = task.texture;
        this.blendTexture.wrapU = BABYLON.Texture.MIRROR_ADDRESSMODE;
        this.blendTexture.wrapV = BABYLON.Texture.MIRROR_ADDRESSMODE;
    }, this);
    
}

BlendMaterial.prototype = Object.create(BABYLON.Material.prototype);

BlendMaterial.prototype.needAlphaBlending = function () {
    return (this.options.baseHasAlpha === true);
};

BlendMaterial.prototype.needAlphaTesting = function () {
    return false;
};

BlendMaterial.prototype.isReady = function (mesh) {
    var engine = this._scene.getEngine();

    // make sure textures are ready
    if (!this.baseTexture || !this.blendTexture) {
        return false;
    }

    if (!this._effect) {
        this._effect = engine.createEffect(

            // shader name
            "blend",

            // attributes describing topology of vertices
            [ "position", "normal", "uv" ],

            // uniforms (external variables) defined by the shaders
            [ "worldViewProjection", "world", "blendScaleU", "blendScaleV", "vFogInfos", "vFogColor" ],

            // samplers (objects used to read textures)
            [ "baseSampler", "blendSampler" ],

            // optional define string
            "");
    }

    if (!this._effect.isReady()) {
        return false;
    }

    return true;
};

BlendMaterial.prototype.bind = function (world, mesh) {

    var scene = this._scene;
    this._effect.setFloat4("vFogInfos", scene.fogMode, scene.fogStart, scene.fogEnd, scene.fogDensity);
    this._effect.setColor3("vFogColor", scene.fogColor);

    this._effect.setMatrix("world", world);
    this._effect.setMatrix("worldViewProjection", world.multiply(scene.getTransformMatrix()));

    // Textures
    this._effect.setTexture("baseSampler", this.baseTexture);
    this._effect.setTexture("blendSampler", this.blendTexture);

    this._effect.setFloat("blendScaleU", this.blendScaleU);
    this._effect.setFloat("blendScaleV", this.blendScaleV);
};

BlendMaterial.prototype.dispose = function () {

    if (this.baseTexture) {
        this.baseTexture.dispose();
    }

    if (this.blendTexture) {
        this.blendTexture.dispose();
    }

    this.baseDispose();
};

Babylon.js makes it easy to create a custom shader-based material. Our Blend material is relatively simple, but it really made a big difference in the appearance of the island when the plane flew low over the ground. Shaders bring the power of the GPU to the browser, expanding the types of creative effects you can apply to your 3D scenes. In our case, that was the finishing touch!

Flight Arcade runs best on screens that are a little bigger

(The minimum recommended size is at least 800px by 600px)

Got it!