On this page
UVs & Time
Textures are still and time is not, so the most basic animated texture in the world is a texture whose UVs move: add a little time to the coordinate, and the pattern flows. Scrolling is the first texture animation you should know, because it is one line, and because a large share of everything that “moves” in games, water, energy, holo projections, is exactly this.
float2 uv = IN.uv + _Time.y * _Flow;
half4 tex = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv);
Adding time to a coordinate¶
A UV is a number, and time is a number, so the sum of the two is a moving UV. Every frame the coordinate advances by the flow amount, and with a Repeat wrap mode the texture advances with it, seamlessly, because the pattern wraps on itself. That is the whole effect, and the reason it is so common: one line of arithmetic gives a texture a direction and a speed.
The flow vector is the interesting part. A flow of (1, 0) scrolls the texture to the right across the surface. (1, 1) scrolls it diagonally, which reads as motion along the surface’s own diagonals and is the classic water look. A negative component reverses the direction. And the magnitude sets the speed in texture repeats per second, so a flow of (0.5, 0) takes one whole second per repeat, an easy number to reason about.
// A slow diagonal drift, half a repeat per second:
float2 uv = IN.uv + _Time.y * half2(0.5, 0.5);
The habit that keeps it healthy¶
_Time.y grows without limit, and feeding an unbounded number into a texture coordinate works for a while and then starts to waver, because floating point loses resolution as numbers grow. The fix is the same one the vertex animation library uses in Time Wrapping: wrap the time to a local phase before you multiply. A scrolling texture has a natural period, one repeat, so the wrap is trivial:
float2 uv = IN.uv + frac(_Time.y * _Speed) * _Flow;
For scrolling specifically the wrap is not even visually necessary in many cases, because the Repeat mode hides the period, but it keeps the numbers small, which is a habit worth having before you meet a precision problem you cannot explain. The full treatment, why the waver happens and when it shows up, is in Time Overflow.
Two compositions to try once you have the line. Add a second texture with the opposite flow and blend them, and you get the interference shimmer of two currents, a water and energy staple. Or drive the flow with a noise sample, and the uniform drift becomes a drifting that wobbles, which is the difference between a conveyor belt and a liquid.
What the shader does¶
The shader scrolls a texture by a flow vector (direction times speed), with a mode toggle for a counter drifting texture blended in. The diagonal drift reads as current, and the counter drift reads as two liquids mixing, from the same line of UV arithmetic.
Illustrative schematic: One flow vector drives the drift.
One texture, a flow vector, a wrapped time, and a counter current toggle.
Shader "DecompiledArt/Textures/Time/ScrollingUVs/ScrollingUVs"
{
Properties
{
_MainTex("Texture (Repeat)", 2D) = "white" {}
_CounterTex("Counter Texture (Repeat)", 2D) = "white" {}
[MainColor] _BaseColor("Base Color", Color) = (0.3, 0.6, 1.0, 1)
_Flow("Flow (direction * speed)", Vector) = (0.2, 0.2, 0, 0)
[Enum(Single,0,CounterDrift,1)] _Mode("Mode", Float) = 0
}
SubShader
{
Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" "Queue" = "Geometry" }
Pass
{
Name "ForwardOnly"
Tags { "LightMode" = "UniversalForward" }
HLSLPROGRAM
#pragma vertex Vert
#pragma fragment Frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
TEXTURE2D(_MainTex); SAMPLER(sampler_MainTex);
TEXTURE2D(_CounterTex); SAMPLER(sampler_CounterTex);
struct Attributes
{
float4 positionOS : POSITION;
float2 uv : TEXCOORD0;
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float2 uv : TEXCOORD0;
};
CBUFFER_START(UnityPerMaterial)
half4 _BaseColor;
half2 _Flow;
half _Mode;
CBUFFER_END
Varyings Vert(Attributes IN)
{
Varyings OUT;
OUT.positionCS = TransformObjectToHClip(IN.positionOS.xyz);
OUT.uv = IN.uv;
return OUT;
}
half4 Frag(Varyings IN) : SV_Target
{
// Wrapped time keeps the coordinate small, see Time Overflow.
float2 flow = frac(_Time.y * 0.5) * 2.0 * _Flow;
half3 a = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.uv + flow).rgb;
half3 color = a;
if (_Mode > 0.5h)
{
half3 b = SAMPLE_TEXTURE2D(_CounterTex, sampler_CounterTex, IN.uv - flow).rgb;
color = max(a, b); // additive interference
}
return half4(_BaseColor.rgb * color, _BaseColor.a);
}
ENDHLSL
}
}
FallBack Off
}
🔗 Related Chapters¶
Time Overflow • Blending over Time • Tiling & Offset
Related functions: Frac • Lerp
❤️ Support Shader Story¶
If this article helped you, consider supporting the project on Patreon: you’ll get access to the related source files and reference cheat-sheets.
Your support helps keep this library open and free for everyone.