# Defining lines from a heightmap

To a pixel shader of a 2D game, I'm passing a 1-row heightmap that holds the height in UV coordinates of evenly distributed points throughout the texture I'm drawing, but as for n points there will obviously be n-1 lines connecting consecutive ones, I can't make these lines as wide as they should be. For example, filling the pixels below the lines of a heightmap that holds 3 values ({0f, 1f, .5f}) should result in something like:

but instead, it looks like this:

It is visible that the correct result lies on the first 2/3 of the second one, and the last 1/3 comes from clamping the heightmap.

Here is a simpler version of the pixel shader code.

float4 PixelShaderFunction(float4 texCoords:TEXCOORD0):COLOR0
{
float verticesCount = 3;
float verticesFrequency = 1/verticesCount;

float lastHeight = tex2D(heightMapSampler, float2(texCoords.x, 0));
float nextHeight = tex2D(heightMapSampler, float2(texCoords.x + verticesFrequency, 0));

float delta = (texCoords.x % verticesFrequency)/verticesFrequency;
float height = lerp(lastHeight, nextHeight, delta);
if(texCoords.y > height)
return float4(1, 0, 0, 1);
return float4(0, 0, 0, 1);
}


I tried multiplying the texture coordinates by (verticesCount-1)/verticesCount but that didn't seem to do the job. Thank you!

• Multiplying texCoords.x by (verticesCount-1)/verticesCount at the top should really do the trick, I'd think. What goes wrong for you in that case? Can you post a screenshot of the result? – Nathan Reed Jun 14 '13 at 5:28
• I tried multiplying the texture coordinates when sampling of lastHeight and nextHeight, only after reading your comment I noticed I could change the texCoords values, and that did work! You should post it as an answer, because it is correct. – V.M. Jun 14 '13 at 14:51

Multiplying texCoords.x by (verticesCount-1)/verticesCount at the top should do the trick.

In this line verticesFrequency becomes equal to one third

float verticesFrequency = 1/verticesCount;


On this line you are adding one third to the texture coordinate

float nextHeight = tex2D(heightMapSampler, float2(texCoords.x + verticesFrequency, 0));


So for the first pixel you are sampling texture coordinate [0.333, 0] but you want to be sampling [0.5, 0] i.e. the middle of the three coordinates.

Perhaps you should use verticesCount - 1