flixel icon indicating copy to clipboard operation
flixel copied to clipboard

Camera shaders don't update after resizing the window

Open ACrazyTown opened this issue 3 months ago • 0 comments

There is an issue with cameras and filters where shader coordinates won't properly update after resizing the window. In the provided example, half of the screen is black while the other half is purple. After resizing the window a couple times, the half is no longer centered. Resizing the window vertically seems to have a more dramatic effect.

You can press the A key to run a hack that seems to resolve the issue. I'm not sure how or why it works, I found it in the #flixel channel on the Haxe discord.

Observed on hashlink (Windows).

package;

import flixel.FlxG;
import flixel.FlxState;
import flixel.graphics.tile.FlxGraphicsShader;
import openfl.filters.ShaderFilter;

class TestShader extends FlxGraphicsShader
{
	@:glFragmentSource('
	#pragma header

	void main()
	{
		vec4 b = vec4(1.0);
		if (openfl_TextureCoordv.x >= 0.5)
		{
			b = vec4(1.0, 0.0, 1.0, 1.0);
		}
		else
		{
			b = vec4(0.0, 0.0, 0.0, 1.0);
		}

		gl_FragColor = b;
	}
	')
	public function new()
	{
		super();
	}
}

class PlayState extends FlxState
{
	override public function create()
	{
		super.create();
		FlxG.camera.filters = [new ShaderFilter(new Shader())];
	}

	override public function update(elapsed:Float)
	{
		super.update(elapsed);

		if (FlxG.keys.justPressed.A)
		{
			// hack to fix this issue, shared around on discord
			@:privateAccess
			{
				FlxG.camera.flashSprite.__cacheBitmap = null;
				FlxG.camera.flashSprite.__cacheBitmapData = null;
			}
		}
	}
}

ACrazyTown avatar Aug 17 '25 20:08 ACrazyTown