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

Added support for shaders in FlxStrip and FlxDrawTrianglesItem #2671

Merged
merged 8 commits into from
Dec 2, 2022

Conversation

UncertainProd
Copy link
Contributor

This pr adds support for shaders with the FlxStrip class.
When I was working with the FlxStrip class I noticed that setting its shader property or alpha property did not have any effect on it, while it did work on regular FlxSprites. It looks like it's because FlxCamera.drawTriangles does not take in any parameters related to them, so I added those in this pr.

Example code:

package;

import flixel.FlxG;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.FlxStrip;
import flixel.graphics.tile.FlxDrawTrianglesItem.DrawData;
import flixel.system.FlxAssets.FlxShader;
import flixel.util.FlxColor;

class PlayState extends FlxState
{
	var shaderStrip:FlxStrip;
	var alphaStrip:FlxStrip;
        var normalsprite:FlxSprite;

        var simpleShader:GreenShader;
	override public function create()
	{
		super.create();

		shaderStrip = new FlxStrip(50, 50);
		shaderStrip.makeGraphic(100, 100, FlxColor.YELLOW);
		shaderStrip.vertices = DrawData.ofArray([100.0, 100.0, 150, 100, 125, 150]);
		shaderStrip.indices = DrawData.ofArray([0, 1, 2]);
		shaderStrip.uvtData = DrawData.ofArray([0, 0, 0, 1, 1, 1.0]);
		shaderStrip.shader = new GreenShader();
		add(shaderStrip);

                alphaStrip = new FlxStrip(0, 0);
		alphaStrip.makeGraphic(100, 100, FlxColor.RED);
		alphaStrip.vertices = DrawData.ofArray([300.0, 100.0, 350, 100, 325, 150]);
		alphaStrip.indices = DrawData.ofArray([0, 1, 2]);
		alphaStrip.uvtData = DrawData.ofArray([0, 0, 0, 1, 1, 1.0]);
		add(alphaStrip);

		normalsprite = new FlxSprite(0, 0).makeGraphic(100, 100, FlxColor.BLUE);
		normalsprite.shader = new GreenShader();
		add(normalsprite);
	}

	override function update(elapsed:Float)
	{
		if(FlxG.keys.pressed.A)
		{
			alphaStrip.alpha -= 0.01;
		}
		if(FlxG.keys.pressed.D)
		{
			alphaStrip.alpha += 0.01;
		}
		super.update(elapsed);
	}
}

class GreenShader extends FlxShader
{
	@glFragmentSource('
	#pragma header

	void main()
	{
		vec4 clr = texture2D(bitmap, openfl_TextureCoordv);
		gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0) * clr.a;
	}
	')
	public function new()
	{
		super();
	}
}

When I tested this on windows and html5 it worked

@Geokureli
Copy link
Member

Thanks for the example, I'll try to get to this soon

@Geokureli
Copy link
Member

looks like it's failing on flash. You don't need to go out of your way to make this feature work in flash, but you do need to use #if flash checks to prevent compile errors on flash

@UncertainProd
Copy link
Contributor Author

Alright, I added in those checks

@Geokureli
Copy link
Member

thanks, will check this manually soon

@Geokureli
Copy link
Member

This rules! Thanks, UncertainProd.

This was my edited test example:
https://github.com/Geokureli/flixel-tests/blob/main/Source/states/FlxStripShaderTestState.hx

@Geokureli Geokureli merged commit 83fab90 into HaxeFlixel:dev Dec 2, 2022
Geokureli added a commit that referenced this pull request Dec 2, 2022
Geokureli added a commit that referenced this pull request Dec 15, 2022
Geokureli added a commit that referenced this pull request Dec 15, 2022
Geokureli added a commit that referenced this pull request Dec 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants