Friday, August 23, 2019

shaders - Math behind XNA's spriteBatch.draw() color parameter?


I'm trying to make a shader that changes the color of a sprite the way the XNA spriteBatch.draw() color parameter changes color. I don't know exactly what it is called (the closest word I can think of is 'tinting' but that's not right I don't think) and it's kind of hard to explain the way it looks.


Say you set the parameter to 'Red' -- white pixels in the original texture would become completely red, while black pixels would remain black. Grey pixels would look dark red, blue pixels might look purple, green pixels might look brownish.


If you set the parameter to 'Black' the texture would become completely black.


Does anyone know the math behind this, or what this technique is called? It's probably very simple and I'm just not seeing it.


Thanks!



Answer




It's probably multiplying the colours. That's pretty typical for a parameter like this.


Each colour channel is interpreted as a value from 0 to 1, then multiplied with the corresponding colour channel of the tint colour. So:


_______________ times red (1, 0, 0)  =
white (1, 1, 1) = red (1, 0, 0)
grey (0.5, 0.5, 0.5) = dark red (0.5, 0, 0)
black (0, 0, 0) = black (0, 0, 0)

Under this scheme, pure blue (0, 0, 1) tinted with pure red (1, 0, 0) will turn black. But if you use intermediate blue & red colours (ones with some non-zero values in the other two channels) you may get a dark purple.


Here's an example of using "multiply" blending to tint colours, via this tutorial Multiplication tinting example


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...