Blink effect and shaders [Solved]

Hello, although I already have a (very) little experience in game development, I’m realtively new to shaders. I read a couple of things on the web and tried to adapt the solution found here. I created a blink_sprite.fp file with the following code:

uniform lowp vec4 blink_effect_trigger;
in mediump vec2 var_texcoord0;
out vec4 out_fragColor;

uniform lowp sampler2D texture_sampler;
uniform fs_uniforms
{
    mediump vec4 tint;
};

void main()
{
    // Pre-multiply alpha since all runtime textures already are
    mediump vec4 tint_pm = vec4(tint.xyz * tint.w, tint.w);
    lowp vec4 color_of_pixel = texture(texture_sampler, var_texcoord0.xy);
    if((blink_effect_trigger.r == 1.0) && (color_of_pixel.a != 0.0))
    {
        color_of_pixel = vec4(1.0, 1.0, 1.0, 1.0);
    }
    out_fragColor = color_of_pixel * tint_pm;
}

I do blinking by calling the go.set function: go.set(SPRITE, “blink_effect_trigger”, vmath.vector4(1))

This solution works perfectly when I test the game but I can’t see the sprite in the IDE editor: it’s invisible. If I reset the sprite’s material to the default one, the sprite re-appears.

Can someone address what I’m doing wrong?

Thanks

What is the default value of blink_effect_trigger in your .material file?

1.0, 1.0, 1.0, 1.0

Huh, could you share the material and vp+fp here in a zip file?

I’m not sure if this affects the display inside the editor, but shouldn’t the custom uniform be inside the block uniform fs_uniforms {.. ?

in mediump vec2 var_texcoord0;
out vec4 out_fragColor;

uniform lowp sampler2D texture_sampler;
uniform fs_uniforms
{
    mediump vec4 tint;
    mediump vec4 blink_effect_trigger;
};

void main()
{
    // Pre-multiply alpha since all runtime textures already are
    mediump vec4 tint_pm = vec4(tint.xyz * tint.w, tint.w);
    lowp vec4 color_of_pixel = texture(texture_sampler, var_texcoord0.xy);
color_of_pixel = clamp(color_of_pixel + vec4(blink_effect_trigger.r)*color_of_pixel.a, 0., 1.);
    out_fragColor = color_of_pixel * tint_pm;
}

Sorry for the late reply, I’m not at home currently. Here is the zip with the material I’m using

blink.zip (1.9 KB)

@Dragosha:

Thank you for your reply. I really don’t know anything about the shading language, I just made a mix of different solutions found on wht web. BTW, I tried your code but still continue having invisible sprites in the editor.

You could separate what you see in the editor and what is executed during runtime by using editor specific code.

You should be able to output the sprites texture for the editor.

I’ll try this solution. Apart this, I would like to undestand if it’s a bug, it’s me that I0m wrong using shaders or a limit or a dìifferent behaviour of the editor

The solution was to do exactly what Dragosha suggested. Move the blink_effect_trigger uniform inside the uniform block:

#version 140

in mediump vec2 var_texcoord0;

out vec4 out_fragColor;

uniform lowp sampler2D texture_sampler;
uniform fs_uniforms
{
    lowp vec4 blink_effect_trigger;
    mediump vec4 tint;
};

void main()
{
    // Pre-multiply alpha since all runtime textures already are
    mediump vec4 tint_pm = vec4(tint.xyz * tint.w, tint.w);
    lowp vec4 color_of_pixel = texture(texture_sampler, var_texcoord0.xy);
    if((blink_effect_trigger.r == 1.0) && (color_of_pixel.a != 0.0))
    {
        color_of_pixel = vec4(1.0, 1.0, 1.0, 1.0);
    }
    out_fragColor = color_of_pixel * tint_pm;
}

1 Like

@britzl I’m sorry, I already said in a previous msg that I tried the solution from @Dragosha but it didn’t work, at least on my environment, as you can see from the attached pic.

Apart from that, I’ll explain what I have to do. While playing the game, I need to set the sprite to a plain white colour with a simple flag, which I would like to use to simulate damage received by the sprite itself. However, I would also like to see the sprite correctly in the editor when I use it. Assuming that the solution works (and as I have already told you, it does not work for me), seeing the sprite completely white in the editor does not help me. For example, I use two similar sprites, one for a classic green slime and one red for a lava slime: if they are not rendered in the editor with their original colours, at first glance I don’t know which is which.

@MasterMind Your suggestion was helpful. By following that guide and using a preprocessor directive, I have now solved the problem. I hope that anyone will then provide an explanation as to why the solution proposed by @britzl does not work on my system.

#version 140
#ifdef EDITOR
in mediump vec2 var_texcoord0;

out vec4 out_fragColor;

uniform mediump sampler2D texture_sampler;
uniform fs_uniforms
{
mediump vec4 tint;
};

void main()
{
// Pre-multiply alpha since all runtime textures already are
mediump vec4 tint_pm = vec4(tint.xyz \* tint.w, tint.w);
out_fragColor = texture(texture_sampler, var_texcoord0.xy) \* tint_pm;
}

#else
in mediump vec2 var_texcoord0;

out vec4 out_fragColor;

uniform lowp sampler2D texture_sampler;
uniform fs_uniforms
{
lowp vec4 blink_effect_trigger;
mediump vec4 tint;
};

void main()
{
// Pre-multiply alpha since all runtime textures already are
mediump vec4 tint_pm = vec4(tint.xyz \* tint.w, tint.w);
lowp vec4 color_of_pixel = texture(texture_sampler, var_texcoord0.xy);
if((blink_effect_trigger.r == 1.0) && (color_of_pixel.a != 0.0))
{
color_of_pixel = vec4(1.0, 1.0, 1.0, 1.0);
}
out_fragColor = color_of_pixel \* tint_pm;
}
#endif

I copied and pasted the script from the sprite.fp file because when I used the directive for only the central part of my code, I couldn’t get the desired effect. Unfortunately, I admit my total ignorance in the use of shaders and I don’t really know what the code does or how it works.

Ok, then you need to tell us a bit about your environment. Windows, Linux or macOS? Are your drivers up to date?

In my screenshot I set the blink_effect_trigger.r value to 1.0 to prove that the editor renders using the material (ie renders white). If I set the value to 0.0 it renders in the original color.

Please try the attached project and confirm if it works or not.

Archive.zip (3.1 KB)

2 Likes

I can confirm that your project DOES work. So I decided to copy all your shader code instead of the one I used, and magically, it worked in my project too!!!

I was literally stunned because I can’t understand why. The only thing I can think of is that the first version of the code I used was missing this line: “#version 140”.

Could that be the reason?

To answer your question, here’s what I’m using:

Manjaro Linux w/kernel 6.17.1

KDE w/Plasma 6.3.6

1 Like

Yes, it’s required, if you want to write the shader in the new SPIR-V rendering pipeline:

I am very happy to have solved the problem and thank everyone for their help. I am marking the thread title as “solved”.

1 Like