Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Matrix Math from default shader #3227

Merged
merged 1 commit into from
Aug 26, 2024
Merged

Conversation

NeeEoo
Copy link
Contributor

@NeeEoo NeeEoo commented Aug 11, 2024

Since the current matrix math can be simplified to a simple multiply

@Geokureli
Copy link
Member

For future PRs, please edit your settings.json's formatting to include:
"editor.formatOnSaveMode": "modifications"
and add the following to your hxformat.json

"indentation":{
    "trailingWhitespace": true
}

mat4 colorMultiplier = mat4(0);
colorMultiplier[0][0] = openfl_ColorMultiplierv.x;
colorMultiplier[1][1] = openfl_ColorMultiplierv.y;
colorMultiplier[2][2] = openfl_ColorMultiplierv.z;
colorMultiplier[3][3] = openfl_ColorMultiplierv.w;

color = clamp(openfl_ColorOffsetv + (color * colorMultiplier), 0.0, 1.0);
color = clamp(openfl_ColorOffsetv + (color * openfl_ColorMultiplierv), 0.0, 1.0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is openfl_ColorMultiplierv set? I tried all the following and found no difference with the old or new code:

sprite.shader.colorMultiplier.value = [1.0, 0.0, 1.0, 1.0];
sprite.color = 0xFFff00ff;
sprite.setColorTransform(1, 0, 1, 1);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its handled in openfl and drawquaditem

Its the same usage as before

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a way I can verify that whatever purpose openfl_ColorMultiplierv served before, is still working after this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing the * openfl_ColorMultiplierv would just make it not react to .color

Copy link
Member

@Geokureli Geokureli Aug 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it doesn't react to .color, now, before and after this change

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On flxsprite?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit is supposed to be a simplification of the math. To replicate the same behavior.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a way I can verify that whatever purpose openfl_ColorMultiplierv served before, is still working after this change?

matrix multiplication actually.
this originally did

r, 0, 0, 0
0, g, 0, 0
0, 0, b, 0
0, 0, 0, a
(rgba is equal to xyzw, its swizzling)

image
matrices multiplied with a vector behave like this.
since all of the other values are 0, it just becomes [x * m11, y * m22, z * m33, w * m44]
and m11 is r, m22 is g, m33 is b, and m44 is a, the exact same values.
the matrix is just nothing but a middleman.

Copy link
Member

@Geokureli Geokureli Aug 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still don't know what purpose openfl_ColorMultiplierv has because I can't seem to write any code that effects this property. Until I understand what purpose it has I can't approve any changes to its behavior

On flxsprite?

Yes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var hue = 0.0;

override public function update(elapsed:Float):Void
{
	super.update(elapsed);
	sprite.color = FlxColor.fromHSB((hue += elapsed * 180) % 360, 0.5, 1.0);
}

Before my change:
https://github.com/user-attachments/assets/1ae37d92-32e9-4fc0-a94b-0bcce7ae1905

After my change:
https://github.com/user-attachments/assets/b3bb82d7-25b3-4cbb-a5a6-95a88a5b1380

@MaybeMaru
Copy link
Contributor

MaybeMaru commented Aug 26, 2024

sprite.color =

0xFFffffff 0xFFff0000
image image

@nebulazorua
Copy link

nebulazorua commented Aug 26, 2024

The intent is for the same behaviour but now its faster
the matrix code was an unnecessary step in the shader and just served to be a bit slower

If you notice no visual change pre and post change with changing sprite.color, that means its working as intended

@Geokureli
Copy link
Member

Geokureli commented Aug 26, 2024

I see the issue, for some reason defining the shader with a fragment sorce causes sprite.color to not work for some reason. This was the state I quickly made, to test this change, because I thought this was something that effected sprites with custom shaders on it:

package states;

class ShaderColorTestState extends flixel.FlxState
{
	override public function create()
	{
		super.create();
		
		final sprite = new flixel.FlxSprite().makeGraphic(100, 100);
		sprite.shader = new SimpleShader();
		sprite.color = 0xFFff00ff;
		add(sprite);
	}
}

class SimpleShader extends flixel.system.FlxAssets.FlxShader
{
	@:glFragmentSource('
		#pragma header
		
		void main()
		{
			gl_FragColor = texture2D(bitmap, openfl_TextureCoordv);
		}
	')
	public function new()
	{
		super();
	}
}

the above resulted in a white square, when I was expecting a magenta square, if I comment out the glFragmentSource, it seems to work, which is odd, but that's unrelated to this.

Thanks!

@Geokureli Geokureli merged commit a1dfd99 into HaxeFlixel:dev Aug 26, 2024
11 checks passed
@Geokureli
Copy link
Member

made a new issue of the behavior specified above: #3246

@Geokureli Geokureli added this to the 5.9.0 milestone Aug 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants