Help understanding Shader to change color at runtime

Hey All
I need to get a shader together that can handle changing the pixel color of my player objects at runtime, but I am getting tripped up/confused on when this happens. I am mainly confused on if it is happening purely in the Texture that the Sprite is mapped to, or if it is happening “realtime” while the animation is playing on that object. I ask, as I have multiple texture sheets for the same object. This is mainly due to the object having a LOT of sprites and also that the sprite sizes differ depending on actions.
I have looked at a lot of demos/assets (PaletteFX, other swappers in the store) and also tutorials on shaders to do this, and this one seems to make the most sense: How to Use a Shader to Dynamically Swap a Sprite's Colors | Envato Tuts+
My main question is this: If the shader is making changes to the texture, then I need to call a palette swap every time the texture changes based on Animation, correct? If so, than is there a shader that could do the calculation based on the existing sprite that is being displayed?

Shaders are not modifying the texture. Shaders modify the color value being displayed. That’s all. In the example you linked to, all of those characters are using the same Sprite texture with no modifications, and the color values are being swapped by the GPU before displaying them on screen. Apart from some very special situations, shaders cannot ever actually directly modify the Sprite/texture’s data. And those special situations never include sprites.

1 Like

Thanks for the info on this. So, I guess my next question on this is (and I apologize for my ignorance) when do I run the code to handle the colors being changed per sprite? I know I can add the shader to my material, and that will handle changing the color “realtime” and not change the texture, but once I load in my player object, I need to change the color based on certain parameters (for me, it is based on equipment), so I need to get the current palette of the player sprite, and then find the right palette to change my colors to. I think I can handle that aspect of my code, but my problem in understanding how the shader works is, once I have my palette and the right color, what/when do I run something to make that change in the shader?
In the example I linked, they have a swap color function, which I get how it works, but do I have to run that every time my sprite changes? Only the first time after loading the sprite in? I am just not knowledgeable to get my head around that. I appreciate the reply!

For your side of things, you need a material with the color swapping shader assigned. You need to set the color values the shader needs on the material once and assign it to the Sprite Renderer component.

And you’re done.

Each character will need a unique material, so you’ll need to modify the values on the .material instead of the .sharedMaterial which will automatically create a copy of the material. Or you can use material property blocks to assign the color values on the Renderer component itself and use the same material on all Sprite Renderer components that need this functionality.

Behind the scenes the GPU is running that shader code every single time it renders each pixel of each sprite. This might sound wasteful, but it’s what GPUs are designed to do. It’s how literally everything on screen gets rendered. There are cases where caching the results of a shader, like saving a modified sprite to a new texture, is something someone might do to improve rendering performance. But if course comes at the cost of increased memory usage, and might not noticeably improve performance unless you’re using a very, very expensive shader or a very slow GPU. Unless you see performance being bad, don’t bother pre-optimizing this kind of thing.