[xna] palette manipulation

Started by
4 comments, last by EvilNando 15 years, 4 months ago
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
Advertisement
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();    }}

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
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.
Ah I see thanks a lot

I will try it as soon as I can
mm Im having trouble trying to figure out how to manipulate the palette indexes.

Can you post a very simple example of this?

This topic is closed to new replies.

Advertisement