Canvas Item Shader gives weird results with text.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Teln0

I got a CS script that overrides the _Draw() method of Node2D to display text. When I tried to add a canvas_item shader to it, it gave me weird results. Here’s the shader code :

shader_type canvas_item;


void vertex() {
    COLOR = COLOR;
}

I put how it should look like and how it is on imgur Godot Shader - Album on Imgur

:bust_in_silhouette: Reply From: Ivy

I had this problem for ages! Just figured it out.

TL;DR:

shader_type canvas_item;
uniform vec4 color = vec4(1, 0, 0, 1);


void fragment(){
	vec4 tex = texture(TEXTURE, UV);

	tex.r *= color.r;
	tex.g *= color.g;
	tex.b *= color.b;
    tex.a *= color.a;

    COLOR = tex;
}

The issue was that we were taking all the pixels in the character’s box (or something similar, I don’t fully understand the backend here) and assigning them to some value regardless of whether or not they were part of the letter or transparent. We need to modify the white text by our desired color, not replace it.

Multiplication might not be the best way to modify the pixels, but it works well enough for me, and I’m too tired from figuring this simple solution out to work through the arithmetic.

You should be able to set_shader_param() a Color to this color (since Godot will translate a Color to a vec4) directly.

Hope this helps!

You’d think someone must have tried to make a shader in Godot and found this out and published it, but I can’t find it!