Domain Warping
Use one pattern to move the input of another: the main pattern keeps its shape but bends, so it almost never turns into a mess.
How it works¶
A pixel pattern is a function of position, f(x, y). Warping asks for it somewhere else instead: f(x + dx, y + dy), where the offsets come from a second pattern. If the offsets change smoothly across the picture (noise, sine waves), the first pattern is bent rather than broken.
Use two different noise values for the two offsets (the + 50 moves to an unrelated part of the noise), and subtract 0.5 so they push both ways. The multiplier is how far, in pixels, a point can move: here up to 40 either way.
Noise warped by noise (noise(p + 4 × noise(p))) gives shapes like marble or smoke; Inigo Quilez's articles take this further, warping two and three times.
The C is for the raylib pixels starter. XOR needs whole numbers, and a bent position can be negative, so it is floored and made positive first.
Rings, bent by noise¶
static RGB shade(float x, float y, float t)
{
float wx = x + 80 * (noise2(x * 0.01f, y * 0.01f) - 0.5f);
float wy = y + 80 * (noise2(x * 0.01f + 50, y * 0.01f) - 0.5f);
return grey(wave(dist(wx, wy, 150, 150) * 0.15f));
}
function shade(x, y, t) {
let wx = x + 80 * (noise2(x * 0.01, y * 0.01) - 0.5);
let wy = y + 80 * (noise2(x * 0.01 + 50, y * 0.01) - 0.5);
return grey(wave(dist(wx, wy, 150, 150) * 0.15));
}
XOR, bent by sine waves¶
static RGB shade(float x, float y, float t)
{
float wx = x + 20 * sinf(y * 0.05f);
float wy = y + 20 * sinf(x * 0.05f);
return grey(((int)fabsf(floorf(wx)) ^ (int)fabsf(floorf(wy))) % 256 / 255.0f);
}
function shade(x, y, t) {
let wx = x + 20 * sinf(y * 0.05);
let wy = y + 20 * sinf(x * 0.05);
return grey((fabsf(floorf(wx)) ^ fabsf(floorf(wy))) % 256 / 255);
}
Noise inside noise: marble¶
static RGB shade(float x, float y, float t)
{
float q = noise2(x * 0.01f, y * 0.01f);
float r = noise2(x * 0.01f + 4 * q, y * 0.01f + 4 * q);
return rainbow(r);
}
function shade(x, y, t) {
let q = noise2(x * 0.01, y * 0.01);
let r = noise2(x * 0.01 + 4 * q, y * 0.01 + 4 * q);
return rainbow(r);
}
Smoke: the warp moving with time¶
static RGB shade(float x, float y, float t)
{
float q = noise3(x * 0.01f, y * 0.01f, t * 0.1f);
float r = noise3(x * 0.01f + 4 * q, y * 0.01f + 4 * q, t * 0.05f);
return ember(r);
}
function shade(x, y, t) {
let q = noise3(x * 0.01, y * 0.01, t * 0.1);
let r = noise3(x * 0.01 + 4 * q, y * 0.01 + 4 * q, t * 0.05);
return ember(r);
}
Related¶
- Sine Waves: a wave inside a wave is the simplest warp.
- Random and Perlin Noise: where smooth offsets come from.
- Building a Piece Step by Step: a warp as step 2.