- Home /
Color Replace Shader Unity2D
Hi,
Im building a 2D RTS game using Unity 4.3. I need a way of colouring the units based on which team they belong to.
The way I had envisioned doing it was to have a separate texture per unit that masks a certain part of the unit and colours it to whatever colour is that team.
So for example the unit is:
Then if I wanted to colour her hair then the mask would be:
Then in a shader I would do something like:
fixed4 frag(v2f IN) : COLOR
{
half4 original = tex2D (_MainTex, IN.texcoord);
half4 greyscale = half4 (original);
greyscale.rgb = dot(greyscale.rgb, float3(0.3, 0.59, 0.11));
half4 maskedPart = greyscale * tex2D(_MaskTex, IN.texcoord1).a * _ReplaceColor;
half4 unmakedPart = original * (1-tex2D(_MaskTex, IN.texcoord1).a);
return maskedPart + unmakedPart;
}
Which will then only tint the hair whatever colour the team is set to.
The problem is that I am unsure how to pass in the extra texture coordinates (IN.texcoord1) as my _MaskTex may be laid out different to _MainTex and thus the per vertex texture coords will be off.
The only way I can think of doing it is by totally rewriting Unity's SpriteRenderer as its a sealed class and cannot be extended :(
Does anyone have any suggestions?
Perhaps mark the area to be recoloured with a specific value of alpha? you know, 1 point off being opaque or something, then recolour those areas to be the new hue? Perhaps a specific colour but that would affect your grayscale range.
@whydoidoit yes, or I could use a HSV replace such as: http://gmc.yoyogames.com/index.php?showtopic=589348 but I would prefer this masking approach.
Your answer
![](https://koobas.hobune.stream/wayback/20220613141826im_/https://answers.unity.com/themes/thub/images/avi.jpg)