ShaderFilter question

hey there,
I’ve found several references across the web to a feature of ShaderFilter that automatically sends the displayobject’s texture to the shader, such as from here: Tip: Custom Mask workaround

there is also a mention of the specific details of this feature here:

i’ve spent the day trying to reproduce this behavior, and I can’t seem to get it to work. setting the shader input manually with shader.data.uImage0.input = bitmapData; works fine, but the implicit conversion does not.

here’s the code i’m testing with, for reference:

var shader = new Shader();
shader.glFragmentSource = 
	"uniform sampler2D uImage0;
	varying vec2 vTexCoord;
	
	void main(void) {
		vec4 color = texture2D (uImage0, vTexCoord);
		float alpha = color.a;
		
		if (color.a <= 0.5) {
			gl_FragColor = vec4 (0.0, 1.0, 0.0, 1.0);
		} else {	
			gl_FragColor = vec4 (1.0, 0.0, 0.0, 1.0);
		}
	}";
shader.glVertexSource = "attribute vec4 openfl_Position;	attribute vec2 openfl_TextureCoord;	varying vec2 vTexCoord;	uniform mat4 openfl_Matrix;	uniform vec2 openfl_TextureSize;void main(void){vTexCoord = openfl_TextureCoord;	gl_Position = openfl_Matrix * openfl_Position;}";

var sg = shape.graphics;
sg.lineStyle(1, 0xFFFFFF, 1, true);
sg.moveTo(20,20);
sg.cubicCurveTo(50,25, 20,60, 40,80);

shape.filters = [ new ShaderFilter(shader) ];

im using openfl version 8.9.5

has this feature been changed/removed?
additionally, is drawing the shape onto a bitmap and assigning that to the shader input computationally identical to how this should work? if so then i’m fine settling for that, i wasn’t sure if there was something openfl did under the hood that optimized it better.

(the motivation behind this is to be able to draw graphics without the antialiasing, and i figure a shader would be the best solution. but that might be better left for another topic)

thank you!

There might be a bug when setting glFragmentSource like this. If you extend the Shader class that should definitely work (as that is what OpenFL uses internally)

Oh and you will also probably want to consult the stock DisplayObjectShader to see what our standard names are. They should all have an openfl_ prefix:

openfl/DisplayObjectShader.hx at develop · openfl/openfl (github.com)

Thank you for the response, I did not know about the standard names! It seems like that was the issue after all, using the standard openfl_Texture as opposed to uImage0 worked fine. So probably the texture was compiled and sent as expected, I was just referencing the wrong uniform. This worked even when setting glFragmentSource directly, though extending Shader worked as well.

Thanks for the help :slight_smile:

2 Likes