- Home /
Cutaway shader for sprites
Hi guys,
I'm attempting to modify the sprite shader so it can discard fragments. The reason I'm doing this is to make a meter, like a health bar. I would use something like scale but the design of the bar would get squished and ugly. The meter is attached to the camera and is essentially a neat workaround for the current horrendous GUI system.
The problem is that the cutaway seems to only work in world space and it needs to work in local space i.e. the cutaway only happens between -1 and 1 in world coordinates.
Shader "Sprites/Default-NoZ-Cutaway"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
_CutAway ("Cut Away", Range(-1,1)) = 1
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
}
SubShader
{
Tags
{
"Queue"="Transparent"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Cull Off
Lighting Off
ZWrite Off
ZTest Always
Fog { Mode Off }
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile DUMMY PIXELSNAP_ON
#include "UnityCG.cginc"
uniform float _CutAway;
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : POSITION;
fixed4 color : COLOR;
half2 texcoord : TEXCOORD0;
float4 posInObjectCoords : TEXCOORD1;
};
fixed4 _Color;
v2f vert(appdata_t IN)
{
v2f OUT;
OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
OUT.texcoord = IN.texcoord;
OUT.color = IN.color * _Color;
OUT.posInObjectCoords = IN.vertex;
#ifdef PIXELSNAP_ON
OUT.vertex = UnityPixelSnap (OUT.vertex);
#endif
return OUT;
}
sampler2D _MainTex;
fixed4 frag(v2f IN) : COLOR
{
if(IN.posInObjectCoords.y > _CutAway)
discard;
return tex2D(_MainTex, IN.texcoord) * IN.color;
}
ENDCG
}
}
}
The discard parts work fine if inserted into a normal 3D shader. I can't find any significant differences, at least none that I would understand =/
Little help?
Alternatively, if anyone knows a better way of masking parts of a sprite for this purpose I'm all ears.