+1 vote

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 https://imgur.com/a/WfEpPFk

asked Jan 25 in Engine by Teln0 (76 points)

1 Answer

0 votes

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 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!

answered May 22 by Ivy (14 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 How to use this Q&A? before posting your first questions.