Saturday, November 2, 2019

xna - How to colorize certain parts of a model - like RTS games have those team colors?


I am in a need of implementing something we see in RTS games: team colors. Basically, I am looking for a way to colorize certain parts of a model. How would I achieve this? I have no idea where to even begin. Do I need to make some adjustments to the 3d model first?



Answer



The way I would do this is based on the texture maps you are applying to the units. For most stuff you would have some regions that need to be tinted and some that do not. So for example, you might have a normal tank but you want to just change the color of some flags.


If you paint the texture as if the colored regions are grayscale you can then use the alpha channel to designate which regions need to be colorized.


So in your shader you could go (for the most simple way):



float3 finalColor;
if (colorFromTex.a > 0)
finalColor = colorFromTex.rgb;
else
finalColor = colorFromTex.rgb*myTintColor.rgb;

That's not optimal though since you will a have a hard transition and have a branch in your shader. Instead it's better to go:


float3 finalColor = lerp(colorFromTex.rgb, 
colorFromTex.rgb*myTintColor.rgb, colorFromTex.a);

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...