Search Unity

Sprites/AlphaCutoff shader for healthbars etc

Discussion in 'Shaders' started by cygnusfear, Nov 12, 2013.

  1. cygnusfear

    cygnusfear

    Joined:
    Oct 24, 2012
    Posts:
    14
    In case anyone's looking to do the good old alpha healthbar trick with the new sprite system, I've whipped this one up quickly. The alpha channel of your sprite is used to determine what to show and fills that with the Tint color.

    Enjoy,

    A

    Code (csharp):
    1.  
    2. Shader "Sprites/AlphaCutoff"
    3. {
    4.     Properties
    5.     {
    6.         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
    7.         _Color ("Tint", Color) = (1,1,1,1)
    8.         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
    9.         _CutOff ("Cutoff", Range (0, 1)) = 0.5
    10.     }
    11.  
    12.     SubShader
    13.     {
    14.         Tags
    15.         {
    16.             "Queue"="Transparent"
    17.             "IgnoreProjector"="True"
    18.             "RenderType"="Transparent"
    19.             "PreviewType"="Plane"
    20.             "CanUseSpriteAtlas"="True"
    21.         }
    22.  
    23.         Cull Off
    24.         Lighting Off
    25.         ZWrite Off
    26.         Fog { Mode Off }
    27.         Blend SrcAlpha OneMinusSrcAlpha
    28.  
    29.         Pass
    30.         {
    31.         CGPROGRAM
    32.             #pragma vertex vert
    33.             #pragma fragment frag
    34.             #pragma multi_compile DUMMY PIXELSNAP_ON
    35.             #include "UnityCG.cginc"
    36.            
    37.             struct appdata_t
    38.             {
    39.                 float4 vertex   : POSITION;
    40.                 float4 color    : COLOR;
    41.                 float2 texcoord : TEXCOORD0;
    42.             };
    43.  
    44.             struct v2f
    45.             {
    46.                 float4 vertex   : SV_POSITION;
    47.                 fixed4 color    : COLOR;
    48.                 half2 texcoord  : TEXCOORD0;
    49.             };
    50.            
    51.             fixed4 _Color;
    52.             fixed _CutOff;
    53.  
    54.             v2f vert(appdata_t IN)
    55.             {
    56.                 v2f OUT;
    57.                 OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
    58.                 OUT.texcoord = IN.texcoord;
    59.                 OUT.color = IN.color * _Color;
    60.                 #ifdef PIXELSNAP_ON
    61.                 OUT.vertex = UnityPixelSnap (OUT.vertex);
    62.                 #endif
    63.                 return OUT;
    64.             }
    65.  
    66.             sampler2D _MainTex;
    67.  
    68.             fixed4 frag(v2f IN) : COLOR
    69.             {
    70.                 return tex2D(_MainTex, IN.texcoord).a > _CutOff ? IN.color : 0;
    71.             }
    72.         ENDCG
    73.         }
    74.     }
    75. }
    76.  
     
    rakkarage likes this.
  2. MABManZ

    MABManZ

    Joined:
    Sep 29, 2011
    Posts:
    144
    Unfortunately I tried this shader and all I see is the pink "something is wrong" look on anything I apply it to. I'm currently using an Alpha cutoff shader I hacked together with little knowledge of shader programming, but I'd like something that allows me to change the tint color!
     
  3. cygnusfear

    cygnusfear

    Joined:
    Oct 24, 2012
    Posts:
    14
  4. Mortis-Mortavius

    Mortis-Mortavius

    Joined:
    Sep 23, 2013
    Posts:
    3
    Hey cygnusfear,

    First I'd like to say, thank you sooooo much for writing this. It's almost exactly what I'm looking for...

    Is there a way to change this in order to display the image/texture, rather than the fill color?

    I've only been using Unity for about a week so I'm pretty new... and I haven't even attempted to wrap my head around shaders yet :)

    Thanks!
     
    Last edited: Dec 17, 2013
  5. Mortis-Mortavius

    Mortis-Mortavius

    Joined:
    Sep 23, 2013
    Posts:
    3
    So I just read up on shaders and devised a method for doing exactly what I wanted. Quite a bit to ingest but I've got a working solution...

    Code (csharp):
    1.  
    2.                         fixed4 frag(v2f IN) : COLOR
    3.                         {
    4.                             fixed4 tex = tex2D(_MainTex, IN.texcoord);
    5.                    
    6.                             if (tex.a > _CutOff) {
    7.                                 tex.a = 1;
    8.                                 return tex * IN.color;                     
    9.                             } else {
    10.                                 return 0;
    11.                             }
    12.                         }
    13.  
     
    rakkarage likes this.
  6. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    902
    Is there any way to add partial transparency as well, so the health-bar is see-through? I'd like alpha clipping with a constant Opacity parameter to set object transparency when the clipped graphic is drawn.
     
  7. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    902
    After a lot of chasing information (documentation in this field is the suckiest yet for Unity, which otherwise has been very good), I've created a suitable shader for my needs. Very easy once you know what Clip() does (explained in HLSL documentation, not Unity docs).

    The shader has independent properties for cutoff threshold and final alpha value.

    Code (csharp):
    1.  
    2. Shader "Custom/NewShader" {
    3.     Properties {
    4.         _MainTex ("Color", 2D) = "white" {}
    5.         _Cutoff ("Threshold", Range (0,1)) = 0.5
    6.         _Fade ("Opacity", Range (0, 1)) = 1
    7.     }
    8.     SubShader {
    9.       Tags { "Queue"="Transparent" "RenderType"="Transparent" }
    10.       Lighting Off
    11.       CGPROGRAM
    12.       #pragma surface surf Lambert alpha
    13.       struct Input {
    14.           float2 uv_MainTex;
    15.       };
    16.       sampler2D _MainTex;
    17.       float _Cutoff;
    18.       float _Fade;
    19.    
    20.       void surf (Input IN, inout SurfaceOutput o) {
    21.         fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
    22.         clip (c.a - _Cutoff);
    23.         o.Albedo = c.rgb;
    24.         o.Alpha =  _Fade;
    25.       }
    26.       ENDCG
    27.     }
    28. }
     
  8. gospod velebrz

    gospod velebrz

    Joined:
    Aug 13, 2013
    Posts:
    3
    sorry for thread necromancy, but does anyone have an idea why the OPs shader wouldnt work on ios ?

    all i get is the pink default-error look
     
  9. eshan-mathur

    eshan-mathur

    Joined:
    Nov 22, 2011
    Posts:
    118
    I get the same and would love to know why.