Sprites/AlphaCutoff shader for healthbars etc

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

Shader "Sprites/AlphaCutoff"
{
	Properties
	{
		[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
		_Color ("Tint", Color) = (1,1,1,1)
		[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
        _CutOff ("Cutoff", Range (0, 1)) = 0.5
	}

	SubShader
	{
		Tags
		{ 
			"Queue"="Transparent" 
			"IgnoreProjector"="True" 
			"RenderType"="Transparent" 
			"PreviewType"="Plane"
			"CanUseSpriteAtlas"="True"
		}

		Cull Off
		Lighting Off
		ZWrite Off
		Fog { Mode Off }
		Blend SrcAlpha OneMinusSrcAlpha

		Pass
		{
		CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#pragma multi_compile DUMMY PIXELSNAP_ON
			#include "UnityCG.cginc"
			
			struct appdata_t
			{
				float4 vertex   : POSITION;
				float4 color    : COLOR;
				float2 texcoord : TEXCOORD0;
			};

			struct v2f
			{
				float4 vertex   : SV_POSITION;
				fixed4 color    : COLOR;
				half2 texcoord  : TEXCOORD0;
			};
			
			fixed4 _Color;
			fixed _CutOff;

			v2f vert(appdata_t IN)
			{
				v2f OUT;
				OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
				OUT.texcoord = IN.texcoord;
				OUT.color = IN.color * _Color;
				#ifdef PIXELSNAP_ON
				OUT.vertex = UnityPixelSnap (OUT.vertex);
				#endif
				return OUT;
			}

			sampler2D _MainTex;

			fixed4 frag(v2f IN) : COLOR
			{
				return tex2D(_MainTex, IN.texcoord).a > _CutOff ? IN.color : 0;
			}
		ENDCG
		}
	}
}
1 Like

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!

Hey MABManZ, I wrote the shader for the new Unity 2D Sprites; it’s not intended to be used on other objects due to how Unity’s new sprite system works. There should be various other shaders on this forum allowing you to do that:

http://forum.unity3d.com/threads/120225-Transparent-Circular-Timer-Shader

Hope it helps, give me a shout if not :slight_smile:

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 :slight_smile:

Thanks!

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…

						fixed4 frag(v2f IN) : COLOR
						{
							fixed4 tex = tex2D(_MainTex, IN.texcoord);
					
							if (tex.a > _CutOff) {
								tex.a = 1;
								return tex * IN.color;						
							} else {
								return 0;
							}
						}
1 Like

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.

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.

Shader "Custom/NewShader" {
  	Properties {
    	_MainTex ("Color", 2D) = "white" {}
    	_Cutoff ("Threshold", Range (0,1)) = 0.5
    	_Fade ("Opacity", Range (0, 1)) = 1
    }
    SubShader {
	  Tags { "Queue"="Transparent" "RenderType"="Transparent" }
      Lighting Off
      CGPROGRAM
      #pragma surface surf Lambert alpha
      struct Input {
          float2 uv_MainTex;
      };
      sampler2D _MainTex;
      float _Cutoff;
      float _Fade;
   
      void surf (Input IN, inout SurfaceOutput o) {
		fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
        clip (c.a - _Cutoff);
        o.Albedo = c.rgb;
        o.Alpha =  _Fade;
      }
      ENDCG
    } 
}

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

I get the same and would love to know why.