Site hosted by Angelfire.com: Build your free website today!
[news] [about us] [games] [current projects] [contact]

The terrain texture synthesis algorithm

Starting with v0.5, the terrain engine features a brand-new texture synthesis algorithm that should be pretty flexible. It's not difficult to set up the texturing, though it often requires some tweaking to really look good. This page serves to explain to you how to use this new feature and also a bit of the inner workings of the algorithm. If you have trouble understanding my explanations, (which wouldn't surprise me at all,) there is an example at the end of the page. Enough talk, let's get to the goods.

The map definition file commands

First you need to tell the engine how many base textures you want to use in your landscape. You can think of every base texture as a kind of terrain, like grass, rock, snow and so on. To do this, you put the following line into the map definition file (.MAP):

numtextures	x

The 'x' should obviously be replaced by the number of textures you are intending to use. After that, you are ready to give the engine information about which textures actually to use and where to place them. Each base texture is specified in one line using the following format:

tex.i	"texfilename height_inf slope_inf minheight maxheight heightfalloff minslope maxslope slopefalloff"

Where:

The inner workings of the algorithm

For every texel in the terrain texture, the algorithm performs these steps:

1. Step through all base textures, computing weight for this texture as follows:
  a) Compute height factor: If the texture is in the specified height range, this
     is 1, else fade to zero according to the falloff value. Then the factor is
     multiplied by the height_inf value.
  b) Compute slope factor in the same manner.
  c) Add those two factors together.
  d) Clamp the value to the range [0;1].
  d) Finally, square the resulting value to increase contrast a bit.
2. Get the sum of all the texture weights and divide them by it so that the
   weights sum up to 1.
3. Use the weights to blend the base textures together.
4. Use the light direction vector and the terrain normal to light the resulting
   texel.

Tracking down problems

Sometimes the engine has stepped through all base textures but found no single one that had any influence on a given texel. In such a case it colors this point with an annoying purple color to point this problem out to you and give you the possibility to correct it.

Example

Here is a short example of how to use all this.

numtextures 4
// nr  texfile                     inf      height       slope
tex.1 "gfx\\tex\\grass2_large.pcx .5 .5   40  150 50    0  10 10"
tex.2 "gfx\\tex\\rock_large.pcx   .5 .5   150 200 100   20 90 10"
tex.3 "gfx\\tex\\mud.png          .4 .6   0   40  5     0  3  0"
tex.4 "gfx\\tex\\snow.png         1  0    200 255 30    0  0  0"

Note how the base textures are split up into the available height space so that mud will occupy the lowest regions, grass will be placed a bit higher, rock above it and snow on the very top. Also, the slope influence factor for mud was increased a bit to have it in only the flattest areas (0-3 degrees, no falloff). Snow, on the other hand, is placed solely according to height, namely in the highest region from 200-255, with a bit of falloff to smooth it out. You can see that its slope factors were set to 0 since they won't matter anyway. Grass and rock overlap a lot through their large height falloff values, however their slope values let grass be placed in the flatter regions while rock dominates in steeper areas.

That's all folks

Well, that was my quick explanation of the texture synthesis algorithm I use in my terrain engine. If something is still unclear, you want to know more about how it's implemented or you feel like commenting on something, feel free to mail me!