Skip to content

Commit

Permalink
shader support
Browse files Browse the repository at this point in the history
  • Loading branch information
Joalor64GH authored Dec 3, 2024
1 parent c348a72 commit 4e6dde3
Show file tree
Hide file tree
Showing 17 changed files with 260 additions and 27 deletions.
4 changes: 2 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
* [ ] Fix Controls settings (Also the controls will crash when enter ACCEPT or something)
* [ ] Make a better charting menu (optional)
* [ ] Rewrite the gameplay (like note too far for each one)
* [X] Shader Support

## Future To-Dos
* [ ] Shader Support
* [ ] Sustain Notes + New song
* [ ] Improve Chart Editor
* [ ] Silly Visualizer Option
* [ ] Gameplay Modifiers
* [ ] Noteskins
* [ ] Noteskins
Empty file.
4 changes: 2 additions & 2 deletions assets/songs/test/script.hxs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import('flixel.FlxSprite');
import('states.PlayState');

function create() {
var spr:FlxSprite = new FlxSprite(0, 0).makeGraphic(50, 50, FlxColor.BLACK);
PlayState.instance.add(spr);
var spr:FlxSprite = new FlxSprite(0, 0).makeGraphic(50, 50, FlxColor.BLACK);
PlayState.instance.add(spr);
}
15 changes: 13 additions & 2 deletions assets/songs/tutorial/script.hxs
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import('flixel.text.FlxText');
import('flixel.tweens.FlxTween');
import('flixel.tweens.FlxEase');
import('backend.Conductor');
import('states.PlayState');

var txt:FlxText;

function create() {
var txt:FlxText = new FlxText(0, 0, 0, "Hit the notes to the beat of the song!", 48).screenCenter();
PlayState.instance.add(txt);
txt = new FlxText(0, 0, 0, "Hit the notes to the beat of the song!", 48).screenCenter();
PlayState.instance.add(txt);
}

