Kav/Effects/HLSL/PaletteCrushEffect.fx

66 lines
1.1 KiB
Plaintext
Raw Permalink Normal View History

2020-12-09 20:44:53 +00:00
#include "Macros.fxh"
DECLARE_TEXTURE(Texture, 0);
DECLARE_TEXTURE(Palette, 1);
BEGIN_CONSTANTS
int PaletteWidth _ps(c0) _cb(c0);
END_CONSTANTS
struct VertexInput
{
float4 Position : POSITION;
float2 TexCoord : TEXCOORD;
};
struct PixelInput
{
float4 Position : SV_POSITION;
float2 TexCoord : TEXCOORD0;
};
PixelInput main_vs(VertexInput input)
{
PixelInput output;
output.Position = input.Position;
output.TexCoord = input.TexCoord;
return output;
}
float4 main_ps(PixelInput input) : SV_TARGET0
{
float4 sampled = SAMPLE_TEXTURE(Texture, input.TexCoord);
float3 sampled_color = sampled.rgb;
float3 closest_color = float3(0, 0, 0);
2020-12-13 05:07:22 +00:00
float closest_dist = 100000;
2020-12-09 20:44:53 +00:00
for (int i = 0; i < PaletteWidth; i++)
{
2020-12-13 05:07:22 +00:00
float texX = (i / (float)PaletteWidth);
float3 palette_color = SAMPLE_TEXTURE(Palette, float2(texX, 0));
2020-12-09 20:44:53 +00:00
float dist = distance(palette_color, sampled_color);
if (dist < closest_dist)
{
closest_dist = dist;
closest_color = palette_color;
}
}
return float4(closest_color, sampled.a);
}
Technique PaletteCrush
{
Pass
{
VertexShader = compile vs_3_0 main_vs();
PixelShader = compile ps_3_0 main_ps();
}
}