Oct
19
2021
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)
This file contains hidden or 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 hidden or 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 | |
| } | |
| } | |
| } |
Related Posts
Leave a comment
Recent posts
- Favorites in PackageManager
- LudumDare59 : Signal
- Unity Editor: Tree Generator
- Leaf/Foliage Generator Tools (Runs in Browser)
- Testing Unity AI Beta
- Ways to Support UnityCoder Development
- Using UI Slider to Create 5-Star Rating Element
- Game Music Library For Unity (editor plugin)
- Fontastic : Easily Test Fonts in Unity Editor!
- GeoTiff Importer & Terrain Generator for Unity
- Create Baked DropShadow for UI images
- .JP2 Ortho Image Converter to PNG/JPG/TIFF
UnityLauncherPro
*free unity hub alternative
Recent Comments
- mgear on Mesh Exploder (sources)
- mgear on Sprite Sheet Flip Book Shader
- Alex Chen on Sprite Sheet Flip Book Shader
- mgear on [Asset Store] PolygonCollider2D Optimizer
- mgear on Trajectory Test Scene 2.0
- mgear on Vector3 maths for dummies!
- mgear on UnityHub 3.6.0: Remove Version Control & Cloud Dashboard columns
- mgear on Using RenderDoc with Unity (graphics debugger)
Support my work at Bags.fm:
https://bags.fm/CUgDSbRqFcAumDSAcdKDvuXsw26VdkJe8C8WGUQHBAGS
Coin:CUgDSbRqFcAumDSAcdKDvuXsw26VdkJe8C8WGUQHBAGS
Subscribe to Blog via Email
Tag Cloud
2d
3D
AI
algorithm
android
asset
build
color
custom
demo
editor
effect
fake
free
game
generator
greasemonkey
indie
javascript
light
line
ludumdare
mesh
paint
particles
physics
plugin
procedural
proto
prototype
script
sea
shader
shadow
sprite
terrain
texture
tutorial
ui
unity
vertex
visibility
water
waves
webgl
An article by











