Oct
19
2021

Old School Scrolling Text with Shaders, RenderTexture

Tried to make scrolling text using shaders and text mesh, video below (results at 59 mins -> )

Shader Sources:
(one for text mesh, one for UI RawImage that displays camera view as rendertexture)

// 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
}
}
}
view raw TextFx.shader hosted with ❤ by GitHub
// 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
}
}
}

Leave a comment

Connect

Twitter View LinkedIn profile Youtube Youtube Join Discord Twitch Instagram

UnityLauncherPro

Get UnityLauncherPRO and work faster with Unity Projects!
*free unity hub alternative

@unitycoder_com

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.