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

Funkin changes #434

Closed
wants to merge 21 commits into from
Closed
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
acb43b2
Added first working iteration of FlxRuntimeShader.
EliteMasterEric Jul 18, 2022
2d39cd7
Merge pull request #1 from MasterEric/feature/flx-runtime-shader
EliteMasterEric Jul 22, 2022
dd3157a
getBitmapData/setBitmapData, reduced code duplication
EliteMasterEric Jul 29, 2022
d83dd75
Merge remote-tracking branch 'flixel/dev' into dev
EliteMasterEric Dec 8, 2022
157eaf3
Merge remote-tracking branch 'origin/feature/flx-runtime-shader' into…
EliteMasterEric Dec 8, 2022
752c3d7
Change glVersion to a string
EliteMasterEric Dec 9, 2022
f107166
Merge pull request #2 from HaxeFlixel/dev
EliteMasterEric Feb 1, 2023
6430c7f
Merge pull request #3 from HaxeFlixel/dev
EliteMasterEric Jun 28, 2023
c51568b
Merge remote-tracking branch 'flixel/dev' into HEAD
EliteMasterEric Aug 25, 2023
0d9022a
Fix bad merge
EliteMasterEric Sep 2, 2023
c8c41e2
ACTUALLY fix merge
EliteMasterEric Sep 2, 2023
56ff320
Fall back to per-platform default shader
EliteMasterEric Dec 15, 2023
fd3aecd
Merge remote-tracking branch 'flixel/dev' into eric-dev
EliteMasterEric Dec 15, 2023
a523c3b
Add support for clipRects to FlxTiledSprite.
EliteMasterEric Feb 3, 2024
4b45f98
Merge remote-tracking branch 'flixel/dev' into dev
EliteMasterEric May 28, 2024
df575b0
fix for FlxShader thingie
ninjamuffin99 Jun 10, 2024
d98f502
Merge branch 'dev' into funkin-changes
Geokureli Jun 12, 2024
f1522a7
regen whenever cliprect is set
Geokureli Jun 12, 2024
bb62c1e
fix cliprect on tiled sprites
Geokureli Jun 12, 2024
9734937
style stuff
Geokureli Jun 12, 2024
37c8fcf
Merge branch 'dev' into funkin-changes
Geokureli Dec 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 57 additions & 54 deletions flixel/addons/display/FlxRuntimeShader.hx
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,29 @@ import lime.utils.Float32Array;
import openfl.display.BitmapData;
import openfl.display.ShaderInput;
import openfl.display.ShaderParameter;
import openfl.display.ShaderParameterType;

using StringTools;

