Skip to content

Combining Two Patterns

Make every pattern 0..1 first, then join them by average, multiply, difference, compare or max, and the result is still 0..1, ready to join with the next.

How it works

Adding patterns together usually ends in static or one flat colour, for a few reasons, each with a fix:

  • The ranges differ. XOR gives 0..255, a sine −1..1, a distance 0..212: the biggest hides the rest. Make every pattern 0..1 first.
  • Everything has fine detail. Many fine details on top of each other look like random noise. Use one large, slow pattern as the main shape and add detail at a low weight (Layering Detail at a Low Weight).
  • Values leave 0..1, so colours clip to pure black or white. Use weights that add to 1, or clamp01().
  • Too many colours. Send one final value through one palette (Cosine Palette) instead of making red, green and blue from different patterns.

With two patterns A and B both 0..1:

Join Code What it does
Average (A + B) / 2 Both show equally. Weights such as 0.8 × A + 0.2 × B let one lead.
Multiply A × B B shows only where A is bright. Good for masks and for keeping dark areas dark.
Difference abs(A − B) Bright where the two disagree. Often gives sharp, interesting edges.
Compare A > B ? 1 : 0 A hard two-colour result. smoothstep(-0.05, 0.05, A − B) softens the edge.
Max / min max(A, B) Keeps the brighter (or darker) at each pixel, like layering.

Every result stays 0..1, so it can be joined with a third pattern the same way.

The C is for the raylib pixels starter. In the joins, A is rings and B is noise.

Without control: static

The same three ingredients as the next example, added raw, with colour from three unrelated numbers.

static RGB shade(float x, float y, float t)
{
    float v = ((int)x ^ (int)y) * sinf(x * 0.7f) + dist(x, y, 150, 150) * cosf(y * 0.9f) + noise2(x, y) * 200;
    return rgb(fmodf(v, 1), fmodf(v * 3, 1), fmodf(v * 7, 1));
}
function shade(x, y, t) {
  let v = (x ^ y) * sinf(x * 0.7) + dist(x, y, 150, 150) * cosf(y * 0.9) + noise2(x, y) * 200;
  return rgb(fmodf(v, 1), fmodf(v * 3, 1), fmodf(v * 7, 1));
}

With control: each 0..1, weighted, one palette

static RGB shade(float x, float y, float t)
{
    float a = ((int)x ^ (int)y) % 256 / 255.0f;   /* 0..1 */
    float b = wave(dist(x, y, 150, 150) * 0.05f);  /* 0..1 */
    float c = noise2(x * 0.01f, y * 0.01f);         /* about 0..1 */
    return rainbow(0.15f * a + 0.55f * b + 0.3f * c);   /* weights add to 1 */
}
function shade(x, y, t) {
  let a = (x ^ y) % 256 / 255;              // 0..1
  let b = wave(dist(x, y, 150, 150) * 0.05); // 0..1
  let c = noise2(x * 0.01, y * 0.01);        // about 0..1
  return rainbow(0.15 * a + 0.55 * b + 0.3 * c);   // weights add to 1
}

Average

static float fieldA(float x, float y) { return wave(dist(x, y, 150, 150) * 0.15f); }
static float fieldB(float x, float y) { return noise2(x * 0.015f, y * 0.015f); }

static RGB shade(float x, float y, float t)
{
    return grey((fieldA(x, y) + fieldB(x, y)) / 2);
}
function fieldA(x, y) { return wave(dist(x, y, 150, 150) * 0.15); }
function fieldB(x, y) { return noise2(x * 0.015, y * 0.015); }

function shade(x, y, t) {
  return grey((fieldA(x, y) + fieldB(x, y)) / 2);
}

Multiply

static float fieldA(float x, float y) { return wave(dist(x, y, 150, 150) * 0.15f); }
static float fieldB(float x, float y) { return noise2(x * 0.015f, y * 0.015f); }

static RGB shade(float x, float y, float t)
{
    return grey(fieldA(x, y) * fieldB(x, y));
}
function fieldA(x, y) { return wave(dist(x, y, 150, 150) * 0.15); }
function fieldB(x, y) { return noise2(x * 0.015, y * 0.015); }

function shade(x, y, t) {
  return grey(fieldA(x, y) * fieldB(x, y));
}

Difference

static float fieldA(float x, float y) { return wave(dist(x, y, 150, 150) * 0.15f); }
static float fieldB(float x, float y) { return noise2(x * 0.015f, y * 0.015f); }

static RGB shade(float x, float y, float t)
{
    return grey(fabsf(fieldA(x, y) - fieldB(x, y)));
}
function fieldA(x, y) { return wave(dist(x, y, 150, 150) * 0.15); }
function fieldB(x, y) { return noise2(x * 0.015, y * 0.015); }

function shade(x, y, t) {
  return grey(fabsf(fieldA(x, y) - fieldB(x, y)));
}

Compare

static float fieldA(float x, float y) { return wave(dist(x, y, 150, 150) * 0.15f); }
static float fieldB(float x, float y) { return noise2(x * 0.015f, y * 0.015f); }

static RGB shade(float x, float y, float t)
{
    return grey(fieldA(x, y) > fieldB(x, y) ? 1 : 0);
}
function fieldA(x, y) { return wave(dist(x, y, 150, 150) * 0.15); }
function fieldB(x, y) { return noise2(x * 0.015, y * 0.015); }

function shade(x, y, t) {
  return grey(fieldA(x, y) > fieldB(x, y) ? 1 : 0);
}

Max

static float fieldA(float x, float y) { return wave(dist(x, y, 150, 150) * 0.15f); }
static float fieldB(float x, float y) { return noise2(x * 0.015f, y * 0.015f); }

static RGB shade(float x, float y, float t)
{
    return grey(fmaxf(fieldA(x, y), fieldB(x, y)));
}
function fieldA(x, y) { return wave(dist(x, y, 150, 150) * 0.15); }
function fieldB(x, y) { return noise2(x * 0.015, y * 0.015); }

function shade(x, y, t) {
  return grey(fmaxf(fieldA(x, y), fieldB(x, y)));
}