Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ASPePeX
Last active October 6, 2016 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ASPePeX/c1fe99aebf4698934cbad2df9c57ebeb to your computer and use it in GitHub Desktop.
Save ASPePeX/c1fe99aebf4698934cbad2df9c57ebeb to your computer and use it in GitHub Desktop.
Unity shader to use a sprite as a transparent mask. You can for example have a animated sprite-sheet in a augmented reality scene and occlude it by real world objects.
Shader "Sprites/SpriteMask"
{
Properties
{
[PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
_Cutoff("Base Alpha cutoff", Range(0,1)) = .5
}
SubShader
{
Tags
{
"Queue" = "AlphaTest"
}
Cull Off
Lighting Off
ZWrite On
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
fixed4 _Color;
float _Cutoff;
v2f vert(appdata_t IN)
{
v2f OUT;
OUT.vertex = UnityObjectToClipPos(IN.vertex);
OUT.texcoord = IN.texcoord;
OUT.color = IN.color * _Color;
return OUT;
}
struct fragOut
{
float4 color : SV_Target;
float zvalue : SV_Depth;
};
sampler2D _MainTex;
fixed4 SampleSpriteTexture(float2 uv)
{
fixed4 color = half4(1,1,1,1 - tex2D(_MainTex, uv).a);
return color;
}
fixed4 frag(v2f IN) : SV_Target
{
fixed4 c;
c = SampleSpriteTexture(IN.texcoord);
c.rgb *= c.a;
if (c.a > _Cutoff)
discard;
return c;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment