On this page
Blending over Time
A single texture shows one state. Two textures show a change, and time decides how much of the change you see. Blending over time crossfades between two samples with a weight that moves, which is the engine behind day and night, growth and decay, and any transition where the surface becomes something else.
float w = smoothstep(0.0, 1.0, frac(_Time.y / _Duration));
half3 a = SAMPLE_TEXTURE2D(_TexA, s_a, uv).rgb;
half3 b = SAMPLE_TEXTURE2D(_TexB, s_b, uv).rgb;
half3 color = lerp(a, b, w);
A weight that moves¶
The structure is a lerp with a time driven input. Sample texture A, sample texture B, and mix them with a weight that travels from 0 to 1 over the duration you choose. At weight 0 the surface is entirely A. At weight 1 it is entirely B. In between it is a little of both, and the smoothstep on the phase gives the transition ease at both ends instead of a hard linear ramp, which is what makes the blend read as a change, not a dissolve of values.
// One full A to B cycle every _Duration seconds:
float phase = frac(_Time.y / _Duration);
float w = smoothstep(0.0, 1.0, phase);
The frac is the loop, and it is the same bounded time habit from UVs & Time and Time Wrapping. A phase between 0 and 1 is a local clock the artist can reason about, and the smoothstep shapes it into a curve. Those two lines, wrap and shape, are the standard answer to “drive a value with time” in this library.
Ping pong, and why it matters¶
A frac runs 0 to 1 and jumps back to 0, so a naive blend snaps from B straight back to A at the end of each cycle, a visible pop if B and A are very different. The standard fix is a ping pong, a triangle wave that goes 0 to 1 and back, so the blend runs to B, holds for a moment in the smoothstep’s flat shoulder, and returns to A:
float tri = abs(1.0 - 2.0 * frac(_Time.y / _Duration)); // 0 -> 1 -> 0
float w = smoothstep(0.0, 1.0, tri);
Now the cycle has no discontinuity at all, and the surface breathes between the two states forever. Which shape you want is an art decision, a one way transition with a reset, or a breathing loop, but the machinery is the same: a bounded phase, shaped by a curve, feeding a lerp.
The easing functions chapter from the vertex animation library is the full catalog of shapes you can put on that phase, and the texture blend is one of the most forgiving places to use them, because two similar textures forgive a lot of curve choice.
What the shader does¶
The shader blends two textures on a surface, with a duration property and a mode toggle between one-way and ping-pong. One-way shows the pop when the cycle restarts, which is the reason the ping-pong exists; the shaped blend eases between states at both ends.
Illustrative schematic: One-way pops at the restart; ping-pong eases at both ends.
Two textures, a duration, and the mode toggle between one way and ping pong.
Shader "DecompiledArt/Textures/Time/TimeBlending/TimeBlending"
{
Properties
{
_TexA("Texture A", 2D) = "white" {}
_TexB("Texture B", 2D) = "white" {}
[MainColor] _BaseColor("Base Color", Color) = (1, 1, 1, 1)
_Duration("Cycle Duration (seconds)", Range(1, 20)) = 6
[Enum(OneWay,0,PingPong,1)] _Mode("Mode", Float) = 1
}
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(_TexA); SAMPLER(sampler_TexA);
TEXTURE2D(_TexB); SAMPLER(sampler_TexB);
struct Attributes
{
float4 positionOS : POSITION;
float2 uv : TEXCOORD0;
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float2 uv : TEXCOORD0;
};
CBUFFER_START(UnityPerMaterial)
half4 _BaseColor;
float _Duration;
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
{
float phase = frac(_Time.y / _Duration);
float tri = abs(1.0 - 2.0 * phase);
float w = smoothstep(0.0, 1.0, (_Mode < 0.5h) ? phase : tri);
half3 a = SAMPLE_TEXTURE2D(_TexA, sampler_TexA, IN.uv).rgb;
half3 b = SAMPLE_TEXTURE2D(_TexB, sampler_TexB, IN.uv).rgb;
half3 color = lerp(a, b, w);
return half4(_BaseColor.rgb * color, _BaseColor.a);
}
ENDHLSL
}
}
FallBack Off
}
🔗 Related Chapters¶
UVs & Time • Dissolve • Easing Functions
Related functions: Lerp • Smoothstep
❤️ 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.