flixel icon indicating copy to clipboard operation
flixel copied to clipboard

Game filters: wrong size?

Open filipp8 opened this issue 5 years ago • 7 comments

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).

filipp8 avatar Jun 15 '20 16:06 filipp8

^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.

Vulpicula avatar Jan 26 '22 00:01 Vulpicula

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.

T1mL3arn avatar Dec 14 '23 10:12 T1mL3arn

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;
})

T1mL3arn avatar Dec 14 '23 17:12 T1mL3arn

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

Geokureli avatar Dec 14 '23 17:12 Geokureli

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

nebulazorua avatar Apr 01 '24 19:04 nebulazorua

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

nebulazorua avatar Apr 01 '24 19:04 nebulazorua