We needed a shader that could be used to tint multiple areas of a texture and still have transparency.
I’m new to writing shaders, but I was able to come up with this (based on various examples I found):
Shader "Custom/LitTransparentTintMask" {
Properties {
//Main Color
_Color ("Main Color", Color) = (1,1,1,1)
//Main Texture - with alpha for transparency
_MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
//Mask Texure - this texture defines which parts of the main texture are tinted
_Mask ("Mask (RGB)", 2D) = "white" {}
//Any parts unerneath red in the mask texture will be tinted this color
_SWColor1 ("Red Channel", Color) = (1.00,0.00,0.00,1.00)
//Any parts unerneath green in the mask texture will be tinted this color
_SWColor2 ("Green Channel", Color) = (0.00,1.00,0.00,1.00)
//Any parts unerneath blue in the mask texture will be tinted this color
_SWColor3 ("Blue Channel", Color) = (0.00,0.00,1.00,1.00)
}
SubShader {
Tags {"Queue"="Transparent" "RenderType"="Transparent"}
LOD 100
CGPROGRAM
#pragma surface surf Lambert alpha
#pragma target 3.0
sampler2D _MainTex;
sampler2D _Mask;
float4 _Color;
float3 _SWColor1;
float3 _SWColor2;
float3 _SWColor3;
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
//color from the main texture
fixed4 base = tex2D(_MainTex, IN.uv_MainTex) * _Color;
//color from the mask texture
fixed3 mask = tex2D(_Mask, IN.uv_MainTex);
//masked color - color of the channel in the mask multiplied by the tint color for that channel
//all non-tined parts are black
fixed3 tinted = base.rgb * (mask.r * _SWColor1.rgb + mask.g * _SWColor2.rgb + mask.b *_SWColor3.rgb);
//main color - color of the main texure with the tinted parts subtracted
//all tinted parts are black
fixed3 reg = base.rgb - (mask.r * base.rgb + mask.g * base.rgb + mask.b * base.rgb);
//add the tinted and non-tinted parts together
o.Albedo = tinted + reg;
//inlcude the alpha from the main texture for transparency
o.Alpha = base.a;
}
ENDCG
}
Fallback "Transparent/Diffuse"
}
It actually does exactly what we want (and is lit, as a bonus).
The problem is that it really drops the fps on mobile (iOS).
Can anyone recommend a way to improve the performance or another way to have tint masks and transparency (with or w/o lighting)?
Thanks.