Heightmap Generator
Introduction
When I first started in XNA, I made a program that would take in
heightmaps and create terrain. I had to download a lot of different
heightmaps online, but they only came in certain sizes and some had
poor resolution which made for terrible looking terrain. Plus the
concept of how they were created always intrigued me, so I investigated
the matter myself and wrote a C# program to generate heightmaps based
on Perlin noise. I create the terrain by summing up several 2D Perlin
noise functions using different amplitudes and frequencies. If you go
to my Links page at the top, you'll find some references which do a
great job explaining how Perlin noise works and gives plenty of good
examples on how to implement it.
How to Use It
The whole thing is pretty simple to use. I created two classes: PerlinNoise2D and HeightMap. The Heightmap class is the one you'll be using. I wouldn't mess with the PerlinNoise2D class unless you know what you're doing; there's a lot of tricky code in there and it's not very well documented.
I gave you two ways of creating a heightmap:
- Use
the included UI with it. All you have to do is enter in the full
filename with extension, the width and you're good to go. If you want
to integrate it into your own program, the next two steps discuss the
methods I exposed which you can use.
- Use Heightmap.saveHeightmap().
This will save your heightmap to a RAW file. It asks for the width in
pixels, the height in pixels, how bumpy you want the terrain to be, and
finally the filename. Keep in mind the filename is the whole filename, extension included ( Examples: "C:/test.RAW", "C:/Program Files/HeightMapGen/myheightmap.RAW" ). After you run that method, go to that location and it will spit out your file. My favorite RAW image viewer is IrfanView, which is very powerful tool (did I mention it's 100% free?)
- Use Heightmap.generateByteArray().
Same in the same parameters as saveHeightmap() except no filename this
time. This will return a single-dimension array of bytes, each one
holding a pixel color. You can use this if you want to generate your
heightmap on the fly and not use it in a file.
How to Tweak It
The only function you really need to play around with is the Heightmap.generateByteArray()
method I just mentioned. This is where it creates the different Perlin
noise functions. Modifying what parameters you use for your Perlin
Noise functions will allow you to change what type of terrain it will
generate.
- If you want large, rolling hills, use smaller frequencies and larger amplitudes.
- If you want a really jagged terrain, use higher frequencies and larger amplitudes.
- If you want small hills, use higher frequencies and small amplitudes.
Play around with it! See what results you can get. It's a very flexible system.
How to Make It Better
- Probably
the biggest thing you can do is put smoothing into the Perlin noise
function. Right now I have none of that so your terrain might have some
jagged spots. This is a little more in depth than I wanted to get, but
if you do get this implemented, let me know and I'll post the code up
here and give you credit for it.
- Be able to make non-uniform heightmap sizes. Right now it has to be a square.
I've provided both the XNA project and executable below in case some of
you visiting the site don't have XNA installed. All of the key binds
are displayed for you so it should all be pretty self explanatory. If
you have any questions, email me at
dan@digitseven.com.
DOWNLOAD HeightmapGenerator HEREDOWNLOAD HeightmapGeneratorEXE HERELast Update: 8/26/09