0
votes

I have a quick question for those who know XNA and Color theory in computer graphics well. I am making a tile game (a bit like Terraria), and I was wondering how to go about applying per-tile lighting to the scene. Colored lighting is a must for this game, and right now I got about that by having 4 2D byte arrays, storing overall light and the 3 different color amounts. When the tiles are drawn, it takes the texture of the specific tile its at, but how would I go about applying the color at the same tile based on the 3 color values and overall lighting?

Right now, this is my Color generating function:

public Color GetColorAt(int x, int y)
{
    byte r = _rMap[x, y]; //Red light
    byte g = _gMap[x, y]; //Green light
    byte b = _bMap[x, y]; //Blue light
    byte mul = _lightMap[x, y]; ///Overall light

    return new Color(r * mul, g * mul, b * mul, 1);
}

Being new at color theory, I have no idea if I am going about this in the right way at all. My lighting system uses a cellular algorithm, decreasing and spreading out to all adjacent tiles. If it decreases the color values each tile, do I even need the overall light value to begin with?

Also, when it comes to blending the tile textures and light to get the result, do I use addition, or multiplication, or some other calculation?

TL;DR: 1. Does the code block above work, considering the light decreases between each tile? 2. How do I combine light and textures to get a texture that looks like its under the color of light?

Thanks for any help, I have no experience with Color combining.

1
Which research have you done prior to asking this question? - autistic

1 Answers

0
votes

You probably don't need the overall/mul value - R, G and B are enough to represent light colors and intensities.

You're using "byte", but it might be easier mathematically to use "float" values that range from 0.0 to 1.0.

In the float representation, R=1, G=1, B=1 is white, R=0.5, G=0.5, B=0.5 is medium gray, R=0, G=0, B=0 is black, etc. Then you can just multiply colors directly, without having to fuss with converting to and from a 0..255 byte range.

When you're drawing your tiles, you can use SpriteBatch's color parameter - basically set it to your light color and it should do the color multiplication automatically when it draws the tile. If you want lighting cells smaller than your tiles (like Terraria) you'd probably need to write a custom shader, which is more involved -but useful to know if you're up for some studying.