From 32ec724da68ba8e146a629168baa2eb7592aaece Mon Sep 17 00:00:00 2001
From: Cameron Taylor <cameron.taylor.ninja@gmail.com>
Date: Mon, 11 Sep 2023 11:10:08 -0400
Subject: [PATCH] rainshader prototype

---
 .../funkin/graphics/shaders/PuddleShader.hx   |  12 ++
 .../shaders/RuntimePostEffectShader.hx        |  51 ++++++++
 .../graphics/shaders/RuntimeRainShader.hx     | 111 ++++++++++++++++++
 source/funkin/play/stage/Stage.hx             |   2 -
 4 files changed, 174 insertions(+), 2 deletions(-)
 create mode 100644 source/funkin/graphics/shaders/PuddleShader.hx
 create mode 100644 source/funkin/graphics/shaders/RuntimePostEffectShader.hx
 create mode 100644 source/funkin/graphics/shaders/RuntimeRainShader.hx

diff --git a/source/funkin/graphics/shaders/PuddleShader.hx b/source/funkin/graphics/shaders/PuddleShader.hx
new file mode 100644
index 000000000..8e04c6453
--- /dev/null
+++ b/source/funkin/graphics/shaders/PuddleShader.hx
@@ -0,0 +1,12 @@
+package funkin.shaderslmfao;
+
+import flixel.addons.display.FlxRuntimeShader;
+import openfl.Assets;
+
+class PuddleShader extends FlxRuntimeShader
+{
+  public function new()
+  {
+    super(Assets.getText(Paths.frag('puddle')));
+  }
+}
diff --git a/source/funkin/graphics/shaders/RuntimePostEffectShader.hx b/source/funkin/graphics/shaders/RuntimePostEffectShader.hx
new file mode 100644
index 000000000..b9ec73c76
--- /dev/null
+++ b/source/funkin/graphics/shaders/RuntimePostEffectShader.hx
@@ -0,0 +1,51 @@
+package funkin.shaderslmfao;
+
+import flixel.FlxG;
+import flixel.addons.display.FlxRuntimeShader;
+import flixel.system.FlxAssets.FlxShader;
+import haxe.CallStack;
+import lime.graphics.opengl.GLProgram;
+import lime.utils.Log;
+
+class RuntimePostEffectShader extends FlxRuntimeShader
+{
+  @:glVertexHeader("
+		varying vec2 fragCoord; // normalized texture coord
+		varying vec2 screenPos; // y: always between 0 and 1, x: between 0 and (width/height)
+		uniform vec2 screenResolution;
+	", true)
+  @:glVertexBody("
+		fragCoord = vec2(
+			openfl_TextureCoord.x > 0.0 ? 1.0 : 0.0,
+			openfl_TextureCoord.y > 0.0 ? 1.0 : 0.0
+		);
+		screenPos = fragCoord * vec2(screenResolution.x / screenResolution.y, 1.0);
+	")
+  @:glFragmentHeader("
+		varying vec2 fragCoord;
+		varying vec2 screenPos;
+
+		vec2 texCoordSize() { // hack
+			return openfl_TextureCoordv / fragCoord;
+		}
+	", true)
+  public function new(fragmentSource:String = null, glVersion:String = null)
+  {
+    super(fragmentSource, null, glVersion);
+    screenResolution.value = [FlxG.width, FlxG.height];
+  }
+
+  override function __createGLProgram(vertexSource:String, fragmentSource:String):GLProgram
+  {
+    try
+    {
+      final res = super.__createGLProgram(vertexSource, fragmentSource);
+      return res;
+    }
+    catch (error)
+    {
+      Log.warn(error);
+      return null;
+    }
+  }
+}
diff --git a/source/funkin/graphics/shaders/RuntimeRainShader.hx b/source/funkin/graphics/shaders/RuntimeRainShader.hx
new file mode 100644
index 000000000..cab0a8964
--- /dev/null
+++ b/source/funkin/graphics/shaders/RuntimeRainShader.hx
@@ -0,0 +1,111 @@
+package funkin.shaderslmfao;
+
+import flixel.system.FlxAssets.FlxShader;
+import openfl.display.BitmapData;
+import openfl.display.ShaderParameter;
+import openfl.display.ShaderParameterType;
+import openfl.utils.Assets;
+
+typedef Light =
+{
+  var position:Array<Float>;
+  var color:Array<Float>;
+  var radius:Float;
+}
+
+class RuntimeRainShader extends RuntimePostEffectShader
+{
+  static final MAX_LIGHTS:Int = 8;
+
+  public var lights:Array<
+    {
+      position:ShaderParameter<Float>,
+      color:ShaderParameter<Float>,
+      radius:ShaderParameter<Float>,
+    }>;
+
+  // This is a property, whenever the value is set it calls the set_time function.
+  // This makes the code cleaner elsewhere.
+  public var time(default, set):Float = 1;
+
+  function set_time(value:Float):Float
+  {
+    this.setFloat('uTime', value);
+    return time = value;
+  }
+
+  public var puddleMap(default, set):BitmapData;
+
+  public var groundMap(default, set):BitmapData;
+
+  function set_groundMap(value:BitmapData):BitmapData
+  {
+    trace("groundmap set");
+    this.setBitmapData('uGroundMap', value);
+    // this.setFloat2('uPuddleTextureSize', value.width, value.height);
+    return groundMap = value;
+  }
+
+  function set_puddleMap(value:BitmapData):BitmapData
+  {
+    this.setBitmapData('uPuddleMap', value);
+    return puddleMap = value;
+  }
+
+  public var lightMap(default, set):BitmapData;
+
+  function set_lightMap(value:BitmapData):BitmapData
+  {
+    trace("lightmap set");
+    this.setBitmapData('uLightMap', value);
+    return lightMap = value;
+  }
+
+  public var numLights(default, set):Int = 0;
+
+  function set_numLights(value:Int):Int
+  {
+    this.setInt('numLights', value);
+    return numLights = value;
+  }
+
+  public function new()
+  {
+    super(Assets.getText(Paths.frag("rain")));
+  }
+
+  public function update(elapsed:Float):Void
+  {
+    time += elapsed;
+  }
+
+  override function __processGLData(source:String, storageType:String):Void
+  {
+    super.__processGLData(source, storageType);
+    if (storageType == "uniform")
+    {
+      lights = [
+        for (i in 0...MAX_LIGHTS)
+          {
+            position: addFloatUniform("lights[" + i + "].position", 2),
+            color: addFloatUniform("lights[" + i + "].color", 3),
+            radius: addFloatUniform("lights[" + i + "].radius", 1),
+          }
+      ];
+    }
+  }
+
+  @:access(openfl.display.ShaderParameter)
+  function addFloatUniform(name:String, length:Int):ShaderParameter<Float>
+  {
+    final res = new ShaderParameter<Float>();
+    res.name = name;
+    res.type = [null, FLOAT, FLOAT2, FLOAT3, FLOAT4][length];
+    res.__arrayLength = 1;
+    res.__isFloat = true;
+    res.__isUniform = true;
+    res.__length = length;
+    __paramFloat.push(res);
+    return res;
+  }
+}
diff --git a/source/funkin/play/stage/Stage.hx b/source/funkin/play/stage/Stage.hx
index f876a0e54..c8cb8ce66 100644
--- a/source/funkin/play/stage/Stage.hx
+++ b/source/funkin/play/stage/Stage.hx
@@ -276,8 +276,6 @@ class Stage extends FlxSpriteGroup implements IPlayStateScriptedClass
     {
       namedProps.set(name, prop);
       prop.name = name;
-      FlxG.debugger.track(prop, name);
-      FlxG.debugger.track(prop.scale, name + '.scale');
     }
     this.add(prop);
   }