function beatHit(curBeat:Int) {
if (curBeat >= 0) {
FlxTween.tween(txt, {alpha: 0}, Conductor.crochet / 1000, {ease: FlxEase.quadOut});
}
}
3 changes: 2 additions & 1 deletion docs/Creating-a-Mod.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Each folder in your mod should be used as follows:
* `languages` - For storing language data.
* `music` - Non-gameplay related music.
* `scripts` - Scripts that run on every song.
* `shaders` - `.frag` or `.vert` shader file that .
* `songs` - Songs used for gameplay.
* `songs/[song-name]/chart.json` - Your song's chart.
* `songs/[song-name]/music.ogg` - Your song's music. Can also be a `.wav`.
Expand All @@ -57,4 +58,4 @@ Also, when it's neccessary, delete any folders you don't need.
For further documentation, check out [polymod.io](https://polymod.io/docs/).

## Quick Example
Here's a quick example by [EliteMasterEric](https://twitter.com/EliteMasterEric) » [here](https://github.com/EnigmaEngine/ModCore-Tricky-Mod) «
Here's a quick example by [EliteMasterEric](https://twitter.com/EliteMasterEric) » [here](https://github.com/EnigmaEngine/ModCore-Tricky-Mod) «
1 change: 1 addition & 0 deletions docs/Scripting.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Otherwise, here is a list of the current classes you can use that are already im
* `FlxGroup`
* `FlxMath`
* `FlxObject`
* `FlxRuntimeShader`
* `FlxSave`
* `FlxSort`
* `FlxSound`
Expand Down
Empty file.
4 changes: 2 additions & 2 deletions source/backend/ExtendableState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import backend.Conductor.TimeScaleChangeEvent;
import flixel.addons.transition.FlxTransitionableState;

class ExtendableState extends FlxState {
var curBeat:Int = 0;
var curStep:Int = 0;
public var curBeat:Int = 0;
public var curStep:Int = 0;

override function create() {
super.create();
Expand Down
4 changes: 2 additions & 2 deletions source/backend/ExtendableSubState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package backend;
import backend.Conductor.BPMChangeEvent;

class ExtendableSubState extends FlxSubState {
var curStep:Int = 0;
var curBeat:Int = 0;
public var curStep:Int = 0;
public var curBeat:Int = 0;

override function create() {
super.create();
Expand Down
6 changes: 6 additions & 0 deletions source/backend/Paths.hx
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ class Paths {
inline static public function script(key:String)
return file('$key.hxs');

inline static public function frag(key:String)
return file('shaders/$key.frag');

inline static public function vert(key:String)
return file('shaders/$key.vert');

static public function sound(key:String, ?cache:Bool = true):Sound
return returnSound('sounds/$key', cache);

Expand Down
2 changes: 2 additions & 0 deletions source/import.hx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import flixel.util.*;
import flixel.math.*;
import flixel.addons.display.FlxBackdrop;
import flixel.addons.display.FlxGridOverlay;
import flixel.addons.display.FlxRuntimeShader;
import flixel.graphics.frames.FlxAtlasFrames;
import flixel.group.FlxGroup;
import flixel.group.FlxGroup.FlxTypedGroup;
Expand Down Expand Up @@ -38,6 +39,7 @@ import substates.*;
import backend.*;
import modding.*;
import objects.*;
import shaders.*;

using Globals;
using StringTools;
Expand Down
1 change: 1 addition & 0 deletions source/modding/Hscript.hx
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class Hscript extends FlxBasic {
setVariable('FlxGroup', FlxGroup);
setVariable('FlxMath', FlxMath);
setVariable('FlxObject', FlxObject);
setVariable('FlxRuntimeShader', FlxRuntimeShader);
setVariable('FlxSave', FlxSave);
setVariable('FlxSort', FlxSort);
setVariable('FlxSound', FlxSound);
Expand Down
23 changes: 11 additions & 12 deletions source/objects/ColorSwap.hx → source/shaders/ColorSwap.hx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package objects;
package shaders;

import flixel.system.FlxAssets.FlxShader;

Expand Down Expand Up @@ -35,21 +35,20 @@ class ColorSwap {

class ColorSwapShader extends FlxShader {
@:glFragmentSource('
#pragma header
#pragma header

uniform float red;
uniform float green;
uniform float blue;
uniform float red;
uniform float green;
uniform float blue;

void main() {

vec4 col = flixel_texture2D(bitmap, openfl_TextureCoordv);
void main() {
vec4 col = flixel_texture2D(bitmap, openfl_TextureCoordv);
// Get difference to use for falloff if required
float diff = col.r - ((col.g + col.b) / 2.0);
// Get difference to use for falloff if required
float diff = col.r - ((col.g + col.b) / 2.0);
gl_FragColor = vec4(((col.g + col.b) / 2.0) + (red * diff), col.g + (green * diff), col.b + (blue * diff), col.a);
}
gl_FragColor = vec4(((col.g + col.b) / 2.0) + (red * diff), col.g + (green * diff), col.b + (blue * diff), col.a);
}
')
public function new() {
super();
Expand Down
176 changes: 176 additions & 0 deletions source/shaders/HueDisplacer.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package shaders;

import flixel.graphics.tile.FlxGraphicsShader;

class HueDisplacer {
public var shader(default, null):HueDisplacerShader = new HueDisplacerShader();
public var hasOutline:Bool = false;
public var hueShit:Float = 0;

public function new():Void {
shader.uTime.value = [0];
shader.money.value = [0];
shader.awesomeOutline.value = [hasOutline];
}

public function update(elapsed:Float):Void {
shader.uTime.value[0] += elapsed;
hueShit += elapsed;
}
}

class HueDisplacerShader extends FlxGraphicsShader {
@:glFragmentSource('
varying float openfl_Alphav;
varying vec4 openfl_ColorMultiplierv;
varying vec4 openfl_ColorOffsetv;
varying vec2 openfl_TextureCoordv;

uniform bool openfl_HasColorTransform;
uniform vec2 openfl_TextureSize;
uniform sampler2D bitmap;

uniform bool hasTransform;
uniform bool hasColorTransform;

vec4 flixel_texture2D(sampler2D bitmap, vec2 coord)
{
vec4 color = texture2D(bitmap, coord);
if (!hasTransform)
{
return color;
}

if (color.a == 0.0)
{
return vec4(0.0, 0.0, 0.0, 0.0);
}

if (!hasColorTransform)
{
return color * openfl_Alphav;
}

color = vec4(color.rgb / color.a, color.a);

mat4 colorMultiplier = mat4(0);
colorMultiplier[0][0] = openfl_ColorMultiplierv.x;
colorMultiplier[1][1] = openfl_ColorMultiplierv.y;
colorMultiplier[2][2] = openfl_ColorMultiplierv.z;
colorMultiplier[3][3] = openfl_ColorMultiplierv.w;

color = clamp(openfl_ColorOffsetv + (color * colorMultiplier), 0.0, 1.0);

if (color.a > 0.0)
{
return vec4(color.rgb * color.a * openfl_Alphav, color.a * openfl_Alphav);
}
return vec4(0.0, 0.0, 0.0, 0.0);
}

uniform float uTime;
uniform float money;
uniform bool awesomeOutline;

const float offset = 1.0 / 128.0;
vec3 normalizeColor(vec3 color)
{
return vec3(
color[0] / 255.0,
color[1] / 255.0,
color[2] / 255.0
);
}

vec3 rgb2hsv(vec3 c)
{
vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));

float d = q.x - min(q.w, q.y);
float e = 1.0e - 10;
return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}

vec3 hsv2rgb(vec3 c)
{
vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}

void main()
{
vec4 color = flixel_texture2D(bitmap, openfl_TextureCoordv);
vec4 swagColor = vec4(rgb2hsv(vec3(color[0], color[1], color[2])), color[3]);
swagColor[0] += uTime;
color = vec4(hsv2rgb(vec3(swagColor[0], swagColor[1], swagColor[2])), swagColor[3]);

if (awesomeOutline)
{
vec2 size = vec2(3, 3);

if (color.a <= 0.5) {
float w = size.x / openfl_TextureSize.x;
float h = size.y / openfl_TextureSize.y;
if (flixel_texture2D(bitmap, vec2(openfl_TextureCoordv.x + w, openfl_TextureCoordv.y)).a != 0.
|| flixel_texture2D(bitmap, vec2(openfl_TextureCoordv.x - w, openfl_TextureCoordv.y)).a != 0.
|| flixel_texture2D(bitmap, vec2(openfl_TextureCoordv.x, openfl_TextureCoordv.y + h)).a != 0.
|| flixel_texture2D(bitmap, vec2(openfl_TextureCoordv.x, openfl_TextureCoordv.y - h)).a != 0.)
color = vec4(1.0, 1.0, 1.0, 1.0);
}
}
gl_FragColor = color;
}')
@:glVertexSource('
attribute float openfl_Alpha;
attribute vec4 openfl_ColorMultiplier;
attribute vec4 openfl_ColorOffset;
attribute vec4 openfl_Position;
attribute vec2 openfl_TextureCoord;

varying float openfl_Alphav;
varying vec4 openfl_ColorMultiplierv;
varying vec4 openfl_ColorOffsetv;
varying vec2 openfl_TextureCoordv;

uniform mat4 openfl_Matrix;
uniform bool openfl_HasColorTransform;
uniform vec2 openfl_TextureSize;

attribute float alpha;
attribute vec4 colorMultiplier;
attribute vec4 colorOffset;
uniform bool hasColorTransform;
void main(void)
{
openfl_Alphav = openfl_Alpha;
openfl_TextureCoordv = openfl_TextureCoord;

if (openfl_HasColorTransform) {
openfl_ColorMultiplierv = openfl_ColorMultiplier;
openfl_ColorOffsetv = openfl_ColorOffset / 255.0;
}

gl_Position = openfl_Matrix * openfl_Position;
openfl_Alphav = openfl_Alpha * alpha;
if (hasColorTransform)
{
openfl_ColorOffsetv = colorOffset / 255.0;
openfl_ColorMultiplierv = colorMultiplier;
}
}')
public function new() {
super();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package objects;
package shaders;

class NoteColors {
public static var noteColors:Array<Array<Int>> = SaveData.settings.notesRGB;
Expand Down
Loading

0 comments on commit 4e6dde3

Please sign in to comment.