Funkin/source/shaderslmfao/WiggleEffect.hx
2022-03-01 20:57:05 -05:00

159 lines
3.7 KiB
Haxe

package shaderslmfao;
// STOLEN FROM HAXEFLIXEL DEMO LOL
import flixel.system.FlxAssets.FlxShader;
enum WiggleEffectType
{
DREAMY;
WAVY;
HEAT_WAVE_HORIZONTAL;
HEAT_WAVE_VERTICAL;
FLAG;
}
class WiggleEffect
{
public var shader(default, null):WiggleShader = new WiggleShader();
public var effectType(default, set):WiggleEffectType = DREAMY;
public var waveSpeed(default, set):Float = 0;
public var waveFrequency(default, set):Float = 0;
public var waveAmplitude(default, set):Float = 0;
public function new(speed:Float, freq:Float, amplitude:Float, effect:WiggleEffectType = DREAMY):Void
{
shader.uTime.value = [0];
this.waveSpeed = speed;
this.waveFrequency = freq;
this.waveAmplitude = amplitude;
this.effectType = effect;
}
public function update(elapsed:Float):Void
{
shader.uTime.value[0] += elapsed;
}
function set_effectType(v:WiggleEffectType):WiggleEffectType
{
effectType = v;
shader.effectType.value = [WiggleEffectType.getConstructors().indexOf(Std.string(v))];
return v;
}
function set_waveSpeed(v:Float):Float
{
waveSpeed = v;
shader.uSpeed.value = [waveSpeed];
return v;
}
function set_waveFrequency(v:Float):Float
{
waveFrequency = v;
shader.uFrequency.value = [waveFrequency];
return v;
}
function set_waveAmplitude(v:Float):Float
{
waveAmplitude = v;
shader.uWaveAmplitude.value = [waveAmplitude];
return v;
}
function toString()
{
return 'WiggleEffect(${shader.uTime.value[0]})';
}
}
class WiggleShader extends FlxShader
{
@:glFragmentSource('
#pragma header
//uniform float tx, ty; // x,y waves phase
uniform float uTime;
const int EFFECT_TYPE_DREAMY = 0;
const int EFFECT_TYPE_WAVY = 1;
const int EFFECT_TYPE_HEAT_WAVE_HORIZONTAL = 2;
const int EFFECT_TYPE_HEAT_WAVE_VERTICAL = 3;
const int EFFECT_TYPE_FLAG = 4;
uniform int effectType;
/**
* How fast the waves move over time
*/
uniform float uSpeed;
/**
* Number of waves over time
*/
uniform float uFrequency;
/**
* How much the pixels are going to stretch over the waves
*/
uniform float uWaveAmplitude;
vec2 sineWave(vec2 pt)
{
float x = 0.0;
float y = 0.0;
if (effectType == EFFECT_TYPE_DREAMY)
{
float w = 1 / openfl_TextureSize.y;
float h = 1 / openfl_TextureSize.x;
// look mom, I know how to write shaders now
pt.x = floor(pt.x / h) * h;
float offsetX = sin(pt.x * uFrequency + uTime * uSpeed) * uWaveAmplitude;
pt.y += floor(offsetX / w) * w; // * (pt.y - 1.0); // <- Uncomment to stop bottom part of the screen from moving
pt.y = floor(pt.y / w) * w;
float offsetY = sin(pt.y * (uFrequency / 2.0) + uTime * (uSpeed / 2.0)) * (uWaveAmplitude / 2.0);
pt.x += floor(offsetY / h) * h; // * (pt.y - 1.0); // <- Uncomment to stop bottom part of the screen from moving
}
else if (effectType == EFFECT_TYPE_WAVY)
{
float offsetY = sin(pt.x * uFrequency + uTime * uSpeed) * uWaveAmplitude;
pt.y += offsetY; // * (pt.y - 1.0); // <- Uncomment to stop bottom part of the screen from moving
}
else if (effectType == EFFECT_TYPE_HEAT_WAVE_HORIZONTAL)
{
x = sin(pt.x * uFrequency + uTime * uSpeed) * uWaveAmplitude;
}
else if (effectType == EFFECT_TYPE_HEAT_WAVE_VERTICAL)
{
y = sin(pt.y * uFrequency + uTime * uSpeed) * uWaveAmplitude;
}
else if (effectType == EFFECT_TYPE_FLAG)
{
y = sin(pt.y * uFrequency + 10.0 * pt.x + uTime * uSpeed) * uWaveAmplitude;
x = sin(pt.x * uFrequency + 5.0 * pt.y + uTime * uSpeed) * uWaveAmplitude;
}
return vec2(pt.x + x, pt.y + y);
}
void main()
{
vec2 uv = sineWave(openfl_TextureCoordv);
gl_FragColor = texture2D(bitmap, uv);
}')
public function new()
{
super();
}
}