Search Unity

Adding transparency 0.o

Discussion in 'Shaders' started by killakiwi, Sep 5, 2013.

  1. killakiwi

    killakiwi

    Joined:
    Sep 5, 2013
    Posts:
    22
    Hey guys, I'm new to writing shaders and finding it a bit difficult to get in. I've read through the Unity tutorials but I'm struggling with adding transparency to my shader.

    so I'm making a "thermal" shader for a 2d game. The conversion from full colour range to blue - yellow works okay, but i cant for the life of me get it to work properly with transparent areas. I've tried changing thermal.a but those alpha values seem to not be accurate(as if every pixel shares the same alpha value)...

    Code (csharp):
    1.   Shader "Custom/ThermalVision" {
    2.         Properties {
    3.             _MainTex ("Base (RGB) Trans (A)", 2D) = "" {}
    4.             _vxOffset ("Offset", Range(0,1)) = 1
    5.         }
    6.         SubShader {
    7.             Blend SrcAlpha OneMinusSrcAlpha
    8.             Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    9.            
    10. //              Pass{
    11. //                  Color(.389, .1465, .4645, 0.5)
    12. //              SetTexture[_MainTex] {Combine one - texture * primary alpha}
    13. //              SetTexture[_] {Combine previous Dot3 primary}
    14. //              SetTexture[_MainTex] {Combine previous, texture alpha}
    15. //              }
    16.             Pass{
    17.            
    18.                
    19.            
    20.                 CGPROGRAM
    21.                 #pragma vertex vert_img
    22.                 #pragma fragment frag
    23.                 #pragma fragmentoption ARB_precision_hint_fastest
    24.                 #include "UnityCG.cginc"
    25.                 #pragma target 3.0
    26.                 #pragma only_renderers d3d9
    27.        
    28.                 struct v2f {
    29.                     float4 pos:SV_POSITION;
    30.                     float4 uv:TEXCOORD0;
    31.                 };
    32.                
    33.                
    34.                 float3 texCol;
    35.                 float _vxOffset;
    36.                
    37.                
    38.                 v2f vert(appdata_base input)
    39.                 {
    40.                     v2f o;
    41.                     o.pos = mul(UNITY_MATRIX_MVP, input.vertex);
    42.                     o.uv = input.texcoord;
    43.                     return o;
    44.                 }
    45.                uniform sampler2D _MainTex;
    46.  
    47.                 float4 thermal;
    48.                
    49.                 float4 frag (v2f_img i) : COLOR {
    50.  
    51.                     float4 pixcol = tex2D(_MainTex, i.uv);
    52.                     float4 colors[3];
    53.                     colors[0] = float4(0.0,0.0,1.0,1.0);
    54.                     colors[1] = float4(1.0,1.0,0.0,1.0);
    55.                     colors[2] = float4(1.0,0.0,0.0,1.0);
    56.                     float lum = (pixcol.r+pixcol.g+pixcol.b)/3.;
    57.                     int ix = (lum < 0.5)? 0:1;
    58.                    
    59.                     while( thermal.x < 0.5 )
    60.                     {
    61.                             thermal += lerp(colors[0], colors[1], (lum-float(ix)*0.5)/0.5);
    62.                             //thermal = pixcol * i.color;
    63.                    
    64.                        
    65.                     }
    66.                    
    67.                     return thermal;
    68.                 }
    69.                
    70.                 ENDCG
    71.             }
    72.            
    73.         }
    74.         FallBack "VertexLit"
    75.     }
    Thanks for reading this far :D
     
  2. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    Your `colors` variables have a 1.0 in the last parameter, setting alpha to 1.0 for all pixels, and then later you add your `thermal` stuff together but it's simply going to bring in those 1.0's, instead of the alpha from the texture?

    You need to extract alpha from pixcol.a somewhere e.g.

    return float4(thermal.rgb,pixcol.a);
     
    Last edited: Sep 5, 2013
  3. killakiwi

    killakiwi

    Joined:
    Sep 5, 2013
    Posts:
    22
    You're a fricken hero