Created
April 19, 2013 10:27
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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