flixel
flixel copied to clipboard
Game filters: wrong size?
Code snippet reproducing the issue:
PlayState.hx
package;
import flixel.text.FlxText;
import flixel.system.FlxAssets.FlxShader;
import openfl.filters.ShaderFilter;
import flixel.FlxG;
import flixel.FlxState;
class PlayState extends FlxState
{
override public function create():Void
{
super.create();
FlxG.game.setFilters([new ShaderFilter(new BorderShader())]);
FlxG.game.filtersEnabled = true;
var text = new FlxText();
text.text = "Hello World!";
text.screenCenter();
add(text);
}
override public function update(elapsed:Float):Void
{
super.update(elapsed);
if (FlxG.mouse.pressed) FlxG.camera.shake(0.01, 0.1);
if (FlxG.keys.anyJustPressed([ONE])) FlxG.game.filtersEnabled = !FlxG.game.filtersEnabled;
}
}
class BorderShader extends FlxShader
{
@:glFragmentSource("
#pragma header
void main()
{
vec2 uv = openfl_TextureCoordv;
vec3 col = texture2D(bitmap, vec2(uv)).rgb;
if ((uv.x > 0.0 && uv.x < 0.2) || (uv.x < 1.0 && uv.x > 0.8) || (uv.y > 0.0 && uv.y < 0.2) || (uv.y < 1.0 && uv.y > 0.8)) {
gl_FragColor = vec4(1., 0., 0., 1.);
return;
}
gl_FragColor = vec4(col, 1.);
}
")
public function new()
{
super();
}
}
Observed behavior:
Game filters appear to detect wrong game size.
This is filter applied to whole game
This is filter applied to whole game after a camera shake
This is filter applied to camera
Expected behavior: Game filter to look like camera filter
In last image bottom border != top border because screenshot cut lowest part to keep window toolbar (dunno why).
^can confirm issue is still present on the latest version of HaxeFlixel, having similar issues right now with shaders I've attempted to apply via FlxG.game.setFilters.
After moving the cursor to the far right side of the screen something seems to snap, causing the entire shader to seemingly bug out. Decided this bit to my shader to test in the same way, with the following results:
if ((uv.x > 0.0 && uv.x < 0.2) || (uv.x < 1.0 && uv.x > 0.8) || (uv.y > 0.0 && uv.y < 0.2) || (uv.y < 1.0 && uv.y > 0.8)) { gl_FragColor = vec4(1., 0., 0., 1.); return; }
On game boot:

After moving cursor to the right edge of the screen:

Main reason I'm using FlxG.game.setFIlters here is that standard FlxCamera stuff doesn't work so well with things like OpenFL Textfields.
The problem also appears on resize even on camera filters. So when I go to fullscreen and back the picture get screwed. What are flixel/openfl internals to hold actual bitmap? If ti is possible to get it and pass to the shader then problem can be solved, I believe. Related issue #2181.
The problem also appears on resize
As a temp solution for such case - reset camera
FlxG.signals.onResize((w,h)->{
Flixel.cameras.reset();
Flixel.camera.filters = yourShaderFilterList;
})
The problem also appears on resize
As a temp solution for such case - reset camera
FlxG.signals.onResize((w,h)->{ Flixel.cameras.reset(); Flixel.camera.filters = yourShaderFilterList; })
Should flixel fix this? Maybe here
Edit: wait this code removes all cameras, that seems like a nuclear option
Something similar appears to happen when resizing a window (specifically maximizing and unmaximizing) Gonna try to get a minimal repro project up real quick and record that, but on projects I've worked on this seems to happen
https://github.com/HaxeFlixel/flixel/assets/12458328/8faa8261-be34-407e-9610-0a5c56ab376b
Seems related. Minimum repro code can be found at https://github.com/nebulazorua/shader-resize-bug
Using Openfl 9.2.2, lime 8.0.0 and flixel 5.5.0