Compare commits

...

7 Commits

Author SHA1 Message Date
micycle ebe0dda6b6 add vertex shader (frost helper my beloved) 2023-05-24 23:56:12 -05:00
yosh 58df89a5af correct matrix 2023-05-24 21:13:50 -05:00
yosh 1aa8027a13 increase threshold. this captures db 2023-05-24 21:13:42 -05:00
micycle e5d45f5d48 fized xna shader compatability with copious static consts 2023-05-24 20:50:10 -05:00
micycle 2343743a43 squash premult 2023-05-24 20:50:10 -05:00
micycle 343f91a324 clean up 2023-05-24 20:50:10 -05:00
micycle 3f97a9f312 stop using premultiplied color for user selectable colors 2023-05-24 20:50:10 -05:00
5 changed files with 171 additions and 71 deletions

View File

@ -30,25 +30,36 @@ float4 hsv2rgb(float4 c) {
DECLARE_TEXTURE(sprite, 0); // The sprite texture
// All of the uniform rgb and hsv colors don't have premultipled alpha!
// We need to multiply the alpha in by hand.
uniform float recolor1_threshold;
uniform float4 recolor1_rgb_from;
uniform float4 recolor1_rgb_to;
uniform float rehue1_threshold;
uniform float4 rehue1_threshold_mul;
uniform float4 rehue1_rgb_from;
uniform float4 rehue1_rgb_to;
uniform float4 rehue1_hsv_from;
uniform float4 rehue1_hsv_to;
static const float rehue1_threshold_norm = rehue1_threshold * length(rehue1_threshold_mul);
static const float4 rehue1_hsv_from = rgb2hsv(rehue1_rgb_from);
static const float4 rehue1_hsv_to = rgb2hsv(rehue1_rgb_to);
uniform float rehue2_threshold;
uniform float4 rehue2_threshold_mul;
uniform float4 rehue2_rgb_from;
uniform float4 rehue2_rgb_to;
uniform float4 rehue2_hsv_from;
uniform float4 rehue2_hsv_to;
static const float rehue2_threshold_norm = rehue2_threshold * length(rehue2_threshold_mul);
static const float4 rehue2_hsv_from = rgb2hsv(rehue2_rgb_from);
static const float4 rehue2_hsv_to = rgb2hsv(rehue2_rgb_to);
// Thank you frost helper my beloved
uniform float4x4 TransformMatrix;
uniform float4x4 ViewMatrix;
void vs_gameplay_transform(
inout float4 color: COLOR0, inout float2 texCoord : TEXCOORD0,
inout float4 position : SV_Position
) {
position = mul(position, ViewMatrix);
position = mul(position, TransformMatrix);
}
float4 hueshift(
@ -59,24 +70,33 @@ float4 hueshift(
return hsv2rgb(hsv_clamp);
}
float4 multiplya(float4 color) {
return color * float4(color.aaa, 1.0);
}
float4 ps_main(float4 pos: SV_Position, float4 sprite_color: COLOR0, float2 uv: TEXCOORD0): COLOR {
float4 ps_main(
float4 pos: SV_Position, float4 sprite_color: COLOR0, float2 uv: TEXCOORD0
): COLOR0 {
float4 tex_rgb = SAMPLE_TEXTURE(sprite, uv);
float4 tex_hsv = rgb2hsv(tex_rgb);
// replace recolor1_rgb_from with recolor1_rgb_to if in threshold
if (distance(tex_rgb, recolor1_rgb_from) < recolor1_threshold) {
return recolor1_rgb_to * sprite_color;
// Multiply the alpha in because our colors are not premultiplied
return multiplya(recolor1_rgb_to) * sprite_color;
}
// hue-shift tex_hsv by the difference between rehue1_hsv_to - rehue1_hsv_from,
// only if it is in threshold scaled by the threshold multipler
if (distance(tex_hsv * rehue1_threshold_mul, rehue1_hsv_from * rehue1_threshold_mul) < rehue1_threshold_norm) {
return hueshift(tex_hsv, rehue1_hsv_from, rehue1_hsv_to) * sprite_color;
return multiplya(hueshift(tex_hsv, rehue1_hsv_from, rehue1_hsv_to))
* sprite_color;
}
if (distance(tex_hsv * rehue2_threshold_mul, rehue2_hsv_from * rehue2_threshold_mul) < rehue2_threshold_norm) {
return hueshift(tex_hsv, rehue2_hsv_from, rehue2_hsv_to) * sprite_color;
return multiplya(hueshift(tex_hsv, rehue2_hsv_from, rehue2_hsv_to))
* sprite_color;
}
return tex_rgb * sprite_color;
@ -85,6 +105,7 @@ float4 ps_main(float4 pos: SV_Position, float4 sprite_color: COLOR0, float2 uv:
technique Recolor {
pass {
PixelShader = compile ps_2_0 ps_main();
VertexShader = compile vs_3_0 vs_gameplay_transform();
PixelShader = compile ps_3_0 ps_main();
}
}

View File

@ -200,28 +200,38 @@ namespace Celeste.Mod.AvaliSkin {
FxRecolor.Parameters["recolor1_rgb_to"].SetValue(dashColor.ToVector4());
// works at 0.12 minus the dark brown (actually works for real)
FxRecolor.Parameters["rehue1_threshold"].SetValue(0.07f);
FxRecolor.Parameters["rehue1_threshold"].SetValue(0.12f);
FxRecolor.Parameters["rehue1_threshold_mul"].SetValue(
new Vector4(1f, 0.1f, 0.1f, 0f)
new Vector4(1f, 0.1f, 0.3f, 0f)
);
FxRecolor.Parameters["rehue1_rgb_from"].SetValue(
(new Color((byte) 0xa2, 0x88, 0x5c, 0xff)).ToVector4() // #a2885c
FxRecolor.Parameters["rehue1_hsv_from"].SetValue(
(new Color((byte) 0xa2, 0x88, 0x5c, 0xff)).ToHSV() // #a2885c
);
FxRecolor.Parameters["rehue1_hsv_to"].SetValue(
lightColor2.ToHSV()
);
FxRecolor.Parameters["rehue1_rgb_to"].SetValue(
lightColor2.ToVector4()
); // #433722
FxRecolor.Parameters["rehue2_threshold"].SetValue(0.07f);
FxRecolor.Parameters["rehue2_threshold_mul"].SetValue(
new Vector4(1f, .8f, .8f, 0f)
);
FxRecolor.Parameters["rehue2_rgb_from"].SetValue(
(new Color((byte) 0x4e, 0x4e, 0x4e, 0xff)).ToVector4() // #4e4e4e
FxRecolor.Parameters["rehue2_hsv_from"].SetValue(
(new Color((byte) 0x4e, 0x4e, 0x4e, 0xff)).ToHSV() // #4e4e4e
);
FxRecolor.Parameters["rehue2_rgb_to"].SetValue(
darkColor2.ToVector4()
FxRecolor.Parameters["rehue2_hsv_to"].SetValue(
darkColor2.ToHSV()
);
Viewport viewport = Engine.Graphics.GraphicsDevice.Viewport;
Camera camera = (Engine.Scene as Level).Camera;
Matrix projection = Matrix.CreateOrthographicOffCenter(
0, viewport.Width, viewport.Height, 0, 0, 1
);
FxRecolor.Parameters["TransformMatrix"].SetValue(projection);
FxRecolor.Parameters["ViewMatrix"].SetValue(camera?.Matrix ?? Matrix.Identity);
FxRecolor.CurrentTechnique = FxRecolor.Techniques["Recolor"];
}
@ -251,15 +261,18 @@ namespace Celeste.Mod.AvaliSkin {
// apply the recolor effect to the player
spriteRecolor(color, PlayerConfig.LightBody, PlayerConfig.DarkBody);
DynData<SpriteBatch> spriteData = new DynData<SpriteBatch>(Draw.SpriteBatch);
Matrix matrix = (Matrix)spriteData["transformMatrix"];
Draw.SpriteBatch.End();
Draw.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointWrap, DepthStencilState.None, RasterizerState.CullNone, FxRecolor, (self.Scene as Level).GameplayRenderer.Camera.Matrix);
Draw.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointWrap, DepthStencilState.None, RasterizerState.CullNone, FxRecolor, matrix);
// render Avali...
orig(self);
// ... and reset rendering to stop using the effect
Draw.SpriteBatch.End();
Draw.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointWrap, DepthStencilState.None, RasterizerState.CullNone, null, (self.Scene as Level).GameplayRenderer.Camera.Matrix);
Draw.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointWrap, DepthStencilState.None, RasterizerState.CullNone, null, matrix);
} else if (self.Scene != null) {
trySpriteSwap(sprite, false);
orig(self);
@ -296,15 +309,18 @@ namespace Celeste.Mod.AvaliSkin {
// apply the recolor effect to the sprite
spriteRecolor(color, PlayerConfig.LightBody, PlayerConfig.DarkBody);
DynData<SpriteBatch> spriteData = new DynData<SpriteBatch>(Draw.SpriteBatch);
Matrix matrix = (Matrix)spriteData["transformMatrix"];
Draw.SpriteBatch.End();
Draw.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointWrap, DepthStencilState.None, RasterizerState.CullNone, FxRecolor, (self.Scene as Level).GameplayRenderer.Camera.Matrix);
Draw.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointWrap, DepthStencilState.None, RasterizerState.CullNone, FxRecolor, matrix);
// render the sprite...
orig(self);
// ... and reset rendering to stop using the effect
Draw.SpriteBatch.End();
Draw.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointWrap, DepthStencilState.None, RasterizerState.CullNone, null, (self.Scene as Level).GameplayRenderer.Camera.Matrix);
Draw.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointWrap, DepthStencilState.None, RasterizerState.CullNone, null, matrix);
} else {
trySpriteSwap(self, false);
orig(self);
@ -376,15 +392,18 @@ namespace Celeste.Mod.AvaliSkin {
// apply the recolor effect to the ghost
spriteRecolor(config.GetColor(ghost), config.LightBody, config.DarkBody);
DynData<SpriteBatch> spriteData = new DynData<SpriteBatch>(Draw.SpriteBatch);
Matrix matrix = (Matrix)spriteData["transformMatrix"];
Draw.SpriteBatch.End();
Draw.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointWrap, DepthStencilState.None, RasterizerState.CullNone, FxRecolor, (self.Scene as Level).GameplayRenderer.Camera.Matrix);
Draw.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointWrap, DepthStencilState.None, RasterizerState.CullNone, FxRecolor, matrix);
// render the ghost...
orig(self);
// ... and reset rendering to stop using the effect
Draw.SpriteBatch.End();
Draw.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointWrap, DepthStencilState.None, RasterizerState.CullNone, null, (self.Scene as Level).GameplayRenderer.Camera.Matrix);
Draw.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointWrap, DepthStencilState.None, RasterizerState.CullNone, null, matrix);
}
@ -404,15 +423,18 @@ namespace Celeste.Mod.AvaliSkin {
PlayerConfig.DarkBody
);
DynData<SpriteBatch> spriteData = new DynData<SpriteBatch>(Draw.SpriteBatch);
Matrix matrix = (Matrix)spriteData["transformMatrix"];
Draw.SpriteBatch.End();
Draw.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointWrap, DepthStencilState.None, RasterizerState.CullNone, FxRecolor, (self.Scene as Level).GameplayRenderer.Camera.Matrix);
Draw.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointWrap, DepthStencilState.None, RasterizerState.CullNone, FxRecolor, matrix);
// render the dead Avali :(
orig(self);
// ... and reset rendering to stop using the effect
Draw.SpriteBatch.End();
Draw.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointWrap, DepthStencilState.None, RasterizerState.CullNone, null, (self.Scene as Level).GameplayRenderer.Camera.Matrix);
Draw.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointWrap, DepthStencilState.None, RasterizerState.CullNone, null, matrix);
} else if (self.Scene != null) {
trySpriteSwap(sprite, false);
orig(self);
@ -448,7 +470,7 @@ namespace Celeste.Mod.AvaliSkin {
// hair flash right after dashing will be copied to the trail.
if (PlayerConfig.DashColorMode != DashColorMode.ExternalDash) {
// replace trail colors with marking colors
return PlayerConfig.GetColor(self);
return PlayerConfig.GetColor(self).Premultiply();
}
}
@ -464,7 +486,7 @@ namespace Celeste.Mod.AvaliSkin {
if (PlayerConfig.IsEnabled(self) && PlayerConfig.DashColorMode != DashColorMode.ExternalDash) {
// change player hair color to match dash colors.
// (hair is invisible, but that influences other things like the orbs when the Avali dies and respawns)
self.Hair.Color = PlayerConfig.GetColor(self);
self.Hair.Color = PlayerConfig.GetColor(self).Premultiply();
}
}
@ -481,7 +503,7 @@ namespace Celeste.Mod.AvaliSkin {
return orig(self);
}
Color color = PlayerConfig.GetColor(self);
Color color = PlayerConfig.GetColor(self).Premultiply();
// back up vanilla particles
ParticleType bakDashA = Player.P_DashA;

View File

@ -85,6 +85,22 @@ namespace Celeste.Mod.AvaliSkin {
}
}
// private static TextMenu.Button NewStringSetting(
// string name, ref string value, int minLength, int maxLength,
// Action<string> onchange
// ) {
// return new TextMenu.Button(name + ": " + value)
// .Pressed(() => {
// Audio.Play(SFX.ui_main_savefile_rename_start);
// menu.SceneAs<Overworld>().Goto<OuiModOptionString>().Init<OuiModOptions>(
// (string) value,
// onchange,
// maxLength,
// minLength
// );
// });
// }
// note: YamlDotNet ignores all private member variables
private bool enabled = true;
public bool Enabled {
@ -170,23 +186,30 @@ namespace Celeste.Mod.AvaliSkin {
(RItem = new TextMenuExt.IntSlider(
"AVALI_SKIN_RED".DialogOrKey(),
0, 255, DashRGBColor[j].R
).Change(
// C# is stupidly pendatic and doesn't support property assignment in List elements
// so we have to do this ugly shit to avoid breaking up this expression into two
c => DashRGBColor[j] = new Color((byte) c, DashRGBColor[j].G, DashRGBColor[j].B)
)),
).Change(c => {
// C# is stupidly pendatic and doesn't support
// property assignment for value classes. So we have
// to do this ugly shit instead.
Color col = DashRGBColor[j];
col.R = (byte) c;
DashRGBColor[j] = col;
})),
(GItem = new TextMenuExt.IntSlider(
"AVALI_SKIN_GREEN".DialogOrKey(),
0, 255, DashRGBColor[j].G
).Change(
c => DashRGBColor[j] = new Color(DashRGBColor[j].R, (byte) c, DashRGBColor[j].B)
)),
).Change(c => {
Color col = DashRGBColor[j];
col.G = (byte) c;
DashRGBColor[j] = col;
})),
(BItem = new TextMenuExt.IntSlider(
"AVALI_SKIN_BLUE".DialogOrKey(),
0, 255, DashRGBColor[j].B
).Change(
c => DashRGBColor[j] = new Color(DashRGBColor[j].R, DashRGBColor[j].G, (byte) c)
)),
).Change(c => {
Color col = DashRGBColor[j];
col.B = (byte) c;
DashRGBColor[j] = col;
})),
(ColorItem = new TextMenuExt.EnumSlider<ColorChoice>(
"AVALI_SKIN_COLOR".DialogOrKey(),
DashPreset[j]
@ -256,21 +279,27 @@ namespace Celeste.Mod.AvaliSkin {
(RItem = new TextMenuExt.IntSlider(
"AVALI_SKIN_RED".DialogOrKey(),
0, 255, LightBodyRGBColor.R
).Change(
c => LightBodyRGBColor = new Color((byte) c, LightBodyRGBColor.G, LightBodyRGBColor.B)
)),
).Change(c => {
Color col = LightBodyRGBColor;
col.R = (byte) c;
LightBodyRGBColor = col;
})),
(GItem = new TextMenuExt.IntSlider(
"AVALI_SKIN_GREEN".DialogOrKey(),
0, 255, LightBodyRGBColor.G
).Change(
c => LightBodyRGBColor = new Color(LightBodyRGBColor.R, (byte) c, LightBodyRGBColor.B)
)),
).Change(c => {
Color col = LightBodyRGBColor;
col.G = (byte) c;
LightBodyRGBColor = col;
})),
(BItem = new TextMenuExt.IntSlider(
"AVALI_SKIN_BLUE".DialogOrKey(),
0, 255, LightBodyRGBColor.B
).Change(
c => LightBodyRGBColor = new Color(LightBodyRGBColor.R, LightBodyRGBColor.G, (byte) c)
)),
).Change(c => {
Color col = LightBodyRGBColor;
col.B = (byte) c;
LightBodyRGBColor = col;
})),
(ColorItem = new TextMenuExt.EnumSlider<ColorChoice>(
"AVALI_SKIN_COLOR".DialogOrKey(),
LightBodyPreset
@ -289,7 +318,9 @@ namespace Celeste.Mod.AvaliSkin {
"AVALI_SKIN_RED".DialogOrKey(),
0, 255, DarkBodyRGBColor.R
).Change(c => {
DarkBodyRGBColor = new Color((byte) c, DarkBodyRGBColor.G, DarkBodyRGBColor.B);
Color col = DarkBodyRGBColor;
col.R = (byte) c;
DarkBodyRGBColor = col;
// we need to manually send the new body color over
updateOptions();
})),
@ -297,14 +328,18 @@ namespace Celeste.Mod.AvaliSkin {
"AVALI_SKIN_GREEN".DialogOrKey(),
0, 255, DarkBodyRGBColor.G
).Change(c => {
DarkBodyRGBColor = new Color(DarkBodyRGBColor.R, (byte) c, DarkBodyRGBColor.B);
Color col = DarkBodyRGBColor;
col.G = (byte) c;
DarkBodyRGBColor = col;
updateOptions();
})),
(BItem = new TextMenuExt.IntSlider(
"AVALI_SKIN_BLUE".DialogOrKey(),
0, 255, DarkBodyRGBColor.B
).Change(c => {
DarkBodyRGBColor = new Color(DarkBodyRGBColor.R, DarkBodyRGBColor.G, (byte) c);
Color col = DarkBodyRGBColor;
col.B = (byte) c;
DarkBodyRGBColor = col;
updateOptions();
})),
(ColorItem = new TextMenuExt.EnumSlider<ColorChoice>(

View File

@ -75,17 +75,6 @@ namespace Celeste.Mod.AvaliSkin {
if (Config.Enabled) {
writer.WriteNoA(Config.LightBody);
writer.WriteNoA(Config.DarkBody);
// byte lengthRGB = Math.Min(ManualRGB.Count, 5);
// writer.Write((byte) ManualRGB.Count);
// foreach (var color in ManualRGB) {
// write.WriteNoA(color);
// }
// byte lengthPreset = Math.Min(ManualPreset.Count, 5);
// writer.Write((byte) ManualPreset.Count);
// foreach (var color in ManualPreset) {
// write.Write((byte) color);
// }
}
}
}

View File

@ -1,4 +1,5 @@
using System;
using System.Threading;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
@ -53,6 +54,7 @@ namespace Celeste.Mod.AvaliSkin {
return $"#{color.R:x2}{color.G:x2}{color.B:x2}{color.A:x2}";
}
// Converts the 6 or 8 character hex to a non-premultiplied color
public static Color HexToColor(this string hex) {
hex = hex.TrimStart(' ', '#');
if (hex.Length < 6) {
@ -68,8 +70,39 @@ namespace Celeste.Mod.AvaliSkin {
}
float a = (float)(Calc.HexToByte(hex[6]) * 16 + Calc.HexToByte(hex[7])) / 255f;
// premultiply the alpha
return new Color(r, g, b) * a;
return new Color(r, g, b, a);
}
public static Color Premultiply(this Color color) {
return Color.FromNonPremultiplied(color.ToVector4());
}
// From https://web.archive.org/web/20200213094821/http://lolengine.net/blog/2013/01/13/fast-rgb-to-hsv
public static Vector4 ToHSV(this Color color) {
// custom deconstructors are broken on mono
float r = color.R / 255.0f;
float g = color.G / 255.0f;
float b = color.B / 255.0f;
float a = color.A / 255.0f;
float K = 0f;
if (g < b) {
b = Interlocked.Exchange(ref g, b);
K = -1f;
}
if (r < g) {
g = Interlocked.Exchange(ref r, g);
K = -2f / 6f - K;
}
float chroma = r - Math.Min(g, b);
float h = Math.Abs(K + (g - b) / (6f * chroma + 1e-20f));
float s = chroma / (r + 1e-20f);
float v = r;
return new Vector4(h, s, v, a);
}
}
}