Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: blending a sf::VertexArray's color and texture together  (Read 610 times)

0 Members and 1 Guest are viewing this topic.

amogusugoma69

  • Newbie
  • *
  • Posts: 4
    • View Profile
blending a sf::VertexArray's color and texture together
« 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!  :)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10821
    • View Profile
    • development blog
    • Email
Re: blending a sf::VertexArray's color and texture together
« Reply #1 on: June 30, 2023, 01:34:16 pm »
You should be able to achieve this with a shader. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: blending a sf::VertexArray's color and texture together
« Reply #2 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.
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 and 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.
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!
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

amogusugoma69

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: blending a sf::VertexArray's color and texture together
« Reply #3 on: July 09, 2023, 11:05:46 am »
Thanks for your help guys! I got it working with the blendColor shader.