/**
* An wrapper for Flixel/OpenFL's shaders, which takes fragment and vertex source
* in the constructor instead of using macros so it can be provided at runtime.
*
* in the constructor instead of using macros, so it can be provided data
* at runtime (for example, when using mods).
*
* HOW TO USE:
* 1. Create an instance of this class, passing the text of the `.frag` and `.vert` files.
* Note that you can set either of these to null (making them both null would make the shader do nothing???).
* 2. Use `flxSprite.shader = runtimeShader` to apply the shader to the sprite.
* 3. Use `runtimeShader.setFloat()`, `setBool()` etc. to modify any uniforms.
* 4. Use `setBitmapData()` to add additional textures as `sampler2D` uniforms
*
* @author MasterEric
* @author Mihai Alexandru (M.A. Jigsaw)
*
* @see https://github.com/openfl/openfl/blob/develop/src/openfl/utils/_internal/ShaderMacro.hx
* @see https://dixonary.co.uk/blog/shadertoy
*/
class FlxRuntimeShader extends FlxGraphicsShader
{
/**
* Creates a `FlxRuntimeShader` with specified shader sources.
* If none is provided, it will use the default shader sources.
*
* @param fragmentSource The fragment shader source.
* @param vertexSource The vertex shader source.
* Replace the `#pragma header` and `#pragma body` with the fragment shader header and body.
*/
public function new(?fragmentSource:String, ?vertexSource:String):Void
@:noCompletion private function __processFragmentSource(input:String):String
{
if (fragmentSource != null && fragmentSource.length > 0)
glFragmentSource = fragmentSource;
Expand All @@ -49,7 +48,14 @@ class FlxRuntimeShader extends FlxGraphicsShader
else
glVertexSource = FlxRuntimeShaderMacro.retrieveMetadata('glVertexSource', false);

super();
/**
* Replace the `#pragma header` and `#pragma body` with the vertex shader header and body.
*/
@:noCompletion private function __processVertexSource(input:String):String
{
var result = StringTools.replace(input, PRAGMA_HEADER, glVertexHeaderRaw);
result = StringTools.replace(result, PRAGMA_BODY, glVertexBodyRaw);
return result;
}

/**
Expand All @@ -72,11 +78,12 @@ class FlxRuntimeShader extends FlxGraphicsShader
}

/**
* Retrieve a float parameter of the shader.
* Modify a float array parameter of the shader.
*
* @param name The name of the parameter to retrieve.
* @param name The name of the parameter to modify.
* @param value The new value to use.
*/
public function getFloat(name:String):Null<Float>
public function setFloatArray(name:String, value:Array<Float>):Void
{
final shaderParameter:ShaderParameter<Float> = Reflect.field(data, name);

Expand All @@ -90,12 +97,12 @@ class FlxRuntimeShader extends FlxGraphicsShader
}

/**
* Modify a float array parameter of the shader.
* Modify an integer parameter of the shader.
*
* @param name The name of the parameter to modify.
* @param value The new value to use.
*/
public function setFloatArray(name:String, value:Array<Float>):Void
public function setInt(name:String, value:Int):Void
{
final shaderParameter:ShaderParameter<Float> = Reflect.field(data, name);

Expand All @@ -109,11 +116,12 @@ class FlxRuntimeShader extends FlxGraphicsShader
}

/**
* Retrieve a float array parameter of the shader.
* Modify an integer array parameter of the shader.
*
* @param name The name of the parameter to retrieve.
* @param name The name of the parameter to modify.
* @param value The new value to use.
*/
public function getFloatArray(name:String):Null<Array<Float>>
public function setIntArray(name:String, value:Array<Int>):Void
{
final shaderParameter:ShaderParameter<Float> = Reflect.field(data, name);

Expand All @@ -127,12 +135,11 @@ class FlxRuntimeShader extends FlxGraphicsShader
}

/**
* Modify an integer parameter of the shader.
*
* Modify a boolean parameter of the shader.
* @param name The name of the parameter to modify.
* @param value The new value to use.
*/
public function setInt(name:String, value:Int):Void
public function setBool(name:String, value:Bool):Void
{
final shaderParameter:ShaderParameter<Int> = Reflect.field(data, name);

Expand All @@ -146,11 +153,11 @@ class FlxRuntimeShader extends FlxGraphicsShader
}

/**
* Retrieve an integer parameter of the shader.
*
* @param name The name of the parameter to retrieve.
* Modify a boolean array parameter of the shader.
* @param name The name of the parameter to modify.
* @param value The new value to use.
*/
public function getInt(name:String):Null<Int>
public function setBoolArray(name:String, value:Array<Bool>):Void
{
final shaderParameter:ShaderParameter<Int> = Reflect.field(data, name);

Expand All @@ -164,12 +171,11 @@ class FlxRuntimeShader extends FlxGraphicsShader
}

/**
* Modify an integer array parameter of the shader.
*
* Modify a bitmap data parameter of the shader.
* @param name The name of the parameter to modify.
* @param value The new value to use.
*/
public function setIntArray(name:String, value:Array<Int>):Void
public function setBitmapData(name:String, value:openfl.display.BitmapData):Void
{
final shaderParameter:ShaderParameter<Int> = Reflect.field(data, name);

Expand All @@ -183,11 +189,11 @@ class FlxRuntimeShader extends FlxGraphicsShader
}

/**
* Retrieve an integer array parameter of the shader.
*
* Retrieve a float parameter of the shader.
* @param name The name of the parameter to retrieve.
* @return The value of the parameter.
*/
public function getIntArray(name:String):Null<Array<Int>>
public function getFloat(name:String):Null<Float>
{
final shaderParameter:ShaderParameter<Int> = Reflect.field(data, name);

Expand All @@ -201,12 +207,11 @@ class FlxRuntimeShader extends FlxGraphicsShader
}

/**
* Modify a bool parameter of the shader.
*
* @param name The name of the parameter to modify.
* @param value The new value to use.
* Retrieve a float array parameter of the shader.
* @param name The name of the parameter to retrieve.
* @return The value of the parameter.
*/
public function setBool(name:String, value:Bool):Void
public function getFloatArray(name:String):Null<Array<Float>>
{
final shaderParameter:ShaderParameter<Bool> = Reflect.field(data, name);

Expand All @@ -220,11 +225,11 @@ class FlxRuntimeShader extends FlxGraphicsShader
}

/**
* Retrieve a bool parameter of the shader.
*
* Retrieve an integer parameter of the shader.
* @param name The name of the parameter to retrieve.
* @return The value of the parameter.
*/
public function getBool(name:String):Null<Bool>
public function getInt(name:String):Null<Int>
{
final shaderParameter:ShaderParameter<Bool> = Reflect.field(data, name);

Expand All @@ -238,12 +243,11 @@ class FlxRuntimeShader extends FlxGraphicsShader
}

/**
* Modify a bool array parameter of the shader.
*
* @param name The name of the parameter to modify.
* @param value The new value to use.
* Retrieve an integer array parameter of the shader.
* @param name The name of the parameter to retrieve.
* @return The value of the parameter.
*/
public function setBoolArray(name:String, value:Array<Bool>):Void
public function getIntArray(name:String):Null<Array<Int>>
{
final shaderParameter:ShaderParameter<Bool> = Reflect.field(data, name);

Expand All @@ -257,11 +261,11 @@ class FlxRuntimeShader extends FlxGraphicsShader
}

/**
* Retrieve a bool array parameter of the shader.
*
* Retrieve a boolean parameter of the shader.
* @param name The name of the parameter to retrieve.
* @return The value of the parameter.
*/
public function getBoolArray(name:String):Null<Array<Bool>>
public function getBool(name:String):Null<Bool>
{
final shaderParameter:ShaderParameter<Bool> = Reflect.field(data, name);

Expand All @@ -280,7 +284,7 @@ class FlxRuntimeShader extends FlxGraphicsShader
* @param name The name of the parameter to modify.
* @param value The new value to use.
*/
public function setSampler2D(name:String, value:BitmapData):Void
public function getBoolArray(name:String):Null<Array<Bool>>
{
final shaderInput:ShaderInput<BitmapData> = Reflect.field(data, name);

Expand All @@ -299,7 +303,7 @@ class FlxRuntimeShader extends FlxGraphicsShader
* @param name The name of the parameter to retrieve.
* @return The value of the parameter.
*/
public function getSampler2D(name:String):Null<BitmapData>
public function getBitmapData(name:String):Null<openfl.display.BitmapData>
{
final shaderInput:ShaderInput<BitmapData> = Reflect.field(data, name);

Expand Down Expand Up @@ -530,4 +534,3 @@ class FlxRuntimeShader extends FlxGraphicsShader
return __glVertexSource = value;
}
}
#end
Loading