Search Unity

Colored alpha-blended shader, always drawn on top

Discussion in 'Shaders' started by japtar10101, Feb 4, 2012.

  1. japtar10101

    japtar10101

    Joined:
    Mar 13, 2010
    Posts:
    138
    Hello there. I hate to ask a newbie question, but I never worked with shaders before (I'm excellent with scripting, though). I'm using the EZ GUI library to create the GUI for our game, and I'm trying to create a shader similar to Angry Bot's Cursor shader: mobile-efficient, alpha-blended shader that's always drawn on top. The only difference is that the shader takes in a color.

    I'm trying to use this for text, and well, I need color on the text for the shader to be useful.

    Thanks in advance!
     
  2. japtar10101

    japtar10101

    Joined:
    Mar 13, 2010
    Posts:
    138
    Well, through experimentation, I got this. It seems to work...

    Code (csharp):
    1. Shader "Mobile/GUI (Colored)" {
    2.     Properties {
    3.         _MainTex ("Base", 2D) = "white" {}
    4.         _MainColor ("Color", Color) = (1.0, 1.0, 1.0, 1.0)
    5.     }
    6.    
    7.     CGINCLUDE
    8.  
    9.         #include "UnityCG.cginc"
    10.  
    11.         sampler2D _MainTex;
    12.         fixed4 _MainColor;
    13.        
    14.         half4 _MainTex_ST;
    15.                        
    16.         struct v2f {
    17.             half4 pos : SV_POSITION;
    18.             half2 uv : TEXCOORD0;
    19.             fixed4 vertexColor : COLOR;
    20.         };
    21.  
    22.         v2f vert(appdata_full v) {
    23.             v2f o;
    24.            
    25.             o.pos = mul (UNITY_MATRIX_MVP, v.vertex);  
    26.             o.uv.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
    27.             o.vertexColor = v.color * _MainColor;
    28.                    
    29.             return o;
    30.         }
    31.        
    32.         fixed4 frag( v2f i ) : COLOR { 
    33.             return tex2D (_MainTex, i.uv.xy) * i.vertexColor;
    34.         }
    35.    
    36.     ENDCG
    37.    
    38.     SubShader {
    39.         Tags { "RenderType" = "Transparent" "Queue" = "Transparent+100"}
    40.         Cull Off
    41.         Lighting Off
    42.         ZWrite Off
    43.         ZTest Always
    44.         Fog { Mode Off }
    45.         Blend SrcAlpha OneMinusSrcAlpha
    46.        
    47.     Pass {
    48.    
    49.         CGPROGRAM
    50.        
    51.         #pragma vertex vert
    52.         #pragma fragment frag
    53.         #pragma fragmentoption ARB_precision_hint_fastest
    54.        
    55.         ENDCG
    56.          
    57.         }
    58.                
    59.     }
    60.     FallBack Off
    61. }
     
    lauraaa likes this.
  3. nbg_yalta

    nbg_yalta

    Joined:
    Oct 3, 2012
    Posts:
    378
    This is what I was lookin for, thx alot.
     
  4. zbalai

    zbalai

    Joined:
    May 28, 2012
    Posts:
    21
    Thank you so much for publishing this great shader!

    Kindest Greetings from Hungary
    Zsolt

     
  5. zbalai

    zbalai

    Joined:
    May 28, 2012
    Posts:
    21