SpriteBatch color tinting

In the spritebatch one can pass a color to tint the texture one is drawing. Does anyone know what xna/monogame is doing with each pixel of the texture? What is the tinting algorithm? I did a google search and some sites say it’s doing a color multiplication but that doesn’t make sense to me. Can anyone explain this to me?

It literally is a component-by-component multiply(with the colors represented as 0-1 floating point numbers). also known as the Multiply blend mode.

The source code of the shader used by SpriteBatch can be found here: MonoGame/SpriteEffect.fx at develop · MonoGame/MonoGame · GitHub

The fragment shader multiplies the texture’s color with the input color(which is that color parameter):

return SAMPLE_TEXTURE(Texture, input.texCoord) * input.color;
1 Like

Is this the basic math? This doesn’t make sense because if the tint color is red then doesn’t it make the green and blue value 0 for the texture colors?

colornew = new Color(texture[x, y].R * tint.R, texture[x, y].G * tint.G, texture[x, y].B * tint.B, texture[x, y].A * tint.A)

That is indeed exactly what happens.

The coloring is not real recoloring - it’s just what the textures color is getting multiplied with. It totally works as tinting when the texture is grayscale tho. If the texture is not grayscale one would need to make it grayscale first, which would be additional steps, spritebatch will not do for you.

Of course you can supply your own shader for SpriteBatch which does the extra math involved on the texture pixel first and multiply this with the color provided by vertex data - but it’s more performant to just supply a grayscale texture in the first place.

1 Like