Home

Flexible Heightfields

Friday, May 16th, 2008 | Author:

If you’ve ever tried to stretch an imported terrain, i.e. a heightfield, over the whole planet like in TG0.9, you’ve probably run into some problems because heightfields don’t follow the curvature of the planet.

So, what exactly is the problem here? If you import a heightfield and scale it up you get the following result:

hf-prob-01.jpg

Not only doesn’t the terrain follow the planet’s curvature at all – to make things worse we get a conflict with the surface system, which does respect the spherical shape of the planet. (Strictly speaking, you can avoid this by activating “Use Y for altitude” for each surface layer)

One thing you could try is disabling “Flatten surface first”. This option can be found in the displacement tab of the heightfield shader:

hf-prob-02.jpg

Problem solved.. right? In some cases, this can be a sufficient solution indeed.

But there are still some problems to be solved. We get a heightfield that is heavily distorted and has a lower resolution near the corners. Another issue: The displacement of the heightfield shader always happens along the Y-axis, strictly upwards.

hf-prob-031.jpg

You can see it here. The terrain is being displaced along the Y-axis instead of the planet’s normal.

Now these things don’t seem too bad, but if we stretch the heightfield even more , for example to cover half of the planet, we will run into problems.

The Solution

What we’re going to do is take a camera, an Image map shader (IMS) and project our terrain on the planet’s surface from the inside!

camplanet.jpg

To do that we’ll simply use the IMS’ displacement option. This method has several advantages over the usual approach:

  • Displacement happens perpendicular to the planet’s surface
  • Less distortions – we’re using the camera for projection
  • If we need more control over the displacement, we can connect the output of the IMS to a Displacement shader and use the additional features it provides.
  • The heightfield can now be rotated by rotating the camera

But there’s a small downside too: The heightfield is mirrored because we’re projecting it from inside the planet.

Till now, we just assumed it would work that way… but does the IMS even load terrain files? Yes, it does… but we need a trick to make it work.

The Image map shader can only load images, not heightfields. So if we have a terrain in the .ter format, it’s basically useless. Most terrain tools support other formats as well. The main difference between them is their precision/color-depth (8 bit/ 16 bit) and in how well they’re supported by other programs.

Preparation and Technical Background

.ter terrains have 16 bits. Saving it as a bitmap (.bmp) or another image format would be no problem, but most of them use only 8 bits per channel (3 color channels * 8 bits = 24 bits ). What we need is a format that uses all bits to store lightness and it should use at least 16 bits. That way, they can store height information with a over 200 times greater precision compared to 8bit formats. The following example, with intentionally ugly settings, shows the difference:

There’s always a large difference between two pixels when using the 8bit image. The 16bit format on the other hand can reproduce even very small differences in height.

One such format with a “color”-depth of 16 bits is TIFF (.tif or .tiff), PNG is another one. Both can also be used to store 3 * 8bit color images, so make sure your terrain editor uses the 16bit variation (which it will do normally).

Unfortunately, TG2 doesn’t accept these formats either:

Please convert image to SGI for non-standard bit depths.

You probably don’t know the SGI format. Fortunately, there are converters for SGI on the web. Again, be sure to get one that saves files in 16bits. If you own some version of Photoshop, you’re lucky: Toby Thain provides a free open source SGI format plugin on his website! It lets you open and save files in the SGI format and compresses them if you want.

Implementation in TG2

Now we have our SGI file, we can finally get started! If you haven’t done so yet, add an Image map shader. You can do this in the terrain tab by clicking Add Terrain > Displacement Shader > Image map shader. The old Heightfield shader can be deleted afterwards.

We need a camera for the projection. A new camera can be created by right-clicking somewhere in the network and choosing Create Other > Camera. We’ll plug it into the middle port of the IMS.

Then we activate “Apply displacement” in the displacement tab of the IMS and enter one million as the Displacement amplitude. That’s unrealistic but it helps us see the results of our work. We also disable Unpremultiply, enable Data is linear in the color tab and enter the desired border blending value (e.g. the default value from the Heightfield shader: 0.1).

We now have to change the camera’s settings so it sits at the center of the planet, facing upward. To do this we need to open the options of the planet and the camera. (use Stay Open if needed)

The position of the planet’s center can be copied. The most important is the Y value. The other ones should be zero. The orientation of the camera should be changed to 90; 0; 0 so it points upwards. Of course you can use your own values too, depending on where you want the terrain to be.

The horizontal fov is the angle that’s covered by the camera and thus covered by our terrain.

Done!

By now, that whole thing should look something like this:

You can clearly see that the displacement is now happening perpendicular to the planet’s surface (blue).

Upside-down

One problem is still left: The heightfield is upside-down. This can be fixed using the “Flip x/y” function of the IMS. Enabling one of the two options should do it. Alternatively, you can flip it in an external editor – right before saving it as SGI in Photoshop, for example.

Conclusion

There’s a lot you can do by using this method, rotating heightfields for example.. or make a heightfield repeat around the planet.¬†

Have fun! :)

Tags »

Trackback: Trackback-URL | Comments Feed: RSS 2.0
Category: Advanced, How to..

You can leave a response.

3 Responses

  1. 1
    Rob 

    Thank you!

  2. 2
    Simon 

    Very interesting technique, not something I would have thought of. Thanks.

  3. 3
    Evan 

    Thanks! I used this tutorial to create realistic continents around a planet, and it works perfectly!

Leave a Reply