Skip to content

Instantly share code, notes, and snippets.

@AncientPixel
Created April 19, 2013 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AncientPixel/5419484 to your computer and use it in GitHub Desktop.
Save AncientPixel/5419484 to your computer and use it in GitHub Desktop.
Method to use the populated region list to colour our texture via height map values.
void ColourIt()
{
// COLOUR!
// Colour in elevations
// _stride - is the size in bytes of the width of the texture RGBA = 4bytes * width of texture.
// _textureSize - size (w & h) in pixels of the texture.
for (int y = 0; y < _textureSize; y++)
{
for (int x = 0; x < _stride; x+=_bytesPerPixel)
{
double value = (double)_heightmapBuffer[index + x];
for (int r = 0; r < _regions.Count; r++)
{
if (value >= _regions[r].range[0] && value < _regions[r].range[1])
{
double top = _regions[r].range[1] - _regions[r].range[0];
// A little formula to shift the colour so we get a nice little ramp.
double shift = ((1.0 - ((value - _regions[r].range[0]) / top)) * 20.0);
double B = _regions[r].baseColor.B - shift;
double G = _regions[r].baseColor.G - shift;
double R = _regions[r].baseColor.R - shift;
// Clamp the values.
if (B < 0) B = 0;
else if (B > 255) B = 255;
if (G < 0) G = 0;
else if (G > 255) G = 255;
if (R < 0) R = 0;
else if (R > 255) R = 255;
// Add colour to the destination texture.
_colourBuffer[index + x + 0] = (byte)B;
_colourBuffer[index + x + 1] = (byte)G;
_colourBuffer[index + x + 2] = (byte)R;
_colourBuffer[index + x + 3] = 255;
}
}
}
index += _stride;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment