Skip to content

Hills, Mountains and Perlin Noise in Train3d

The terrain system in Train3D is based on tiles. Each tile can be a specific height and the top of the tile can take different shapes base on if any of the four corners is raised or lowered. This system provides a map that can display smoothly scrolling hills and valleys but also cliffs.

One of the tools available when adjusting the world is the hill tool. To build this tool, I started in a Google Sheet with some formulas to calculate the height of a sphere at different x and y coordinates. Here are some examples:

The Hill tool first starts by saving the existing height values of all the tiles out to the maximum radius of the tool. This lets the user cancel the operation if they don't like what they see and the land will return to the original state. The user click-drags the mouse which starts the process. The mouse moves up or down and after a specific threshold is passed, the radius of the operation is increased by 1. The tool calculates the new height for a sphere for each impacted tile and adds it the original tile height. This lets you build up hills upon hills for more natural terrain. Dragging down is also supported so you can dig holes or make river channels.

I also created a mountain tool for more dramatic landscapes. The original mountain tool used a random function to generate a bunch of heights but it didn't look good at all. It also looked really bad because the random was called every time you moved to a new radius meaning everything just randomly changed. The second iteration, shown below, is just a modified hill tool which the heights are scaled up so things grow taller faster.

Today I upgraded the mountain tool with a more natural feel. At the start of a mountain operation, I use the same Perlin Noise function that generates the terrain to generate a new terrain on a much smaller scale. Unity3D has a builtin Perlin Noise function that makes this easy. With the new terrain heights generated on a small scale, I then scale them down based on the hill tool height. This means the mountain terrain in the middle should be higher and gently slope towards the edges but also have a more natural feel from the Perlin Noise. All of this gets added to the starting terrain height to produce something like this:

One random mountain blob doesn't really look spectacular like I wanted. However, if you click the tool a couple of times and maybe drag it down once or twice if you hit the height limit, you end up with a much better mountain range:

The combination of the hill and mountain tools can create some interesting landscapes but still gives the editor complete control over the land. If you're really picky, you can grab the individual tile editor tool and tweak each tile as you need for your scenario.