# Material to draw lines at set intervals (contour lines on a map, for example)

veteran polycounter
Online / Send Message
veteran polycounter
Here's a handy material that's helped me out a lot so I figured I'd share with the community. I didn't invent this- @almighty_gir gave me a hand and was kind enough to take some time to explain how it works.

The problem: I need to create accurate topographic lines from a 3d terrain, which are later used to create a map like this:

Accuracy is key here since the map does have to function along with a compass. If the map isn't accurate people will die, so let's not fuck around, okay?

This is a problem I had already solved actually, but my way of solving it involved a lot of labor. What I did was make terraces in world machine, then selected by slopes and colored it that way. But there was no way to input precise distances so I had to do it all by eye which took a lot of back and forth before I was able to get 10 meter increments.

Another way was to export the terrain as a mesh and then use grids in 3d software to assign separate materials from a side ortho view. This was perfectly accurate, but involved moving tons of heavy meshes and took days.

I knew a shader would be a faster and more flexible approach, I just don't know how to them. Since I needed to do this job again I figured I'd get someone with a more evolved brain than I got to help.

Math to the rescue:

There are two main parameters I input. Line Distance and Line Thickness. You only need contour lines every 10 meters for a standard military map, but having every 100 meter line slightly bolder helps for map legibility, hence two separate functions.

The saturate node ensures that when adding these two results together we don't exceed a value of 1.

Inside the material function:

First we grab the world position. Isolate the Z axis only. FMOD gets the remainder of dividing the Z position by the Line Distance number.

We get the distance from zero - the absolute value.

The If statement is kind of confusing to me, so I have to take numbers through it one at a time to understand.

Basically we compare the line interval (A) against the Line Thickness (B).
We have two numbers, 0 and 1 - white and black.
The comparisons determine how we draw the two colors.

Here is the result. Obviously to get clean lines you have to up-res the terrain a bit. To get the lines for use with a 2d map just take a screenshot from Top ortho camera, then go to town in photoshop.

Even if your players aren't orienteering with a compass in your game, having accurate topo lines on your world map could lend a sense of authenticity, especially if you're making a military themed game. Try it out.