Masks
A mask is a value from 0 to 1 that says where something shows: multiply by it to fade a pattern out, or use it to choose between two.
How it works¶
Multiplying a colour by 0 makes it black and by 1 leaves it alone, so a value that is 1 inside a shape and 0 outside cuts the pattern to that shape. smoothstep(a, b, v) makes the edge soft: it is 0 when v is below a, 1 above b, and an S-shaped curve in between. For a circle, v is the distance from the centre and 1 − smoothstep(90, 140, d) is 1 inside 90, fading to 0 by 140.
A mask can also choose: mix(A, B, m) shows A where m is 0 and B where it is 1, blending between. A narrow smoothstep of noise, smoothstep(0.45, 0.55, noise), makes islands with soft coasts.
The C is for the raylib pixels starter.
XOR in a soft circle¶
static RGB shade(float x, float y, float t)
{
float d = dist(x, y, 150, 150);
float m = 1 - smoothstep(90, 140, d); /* 1 inside, 0 outside */
float v = ((int)x ^ (int)y) % 256 / 255.0f;
return scale(rainbow(v), m);
}
function shade(x, y, t) {
let d = dist(x, y, 150, 150);
let m = 1 - smoothstep(90, 140, d); // 1 inside, 0 outside
let v = (x ^ y) % 256 / 255;
return scale(rainbow(v), m);
}
Noise choosing between stripes and rings¶
static RGB shade(float x, float y, float t)
{
float stripes = wave(x * 0.3f);
float rings = wave(dist(x, y, 150, 150) * 0.3f);
float m = smoothstep(0.45f, 0.55f, noise2(x * 0.01f, y * 0.01f));
return grey(mix(stripes, rings, m));
}
function shade(x, y, t) {
let stripes = wave(x * 0.3);
let rings = wave(dist(x, y, 150, 150) * 0.3);
let m = smoothstep(0.45, 0.55, noise2(x * 0.01, y * 0.01));
return grey(mix(stripes, rings, m));
}
A moving mask¶
The islands drift, so the stripes and rings swap places.
static RGB shade(float x, float y, float t)
{
float stripes = wave(x * 0.3f);
float rings = wave(dist(x, y, 150, 150) * 0.3f - t);
float m = smoothstep(0.45f, 0.55f, noise3(x * 0.01f, y * 0.01f, t * 0.2f));
return mixc(sea(stripes), ember(rings), m);
}
function shade(x, y, t) {
let stripes = wave(x * 0.3);
let rings = wave(dist(x, y, 150, 150) * 0.3 - t);
let m = smoothstep(0.45, 0.55, noise3(x * 0.01, y * 0.01, t * 0.2));
return mixc(sea(stripes), ember(rings), m);
}
Related¶
- Easing Functions: smoothstep's curve.
- Linear Interpolation (Lerp):
mix, which the mask drives. - Combining Two Patterns: multiply as a join.