Skip to content
On this page

Lerp

lerp(a, b, t) returns the linear interpolation between values a and b, using t (0-1) as the blend factor. It’s foundational in shaders for blending, animation, transitions, gradients, and cross-fading effects.

float3 result = lerp(colorA, colorB, blendFactor); // Linear interpolation

Visual demo

This shader demonstrates a color blend using lerp() across the horizontal UV (x) axis.

Shader Story: Function - Lerp

Node logic (engine-agnostic)

Shader Story: Function - Lerp


Try it live

The same blend, live: drag the blend factor and watch the blend shift across the axis.


Shader code
Shader "DecompiledArt/CommonFunctions/Lerp/Lerp"
{
    Properties
    {
        _Tint_01("Tint_01", Color) = (1.0, 0.0, 0.0, 1.0)
        _Tint_02("Tint_02", Color) = (0.0, 0.0, 1.0, 1.0)
    }

    SubShader
    {
        Tags { "RenderType"="Opaque" "RenderPipeline" = "UniversalPipeline" }

        Pass
        {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

            struct Attributes
            {
                float4 positionOS : POSITION;
                half2 uvs: TEXCOORD0;
            };

            struct Varyings
            {
                float4 positionHCS : SV_POSITION;
                half2 uvs: TEXCOORD0;
            };

            CBUFFER_START(UnityPerMaterial)
            half4 _Tint_01;
            half4 _Tint_02;
            CBUFFER_END

            Varyings vert (Attributes IN)
            {
                Varyings OUT;

                OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
                OUT.uvs = IN.uvs;
                return OUT;
            }

            half4 frag(Varyings IN) : SV_Target
            {
                return half4(lerp(_Tint_01.xyz, _Tint_02.xyz, IN.uvs.x), 1.0);
            }

            ENDHLSL
        }
    }
}

Step • Remap • Smoothstep • Exp • Abs


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