Skip to content
On this page

Time Overflow

Every frame _Time.y grows a little, and it never shrinks. For an effect that runs for seconds that is fine. For motion that runs for hours, the number eventually grows past what a float can still count in, and the motion that was smooth starts to waver. Time overflow is that failure, and its prevention is a single habit: wrap time into a bounded phase before it touches any motion.

half phase = frac(_Time.y * _Speed);   // 0..1, forever

A precision that erodes

A float carries about seven significant digits, and the gap between two neighboring representable values doubles every time the number doubles. Near 1 second, those gaps are far smaller than a frame. An hour of continuous running puts _Time.y near 3600, and the gap between floats there is a quarter of a millisecond, still smaller than a 16.7 ms frame, so nothing is wrong yet. But the gap keeps growing while the frame stays the same size. Somewhere around a full day of running, one frame of time shrinks below one float step, and from then on each frame either leaves the value exactly where it was or moves it by a whole step. The wave stutters first, then freezes.

In a texture the same math reads even earlier, because the error lands in texels. A scrolling UV built on raw time drifts by fractions of a pixel, and the pattern shimmers where it should flow, which is the tell you will actually see in a long session. The cruel part is that the failure is slow. You test for two minutes, everything is smooth, you ship it, and an always-on scene brings the waver back later. On mobile, where half precision is in play with ten mantissa bits instead of twenty-three, the same motion can start wavering within minutes.

The trap is the diagnosis. When a wave stutters in a long session, the instinct is to blame the driver, the GPU, the frame rate. The motion is usually just running out of room inside its own number.

Shader Story: Textures - Time Overflow

Wrap, don’t patch

The fix is to make the argument small, not to repair the big number. Motion that repeats has a period, and anything that repeats can be described by a phase between 0 and 1:

// Periodic motion: a bounded phase, one cycle per 1/_Speed seconds.
half phase = frac(_Time.y * _Speed);

// Duration-based motion: local time inside one cycle.
float local = fmod(_Time.y, _Duration) / _Duration;

The shape of the motion does not change. The frac removes whole cycles, and the wave does not care about whole cycles, because a sine with one extra full turn is the same sine. Only the size of the input changes, and that is exactly what the precision needs. The Time Wrapping chapter states the same rule for the vertex animation library, and it holds here: continuous time for systems, wrapped time for motion. A one shot event, a five second cutscene, a quest counter, can stay on raw time, because it never runs long enough to overflow. Anything that loops, a scroll, a pulse, a breathing blend, wraps first.

One place the habit breaks is the order of operations. frac(_Time.y) * _Speed looks almost the same as frac(_Time.y * _Speed), but it wraps before scaling, so the value pops back to zero at every whole second of time whenever the speed is not a whole number. Wrap the quantity that has the period.


What the shader does

The shader draws two waves from the same motion, one from raw time and one from a wrapped phase, with a session offset that simulates hours of running. As the offset climbs, the raw wave starts to stutter and freeze while the wrapped wave keeps moving smoothly.

Shader Story: Textures: Time Overflow - schematic

Illustrative schematic: Raw time stutters and freezes; wrapped phase keeps moving.


Two scanline waves, one session slider, and the precision difference made visible.

Shader code
Shader "DecompiledArt/Textures/Time/TimeOverflow/TimeOverflow"
{
    Properties
    {
        [MainColor] _BaseColor("Base Color", Color) = (1, 1, 1, 1)
        _Speed("Cycles Per Second", Range(0.1, 4)) = 1
        _Wavelengths("Wavelengths Across", Range(1, 10)) = 5
        _Amp("Wave Height", Range(0.02, 0.3)) = 0.18
        _SessionOffset("Simulated Session (seconds)", Range(0, 200000)) = 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"

            struct Attributes
            {
                float4 positionOS : POSITION;
                float2 uv         : TEXCOORD0;
            };

            struct Varyings
            {
                float4 positionCS : SV_POSITION;
                float2 uv         : TEXCOORD0;
            };

            CBUFFER_START(UnityPerMaterial)
                half4 _BaseColor;
                half  _Speed;
                half  _Wavelengths;
                half  _Amp;
                float _SessionOffset;
            CBUFFER_END

            Varyings Vert(Attributes IN)
            {
                Varyings OUT;
                OUT.positionCS = TransformObjectToHClip(IN.positionOS.xyz);
                OUT.uv = IN.uv;
                return OUT;
            }

            // The line is the wave: bright where the pixel sits on the curve.
            half LineWave(float2 uv, float centerY, half amp, half wave)
            {
                float y = centerY - amp * wave;
                return 1.0 - smoothstep(0.0, 0.015, abs(uv.y - y));
            }

            half4 Frag(Varyings IN) : SV_Target
            {
                // The session offset is a time machine: it ages the clock
                // without waiting a day, so the failure is visible now.
                float t = _Time.y + _SessionOffset;

                // Raw time: the argument to sin keeps growing without bound,
                // so its float steps get coarser as the number grows.
                half waveRaw = sin((IN.uv.x * _Wavelengths - t * _Speed) * TWO_PI);

                // Wrapped: a 0..1 phase. The wave looks identical, because
                // whole extra turns of the sine are the same sine, but the
                // argument stays small, so it stays exact.
                half phase    = frac(t * _Speed);
                half waveWrap = sin((IN.uv.x * _Wavelengths - phase) * TWO_PI);

                half raw  = LineWave(IN.uv, 0.72, _Amp, waveRaw);
                half wrap = LineWave(IN.uv, 0.28, _Amp, waveWrap);

                half3 color = half3(0.95, 0.30, 0.45) * raw
                            + half3(0.20, 0.83, 0.59) * wrap;
                return half4(color * _BaseColor.rgb, _BaseColor.a);
            }

            ENDHLSL
        }
    }

    FallBack Off
}

UVs & Time • Blending over Time • Time Wrapping

Related functions: Frac • Fmod


❤️ 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.

DecompiledArt on Patreon DecompiledArt on Buy Me a Coffee

Your support helps keep this library open and free for everyone.