Easy Coding adventures
Gradient editor: four color stops
Like the gradient editor in a game engine: a ramp through four colors placed at 0.0, 0.4, 0.7 and 1.0 across the picture.
About the Gradient editor: four color stops shader
Like the gradient editor in a game engine: a ramp through four colors placed at 0.0, 0.4, 0.7 and 1.0 across the picture. Experiments in the spirit of well-known open-source graphics projects: a software rasterizer, a ray tracer, procedural planets, fluid particles and more, rebuilt as pixel shaders.
How this challenge works
You write a fragment shader that draws the picture above, live in your browser. A match bar shows how close you are, and the score combines accuracy, code size (the par is 301 characters in Unity (Built-in)) and GPU speed. Work in HLSL, GLSL, WGSL, Metal or Godot and export the result when you are done.
The numbers you need
- Position
t = uv.x- Stops
float3(0.05, 0.05, 0.2) at 0.0, float3(0.6, 0.1, 0.5) at 0.4, float3(1.0, 0.55, 0.15) at 0.7, float3(1.0, 0.95, 0.7) at 1.0- Blend
start with the first color, then for each next stop: col = lerp(col, stop, saturate((t - start) / (end - start))) where start and end are the positions of the previous and next stop
How to make a gradient editor: four color stops shader
- One lerp per segment: between stops 0 and 1 the weight goes from 0 to 1 while t goes from 0.0 to 0.4.
- saturate() keeps the weight at 0 before the segment and at 1 after it, so the lerps can simply be chained.
Techniques used
- lerp (mix): blend between two colors or values.
Gradient editor: four color stops shader code, explained step by step
Try to build it yourself first, because that is how it sticks. When you want to check your work or you are stuck, this is a complete working solution cut into parts, with the idea behind each part. The parts are in HLSL (Unity and Unreal); the full code below comes in every language.
Part 1: Set up the coordinates
Inside float4 frag(float2 uv)
float t = uv.x;
float3 col = float3(0.05, 0.05, 0.2);
col = lerp(col, float3(0.6, 0.1, 0.5), saturate(t / 0.4)); - uv runs from 0 to 1 across the picture, so uv.x and uv.y say how far along the screen this pixel is.
- lerp(a, b, t) blends from a to b. With a mask as t it paints one colour over another.
- saturate() clamps a value into the 0 to 1 range.
- float3(r, g, b) is a colour: each channel from 0 (none) to 1 (full).
Part 2: Colour the pixel and return it
Still inside frag()
col = lerp(col, float3(1.0, 0.55, 0.15), saturate((t - 0.4) / 0.3));
col = lerp(col, float3(1.0, 0.95, 0.7), saturate((t - 0.7) / 0.3));
return float4(col, 1.0); - lerp(a, b, t) blends from a to b. With a mask as t it paints one colour over another.
- saturate() clamps a value into the 0 to 1 range.
- float3(r, g, b) is a colour: each channel from 0 (none) to 1 (full).
- The shader returns the final colour: red, green, blue, and an alpha of 1 (fully opaque).
The full code
HLSL Unity, Unreal
float4 frag(float2 uv)
{
float t = uv.x;
float3 col = float3(0.05, 0.05, 0.2);
col = lerp(col, float3(0.6, 0.1, 0.5), saturate(t / 0.4));
col = lerp(col, float3(1.0, 0.55, 0.15), saturate((t - 0.4) / 0.3));
col = lerp(col, float3(1.0, 0.95, 0.7), saturate((t - 0.7) / 0.3));
return float4(col, 1.0);
} GLSL Shadertoy, three.js, OpenGL
vec4 frag(vec2 uv)
{
float t = uv.x;
vec3 col = vec3(0.05, 0.05, 0.2);
col = mix(col, vec3(0.6, 0.1, 0.5), clamp(t / 0.4, 0.0, 1.0));
col = mix(col, vec3(1.0, 0.55, 0.15), clamp((t - 0.4) / 0.3, 0.0, 1.0));
col = mix(col, vec3(1.0, 0.95, 0.7), clamp((t - 0.7) / 0.3, 0.0, 1.0));
return vec4(col, 1.0);
} WGSL WebGPU, Bevy
fn frag(uv: vec2f) -> vec4f
{
let t = uv.x;
var col = vec3f(0.05, 0.05, 0.2);
col = mix(col, vec3f(0.6, 0.1, 0.5), saturate(t / 0.4));
col = mix(col, vec3f(1.0, 0.55, 0.15), saturate((t - 0.4) / 0.3));
col = mix(col, vec3f(1.0, 0.95, 0.7), saturate((t - 0.7) / 0.3));
return vec4f(col, 1.0);
} Godot Godot 4 shading language
shader_type canvas_item;
vec4 frag(vec2 uv)
{
float t = uv.x;
vec3 col = vec3(0.05, 0.05, 0.2);
col = mix(col, vec3(0.6, 0.1, 0.5), clamp(t / 0.4, 0.0, 1.0));
col = mix(col, vec3(1.0, 0.55, 0.15), clamp((t - 0.4) / 0.3, 0.0, 1.0));
col = mix(col, vec3(1.0, 0.95, 0.7), clamp((t - 0.7) / 0.3, 0.0, 1.0));
return vec4(col, 1.0);
}
void fragment()
{
// Godot's UV starts at the top-left, the lessons use bottom-left
COLOR = frag(vec2(UV.x, 1.0 - UV.y));
} Metal iOS, macOS
float4 frag(float2 uv)
{
float t = uv.x;
float3 col = float3(0.05, 0.05, 0.2);
col = mix(col, float3(0.6, 0.1, 0.5), saturate(t / 0.4));
col = mix(col, float3(1.0, 0.55, 0.15), saturate((t - 0.4) / 0.3));
col = mix(col, float3(1.0, 0.95, 0.7), saturate((t - 0.7) / 0.3));
return float4(col, 1.0);
} Solve it in your language
Unity (Built-in), Unity (URP), Unreal, Shadertoy / WebGL, OpenGL (LWJGL, raylib), three.js, GameMaker, LÖVE (Lua), Godot, Apple (iOS, macOS), WebGPU / Bevy.
Start codingQuestions about this shader
How do I make a gradient editor: four color stops shader?
Like the gradient editor in a game engine: a ramp through four colors placed at 0.0, 0.4, 0.7 and 1.0 across the picture. A good first step: one lerp per segment: between stops 0 and 1 the weight goes from 0 to 1 while t goes from 0.0 to 0.4. In ShaderBattle you build it in the browser with a live match bar, so you see how close you are after every change.
What techniques does the gradient editor: four color stops shader use?
It is built from lerp (mix). Each one is a small idea you can reuse in other effects.
Does this gradient editor: four color stops shader work in Unity, Unreal, Godot and Shadertoy?
Yes. The idea is the same in every shader language. ShaderBattle shows the code in HLSL (Unity, Unreal), GLSL (Shadertoy, three.js, OpenGL), WGSL (WebGPU), Metal and Godot, and exports a ready-to-paste version.