Search Unity

_Color alpha doesn't affect output

Discussion in 'Shaders' started by Damocles, Jun 24, 2013.

  1. Damocles

    Damocles

    Joined:
    Jul 21, 2012
    Posts:
    46
    I have a really simple shader (below) which should, as far as I can tell, allow me to change the _Color property at run time, and have the alpha reflect the changes. Yet it doesn't. I can change the color, and the color on the mesh changes, but the alpha is completely unaffected. The mesh has vertex alpha, and by toggling that in the shader I can see that the alpha is working as intended right up to the point where the fragment program returns, and for some reason it ignores the alpha in the returned result. I even tried making the last line of the frag shader "c.a = 0;" and no change.

    Does anyone know why the shader hates me?

    code:
    Code (csharp):
    1.  
    2. Shader "Labyrinth/Cone Circle" {
    3.     Properties {
    4.         _MainTex ("Base (RGBA)", 2D) = "white" {}
    5.         _Color ("Color", Color) = (1,1,1,1)
    6.     }
    7.     SubShader {
    8.         Tags { "RenderType"="Transparent"
    9.                 "Queue" = "Transparent+2"
    10.                 "IgnoreProjector"="True"
    11.             }
    12.         ZWrite Off
    13.         Blend OneMinusDstColor One
    14.         Cull Off
    15.         ZTest LEqual
    16.        
    17.         Pass{
    18.             CGPROGRAM
    19.             #include "UnityCG.cginc"
    20.             #pragma vertex vert
    21.             #pragma fragment frag
    22.                
    23.             sampler2D _MainTex;
    24.             fixed4 _MainTex_ST;
    25.             fixed4 _Color;
    26.            
    27.             struct v2f
    28.             {
    29.                 half4 color : COLOR;
    30.                 fixed4 pos : SV_POSITION;
    31.                 fixed2 uv : TEXCOORD0;
    32.             };
    33.            
    34.             v2f vert(appdata_full v)
    35.             {
    36.                 v2f o;
    37.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    38.                 o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    39.                 o.color.rgba = v.color.rgba;
    40.                 return o;
    41.             }
    42.            
    43.         fixed4 frag(v2f i) : COLOR
    44.                 {
    45.                     i.uv.y -= _Time.x * 5;
    46.                      fixed4 c = tex2D(_MainTex, i.uv) * i.color;
    47.                     c.rgb *= _Color.rgb;
    48.  
    49.                     c.a *= _Color.a;
    50.  
    51.                      return c;
    52.                 }
    53.             ENDCG
    54.         }
    55.     }
    56. }
    57.  
    58.  
     
  2. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    It's doing exactly what you're telling it to. You're not using alpha for anything.

    I don't understand what this means.
     
    Last edited: Jun 24, 2013
  3. Damocles

    Damocles

    Joined:
    Jul 21, 2012
    Posts:
    46
    D'oh! You're right, I was having a brain fart. It's additive blending, not alpha. I was convinced that was alpha blending for some reason. Thanks Jessy.

    That said, it is very odd that I can use two different meshes - one with alpha values all at 1, and one with varying alpha values, and I can see the difference. There must be something funny going on somewhere in the mesh data, but I'll worry about that another time.