Sign in to follow this  

[xna] palette manipulation

This topic is 3291 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is it possible to manipulate palettes in Xna? I want to display many sprites of the same character but changing its colors so it can give some sort of illusion that they are different from each other something like in old snes games or fighting games

Share this post


Link to post
Share on other sites
Yes, via shaders.

You create your indexed image and load it in a format such as L8 or A8 for 8-bit(I don't know the format names in XNA). Then inside the program take the palette and create a texture out of it, for 8-bit it would be 256x1 or 1x256. In the pixel shader you use a color component of the source texture as a texture coordinate to lookup the color in the palette texture.

I have an example HLSL shader someplace that I'll try to find, I feel like I've answered this question a million times.

Edit:

float4x4 WorldViewProj : WORLDVIEWPROJ;

Texture ColorTexture;

sampler ColorTextureSampler = sampler_state { texture = <ColorTexture> ; magfilter = NONE; minfilter = NONE; mipfilter = NONE; AddressU = wrap; AddressV = wrap; };

Texture PaletteTexture;

sampler PaletteTextureSampler = sampler_state { texture = <PaletteTexture> ; magfilter = NONE; minfilter = NONE; mipfilter = NONE; AddressU = wrap; AddressV = wrap; };

struct a2v
{
float4 Position : POSITION0;
float2 TexCoords : TEXCOORD0;
};

struct v2p
{
float4 Position : POSITION0;
float2 TexCoords : TEXCOORD0;
};

struct p2f
{
float4 Color : COLOR0;
};

void vs( in a2v IN, out v2p OUT )
{
OUT.Position = mul( IN.Position, WorldViewProj );
OUT.TexCoords = IN.TexCoords;
}

void ps( in v2p IN, out p2f OUT )
{
float4 TextureColor = tex2D( ColorTextureSampler, IN.TexCoords );
OUT.Color = tex1D( PaletteTextureSampler, TextureColor.r );
OUT.Color.a = OUT.Color.r;
OUT.Color.r = OUT.Color.b;
OUT.Color.b = OUT.Color.a;
}

technique PaletteTechnique
{
pass p0
{
vertexshader = compile vs_1_1 vs();
pixelshader = compile ps_1_4 ps();
}
}


Share this post


Link to post
Share on other sites
Quote:
Original post by Scet
Yes, via shaders.

You create your indexed image and load it in a format such as L8 or A8 for 8-bit(I don't know the format names in XNA). Then inside the program take the palette and create a texture out of it, for 8-bit it would be 256x1 or 1x256. In the pixel shader you use a color component of the source texture as a texture coordinate to lookup the color in the palette texture.

I have an example HLSL shader someplace that I'll try to find, I feel like I've answered this question a million times.

Edit:
*** Source Snippet Removed ***


in directX how would I do that. Would I use the PALETTEENTRY array gotten from one of the CreateTexture... functions, and then what would I do with it?

thanks

Share this post


Link to post
Share on other sites
He means make a full-color (32-bit) bitmap where each pixel on the bitmap is one of your palette colors. Kind of like a real life palette. Then you send two textures to the shader-one for the original image, and one for the color palette "lookup table".

The pixel shader will see a color on your source bitmap, then look at the second bitmap to find the replacement color to draw to the screen.

Then, to render the same source bitmap with a different palette, you just need to make another secondary bitmap for each different palette you want to use (i.e. one for the blue player, one for the red player, etc). Then on the C# side you decide which palette texture to use based on your player object or whatever.

Share this post


Link to post
Share on other sites

This topic is 3291 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this