Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TeddyDief · Sep 12, 2012 at 07:03 AM · shadershadersalphaalpha channel

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!

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PProductions · Sep 12, 2012 at 02:53 PM 0
Share

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.

avatar image TeddyDief · Sep 12, 2012 at 05:42 PM 0
Share

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?

avatar image PProductions · Sep 12, 2012 at 05:47 PM 0
Share

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

avatar image Vanblam · Aug 04, 2014 at 08:20 PM 0
Share

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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
                }
     }
         
 }
 
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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:
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TeddyDief · Sep 12, 2012 at 10:31 PM 0
Share

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)

} }

avatar image
0

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?

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DaveA · Sep 12, 2012 at 11:26 PM 0
Share

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.

avatar image justinl · Sep 13, 2012 at 02:55 AM 0
Share

yeah after thinking about it I think you're right about the shader engine using it for whatever it wants. Thank you Ed Catmull!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiply Shader with Alpha 0 Answers

Changing custom shader. Add alpha cutout 1 Answer

How to access alpha value of transparent shader. 3 Answers

Transparent Diffuse Specular Normal Shader problem 1 Answer

How to add alpha cutout shader to this curveworld shader 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges