Understanding Heightmaps and how to paint them in Photoshop
by [EA]Lawrence Brown, BF2 Community manager
If you’ve tried painting a custom terrain map, you’ve probably discovered that the results don’t always match your expectations. This tutorial is an explanation of how to create heightmaps using Photoshop in such a way that you can accurately calculate the resultant in-game height at any given point.
You will of course need the editor, and either Photoshop or another image painting program. If you are not using Photoshop, you should still be able to get the basic concepts. You will just have to adopt them to whatever program you are using.
In addition you should be familiar with the basics of how to use the editor, including creating and editing terrains and levels.
Creating a blank heightmap
We’re actually going to start out by letting the editor create a heightmap to use as a basis for this study.
- Launch the editor.
- When the “Select Startup MOD” window appears, choose any one you’ve been working in. It doesn’t matter since we are just interested in the map. As usual, I will choose “MyMod”.
- When the mod loads, switch to TerrainEditor mode.
- Now click on the “new” icon or choose “File>New” from the menu.
This will bring up the “Create new terrain” window. Let’s take a look at the different values you can set here:
Image File: HMimage001.jpg
Obviously this is where you name your level. Note that you have to use underscores instead of spaces.
This is the initial size of your map in meters. All maps are square, so 256 is 256x256 meters and so on.
This is new to BF2. If you set the Size of your map to 256 and Scale to 1, then your map will be 256x256 meters. If you set the Size to 256 and the Scale to 2, then your map will actually be 512x512 meters. Your map grid still contains the same number of squares. The only difference is that each grid square now covers 2 meters instead of one. The other setting for scale is 4, which would make the size of your map 1024x1024. You can see the resulting map size in the info window directly below that is labeled “Level size:”.
Note that all the scale setting does it change the size of the grid squares. It doesn’t add more squares. This can be useful for such things as creating a very small map where you want the terrain to have more detail covering a small area or creating a large map for things like aircraft where the area you can fly in is larger, but the terrain has less detail per meter.
also note that changing the scale setting can affect performance. Leave this at it’s default value unless you really need to change it, and if you do, make sure that you perform thorough testing over the internet with the maximum amount of players to be sure it’s not going to drag down slower systems.
When you first create a new map, the terrain will be a flat plane. This lets you set the initial height of that plane. This can come in handy, for instance, if you know that most of your map is going to be a certain height above “sea level”.
The short answer: This is the size of each of the secondary terrain meshes.
The long answer: This is also a new feature in BF2. Instead of your terrain just wrapping around at the edges, in BF2 there are 8 “secondary meshes” that surround the main mesh of your map. The purpose of this is to provide the game with additional terrain that you can see, but never actually get to.
The number in the Low-Res size field works the same as the first “Size” field. Setting it to 128 means that each of the 8 meshes that surround your main mesh will be 128x128 units square. The editor will take care of matching the surrounding meshes to the main mesh. For the most part, you sculpt and paint these surrounding meshes the same way as your main terrain.
Note: The size of these meshes also greatly affects performance. You should always use the lowest number you can get away with and still look good.
This is another info field that tells you the ratio of your main mesh to the surrounding mesh.
Example: if set “Size” to 256, “Scale” to 1, and “Low-Res size” to 256, then your scale will be 1 to 1. If you now change “Low-Res size” to 128, the ratio becomes 1 to 2. In other words, your surrounding terrain mesh has ½ the number of grid squares as the main mesh.
To make things more complicated, if you now change “Scale” to 2 and leave “Low-Res size” at 128, your Low-Res Scale readout now says 4, meaning ¼. At this point the ratio might not strictly make sense, but you don’t really need to worry about it. The info is just a guideline. As stated earlier, just try to use the smallest Low-Res size you can that looks good.
For the purposes of this tutorial, we are only interested in the main mesh, so:
- set everything according to the image above
- click “Okay” to create the new map.
Once the level is created, notice the two tabs in the Tweaker Bar. The first one will say “Tweak”, and the second one will be the name of your level. This is where you make a lot of the basic terrain settings.
- Click on the second tab.
- Expand the menu section labeled “LevelSettings” by clicking on the icon to the left of the name that looks like a plus sign in a white box.
- Find the field labeled “TerrainHeight” and change the number to 100:
Image File: HMimage002.jpg
The number you type into the “TerrainHeight” field sets the highest your heightmap can possibly go. When you set it to 100, this means that the tallest your mountains can be is 100 meters.
It is important to be aware of this number because a lot of other things are based on this number. You can set it to anything you want,
- but for now set it to 100 because we have to do some calculations based on it, and this way makes it simpler.
- choose “File>Save All” from the menu bar. In the windows that pop up, just leave everything at their defaults
- click “Yes” to save your level.
- You can now close the editor.
Painting the Heightmap:
When we created the map, a default Heightmap was created. We’re now going to make some changes to it and examine how it works. First let’s take a look at a few of the files that were created.
- Open the folder of the mod you created the map in.
If you created the map for normal BF2, the path will be “C:\Program Files\EA G AMES\Battlefield 2\mods\bf2” by default.
- Inside the mod folder, double-click on the folder labeled “Levels” to open it.
- In the levels folder, double-click again on the folder with the name of your level to open it.
You should now see something like this:
“HeightmapPrimary.raw” is the file I’ve drawn an arrow next to and is the one we’ll be working with in a minute. Also notice the files directly below it that are labeled “HeightmapSecondary_D1”, “…_L1”, “…L1D1”, and so on. These are the heightmaps for the surrounding terrain that we talked about earlier and they work the same way as the primary Heightmap.
The letters U, D, L, and R stand for Up, Down, Left, and Right. Normally you would just sculpt these secondary meshes in the editor, but if you want to try editing them by hand, something to remember is that Heightmaps are always read by the game from bottom to top, so if you are in the editor looking at the North side of your map, the corresponding Heightmap is actually “HeightmapSecondary_D1.raw”. It’s just one of the many quirks you need to get used to.
Now let’s move on to actually doing something with our primary Heightmap.
- Launch Photoshop choose “File>Open”.
- Navigate to the “Heightmap.raw” file we just looked at and open it.
You should now see a window labeled “Photoshop Raw Options.” Set all options as follows:
Image File: HMimage004.jpg
If your map size was 256 when you created it, the Width and Height should be set to 257. Make sure you match all the rest of the options exactly or your image won’t open correctly. Don’t click on okay yet.
While we have this window open, I should explain about the .raw format.
“.raw” files are nothing more than raw data, hence the extension. They aren’t necessarily image files. They are just a long string of data that can be produced by any program. It would be kind of like if you typed an e mail and used no spaces or punctuation. It would just be one long string of letters. The program that’s opening a .raw file needs to know how to interpret that data. This is why we have to manually fill in a lot of the values in this window. Width and Height are the dimensions of the image. In the editor we set the map size to 256 square. That number refers to the number of squares in the terrain grid. In this window we have to set the number to 257 because this time we are referring to the number of vertices that make up those grid squares. You need 257 points to make up one edge of 256 squares. If the map size was 512, you would set this number to 513, and so on. In the “Channels” section, we set the Count to 1, Depth to 16 Bits, and Byte Order to “ IBM PC”. These are just more images settings: Since it’s grayscale, the Count is 1, the grayscale format is 16 Bits, so that’s the Depth, and of course we’re working in Windows, so the Byte Order is IBM PC. You don’t need to remember what all this means. Just make sure they are set the same every time. Lastly, make sure that “Size” in the “Header” section is set to 0. Some programs put a label at the beginning of the file called a “Header”. The program opening this needs to know how long that label is. BF2 doesn’t use this, so we set it to 0.
- When you’ve finished with all the settings, click “Okay”. If everything went smoothly, you should now see this:
It’s pretty boring right now. That’s because the default terrain that the editor created is flat, so there are no details to see yet.
This is the part where it gets a bit tricky. If we want to be able to paint a Heightmap with any kind of accuracy, then we need to go through a bit of theory and some calculations. It’s not too difficult if we just take it one step at a time. First the theory:
A heightmap is nothing more than a gray scale image. Each pixel of the image represents one point, or vertex, in the terrain mesh. The height of each vertex is determined by the value of its corresponding pixel in the image. In other words, the “whiter” the pixel, the higher the terrain at that point.
Remember the “TerrainHeight” value we set to 100? What this means is that pure white in the heightmap image will cause the terrain to be 100 meters tall at the corresponding points in the game. Anything that is pure black in the image will be at 0 meters in the game. (The editor also calls this “sea level”.)
The relationship between the color of the image and the terrain can be seen in this diagram:
Here’s where the calculation part comes in. What happens if we were to change the “TerrainHeight” value in the editor from 100 to 200? The grayscale values stay the same, but since the highest point on the terrain is now 200, pure black is still 0 meters, but pure white becomes 200 meters.
Since the height of the terrain will change depending on what the “TerrainHeight” value is, you have to treat the values of the grayscale image as percentages, not absolute numbers.
The tricky part is that because of the way Photoshop works, you are painting in percentages of black, not white. (more on why in a minute) This means that 100% black is 0 meters and 0% black is the top. Here is the previous example again with black percentages applied:
Image File: HMimage007.jpg
Let’s apply this theory to our Heightmap.
In Photoshop, with the “HeightmapPrimary.raw” file still open, switch to brush mode. Choose a hard-edged brush (one of the first two from the brushes palette) and enlarge the brush to about 40 pixels. Now locate the “Color” tab. If it’s not visible, you may have to choose “Window>Color” from the menu bar. In the Color tab, there is a slider. On the left is the label “K” (More about this in a bit) and to the right of the slider is a percentage box with a number in it. Slide the slider all the way to the left or enter 0% into the box. Place a dot near the top center of the image. It should look like the following image. Note that I’ve highlighted the color window and the percentage input:
Image File: HMimage008.jpg
Back in the color window, change the percentage to 25% and place a second dot just below the first one. Continue to do this for 50%, 75%, and 100%. Notice how the color gets darker as the percentage goes up. When you are finished, your image should look something like this. It doesn’t have to be exact, but you should be able to remember where each different height is:
Image File: HMimage009.jpg
If we use this new image for our Heightmap, White will be the highest point and black will be the lowest.
Before we can save this image, we have to do two more things. Because of the way the game engine reads these files, it will end up upside-down in the game.
- Select “Image>Rotate Canvas>Flip Canvas Vertical” from the menu bar.
Your image should now look like this:
Image File: HMimage010.jpg
The last thing we need to do is apply a tiny bit of blur.
- Select “Filter>Blur> G aussian Blur…” from the menu bar. In the window that pops up, set the “Radius” to 0.5 pixels. And hit “Okay”:
Normally you will want to do this as a last step when saving a heightmap to smooth out the edges just a tiny bit in the terrain. You can experiment with this if you like. A higher radius will make smoother edges.
We are now ready to save our custom Heightmap.
- Choose “File>Save” from the menu bar.
In the “Save As” window that pops up, change the Format to “Photoshop Raw (*.RAW)”. You will also want to be sure you are in the same directory as the original image so that you overwrite it:
- Click “Save”.
A window should pop up asking you if you want to replace the original image.
- Click Okay again, and another window will pop up labeled “Photoshop Raw Options” again.
- This time all you have to do is make sure that “Header” is set to 0 and “Byte order” is set to “ IBM PC”, just like when you opened it.
- Click “Okay” after you have checked the settings.
Yet another window will pop up warning that you will lose things like printer settings. Just ignore this.
- click “Okay” one last time.
Here are those last two windows:
Image File: HMimage013.jpg
We are now ready to go back into the editor and look at what we’ve done.
- Close Photoshop.
- If a window comes up asking if you want to save the image, ignore it and press “no”.
We’ve already saved what we need to.
- Launch the editor again.
- When the “Select Startup Mod” window appears, choose the mod that you saved your level in.
- Switch to the Terrain Editor and load your level. You should now see the changes that you made to your terrain.
You may have to move the camera around to get a good view.
- Choose “Render>Toggle Draw Fog” from the menu bar to turn off the fog.
This way you can see the whole map. With the fog off and no textures painted, your map may go all white, so:
- choose “Render> G rid With Texture Mode”.
Your terrain should now look something like this:
Image File: HMimage014.jpg
It’s time to examine what we’ve done.
- In the Tweaker bar, find the “TerrainHeight” setting again. Make sure the height is still set to 100.
- Now Expand the section labeled “WaterSettings”.
- The first entry in this section is labeled “SeaWaterLevel” and is set to -10000 by default. Change this to 49 and hit enter.
Your terrain should now look like this:
What we’ve done is to change the height of the water to 49 meters. You can now see 3 of the “dots” that we painted onto the terrain.
The tallest one was 0% black and is at 100 meters because that’s the number we set in the “TerrainHeight” setting. The next one is 25% black and so is at 75% of the “TerrainHeight” setting, or 75 meters. (75% of 100 is 75.) The third dot is 50% black, which translates into 50 meters of height. I set the water level to just below this so you could see it.
If I had set the water level to 50 meters and the dot is at 50 meters, it might not render properly in your view. G et the idea?
- Now without changing the height of the water, go back to the “TerrainHeight” setting and change it to 200.
You should see this:
Image File: HMimage016.jpg
Since the Highest point of the terrain is now set to 200 meters, the first dot is now at 200 meters. (The dot on the image is pure white, which is 0% black. This translates into 100% height.) The second dot was painted with 25% black, which translates into 75% maximum height, so it’s now at 150 meters. (You can check this by changing the water height to 149. You will see the dot just above the surface of the water. If you set the water level to 150, it barely disappears.) The third dot used to be at 50 meters. The color on the Heightmap still represents 50%, but since the Terrain Height is now set to 200 meters, this dot has moved up to 100 meters. If your water level is still set to 49 meters, you will see the fourth dot. It was painted at 75% black, which translates to 25% white. With the terrain height set to 200 meters, 25% of that is now 50, so the dot is at 50 meters.
You can paint accurate Heightmaps in Photoshop, but you need to get used to the idea that you are painting percentages, not actual values. The hard part is that the color scale you use to set the color you paint with is based on percentages of black instead of white, so the height is the inverse of that setting. 25% black is 75% height, and so on.
If you like, change the “TerrainHeight” setting to 50. Now change the water height to different numbers and see if you can predict where the dots will be height-wise. Remember to set the height of the water just slightly below where you think the dot will be. If they are both at the same level, you may see the water and not the dot.
The following is a quick explanation of the color slider used in Photoshop. I’m just offering it as extra info for those of you that are curious as to why it’s set up like it is:
First, the “K”:
When Photoshop was first created, it was used mainly by the printing industry for things like magazines, newspapers, and so on. Color printing presses originally used 4 colors: Cyan, Magenta, Yellow, and black, or “C, M, Y, K” for short, so in this case the “K” stands for black. Since computer screens use Red, G reen, and Blue, they couldn’t just use “B” for black. I have no idea who came up with “K”, but there it is.
Now, the inverse percentage:
When you work with an image on your computer screen, it starts out black. As you add color, like when painting in Photoshop, the screen gets brighter. That means that if you paint something with the brightest red you can get, it will be at 100%.
Printing uses just the opposite. You start out with a blank white page, so it’s already as bright as it can possibly be, but the “color” amounts are 0% because you haven’t added anything yet. When you start adding ink (in our case Black), the percentage of color applied goes up, but the brightness goes down. This means that when you’ve applied as much black as possible (100%) the page will be it’s darkest (O% brightness, or pure Black).
The problem is that Photoshop doesn’t have a color slider for White, so when you open up a grayscale .raw image, it uses the Black slider labeled “K”. Unfortunately there is no way to change this, so you just have to get used to the way it works.
Thoroughly confused with the explanation? Don’t worry. All you have to remember is that “K” stands for black and the blacker the pixel, the lower the terrain. Just play around with it a bit. Paint a few maps and note the percentage numbers you are using and what you have your terrain height set to. After a while you will get the idea.