From 60108122049292bbcfe90d8322d97bf4852449c8 Mon Sep 17 00:00:00 2001 From: KadeDeveloper Date: Mon, 26 Jul 2021 13:43:01 -0700 Subject: [PATCH] shaders and editor optimizations --- source/ChartingState.hx | 31 ++++++++++++++++- source/FreeplayState.hx | 7 ++-- source/Main.hx | 4 +++ source/ModchartShader.hx | 74 ++++++++++++++++++++++++++++++++++++++++ source/ModchartState.hx | 37 ++++++++++++++++++++ source/PlayState.hx | 17 +++++++-- source/ShaderShader.hx | 41 ++++++++++++++++++++++ 7 files changed, 206 insertions(+), 5 deletions(-) create mode 100644 source/ModchartShader.hx create mode 100644 source/ShaderShader.hx diff --git a/source/ChartingState.hx b/source/ChartingState.hx index 834064b..ad7654e 100644 --- a/source/ChartingState.hx +++ b/source/ChartingState.hx @@ -1,7 +1,6 @@ package; import openfl.system.System; -import lime.app.Application; #if sys import sys.io.File; #end @@ -1491,6 +1490,36 @@ class ChartingState extends MusicBeatState { updateHeads(); + for(i in sectionRenderes) + { + var diff = getYfromStrum(i.y) - Conductor.songPosition; + if (diff < 10000 && diff >= -40000) + { + i.active = true; + i.visible = true; + } + else + { + i.active = false; + i.visible = false; + } + } + + for(i in curRenderedNotes) + { + var diff = i.strumTime - Conductor.songPosition; + if (diff < 10000 && diff >= -40000) + { + i.active = true; + i.visible = true; + } + else + { + i.active = false; + i.visible = false; + } + } + var doInput = true; for (i in Typeables) diff --git a/source/FreeplayState.hx b/source/FreeplayState.hx index d1efd2c..96d0c30 100644 --- a/source/FreeplayState.hx +++ b/source/FreeplayState.hx @@ -85,10 +85,10 @@ class FreeplayState extends MusicBeatState } var diffs = []; - - var diffsThatExist = []; + + #if sys if (FileSystem.exists('assets/data/${format}/${format}-hard.json')) diffsThatExist.push("Hard"); if (FileSystem.exists('assets/data/${format}/${format}-easy.json')) @@ -101,6 +101,9 @@ class FreeplayState extends MusicBeatState Application.current.window.alert("No difficulties found for chart, skipping.",meta.songName + " Chart"); continue; } + #else + diffsThatExist = ["Easy","Normal","Hard"]; + #end if (diffsThatExist.contains("Easy")) FreeplayState.loadDiff(0,format,meta.songName,diffs); if (diffsThatExist.contains("Normal")) diff --git a/source/Main.hx b/source/Main.hx index 788ba61..f17cec3 100644 --- a/source/Main.hx +++ b/source/Main.hx @@ -78,6 +78,10 @@ class Main extends Sprite gameHeight = Math.ceil(stageHeight / zoom); } + #if !cpp + framerate = 60; + #end + #if cpp initialState = Caching; game = new FlxGame(gameWidth, gameHeight, initialState, zoom, framerate, framerate, skipSplash, startFullscreen); diff --git a/source/ModchartShader.hx b/source/ModchartShader.hx new file mode 100644 index 0000000..9077d22 --- /dev/null +++ b/source/ModchartShader.hx @@ -0,0 +1,74 @@ +import flixel.system.FlxAssets.FlxShader; + +class ModchartShader extends FlxShader +{ + public var vertexHeader = "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;"; + public var vertexBody = "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;"; + public var vertexSource = "#pragma header + void main(void) { + #pragma body + }"; + public var fragmentHeader = "varying float openfl_Alphav; + varying vec4 openfl_ColorMultiplierv; + varying vec4 openfl_ColorOffsetv; + varying vec2 openfl_TextureCoordv; + uniform bool openfl_HasColorTransform; + uniform sampler2D openfl_Texture; + uniform vec2 openfl_TextureSize;"; + public var fragmentBody = "vec4 color = texture2D (openfl_Texture, openfl_TextureCoordv); + if (color.a == 0.0) { + gl_FragColor = vec4 (0.0, 0.0, 0.0, 0.0); + } else if (openfl_HasColorTransform) { + 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] = 1.0; // openfl_ColorMultiplierv.w; + color = clamp (openfl_ColorOffsetv + (color * colorMultiplier), 0.0, 1.0); + if (color.a > 0.0) { + gl_FragColor = vec4 (color.rgb * color.a * openfl_Alphav, color.a * openfl_Alphav); + } else { + gl_FragColor = vec4 (0.0, 0.0, 0.0, 0.0); + } + } else { + gl_FragColor = color * openfl_Alphav; + }"; + + public function new(frag:String,?vert:String = "") + { + if (vert != "") + glVertexSource = vert; + glFragmentSource = frag; + + if (glVertexSource != null) + { + glVertexSource = StringTools.replace(glVertexSource, "#pragma header", vertexHeader); + glVertexSource = StringTools.replace(glVertexSource, "#pragma body", vertexBody); + } + + if (glVertexSource != null) + { + glFragmentSource = StringTools.replace(glFragmentSource, "#pragma header", fragmentHeader); + glFragmentSource = StringTools.replace(glFragmentSource, "#pragma body", fragmentBody); + } + super(); + } +} \ No newline at end of file diff --git a/source/ModchartState.hx b/source/ModchartState.hx index 789f0fb..4132c4c 100644 --- a/source/ModchartState.hx +++ b/source/ModchartState.hx @@ -347,6 +347,8 @@ class ModchartState lua = null; } + public var luaWiggles:Map = new Map(); + // LUA SHIT function new() @@ -427,6 +429,41 @@ class ModchartState Lua_helper.add_callback(lua,"getProperty", getPropertyByName); + Lua_helper.add_callback(lua,"setNoteWiggle", function(wiggleId) { + PlayState.instance.camNotes.setFilters([new ShaderFilter(luaWiggles.get(wiggleId).shader)]); + }); + + Lua_helper.add_callback(lua,"setSustainWiggle", function(wiggleId) { + PlayState.instance.camSustains.setFilters([new ShaderFilter(luaWiggles.get(wiggleId).shader)]); + }); + + Lua_helper.add_callback(lua,"createWiggle", function(freq:Float,amplitude:Float,speed:Float) { + var wiggle = new WiggleEffect(); + wiggle.waveAmplitude = amplitude; + wiggle.waveSpeed = speed; + wiggle.waveFrequency = freq; + + var id = Lambda.count(luaWiggles) + 1 + ""; + + luaWiggles.set(id,wiggle); + return id; + }); + + Lua_helper.add_callback(lua,"setWiggleTime", function(wiggleId:String,time:Float) { + var wiggle = luaWiggles.get(wiggleId); + + wiggle.shader.uTime.value = [time]; + }); + + + Lua_helper.add_callback(lua,"setWiggleAmplitude", function(wiggleId:String,amp:Float) { + var wiggle = luaWiggles.get(wiggleId); + + wiggle.waveAmplitude = amp; + }); + + + // Lua_helper.add_callback(lua,"makeAnimatedSprite", makeAnimatedLuaSprite); // this one is still in development diff --git a/source/PlayState.hx b/source/PlayState.hx index b19a3d1..921c87b 100644 --- a/source/PlayState.hx +++ b/source/PlayState.hx @@ -1,5 +1,6 @@ package; + import Song.Event; import openfl.media.Sound; #if sys @@ -178,6 +179,8 @@ class PlayState extends MusicBeatState public var iconP1:HealthIcon; // making these public again because i may be stupid public var iconP2:HealthIcon; // what could go wrong? public var camHUD:FlxCamera; + public var camSustains:FlxCamera; + public var camNotes:FlxCamera; private var camGame:FlxCamera; public var cannotDie = false; @@ -382,9 +385,15 @@ class PlayState extends MusicBeatState camGame = new FlxCamera(); camHUD = new FlxCamera(); camHUD.bgColor.alpha = 0; + camSustains = new FlxCamera(); + camSustains.bgColor.alpha = 0; + camNotes = new FlxCamera(); + camNotes.bgColor.alpha = 0; FlxG.cameras.reset(camGame); FlxG.cameras.add(camHUD); + FlxG.cameras.add(camSustains); + FlxG.cameras.add(camNotes); FlxCamera.defaultCameras = [camGame]; @@ -2327,10 +2336,10 @@ class PlayState extends MusicBeatState luaModchart.setVar('cameraZoom', FlxG.camera.zoom); luaModchart.executeState('update', [elapsed]); - for (i in luaWiggles) + for (key => value in luaModchart.luaWiggles) { trace('wiggle le gaming'); - i.update(elapsed); + value.update(elapsed); } /*for (i in 0...strumLineNotes.length) { @@ -2904,6 +2913,10 @@ class PlayState extends MusicBeatState { var dunceNote:Note = unspawnNotes[0]; notes.add(dunceNote); + if (!dunceNote.isSustainNote) + dunceNote.cameras = [camNotes]; + else + dunceNote.cameras = [camSustains]; var index:Int = unspawnNotes.indexOf(dunceNote); unspawnNotes.splice(index, 1); diff --git a/source/ShaderShader.hx b/source/ShaderShader.hx new file mode 100644 index 0000000..fc19f21 --- /dev/null +++ b/source/ShaderShader.hx @@ -0,0 +1,41 @@ +import flixel.system.FlxAssets; + +class ShaderShader extends FlxShader +{ + @:glFragmentSource(' + #pragma header + uniform float fade; + + varying vec4 color; + varying vec2 textureCoord; + varying vec2 textureSize; + uniform sampler2D sampler0; + + float luma(vec3 color) { + return dot(color, vec3(0.299, 0.587, 0.114)); + } + + vec3 rgb(float r, float g, float b) { + return vec3(r/255.0,g/255.0,b/255.0); + } + + void main() + { + vec2 uv = textureCoord; + + vec3 col = texture2D( sampler0, uv ).rgb; + float bright=floor(luma(col+0.4)*4.0)/4.0; + + vec3 newcol; + if (bright<0.3) newcol = rgb(54.0,87.0,53.0); + else if (bright<0.6) newcol = rgb(128.0,128.0,0.0); + else newcol = rgb(157.0,187.0,97.0); + gl_FragColor = vec4( newcol*(fade)+col*(1.0-fade), 1.0 ) * color; + } + ') + + public function new() + { + super(); + } +} \ No newline at end of file