Change multiple colors of a sprite using shader

Hi,

I’m trying to create multiple instances of the same 2D sprite but with different color combinations. I’ve already tried using the solution described here and it works but it only addresses the R value of the RGBA scale of each pixel in the original texture. I need something that will recognise the G and B values as well since my sprite texture can use several different colors that have the same amount of red.

I’ve tried adding _SwapTexG and _SwapTexB to the shader and thus working with three 1x256 textures to identify the color that needs to be replaced and I think I may be on the right track but I can’t get it to work. Does anyone know how to alter the shader code to be able to do this properly?

Maybe not the solution you’re looking for, but unless you need more than 256 colors in a single sprite it may be simpler and possibly a little less costly to calculate (I don’t know how shaders work yet but noticed this article mentioning calculation costs to finding exact rgb values) to make the actual colors of the sprite different on the red channel anyway, even if it’s not necessarily the colors you want, and then set them to the actual default colors you do want using variables at the start instead. A lot of sprites in games that make heavy use of palette swapping or player selected colors have colors that are pretty ugly because the player is never intended to see them and they’re just there to make the swapping system easier to implement and work with on the code side.

Not sure exactly what you mean, if you have a grayscale sprite (single channel), make each part a different shade, which maps to a lookup texture of colors. Your base sprite pixels act only as the index to another texture of colors.

If you really need to have baked-in colors for the base sprite for some reason, you could pair the sprite with a secondary map for the lookup texture.

www.youtube.com/watch?v=u4Iz5AJa31Q

1 Like

Thanks for the link to the video, it seems there’s a lot of useful stuff I can learn from that channel. I still don’t have a solution to my problem but it gave me an idea on what to try next.

Perhaps I didn’t make myself clear in my first post, let me give an example:
I have a character sprite with, let’s say, a range of brown colors for the skin and a range of blue ones for the hair. Some of these colors will have the same amount of red, like RGB(209, 104, 0) (brown) and RGB(209, 255, 255) (light blue) and the shader from the tutorial I mentioned replaces both of them with the same replacement color because it only uses a 1x256 texture for this. I need it to somehow assign a pixel color based on all three values. The color matrix solution from the video seems like a step in the right direction, though.