Shader not working

Hello,

I’m trying to add this simple Shader to invert color of a bitmap.

class Invert extends Shader
{

@:glFragmentSource(
		"varying vec2 vTexCoord;
		uniform sampler2D uImage0;
		
		void main(void)
		{
			vec4 color = texture2D(uImage0, vTexCoord);
			gl_FragColor = vec4(1.0 - color.r, 1.0 - color.g, 1.0 - color.b, color.a);
		}"

)

public function new(code:ByteArray = null)
{
	super(code);
}
}

I’ve tried both on Tilemap and Sprite but this Shader doesn’t work. (tested on HTML5, neko and cpp).

In Tilemap: tile.shader = new Invert();
Color is inverted but alpha turns white.

In Sprite: sprite.filters = [new ShaderFilter(new Invert())];
Not working at all.

So, is my shader wrong or is there a bug in openFl?

We use premultiplied alpha in our render code, so you will need to multiply your r, g and b values by color.a in order to get the proper effect. You can see our standard code here:

https://github.com/openfl/openfl/blob/develop/openfl/display/Shader.hx#L108

For the meantime, ShaderFilter is disabled, but we hope to add hardware-based filters again when we can design a better system that runs faster than our previous approach

Thanks! It’s working.

So here is my shader now:

        varying vec2 vTexCoord;
		varying float vAlpha;
		uniform sampler2D uImage0;
		
		void main(void)
		{
			vec4 color = texture2D(uImage0, vTexCoord);
			gl_FragColor = vec4(1.0 - color.r, 1.0 - color.g, 1.0 - color.b, color.a * vAlpha);
			gl_FragColor.rgb *= gl_FragColor.a;
		}

Hi!

I have tried with the shader that you have posted but I can’t get it work. Do I have to configure something? When I apply the shader the bitmap disappear. I’m targeting to HTML.

Thanks!