112 lines
3.5 KiB
HLSL
112 lines
3.5 KiB
HLSL
// Compile with fxc.exe AvaliRecolor.fx /T fx_2_0 /O3
|
|
|
|
// Creates a texture at a given texture index
|
|
#define DECLARE_TEXTURE(Name, index) \
|
|
texture Name: register(t##index); \
|
|
sampler Name##Sampler: register(s##index)
|
|
|
|
// Samples the texture and returns a color
|
|
#define SAMPLE_TEXTURE(Name, texCoord) tex2D(Name##Sampler, texCoord)
|
|
|
|
|
|
// https://web.archive.org/web/20200207113336/http://lolengine.net/blog/2013/07/27/rgb-to-hsv-in-glsl
|
|
float4 rgb2hsv(float4 c) {
|
|
float4 K = float4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
|
|
float4 p = lerp(float4(c.bg, K.wz), float4(c.gb, K.xy), step(c.b, c.g));
|
|
float4 q = lerp(float4(p.xyw, c.r), float4(c.r, p.yzx), step(p.x, c.r));
|
|
|
|
float d = q.x - min(q.w, q.y);
|
|
float e = 1.0e-10;
|
|
return float4(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x, c.a);
|
|
}
|
|
|
|
float4 hsv2rgb(float4 c) {
|
|
float4 K = float4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
|
|
float3 p = abs(frac(c.xxx + K.xyz) * 6.0 - K.www);
|
|
float3 h = c.z * lerp(K.xxx, saturate(p - K.xxx), c.y);
|
|
return float4(h.rgb, c.a);
|
|
}
|
|
|
|
|
|
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_hsv_from;
|
|
uniform float4 rehue1_hsv_to;
|
|
static const float rehue1_threshold_norm = rehue1_threshold * length(rehue1_threshold_mul);
|
|
|
|
uniform float rehue2_threshold;
|
|
uniform float4 rehue2_threshold_mul;
|
|
uniform float4 rehue2_hsv_from;
|
|
uniform float4 rehue2_hsv_to;
|
|
static const float rehue2_threshold_norm = rehue2_threshold * length(rehue2_threshold_mul);
|
|
|
|
// 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(
|
|
float4 hsv_color, float4 hsv_from, float4 hsv_to
|
|
) {
|
|
float4 hsv = hsv_to - hsv_from + hsv_color;
|
|
float4 hsv_clamp = float4((hsv.r + 1.0) % 1.0, saturate(hsv.gba));
|
|
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
|
|
): 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) {
|
|
// 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 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 multiplya(hueshift(tex_hsv, rehue2_hsv_from, rehue2_hsv_to))
|
|
* sprite_color;
|
|
}
|
|
|
|
return tex_rgb * sprite_color;
|
|
}
|
|
|
|
|
|
technique Recolor {
|
|
pass {
|
|
VertexShader = compile vs_3_0 vs_gameplay_transform();
|
|
PixelShader = compile ps_3_0 ps_main();
|
|
}
|
|
}
|