4

I have made a color, decent, recursive, fast tile lighting system in my game. It does everything I need except one thing: different colors are not blended at all:

Light Colors Not Blending

Here is my color blend code:

return (new Color(
       (byte)MathHelper.Clamp(color.R / factor, 0, 255),
       (byte)MathHelper.Clamp(color.G / factor, 0, 255),
       (byte)MathHelper.Clamp(color.B / factor, 0, 255)));

As you can see it does not take the already in place color into account. color is the color of the previous light, which is weakened by the above code by factor. If I wanted to blend using the color already in place, I would use the variable blend.

Here is an example of a blend that I tried that failed, using blend:

return (new Color(
       (byte)MathHelper.Clamp(((color.R + blend.R) / 2) / factor, 0, 255),
       (byte)MathHelper.Clamp(((color.G + blend.G) / 2) / factor, 0, 255),
       (byte)MathHelper.Clamp(((color.B + blend.B) / 2) / factor, 0, 255)));

This color blend produces inaccurate and strange results. I need a blend that is accurate, like the first example, that blends the two colors together.

What is the best way to do this?

ben
  • 133
  • 13
  • May want to try here: http://gamedev.stackexchange.com/ – Inisheer Jun 07 '14 at 02:20
  • Try calling your lighting code for red, green, and blue light, then combine them, not all at once. – Cyral Jun 07 '14 at 02:24
  • @Cyral Wouldn't that produce the same result, though? – ben Jun 07 '14 at 05:50
  • Haven't done/try this, but maybe you could use transparency on the places where you want to blend, that way two colors will be shown at once... BTW, your image caught my attention for the game :o (and made me want to go back to XNA once again). – Nahuel Ianni Jun 07 '14 at 08:34
  • It should be enough to just add the colors together. It looks like you are already avoiding byte overflow with the `Clamp` calls. Try not dividing by 2 or by factor and see what you get. – Dave Cousineau Jun 07 '14 at 20:23
  • "This color blend produces inaccurate and strange results." Want to share an image of that? Your second example looks like how I'd handle it. – Mason11987 Jun 09 '14 at 18:54

1 Answers1

0

I think you might have to do something like this:

return (new Color(
   (byte)MathHelper.Clamp(((color.R / factor_1 + blend.R / factor_2)), 0, 255),
   (byte)MathHelper.Clamp(((color.G / factor_1 + blend.G / factor_2)), 0, 255),
   (byte)MathHelper.Clamp(((color.B / factor_1 + blend.B / factor_2)), 0, 255)));

you see, if you add for example red (255,0,0) and green (0,255,0), you should have yellow (255,255,0), right? With the code that you're using, you'd have dark yellow (128, 128, 0). I think the above code might work, but you need the variable factor_2, the factor of your first light. I tried this manualy and it looked like this. (I am not allowed to upload images with no reputation :[ )

ytbah
  • 1
  • 2