Skip to content

Commit

Permalink
closes #755
Browse files Browse the repository at this point in the history
you'll have to do your own testing btw, I'm too tired (and lazy) :P
but it looks fine to me, so it should work in theory (and yeah, it works the same as setSpriteShader, you must initialize the shader first before you can use it)
  • Loading branch information
moxie-coder committed Jan 1, 2025
1 parent 82b3b98 commit c690435
Showing 1 changed file with 70 additions and 3 deletions.
73 changes: 70 additions & 3 deletions source/FunkinLua.hx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import flixel.util.FlxSave;
import flixel.addons.transition.FlxTransitionableState;
import flixel.system.FlxAssets.FlxShader;
import Shaders;
import openfl.filters.ShaderFilter;

#if (!flash && sys)
import flixel.addons.display.FlxRuntimeShader;
Expand Down Expand Up @@ -65,6 +66,10 @@ class FunkinLua {
public static var Function_Continue:Dynamic = "##PSYCHLUA_FUNCTIONCONTINUE";
public static var Function_StopLua:Dynamic = "##PSYCHLUA_FUNCTIONSTOPLUA";

#if (!flash && MODS_ALLOWED && sys)
private static var storedFilters:Map<String, ShaderFilter> = []; // for a few shader functions
#end

//public var errorHandler:String->Void;
#if LUA_ALLOWED
public var lua:State = null;
Expand Down Expand Up @@ -276,7 +281,7 @@ class FunkinLua {
#if (!flash && MODS_ALLOWED && sys)
if(!PlayState.instance.runtimeShaders.exists(shader) && !initLuaShader(shader))
{
luaTrace('setSpriteShader: Shader $shader is missing!', false, false, FlxColor.RED);
luaTrace('setSpriteShader: Shader $shader is missing! Make sure you\'ve initalized your shader first!', false, false, FlxColor.RED);
return false;
}

Expand Down Expand Up @@ -310,6 +315,61 @@ class FunkinLua {
return false;
});

// camera shaders
Lua_helper.add_callback(lua, "setCameraShader", function(cam:String, shader:String, ?index:String) {
final funk = PlayState.instance;
if (!ClientPrefs.shaders) return false;

if (index == null || index.length < 1)
index = shader;

#if (!flash && MODS_ALLOWED && sys)
if (!funk.runtimeShaders.exists(shader) && !funk.initLuaShader(shader)) {
luaTrace('addShaderToCam: Shader $shader is missing! Make sure you\'ve initalized your shader first!', false, false, FlxColor.RED);
return false;
}

var arr:Array<String> = funk.runtimeShaders.get(shader);
// Both FlxGame and FlxCamera has a _filters array and a setFilters function
// We should maybe make an interface for that?
var camera = getCam(cam);
@:privateAccess {
if (camera._filters == null)
camera._filters = [];
final filter = new ShaderFilter(new FlxRuntimeShader(arr[0], arr[1]));
storedFilters.set(index, filter);
camera._filters.push(filter);
}
return true;
#else
luaTrace("addShaderToCam: Platform unsupported for Runtime Shaders!", false, false, FlxColor.RED);
#end
return false;
});

Lua_helper.add_callback(lua, "removeCameraShader", function(cam:String, shader:String) {
#if (!flash && MODS_ALLOWED && sys)
final camera = getCam(cam);
@:privateAccess {
if(!storedFilters.exists(shader)) {
luaTrace('removeCamShader: $shader does not exist!', false, false, FlxColor.YELLOW);
return false;
}

if (camera.filters == null) {
luaTrace('removeCamShader: camera $cam does not have any shaders!', false, false, FlxColor.YELLOW);
return false;
}

camera.filters.remove(storedFilters.get(shader));
storedFilters.remove(shader);
return true;
}
#else
luaTrace('removeCamShader: Platform unsupported for Runtime Shaders!', false, false, FlxColor.RED);
#end
return false;
});

Lua_helper.add_callback(lua, "getShaderBool", function(obj:String, prop:String) {
#if (!flash && MODS_ALLOWED && sys)
Expand Down Expand Up @@ -3227,14 +3287,21 @@ class FunkinLua {
return NORMAL;
}

function cameraFromString(cam:String):FlxCamera {
static function cameraFromString(cam:String):FlxCamera {
switch(cam.toLowerCase()) {
case 'camhud' | 'hud': return PlayState.instance.camHUD;
case 'camother' | 'other': return PlayState.instance.camOther;
}
return PlayState.instance.camGame;
}

// alias for above, helper function basically
public static function getCam(obj:String):Dynamic {
if (obj.toLowerCase().trim() == "global")
return FlxG.game;
return cameraFromString(obj);
}

public static function luaTrace(text:String, ignoreCheck:Bool = false, deprecated:Bool = false, color:FlxColor = FlxColor.WHITE) {
#if LUA_ALLOWED
if(ignoreCheck || getBool('luaDebugMode')) {
Expand Down Expand Up @@ -3524,7 +3591,7 @@ class HScript
#if (!flash && sys)
interp.variables.set('FlxRuntimeShader', FlxRuntimeShader);
#end
interp.variables.set('ShaderFilter', openfl.filters.ShaderFilter);
interp.variables.set('ShaderFilter', ShaderFilter);
interp.variables.set('StringTools', StringTools);

interp.variables.set('setVar', function(name:String, value:Dynamic)
Expand Down

4 comments on commit c690435

@SyncGit12
Copy link

Choose a reason for hiding this comment

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

thank you moxie i love you ill invite you to a mcdonalds

last part is /j

@JordanSantiagoYT
Copy link
Owner

Choose a reason for hiding this comment

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

wait.

weren't you able to do this before using runHaxeCode

@moxie-coder
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

wait.

weren't you able to do this before using runHaxeCode

not that I think, I don't know if it could access runtimeShaders, well if it was possible, then this is more or less an easier way to do it without HScript, but I could look into it to see if it is possible via hscript that way

@JordanSantiagoYT
Copy link
Owner

Choose a reason for hiding this comment

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

i mean, this is what i did before to apply frag shaders to cameras:

		runHaxeCode([[
      			game.camGame.setFilters([new ShaderFilter(game.getLuaObject("temporaryShader").shader)]);
      	 		game.camHUD.setFilters([new ShaderFilter(game.getLuaObject("temporaryShader").shader)]);
   		]])

but itll be nice to have an easier way of doing it now

Please sign in to comment.