2D toolkit · lesson 2 of 8 · Intermediate · about 5 min

Moving a shape (the key trick)

You will build: Draw the yellow disc (radius 0.2) centred 0.3 to the right and 0.15 up from the middle.

The picture produced by the shader in the lesson "Moving a shape (the key trick)"
The target picture. In the lesson you write the shader that draws it, and a match bar shows how close you are.

This is the most important trick for drawing in a shader: you never move a shape. You change which position each pixel looks at.

So far every pixel asked: “am I inside a circle in the middle of the screen?” To draw the circle somewhere else, don't move the circle. Give every pixel a different position to look at: p - float2(0.3, 0.0). That pixel now measures from a spot 0.3 further left than where it really is, so the circle shows up 0.3 to the right.

Check with one number: the pixel at x = 0.3 computes 0.3 − 0.3 = 0, which is the middle of the circle. ✓

In the diagram the dashed box never moves. The gold outline is where it appears on screen. Hover a dot to see the position that pixel was handed.

float2 p = (uv - 0.5) * float2(aspect, 1.0);
p = p - float2(0.3, 0.15);      // shift: the picture appears 0.3 right and 0.15 up
float d = length(p);            // distance from the (shifted) middle

The numbers you need

Everything the picture depends on is given, so the challenge is the code, not guessing.

Disc
radius 0.2, color float3(1.0, 0.8, 0.2)
Background
float3(0.08, 0.08, 0.12)
Centre
0.3 right, 0.15 up
Inside test
step(length(p), 0.2)

Try it in your own language

The lesson is written once and shown in every language. Pick yours: Unity (Built-in), Unity (URP), Unreal, Shadertoy / WebGL, OpenGL (LWJGL, raylib), three.js, GameMaker, LÖVE (Lua), Godot, Apple (iOS, macOS), WebGPU / Bevy.

Start coding

Go deeper

Part of the learning path: Shader art basics.