SFML community forums

Help => Graphics => Topic started by: amogusugoma69 on June 29, 2023, 09:34:16 pm

Title: blending a sf::VertexArray's color and texture together
Post by: amogusugoma69 on June 29, 2023, 09:34:16 pm
hi guys,

I am rendering a tilemap that uses vertex arrays to render all of the tiles. Now I want to give some tiles a colored hue, but just setting the color atributes of the vertices doesn't really give me the results that i am looking for...
I dont really want to make a second vertex array just to give some tiles a color. Is there a way to somehow blend the texture and the color together?

thanks in advance!  :)
Title: Re: blending a sf::VertexArray's color and texture together
Post by: eXpl0it3r on June 30, 2023, 01:34:16 pm
You should be able to achieve this with a shader. :)
Title: Re: blending a sf::VertexArray's color and texture together
Post by: Hapax on July 02, 2023, 04:43:49 pm
For a bit more detail, you should know that vertex colours are 'multiplied' with the texture colours, by default.
This means that if either colour is black, the result will be black; if one colour is pure red and one is the other is pure blue, the result is still black; if one colour is white, the result is the other colour.

I have made a few very basic shaders for simple use with SFML that you can try out. They are here:
https://github.com/Hapaxia/Lens/tree/master/Lens

First one to try is standard.frag (https://github.com/Hapaxia/Lens/blob/master/Lens/standard.frag).
This does the same multiplication with the two colours but allows you specify an "amount" ratio. This is just the mix between the two. e.g. 0 is texture colour, 1 is vertex colour multiplied in, 0.5 is half way between the two.

If that isn't what you want, you should maybe try blend.frag (https://github.com/Hapaxia/Lens/blob/master/Lens/blend.frag) and blendColor.frag (https://github.com/Hapaxia/Lens/blob/master/Lens/blendColor.frag).
These do not multiply the two colours together. Instead, the "amount" ratio is just the 'mix' between the two colours. This can be useful for 'tinting' textures. e.g. 0 is texture colour, 1 is vertex colour, 0.5 is half of each.
and example would be if the texture colour is red and the vertex colour is blue, with an "amount" of 0.5, it would be a purple (0.5 red + 0.5 blue).
blendColor is only different from blend is that it ignores the alpha of the vertex colour.

If that still doesn't do what you want, you could try adjustments.frag (https://github.com/Hapaxia/Lens/blob/master/Lens/adjustments.frag).
This allows you to perform some standard image colour manipulations: gamma, de-saturation, inversion, contrast and brightness. Note that this shader completely ignores vertex colours.



In conclusion, I think blend or blendColor may be what you're looking for but it would depend on your exact requirements.

Also, these shaders are based on the basic SFML shader ideas so you can just use the fragment shader alone without a custom vertex shader and it should work immediately!
Title: Re: blending a sf::VertexArray's color and texture together
Post by: amogusugoma69 on July 09, 2023, 11:05:46 am
Thanks for your help guys! I got it working with the blendColor shader.