Saturday, January 11, 2020

What is a good method for coloring textures based on a palette in XNA?


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.



Answer



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.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...