+2 votes

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() {

I put how it should look like and how it is on imgur https://imgur.com/a/WfEpPFk

in Engine by (83 points)

1 Answer

+3 votes

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


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 setshaderparam() 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!

by (20 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.