Medium 2DAnimated
Beating heart
A heart that pulses, using an implicit equation.
About the Beating heart shader
A heart that pulses, using an implicit equation. Two-dimensional shapes and compositions: circles, boxes, lines and how to combine them.
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 343 characters in Unity (Built-in)) and GPU speed. Work in HLSL, GLSL, WGSL, Metal or Godot and export the result when you are done.
What it is built from
- Space
q = (uv - 0.5) * float2(aspect, 1.0) * ?- Pulse
q = q / (1.0 + ? * sin(time * ?))- Shift
q.y += ?- Equation
t = q.y - sqrt(abs(q.x)); h = q.x * q.x + t * t - 1.0; inside when h < 0- Edge
w = fwidth(h) + ?; 1.0 - smoothstep(-w, w, h)- Colors
float3(0.1,0.05,0.12) / float3(0.95,0.2,0.35)
The exact numbers are left out (shown as ?), but every colour is given. Measure the rest with the pixel readout on both pictures and Diff.
How to make a beating heart shader
- It is a circle, x² + t² = 1, whose height t is bent by sqrt(abs(x)): that bend makes the two lobes and the dent between them.
- Divide q by a number slightly above 1 to make the heart bigger (pulse).
- A fixed smoothstep(-0.01, 0.01, h) smears wherever the curve is flat. fwidth(h) is how much h changes across one pixel: use it as the smoothing width and the edge stays about one pixel sharp everywhere.
Techniques used
- smoothstep: turns a number into a soft 0-to-1 transition, used for soft edges and masks.
- Anti-aliasing with fwidth: edges that are exactly one pixel soft at any size.
- Sine waves: smooth oscillation for waves, pulses and motion over time.
- lerp (mix): blend between two colors or values.
Beating heart 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)
float2 q = (uv - 0.5) * float2(aspect, 1.0) * 3.4;
q = q / (1.0 + 0.1 * sin(time * 6.0));
q.y += 0.28; - Centre the coordinates on the middle of the picture and multiply x by the aspect ratio, so a circle stays round on a wide picture.
- Subtracting 0.5 moves the origin from the bottom-left corner to the middle of the picture.
- Putting time inside sin or cos makes the value swing back and forth, which is what animates the effect.
- time is the number of seconds since the shader started, so anything that uses it moves.
Part 2: Build the pattern
Still inside frag()
float t = q.y - sqrt(abs(q.x));
float h = q.x * q.x + t * t - 1.0;
float w = fwidth(h) + 0.0001; - fwidth(x) is how much x changes between neighbouring pixels, so a smoothstep over that width gives an edge exactly one pixel soft.
- abs() mirrors a value around zero, which gives symmetric shapes, outlines and sharp ridges.
Part 3: Colour the pixel and return it
Still inside frag()
float m = 1.0 - smoothstep(-w, w, h);
return float4(lerp(float3(0.1, 0.05, 0.12), float3(0.95, 0.2, 0.35), m), 1.0); - smoothstep(a, b, x) turns x into a smooth 0-to-1 value between a and b. It is the soft edge or mask.
- lerp(a, b, t) blends from a to b. With a mask as t it paints one colour over another.
- 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)
{
float2 q = (uv - 0.5) * float2(aspect, 1.0) * 3.4;
q = q / (1.0 + 0.1 * sin(time * 6.0));
q.y += 0.28;
float t = q.y - sqrt(abs(q.x));
float h = q.x * q.x + t * t - 1.0;
float w = fwidth(h) + 0.0001;
float m = 1.0 - smoothstep(-w, w, h);
return float4(lerp(float3(0.1, 0.05, 0.12), float3(0.95, 0.2, 0.35), m), 1.0);
} GLSL Shadertoy, three.js, OpenGL
vec4 frag(vec2 uv)
{
vec2 q = (uv - 0.5) * vec2(aspect, 1.0) * 3.4;
q = q / (1.0 + 0.1 * sin(time * 6.0));
q.y += 0.28;
float t = q.y - sqrt(abs(q.x));
float h = q.x * q.x + t * t - 1.0;
float w = fwidth(h) + 0.0001;
float m = 1.0 - smoothstep(-w, w, h);
return vec4(mix(vec3(0.1, 0.05, 0.12), vec3(0.95, 0.2, 0.35), m), 1.0);
} WGSL WebGPU, Bevy
fn frag(uv: vec2f) -> vec4f
{
var q = (uv - 0.5) * vec2f(aspect, 1.0) * 3.4;
q = q / (1.0 + 0.1 * sin(time * 6.0));
q.y += 0.28;
let t = q.y - sqrt(abs(q.x));
let h = q.x * q.x + t * t - 1.0;
let w = fwidth(h) + 0.0001;
let m = 1.0 - smoothstep(-w, w, h);
return vec4f(mix(vec3f(0.1, 0.05, 0.12), vec3f(0.95, 0.2, 0.35), m), 1.0);
} Godot Godot 4 shading language
shader_type canvas_item;
vec4 frag(vec2 uv)
{
vec2 q = (uv - 0.5) * vec2(aspect, 1.0) * 3.4;
q = q / (1.0 + 0.1 * sin(TIME * 6.0));
q.y += 0.28;
float t = q.y - sqrt(abs(q.x));
float h = q.x * q.x + t * t - 1.0;
float w = fwidth(h) + 0.0001;
float m = 1.0 - smoothstep(-w, w, h);
return vec4(mix(vec3(0.1, 0.05, 0.12), vec3(0.95, 0.2, 0.35), m), 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)
{
float2 q = (uv - 0.5) * float2(aspect, 1.0) * 3.4;
q = q / (1.0 + 0.1 * sin(time * 6.0));
q.y += 0.28;
float t = q.y - sqrt(abs(q.x));
float h = q.x * q.x + t * t - 1.0;
float w = fwidth(h) + 0.0001;
float m = 1.0 - smoothstep(-w, w, h);
return float4(mix(float3(0.1, 0.05, 0.12), float3(0.95, 0.2, 0.35), m), 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 beating heart shader?
A heart that pulses, using an implicit equation. A good first step: it is a circle, x² + t² = 1, whose height t is bent by sqrt(abs(x)): that bend makes the two lobes and the dent between them. 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 beating heart shader use?
It is built from smoothstep, anti-aliasing with fwidth, sine waves, lerp (mix). Each one is a small idea you can reuse in other effects.
Does this beating heart 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.