Skip to content

Game filters: wrong size? #2258

Open

Description

@filipp8

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.
GameFilter 15_06_2020 17_48_07
This is filter applied to whole game

GameFilter 15_06_2020 17_48_26
This is filter applied to whole game after a camera shake

GameFilter 15_06_2020 17_48_49
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).

Activity

  1. Vulpicula commented on Jan 26, 2022

    @Vulpicula

    ^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:
    image

    After moving cursor to the right edge of the screen:
    image

    Main reason I'm using FlxG.game.setFIlters here is that standard FlxCamera stuff doesn't work so well with things like OpenFL Textfields.

  2. T1mL3arn commented on Dec 14, 2023

    @T1mL3arn
    Contributor

    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.

  3. T1mL3arn commented on Dec 14, 2023

    @T1mL3arn
    Contributor

    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;
    })
  4. Geokureli commented on Dec 14, 2023

    @Geokureli
    Member

    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

  5. nebulazorua commented on Apr 1, 2024

    @nebulazorua

    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

  6. nebulazorua commented on Apr 1, 2024

    @nebulazorua
    2024-04-02.03-38-24.mp4

    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

  7. 47rooks commented on Jul 19, 2024

    @47rooks
    Contributor

    There is a different workaround for the camera case. If you disable the filter on the resize - I overrode onResize() but I would expect the signal would work too - and then enable it on the next frame - next update call - then it resets properly and the border looks as it should in the resized window. Seems OpenFL needs to know the filter has gone to reset stuff. Something like this is what I did - no doubt there are neater ways like a timer function, but it works.

    	override function onResize(Width:Int, Height:Int)
    	{
    		super.onResize(Width, Height);
    		FlxG.camera.filtersEnabled = !FlxG.camera.filtersEnabled;
    		_toggleFilters = Timer.stamp();
    	}
    
    	override public function update(elapsed:Float)
    	{
    		super.update(elapsed);
    
    		if (_toggleFilters > 0 && Timer.stamp() > _toggleFilters + elapsed)
    		{
    			FlxG.camera.filtersEnabled = !FlxG.camera.filtersEnabled;
    			_toggleFilters = 0;
    		}
            }
    
  8. ACrazyTown commented on Sep 15, 2025

    @ACrazyTown
    Contributor

    Stumbled onto this while investigating these issues.

    Camera filters breaking when resizing should be fixed by this OpenFL pull request: openfl/openfl#2797, and is being tracked here: #3463

    Game filters reporting an incorrect size I believe happens due to how Flixel handles various OpenFL level objects.
    Internally, Flixel's cameras (let's call this the game view) have an OpenFL Sprite which they draw to. These Sprites are later added to the FlxGame so that they can get updated and rendered.

    The issue is that aside from the cameras' Sprites, FlxGame also holds various Sprites not related to the game view. Things like the mouse, debugger, volume tray, etc. Some of them are even off screen, so when you apply a filter to the entire FlxGame, it's taking all those things in account. Notice in the video how the mouse and the sound tray cause the lens distortion filter to move around.

    2025-09-15.15-49-04.mp4

    I think a fix for this could be very simple, and I could do a pull request for it myself BUT it depends on what the filter should be applied to. If it should only cover the "game view" (all cameras) and disregard the mouse, sound tray, debugger etc. it should be a matter of just adding a new OpenFL Sprite to FlxGame called "view" or something, and adding all of the cameras' Sprites to that instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions