Five colors gradient on sprite

Hello everybody! Nobody ever answers any question I post here… but anyway!!

I’m starting to study about vertex-fragment shaders, because I want to create a shader for the sprites of my game that allows me to tint the texture with a gradient of up to 5 different colors.

I think this picture shows better what I want to achieve:


Well, first of all with the information given it’s not possible. What i mean is that the outside of your “cape” still has it’s original color while everything else got tinted. Either you tint the whole sprite or if you really need seperate regions that you want to tint you have to create a B/W mask image for that sprite to choose which “pixels” / texels you want to tint.

To create the gradient you mignt want to use Unity’s Gradient class. What allows you to easily specify a gradient with as many colors as you like inside the inspector. You can use it’s Evaluate method to create a gradient texture out of the Gradient. The texture should have the same pixel height as your sprite texture and a pixel width of “1”. So to actually “fill” the gradient texture you just evaluate your Gradient for every pixel of the gradient texture.

Use a shader that simply blends the two textures together. If you additionally want to mask certain areas you would use the mask texture to lerp between the original color and the color you obtain from mixing (multiplying) the original with the gradient texture.

Though if you set the gradient once and want to use the same gradient all the time it would make more sense to do the blending inside a script at start and create a new texture out of the result. That way you can simply use the already blended texture without any special shader. It would improve performance especially when using a mask as well since the shader would require three texture lookups and a lot of blending math.