0 votes

I have added a shader to a Sprite so as to change a part of its color. While this works fine in the editor, this change isn't shown in the animations of the Sprite. I first animated it in an AnimationPlayer, and then I further animated it in an AnimationTree. The AnimationTree is used to play advanced animations which are controlled from code. Here's a video to illustrate what I mean.

When the game is ran, the changes from the shader aren't shown in the Sprite. No part of the character's animations show the changed color.

Godot version 3.4.4
in Engine by (3,112 points)

what is in AnimationPlayer ? Do You change shader_param inside it ? Or modulate ?

I tried adding the shader parameters as keys to AnimationPlayer, but that didn't affect the animation. I also tried changing the shader parameters from code, but that didn't change anything, ether.

1 Answer

0 votes

if shader works in editor, but doesn't work at runtime, something is blocking it, conflicting with its code. Show your AnimationPlayer keyframes.

by (7,742 points)

I tried changing the colors for other characters, and the only one that I could change the color on was the skeleton enemy. And I don't know why.

or can your shader code be calculated against background ?
Can You show shader code ?

Here's the shader code:

shader_type canvas_item;

uniform vec4 origin:hint_color;
uniform vec4 new:hint_color;

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

    if (current_pixel == origin)
        COLOR = new;
    else
        COLOR = current_pixel;
}

wow, that is performance heavy shader.
It is quite possible, that it is unable to compare 4 floating values perfectly at runtime.
You should use mask for this kind of tasks. Isolate trousers from your png, make them black against tranmsparent background and use this as a uniform Sampler2D to mark certain area of original sprite. With this You won't need costly if statement, You will just multiply by color or use mix statement

uniform Sampler2D mask
uniform vec4 new
void fragment() :
        float maskpixel = texture(mask, UV).a
       COLOR = mix( COLOR, new, maskpixel)

I don't quite follow your directions. Should your shader be applied to a node which is a child of the original Sprite?

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.