kem0x / FNF-ModShaders

Shaders in FNF with Haxe
MIT License
19 stars 3 forks source link

Shader stealing porting 101

1. Adding this to FNF:

(THIS MIGHT BREAK ANY SHADERS YOU USED BEFORE)

import DynamicShaderHandler;
public static var animatedShaders:Map<String, DynamicShaderHandler> = new Map<String, DynamicShaderHandler>();
public var luaShaders:Map<String, DynamicShaderHandler> = new Map<String, DynamicShaderHandler>();
public var camTarget:FlxCamera;

Lua_helper.add_callback(lua, "createShaders", function(shaderName, ?optimize:Bool = false) { var shader = new DynamicShaderHandler(shaderName, optimize);

return shaderName;

});

Lua_helper.add_callback(lua, "modifyShaderProperty", function(shaderName, propertyName, value) { var handler = luaShaders[shaderName]; handler.modifyShaderProperty(propertyName, value); });

// shader set

Lua_helper.add_callback(lua, "setShadersToCamera", function(shaderName:Array, cameraName) { switch (cameraName) { case 'hud': camTarget = PlayState.instance.camHUD; case 'notes': camTarget = PlayState.instance.camNotes; case 'sustains': camTarget = PlayState.instance.camSustains; case 'game': camTarget = FlxG.camera; }

var shaderArray = new Array<BitmapFilter>();

for (i in shaderName)
{
    shaderArray.push(new ShaderFilter(luaShaders[i].shader));
}

camTarget.setFilters(shaderArray);

});

// shader clear

Lua_helper.add_callback(lua, "clearShadersFromCamera", function(cameraName) { switch (cameraName) { case 'hud': camTarget = PlayState.instance.camHUD; case 'notes': camTarget = PlayState.instance.camNotes; case 'sustains': camTarget = PlayState.instance.camSustains; case 'game': camTarget = FlxG.camera; } camTarget.setFilters([]); });


---

## 2. Porting Shaders from shadertoy:

**(PLEASE MAKE SURE TO READ THE LICENSE OF THE SHADER YOU ARE PORTING)**
- First of all, you need to know that shadertoy automatically uses the inputs below:

 ```cpp
 uniform vec3      iResolution;           // viewport resolution (in pixels)
 uniform float     iTime;                 // shader playback time (in seconds)
 uniform float     iTimeDelta;            // render time (in seconds)
 uniform int       iFrame;                // shader playback frame
 uniform float     iChannelTime[4];       // channel playback time (in seconds)
 uniform vec3      iChannelResolution[4]; // channel resolution (in pixels)
 uniform vec4      iMouse;                // mouse pixel coords. xy: current (if MLB down), zw: click
 uniform samplerXX iChannel0..3;          // input channel. XX = 2D/Cube
 uniform vec4      iDate;                 // (year, month, day, time in seconds)
 uniform float     iSampleRate;           // sound sample rate (i.e., 44100)

Currently, my implementation only supports the following inputs: (iResolution, iTime, iChannel0).

Support for iMouse input is planned for the future.