Applying a Color to a Texture's Alpha Channel

Hi Everyone,

My team is working on a mobile game in which we are trying to change a character’s shirt color in different levels (e.g. if I feed the shader Color.blue, then the shirt is blue and the rest of his outfit stays its original colors). To start, our artist has created a texture for the character model, with a white shirt that is set to be the image’s alpha channel. I know this is working because if I use a Transparent shader, I can see only the shirt (not the rest of the character’s texture) disappear.

I know there is a way to make a shader apply a color instead of transparency to an alpha channel, but it’s a bit beyond my first month of shader experience. We’ve just been working with the Unity-provided Mobile VertexLit shader, and I’m trying to find a way to create a variant of that shader that gives me that effect. I don’t need to apply color to any other part of the texture - just the alpha channel.

Can anyone please help steer me in the right direction? Thank you very much!

In formal terms, you’re using the Alpha channel as a mask. For example, the standard Unity specular shader expects the alpha channel to be a spec-map, instead of transparency.

Assume shirtCol is the shirt color, which could be a single color value, or could have been looked up from a second texture (plaid, say.) And assume C is the regular texture, with the shirt alpha. Clearly, you could write if(C.a>0.5) C2=shirtCol; else C2=C;.

But might have some fuzzy boundaries, so want alpha=0.2 to blend the shirt color in at 20%: C2=0.8*C+0.2*shirtCol;. It turns out that Lerp was designed specifically for that purpose (lerp is in hardware on real graphics cards, not sure about mobiles):

// after lookup base texture C, possibly look up shirtCol
fixed4 C2; // final color
C2 = Lerp(C, shirtCol, C.a);
// lighting comes next:

I am pretty certain that you cannot add colour to an alpha channel (I could be wrong as I am also new to game engine shaders but I’ve been working in 3D and 2D graphics for a while and I’ve never ever seen a coloured alpha channel before). It would turn into shades of grey if you were able to apply any type of shade to it. Instead of using a transparent shirt, could you not have a white shirt and then tint that portion of the UV?

I got it working in ShaderLab, everyone! Thank you for your help!! This Shader colors the material with _AlphaColor only where the texture is set to the alpha channel as a mask. It will appear identical to the Unity Mobile VertexLit built-in shader, but with this coloring effect.

Here’s the code, for anyone who wants it!
Shader “Custom/VertexLitAlphaColoring” {
Properties {
_AlphaColor (“Alpha Color”, Color) = (1,1,1,1)
_MainTex (“Base (RGB)”, 2D) = “white” {}
}

SubShader {
	Tags { "RenderType"="Opaque" }
	LOD 80
		
	Pass {
		Tags { "LightMode" = "Vertex" }
			
		// Setup Basic
		Material {
			Diffuse (1,1,1,1)
			Ambient (1,1,1,1)
		} 
		Lighting On
		// Lerp between AlphaColor and the basic vertex lighting color
		SetTexture [_MainTex] {
			constantColor [_AlphaColor]
        		combine previous lerp(texture) constant DOUBLE, previous lerp(texture) constant
		}
		// Multiply in texture
                SetTexture [_MainTex] {
			combine texture * previous
               }
	}
		
}

}