docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Sample Gradient Node

    Description

    Samples a Gradient given the input of Time. Returns a Vector 4 color value for use in the shader.

    Ports

    Name Direction Type Binding Description
    Gradient Input Gradient None Gradient to sample
    Time Input Float None Point at which to sample gradient (0.0–1.0)
    Out Output Vector 4 None Output value as Vector4

    Generated Code Example

    The following example code represents one possible outcome of this node.

    void Unity_SampleGradient_float(float4 Gradient, float Time, out float4 Out)
    {
        float3 color = Gradient.colors[0].rgb;
        [unroll]
        for (int c = 1; c < 8; c++)
        {
            float colorPos = saturate((Time - Gradient.colors[c-1].w) / (Gradient.colors[c].w - Gradient.colors[c-1].w)) * step(c, Gradient.colorsLength-1);
            color = lerp(color, Gradient.colors[c].rgb, lerp(colorPos, step(0.01, colorPos), Gradient.type));
        }
    #ifndef UNITY_COLORSPACE_GAMMA
        color = SRGBToLinear(color);
    #endif
        float alpha = Gradient.alphas[0].x;
        [unroll]
        for (int a = 1; a < 8; a++)
        {
            float alphaPos = saturate((Time - Gradient.alphas[a-1].y) / (Gradient.alphas[a].y - Gradient.alphas[a-1].y)) * step(a, Gradient.alphasLength-1);
            alpha = lerp(alpha, Gradient.alphas[a].x, lerp(alphaPos, step(0.01, alphaPos), Gradient.type));
        }
        Out = float4(color, alpha);
    }
    
    In This Article
    Back to top
    Copyright © 2025 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)