- Home /
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!
Alpha Channels are black and white. There is no way to add colour to an alpha channel, You should be able to set the diffuse colour to be the colour you wish. Also - why are you using alpha channels? You could just assign a different material to the shirt than the rest of the body.
Thanks P. I don't need to indicate color in the alpha channel. I would like the shader to have a _Color property that ONLY gets applied to parts of the texture set as the alpha channel, not the whole texture. That part of the texture, yes, would be in greyscale, and I'd want to apply a Color value set in the $$anonymous$$aterial inspector to that.
I'm trying to not use two materials because it's an extra draw call, which I want to avoid for this mobile game.
Does that help?
Ah! I didn't realise you were shader editing, this is beyond any of my unity knowledge - I would have gone the lazy route and stuck with an extra draw call :p
I'm using that shader above and it works great. But I was wonder how I could allow lighting to effect the whole thing. One part is effected by light the alpha part is unaffected and full bright, I just basically want it all to be effected by light.
Thanks
Answer by TeddyDief · Sep 12, 2012 at 11:00 PM
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
}
}
}
}
Answer by Owen-Reynolds · Sep 12, 2012 at 05:42 PM
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:
Thanks Owen! This definitely seems to be what I'm looking for. I'm trying to fiddle around with this, but the Unity $$anonymous$$obile VertexLit shader I'm basing off of is written in Unity's ShaderLab syntax, not GS$$anonymous$$L. Sorry to bother you again, but you wouldn't happen to know how to do this in ShaderLab would you? In short, I'm looking to fill:
Pass {
Tags { "Light$$anonymous$$ode" = "Vertex" } $$anonymous$$aterial { Diffuse (1,1,1,1) Ambient (1,1,1,1) } Lighting On SetTexture [_$$anonymous$$ainTex] {
// SO$$anonymous$$ETHING GOES HERE TO SET THE TEXTURE AS WHITE,
// BUT THE ALPHA $$anonymous$$AS$$anonymous$$ TO _ALPHACOLOR)
} }
Answer by justinl · Sep 12, 2012 at 09:53 PM
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 believe they call it 'alpha' because it has no particular meaning. It usually means mask or transparency, but you could use it for anything you like. The shader could use it for tinting, for example.
yeah after thinking about it I think you're right about the shader engine using it for whatever it wants. Thank you Ed Catmull!