🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Rendering text = wrong color

Started by
4 comments, last by _WeirdCat_ 2 years, 12 months ago

So my base texture looks like this D below (black background white text) now i want to say rendered text to appear black that means this image above, how should i mix colors to actually achieve this,

In my opinion i should somehow find transition between one to another, by that i mean that if i want to render red text i should somehow check how much from original color to its inverse i need to go to match desired font color anyway i fail at that greatly….

here is sample code if someone is eager to read alpha is always 1.0 and lux is supposed to check whenever we want to sample background and discard.

precision highp float;
uniform sampler2D font_tex;
varying vec2 texcoord;
uniform int use_boundary;
uniform vec2 bmin;
uniform vec2 bmax;
varying vec2 scr_coord;
uniform float alpha;
uniform vec3 FONT_COLOR;


vec3 vectorAB(vec3 a, vec3 b) { return b-a; }


void main()
{
vec3 color = texture2D( font_tex, texcoord ).rgb;

vec3 finalColor = vec3(0.0);

vec3 DESIGN_FONT_COLOR = vec3(1.0, 1.0, 1.0);
	vec3 inverse = vec3(1.0) - color;
	vec3 toInverse = vectorAB(color, inverse);
	finalColor =	color - FONT_COLOR * toInverse;




float lux = ( (color.x+color.y+color.z) / 3.0 );
if (lux < 0.005) lux = 0.0;
float intensity = lux * alpha;


if ( (use_boundary == 1) )
{
	if ( (scr_coord.x > bmin.x) &&
	(scr_coord.x < bmax.x) &&
	(scr_coord.y > bmin.y) &&
	(scr_coord.y < bmax.y) )
gl_FragColor = vec4(finalColor, intensity);
else
gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);


} else
gl_FragColor = vec4(finalColor, intensity);


}

cheers….

Advertisement

In general color blending in a shader is just a multiplication, so if you have a white text and black background to make it change color you just multiply the texture color by the desired color you want. I hope you background has an alpha chanel that is only 1 where you have white pixels, so also turn alpha blending on.

When you are using SDF fonts your shader starts to do calculations to determine the edge of the font and render the internals according to the color you specify. (https://github.com/libgdx/libgdx/wiki/Distance-field-fonts​ that gives an idea of the technique). You generally use SDF fonts to get away from having to load a font page for each font size you use.

uniform vec4 textColor; //Make sure this has a 1 for the alpha value
varying vec2 texcoord;
void main
{
    vec4 color = texture2D( font_tex, texcoord );
    gl_FragColor= color * textColor;
}

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

I am confused there is not image attached let me clear the thing out

This won't work because multiplyuing blueish on left by 0 gives 0 instead of orangeish color desired (the case above)

basically this black bg white text texture needs to be the one above it this is why i am so confused about calculation

The blue/orange tint exists because of subpixel rendering. This will result in crisp text if, and only if, your texel and display pixel are 1:1 in scale, and you are viewing it on an LCD monitor the matching given RGB striping.

Either use a simple white+alpha texture atlas for your font, or use signed distance fields (SDF) or multi-channel signed distance fields MSDF.

SDF encodes edge data into the texels, which a shader will use determine exactly where an edge starts/ends between two pixels.

Contrast to a simple white+alpha texture, those subpixels will get scaled as well. In your case, you will see streaks of blue and orange spanning across multiple pixels.

Seems like i'll have to go just by disabling colored edges and multiplying mask with font color, like nc83 suggested, signed dst fields are an overkill, i am making a sytnax colored text editor and i draw each string that is separated by a list of characters like “ ” - space character . , []{}=\/_-+*"':;!?& () to eaisly implement text editing features, yes it does lag alot…

This topic is closed to new replies.

Advertisement