Skip to content

Instantly share code, notes, and snippets.

@cukiakimani
Last active July 17, 2017 12:51
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save cukiakimani/baf20cef3efbf9a55f9a9599efd73e5f to your computer and use it in GitHub Desktop.
Transition shader using mask texture.
Shader "Sprites/Transition With Mask" {
Properties {
_MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
_TransitionMask ("Mask (RGB)", 2D) = "white" {}
_SecondaryTex ("Secondary (RGB)", 2D) = "white" {}
_AlphaCutoff ( "Alpha Cutoff", Range( 0.01, 1.0 ) ) = 0.1
_Threshold ("Threshold", Range(0,1)) = 0.5
}
SubShader {
Tags
{
"Queue"="Transparent+1"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Cull Off
Lighting Off
ZWrite Off
Blend One OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f {
float4 vertex : SV_POSITION;
half2 texcoord : TEXCOORD0;
fixed4 color : COLOR;
};
sampler2D _MainTex;
sampler2D _TransitionMask;
sampler2D _SecondaryTex;
float4 _MainTex_ST;
float4 _SecondaryTex_ST;
fixed _AlphaCutoff;
float _Threshold;
fixed4 _Color;
v2f vert (appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
o.color = v.color * _Color;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 main = tex2D(_MainTex, i.texcoord); // background texture
fixed4 mask = tex2D(_TransitionMask, i.texcoord); // masking texture
fixed4 sec = tex2D(_SecondaryTex, i.texcoord); // foreground texture
main.rgb *= main.a;
main *= i.color;
if((mask.r + mask.g + mask.b) * 0.33333 < _Threshold && main.a > _AlphaCutoff)
{
// blend main and sec
fixed4 col = fixed4(0, 0, 0, 0);
col.a = 1 - (1 - sec.a) * (1 - main.a);
if (col.a < 0.00001)
return col;
col.r = sec.r * sec.a / col.a + main.r * main.a * (1 - sec.a) / col.a;
col.g = sec.g * sec.a / col.a + main.g * main.a * (1 - sec.a) / col.a;
col.b = sec.b * sec.a / col.a + main.b * main.a * (1 - sec.a) / col.a;
return col;
}
else
{
return main;
}
}
ENDCG
}
}
}
@cukiakimani
Copy link
Author

006

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment