Last active
October 19, 2021 05:55
-
-
Save unitycoder/92054a4b16e903fe55f43c033164fca2 to your computer and use it in GitHub Desktop.
Scrolling Text Effect Shaders
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
// https://unitycoder.com/blog/2021/10/19/old-school-scrolling-text-with-shaders-rendertexture/ | |
// this is for text mesh | |
Shader "Unlit/TextFx" { | |
Properties { | |
_MainTex ("Texture", 2D) = "white" { } | |
_Col1 ("Color 1", Color) = (1, 0.0, 0.0, 1.0) | |
_Col2 ("Color 2", Color) = (0, 1, 0.0, 1.0) | |
_Col3 ("Color 3", Color) = (0, 0, 1, 1.0) | |
_Scale ("Scale", Float) = 1 | |
} | |
SubShader { | |
Tags { | |
"Queue"="Transparent" | |
"IgnoreProjector"="True" | |
"RenderType"="Transparent" | |
"PreviewType"="Plane" | |
} | |
Lighting Off Cull Off ZTest Always ZWrite Off | |
Blend SrcAlpha OneMinusSrcAlpha | |
Pass { | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
struct appdata { | |
float4 vertex : POSITION; | |
float2 texcoord : TEXCOORD0; | |
}; | |
struct v2f { | |
float4 vertex : SV_POSITION; | |
float2 texcoord : TEXCOORD0; | |
float4 screenPosition : TEXCOORD1; | |
}; | |
sampler2D _MainTex; | |
float4 _MainTex_ST; | |
float4 _Col1; | |
float4 _Col2; | |
float4 _Col3; | |
float _Scale; | |
v2f vert(appdata v) { | |
v2f o; | |
o.vertex = UnityObjectToClipPos(v.vertex); | |
o.screenPosition = ComputeScreenPos(o.vertex); | |
o.vertex.xy*=_Scale; | |
o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex); | |
return o; | |
} | |
float4 frag(v2f i) : SV_Target { | |
float2 scroll = float2(0,sin(frac(_Time.x)*20)*0.5-0.5); | |
float2 uv = ((i.screenPosition.xy+scroll) / i.screenPosition.w); | |
float h = 0.5; | |
float4 c1 = lerp(lerp(_Col1, _Col2, uv.y/h), lerp(_Col2, _Col3, (uv.y - h)/(1.0 - h)), step(h, uv.y)); | |
float4 c2 = lerp(lerp(_Col1, _Col2, uv.x/h), lerp(_Col2, _Col3, (uv.x - h)/(1.0 - h)), step(h, uv.x)); | |
float4 c = lerp(c1,c2,uv.y*(cos(frac(_Time.y))*0.5-0.5)); | |
float2 s2 = float2(frac(_Time.x),0); | |
c.a *= tex2D(_MainTex, i.texcoord).a; | |
return c; | |
} | |
ENDCG | |
} | |
} | |
} |
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
// https://unitycoder.com/blog/2021/10/19/old-school-scrolling-text-with-shaders-rendertexture/ | |
// this is for render texture raw image | |
// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt) | |
Shader "UI/Default" | |
{ | |
Properties | |
{ | |
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {} | |
_Color ("Tint", Color) = (1,1,1,1) | |
_StencilComp ("Stencil Comparison", Float) = 8 | |
_Stencil ("Stencil ID", Float) = 0 | |
_StencilOp ("Stencil Operation", Float) = 0 | |
_StencilWriteMask ("Stencil Write Mask", Float) = 255 | |
_StencilReadMask ("Stencil Read Mask", Float) = 255 | |
_ColorMask ("Color Mask", Float) = 15 | |
_Speed ("ScrollSpeed", Float) = 2 | |
_SpeedY ("ScrollSpeedY", Float) = 3 | |
[Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0 | |
} | |
SubShader | |
{ | |
Tags | |
{ | |
"Queue"="Transparent" | |
"IgnoreProjector"="True" | |
"RenderType"="Transparent" | |
"PreviewType"="Plane" | |
"CanUseSpriteAtlas"="True" | |
} | |
Stencil | |
{ | |
Ref [_Stencil] | |
Comp [_StencilComp] | |
Pass [_StencilOp] | |
ReadMask [_StencilReadMask] | |
WriteMask [_StencilWriteMask] | |
} | |
Cull Off | |
Lighting Off | |
ZWrite Off | |
ZTest [unity_GUIZTestMode] | |
Blend SrcAlpha OneMinusSrcAlpha | |
ColorMask [_ColorMask] | |
Pass | |
{ | |
Name "Default" | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#pragma target 2.0 | |
#include "UnityCG.cginc" | |
#include "UnityUI.cginc" | |
#pragma multi_compile __ UNITY_UI_ALPHACLIP | |
struct appdata_t | |
{ | |
float4 vertex : POSITION; | |
float4 color : COLOR; | |
float2 texcoord : TEXCOORD0; | |
UNITY_VERTEX_INPUT_INSTANCE_ID | |
}; | |
struct v2f | |
{ | |
float4 vertex : SV_POSITION; | |
fixed4 color : COLOR; | |
float2 texcoord : TEXCOORD0; | |
float4 worldPosition : TEXCOORD1; | |
UNITY_VERTEX_OUTPUT_STEREO | |
}; | |
fixed4 _Color; | |
fixed4 _TextureSampleAdd; | |
float4 _ClipRect; | |
float _Speed; | |
float _SpeedY; | |
v2f vert(appdata_t IN) | |
{ | |
v2f OUT; | |
UNITY_SETUP_INSTANCE_ID(IN); | |
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT); | |
OUT.worldPosition = IN.vertex; | |
OUT.vertex = UnityObjectToClipPos(OUT.worldPosition); | |
OUT.texcoord = IN.texcoord; | |
OUT.color = IN.color * _Color; | |
return OUT; | |
} | |
sampler2D _MainTex; | |
fixed4 frag(v2f IN) : SV_Target | |
{ | |
float2 s = float2(frac(_Time.x)*_Speed, cos(_Time.y+IN.texcoord.x*_SpeedY)*0.3); | |
float2 uv = float2(IN.texcoord.x+s.x,IN.texcoord.y+s.y); | |
uv %= 1; | |
half4 color = (tex2D(_MainTex, uv) + _TextureSampleAdd) * IN.color; | |
color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect); | |
#ifdef UNITY_UI_ALPHACLIP | |
clip (color.a - 0.001); | |
#endif | |
return color; | |
} | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment