Created
June 4, 2021 12:06
-
-
Save cs-altshift/465d528b4629abf7135dabbf75513e6b to your computer and use it in GitHub Desktop.
Crying Suns' Animated Alpha Shader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Animated Alpha" | |
{ | |
Properties | |
{ | |
[PerRendererData] _MainTex ("Texture", 2D) = "white" {} | |
_TargetColor ("Target Color", Color) = (1, 1, 1, 0) | |
_AlphaAttenuation ("Alpha Attenuation", Vector) = (0, 0.25, 0.75, 1) | |
} | |
SubShader | |
{ | |
Tags { | |
"Queue"="Transparent" | |
"RenderType"="Transparent" | |
} | |
ZTest Always | |
ZWrite Off | |
Pass | |
{ | |
Blend SrcAlpha OneMinusSrcAlpha | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
struct appdata | |
{ | |
float4 vertex : POSITION; | |
float2 uv : TEXCOORD0; | |
float4 color : COLOR0; | |
}; | |
struct v2f | |
{ | |
float4 vertex : SV_POSITION; | |
float2 uv : TEXCOORD0; | |
float4 color : COLOR0; | |
}; | |
sampler2D _MainTex; | |
float4 _MainTex_ST; | |
v2f vert (appdata v) | |
{ | |
v2f OUT; | |
OUT.vertex = UnityObjectToClipPos(v.vertex); | |
OUT.uv = TRANSFORM_TEX(v.uv, _MainTex); | |
OUT.color = v.color; | |
return OUT; | |
} | |
fixed4 _TargetColor; | |
float4 _AlphaAttenuation; | |
fixed animatedAlpha(fixed _a) { | |
if (_a < _AlphaAttenuation[0]) { | |
return 0; | |
} | |
else if (_a < _AlphaAttenuation[1]) { | |
return (_a - _AlphaAttenuation[0]) / (_AlphaAttenuation[1] - _AlphaAttenuation[0]); | |
} | |
else if (_a < _AlphaAttenuation[2]) { | |
return 1; | |
} | |
else if (_a < _AlphaAttenuation[3]) { | |
return 1.0 - (_a - _AlphaAttenuation[2]) / (_AlphaAttenuation[3] - _AlphaAttenuation[2]); | |
} | |
else { | |
return 0; | |
} | |
} | |
fixed4 frag (v2f IN) : SV_Target | |
{ | |
fixed4 col = tex2D(_MainTex, IN.uv); | |
col.rgb = _TargetColor; | |
col.a = animatedAlpha(col.a); | |
return col; | |
} | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment