Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Can I fade an object with no alpha component in the Shader?

Discussion in 'Scripting' started by petey, Dec 28, 2009.

  1. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,817
    Hi there,
    I'm using a fullbright shader in my game and it's working great but... I'd like to be able to fade some objects that are using it in and out over time. Since there is no alpha component to it, I'm not too sure how to do it.

    Is there a way to fade objects using this shader?

    This Shader is a FullBright Texture with an alpha from the image alpha.


    Code (csharp):
    1. Shader "My Shaders/No Lighting + Alpha" {
    2.     Properties {
    3.         _Color ("Color Tint", Color) = (1,1,1,1)
    4.         _MainTex ("SelfIllum Color (RGB) Alpha (A)", 2D) = "white"
    5.     }
    6.     Category {
    7.        Lighting On
    8.        ZWrite Off
    9.        Cull Back
    10.        Blend SrcAlpha OneMinusSrcAlpha
    11.        Tags {Queue=Transparent}
    12.        SubShader {
    13.             Material {
    14.                Emission [_Color]
    15.             }
    16.             Pass {
    17.                SetTexture [_MainTex] {
    18.                       Combine Texture * Primary, Texture * Primary
    19.                 }
    20.             }
    21.         }
    22.     }
    23. }
    Thanks
    Pete
     
  2. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    First off, don't put Lighting On in there if you don't need it! Also, this is probably a topic for the ShaderLab forum, not the scripting forum.

    I'm not sure if this is what you need because you are giving what seems to me to be contrary information:

    "Since there is no alpha component to it..."
    "This Shader is a FullBright Texture with an alpha from the image alpha."

    So, does your texture have an alpha channel, or not? If not, then this shader is appropriate.

    You can script the opacity of this shader using Material.SetFloat("_Opacity", opacity);

    Code (csharp):
    1. Shader "My Shaders/Tinted Texture with Opacity Control" {
    2.  
    3.  
    4. Properties
    5. {
    6.     _Opacity ("Opacity", Range (0, 1) ) = 1
    7.     _Color ("Tint Color", Color) = (1,1,1)
    8.     _MainTex ("Texture  (RGB)", 2D) = ""
    9. }
    10.  
    11. SubShader
    12. {
    13.     Tags {Queue = Transparent}
    14.     ZWrite Off
    15.     Colormask RGB
    16.     Blend SrcAlpha OneMinusSrcAlpha
    17.    
    18.     Color [_Color]
    19.     Pass
    20.     {      
    21.         SetTexture [_MainTex]
    22.         {
    23.             ConstantColor (0,0,0, [_Opacity])
    24.             combine texture * primary, constant
    25.         }              
    26.     }
    27. }
    28.  
    29.  
    30. }
     

    Attached Files:

  3. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,817
    Hey thanks!

    Damn, that was a daft post of mine, sorry about that. I hadn't seen the shader lab forum :)
    I guess what I meant was that I want the shader to still use the alpha from the image but have an overall opacity control that I can access to fade it in or out.

    I'll have a look and see what I can get, I've just not had much to do with shader programming.


    Thanks for the heads up on the lighting part! (would that have been chewing up CPU unnecessarily?)

    Pete
     
  4. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Not a problem. This shader does that:

    Code (csharp):
    1. Shader "My Shaders/Tinted Transparent with Opacity Control" {
    2.  
    3.  
    4. Properties
    5. {
    6.     _Color ("Tint Color (A = Opacity)", Color) = (1,1,1)
    7.     _MainTex ("Texture  (A = Opacity)", 2D) = ""
    8. }
    9.  
    10. SubShader
    11. {
    12.     Tags {Queue = Transparent}
    13.     ZWrite Off
    14.     Colormask RGB
    15.     Blend SrcAlpha OneMinusSrcAlpha
    16.    
    17.     Color [_Color]
    18.     Pass
    19.     {      
    20.         SetTexture [_MainTex]
    21.         {
    22.             combine texture * primary
    23.         }      
    24.     }
    25. }
    26.  
    27.  
    28. }
    Not the CPU; this workload is on the GPU. Now, this is an extremely simple shader, so I doubt it's going to affect overall performance for you noticeably, but in my test, I found that using what I did was about 12% faster than using emission, even though they are visually identical. I had to get well over a million triangles on the screen to get any solid numbers, though!

    I also did a test to see if it was worth having a separate variable for opacity, outside of material.color. I figured it would be faster to multiply the RGB texture by an RGB color, which could stay consistent, and only send a single value using Material.SetFloat() for opacity as needed. However, I actually found it to be 4% faster to send the entire color over, and use a single color variable for both tint and opacity. So that's why this shader looks a little different than the last. You can use something like the following to script your overall opacity. Keep in mind that you never have to worry about increasing the opacity of the grey areas of your texture's alpha channel, because the values get clamped between 0 -1 automatically.

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. class SetOpacityEveryFrame : MonoBehaviour {
    4.  
    5.  
    6. // Drag the material onto this variable slot
    7. // in the Inspector to avoid the GetComponent call
    8. // that comes with using renderer.material.
    9. public Material material;
    10.  
    11. Color color;
    12.  
    13. void Start ()
    14. {
    15.     color = material.color;
    16. }
    17.  
    18. void Update ()
    19. {
    20. //  color.a = (0-1 opacity value);
    21.     material.color = color;
    22. }
    23.  
    24.    
    25. }
     

    Attached Files:

  5. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,817
    Thanks Jessy!

    That worked great! I'll have to look into shaders a little more, there's probably a lot of things i could do to optimise my materials. Which is kinda nessesary with iphone stuff ect.

    Thanks again!
    Pete