Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I've been trying to work on a game with the look of an 8-bit game using XNA, specifically using the NES as a guide.

The NES has a very specific palette and each sprite can use up to 4 colors from that palette. How could I emulate this? The current way I accomplish this is I have a texture with defined values which act as indexes to an array of colors I pass to the GPU. I imagine there must be a better way than this, but maybe this is the best way?

I don't want to simply make sure I draw every sprite with the right colors because I want to be able to dynamically alter the palette. I'd also prefer not to alter the texture directly using the CPU.

share|improve this question

3 Answers 3

up vote 7 down vote accepted

I think what you are doing is just fine.

If you had more than four colours I might suggest doing a dependent texture read: Instead of using the value of the texture to look up an array of colours (I assume you are passing these as shader parameters), you could use it to look up a colour in another 1D texture containing your palette.

But this has some performance implications - and I think it limits you to PS 2.0.

(There are some other sneaky optimisations possible if you're having to switch palettes every sprite and all the extra batches are killing performance. But I imagine in a NES-like game this is not the case.)

I think what might be useful in your situation is to use the XNA content pipeline to take an easily editable, colour version of the sprite, check it only uses four colours, convert it to a texture with your four levels of grayscale (usable by your shader), and perhaps also output the original colour palette along side the texture.

The Creators Club samples for the content pipeline might be a good place to start.

share|improve this answer
1  
+1 The content pipeline can be your friend sometimes. Use it to your advantage. –  Michael Coleman Apr 19 '11 at 0:02

Would it be to slow to create a composite class that can contain up to 4 objects that are one of each color for the sprite and combine that into the sprite that gets drawn. So each sprite is a composite of up to 4 sprites combined and then drawn.

Then you could change 1 color via that class and the entire sprite would change.

(note, I haven't done XNA but this might spark some ideas with you.)

share|improve this answer

If you don't care at all about memory performance, you could store your sprites uncompressed such that pixels with color 0 are pure red, 1 are pure green, 2 are pure blue, and 3 are pure alpha.

When about to render a sprite, set a matrix pixel shader constant made of four vectors that correspond to colors 0, 1, 2, 3. In the pixel shader, execute the following code to recolor the sprite texel:

float4 unpaletted = tex2D( texture, uv );
float4 paletted = mul( unpaletted, paletteMatrix );

This works because when a pixel is color 0, it is pure red, and so it will end up as

1 * palette[0] + 0 * palette[1] + 0 * palette[2] + 0 * palette[3]

or

1 * palette[0]

and the same applies to colors 1, 2, and 3.

If you do care about memory performance, then by all means store your NES palette as a 256x1 RGB uncompressed texture, set texture filtering to nearest-neighbor, store your sprites as uncompressed 8 bit single-channel textures, and do shader code something like this:

float palette_index = tex2D( texture, uv );
float4 color = tex1D( palette, palette_index * 255 );
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.