I’ve been trying to use openfl Shaders from within HaxeFlixel, using ShaderFilter. So as a test I wrote this:
class PlayState extends FlxState
{
var testbitmapdata:BitmapData;
var sp:FlxSprite;
override public function create()
{
testbitmapdata = Assets.getBitmapData("assets/images/test.png");
var redshader = new Shader();
redshader.glVertexSource = Assets.getText("assets/data/shaders/simpleredshader/redvertshader.vert");
redshader.glFragmentSource = Assets.getText("assets/data/shaders/simpleredshader/redfragshader.frag");
testbitmapdata.applyFilter(testbitmapdata, testbitmapdata.rect, new Point(), new ShaderFilter(redshader));
sp = new FlxSprite(50, 100);
sp.pixels = testbitmapdata;
sp.setGraphicSize(100);
sp.updateHitbox();
add(sp);
}
override public function update(elapsed:Float)
{
super.update(elapsed);
}
}
My shaders look like this:
Vertex Shader:
attribute float openfl_Alpha;
attribute vec4 openfl_ColorMultiplier;
attribute vec4 openfl_ColorOffset;
attribute vec4 openfl_Position;
attribute vec2 openfl_TextureCoord;
varying float openfl_Alphav;
varying vec4 openfl_ColorMultiplierv;
varying vec4 openfl_ColorOffsetv;
varying vec2 openfl_TextureCoordv;
uniform mat4 openfl_Matrix;
uniform bool openfl_HasColorTransform;
uniform vec2 openfl_TextureSize;
void main(void)
{
gl_Position = openfl_Matrix * openfl_Position;
}
Fragment Shader: (for this test I just wanted to output the color red to see if the shader worked)
void main(void)
{
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
But it seems like the applyFilter() method does not seem to work. The sprite does not turn red as I would expect. However, when I apply the same shader object as a filter to an FlxCamera (using sp.camera.setFilters([ new ShaderFilter(redshader) ]) ) it does turn the screen red, as expected.
Is there something else I should have added to the shader code or the haxe code to make it work for the flxsprite?