mathe/Library/PackageCache/com.unity.shadergraph@14.0.8/Documentation~/Sample-Gradient-Node.md
2024-09-20 20:30:10 +02:00

1.5 KiB
Raw Blame History

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.01.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);
}