Hi there,
I’ve see quite a few developers who create grayscale textures and change their color programmatically using RGB values. How is this done using textures in LibGDX?
Thanks!
- A
Hi there,
I’ve see quite a few developers who create grayscale textures and change their color programmatically using RGB values. How is this done using textures in LibGDX?
Thanks!
In the shader, of course!
...
uniform vec3 u_TexColor;
...
gl_FragColor = texture2D(u_Sampler02D, v_TextureCoordinate) * vec4(u_TexColor, 1);
Set the u_TexColor to be (1, 0, 0) for only the red component, (0, 1, 0) for only the green component, and (0, 0, 1) for only the red component.
Grayscaling is a bit more complex…
gl_FragColor = vec3(dot(texture2D(u_Sampler02D, v_TextureCoordinate).xyz, vec3(0.299, 0.587, 0.114)));
[spoiler](0.299, 0.587, 0.114) is called luminosity and is a weird vector decided by a cult of hooded neckbeards that we are not allowed to question…[/spoiler]
Actually grayscale is very easy.
vec3 grayScale = vec3(col.r + col.g + col.b) / 3.0;
While that may give alright results, I believe the problem is it doesn’t consider how our eyes perceive differences in different colours.
Good point. Only use that method if you just want an average of the colors I guess. Cheap grayscale in other terms.
this is mixed up quick :
gray-scale : icode/3.0[/icode]
luminance : [icode]dot( rgb, scale )[/icode] or [icode]sqrt(dot( rgb, scale ))[/icode], scale is usually [icode]vec3(0.3,0.59,0.11)[/icode] (opengl) or [icode]vec3(0.212655,0.715158,0.072187)[/icode] (photometric)
https://www.opengl.org/registry/specs/KHR/blend_equation_advanced.txt
approx luminance : [icode]( r + r + b + g + g + g )/6.0[/icode] = scale : [icode]vec3(0.33,0.5,0.16)[/icode]
brightness : [icode]sqrt( dot(rgb,rgb)/3.0 )[/icode] ( distance to black / 3)
lightness : [icode]( max(rgb) + min(rgb) ) / 2.0[/icode] (as in hsl)
very different things.
Not sure if I or the others are missing the point…
From what I understand you want to take a grayscaled image and apply a color to it (if you would use red for coloring the different shades of gray would change to different shades of red).
If this is your question then check out spriteBatch.setColor to set the rendering color before you draw the actual texture with a SpriteBatch.
I you are using scene2d then you can set the color via the actors setColor method.
The sprite class has a set colour method that sets the color of the default shader to use while drawing the texture belonging to the sprite.