Shadow shader!

This commit is contained in:
2024-06-27 16:40:55 -06:00
parent b17677d2c9
commit 667785c62a
6 changed files with 77 additions and 4 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

View File

@@ -18,6 +18,7 @@
(array Class<FlxState>
CheckerBoardState
InvertState
MirroredOrnamentState)
MirroredOrnamentState
ShadowState)
->choice (FlxG.switchState (Type.createInstance choice [])))))

View File

@@ -0,0 +1,37 @@
(import flixel.FlxState)
(import flixel.FlxG)
(import flixel.FlxSprite)
(import flixel.util.FlxColor)
(import flixel.FlxCamera)
(import openfl.filters.BitmapFilter)
(import openfl.filters.ShaderFilter)
(import flixel.system.FlxAssets.FlxShader)
(extends FlxState)
(prop &mut shader null)
(prop &mut fgCamera null)
(prop &mut x 200)
(prop &mut y 200)
(prop &mut sprite null)
(method &override :Void create []
(super.create)
(set FlxG.camera.bgColor FlxColor.WHITE)
(set fgCamera (new FlxCamera))
(set fgCamera.bgColor FlxColor.TRANSPARENT)
(FlxG.cameras.add fgCamera)
(set sprite (new FlxSprite x y "assets/images/flymanEdited.png"))
(set sprite.cameras [fgCamera])
(add sprite)
(set shader (new kiss_flixel.shaders.Shadow))
(kiss_flixel.CameraTools.addShaderFilter fgCamera (array FlxShader shader)))
(method &override :Void update [:Float elapsed]
(super.update elapsed))

View File

@@ -187,11 +187,17 @@ class ShaderFrontend implements FrontendPlugin {
transformedCode += "iTime";
case Some("COLOR"):
transformedCode += "gl_FragColor";
case Some("TEXTURE"):
transformedCode += "bitmap";
case Some("texture"):
transformedCode += "flixel_texture2D";
// Not totally sure this actually is a 1-to-1 equivalency:
case Some("SCREEN_UV"):
case Some("SCREEN_UV" | "UV"):
transformedCode += "openfl_TextureCoordv";
case Some("SCREEN_PIXEL_SIZE"):
case Some("SCREEN_PIXEL_SIZE" | "TEXTURE_PIXEL_SIZE"):
transformedCode += "vec2(1.0 / openfl_TextureSize.x, 1.0 / openfl_TextureSize.y)";
// Uniform handling:

View File

@@ -1,7 +1,8 @@
#pragma header
// Source: https://godotshaders.com/shader/animated-mirrored-ornament/
// Modified by Nat
// by FencerDevLog (CC0)
// Ported to kiss-flixel by NQNStudios
uniform vec3 color_a = vec3(0.5);
uniform vec3 color_b = vec3(0.5);

View File

@@ -0,0 +1,28 @@
// Based on https://gist.github.com/deakcor/f9dfed4cf82cbd86b49bd1b56a6ebd9e
// by deakcor
// Ported to kiss-flixel by NQNStudios
/**
* Shadow 2D.
* License: CC0
* https://creativecommons.org/publicdomain/zero/1.0/
*/
uniform vec2 deform = vec2(2.0, 2.0);
uniform vec2 offset = vec2(0.0, 0.0);
uniform vec4 modulate = vec4(0.5, 0.5, 0.5, 1); // : hint_color;
void fragment() {
vec2 ps = TEXTURE_PIXEL_SIZE;
vec2 uv = UV;
float sizex = openfl_TextureSize.x; // float(textureSize(TEXTURE,int(ps.x)).x);
float sizey = openfl_TextureSize.y; // float(textureSize(TEXTURE,int(ps.y)).y);
uv.y+=offset.y*ps.y;
uv.x+=offset.x*ps.x;
float decalx=((uv.y-ps.x*sizex)*deform.x);
float decaly=((uv.y-ps.y*sizey)*deform.y);
uv.x += decalx;
uv.y += decaly;
vec4 shadow = vec4(modulate.rgb, texture(TEXTURE, uv).a * modulate.a);
vec4 col = texture(TEXTURE, UV);
COLOR = mix(shadow, col, col.a);
}