Blink effect and shaders

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;
}