Seamless Texture — C++ source
Make images tile seamlessly or generate tileable noise textures — entirely in your browser. Nothing uploads.
This is the C++ implementation — the same logic the interactive tool runs, in a shareable, citable form.
// seamless-texture — tileable-texture math, C++ port: wrap-blend weights,
// integer lattice hash, wrapping value noise, torus Worley. C++17, stdlib
// only. C++ port of the pure src/lib/seamless.ts (the TS lib has no canvas
// or DOM dependency at all, so this is a 1:1 port); the browser island owns
// all drawing. uint32_t arithmetic reproduces Math.imul exactly.
#include <cmath>
#include <cstdint>
// 32-bit wrapping multiplication — uint32_t multiply has Math.imul's
// exact truncating semantics.
static int32_t imul(int32_t a, int32_t b) {
return static_cast<int32_t>(static_cast<uint32_t>(a) * static_cast<uint32_t>(b));
}
// The JS `h >>> n`: a logical right shift of the int32 bit pattern.
static int32_t shr(int32_t h, unsigned n) {
return static_cast<int32_t>(static_cast<uint32_t>(h) >> n);
}
// JS Math.round (half up) — std::round is half away from zero.
static double jsRound(double v) {
return std::floor(v + 0.5);
}
// Forward declarations — valueNoise runs one octave per iteration.
static double layer(double x, double y, double period, int32_t seed);
static double wrap(double i, double period);
static double fade(double t);
static double lerp(double a, double b, double t);
double blendWeight(double x, double y, double w, double h, double feather) {
if (!(feather > 0)) return 1;
double fx = std::fmin(x, w - x) / feather;
double fy = std::fmin(y, h - y) / feather;
return std::fmin(1, std::fmax(0, fx)) * std::fmin(1, std::fmax(0, fy));
}
double hash2(int32_t x, int32_t y, int32_t seed) {
int32_t h = imul(x, static_cast<int32_t>(0x9e3779b1)) ^ imul(y, static_cast<int32_t>(0x85ebca77)) ^ imul(seed, static_cast<int32_t>(0xc2b2ae35));
h ^= shr(h, 15);
h = imul(h, static_cast<int32_t>(0x2c1b3c6d));
h ^= shr(h, 12);
h = imul(h, static_cast<int32_t>(0x297a2d39));
h ^= shr(h, 15);
return static_cast<double>(static_cast<uint32_t>(h)) / 4294967296.0;
}
// Mirrors the TS NoiseOptions: `scale` is the tile period in pixels;
// `octaves` < 1 snaps to 1.
struct NoiseOptions {
int32_t seed;
double scale;
int32_t octaves = 1;
};
// Wrapping value noise over a unit lattice (lattice spacing 1 px, tile
// period `scale` px): noise(x + scale, y) === noise(x, y) exactly. Octaves
// >= 2 sum frequency-doubled, amplitude-halved layers normalized by total
// amplitude; scale < 1 snaps to 1.
double valueNoise(double x, double y, NoiseOptions opts) {
double scale = std::fmax(1, jsRound(opts.scale));
int32_t octaves = opts.octaves < 1 ? 1 : opts.octaves;
double sum = 0, total = 0, amp = 1, freq = 1;
for (int32_t o = 0; o < octaves; o++) {
sum += amp * layer(x * freq, y * freq, scale * freq, opts.seed + o * 101);
total += amp;
amp /= 2;
freq *= 2;
}
return sum / total;
}
// One octave: bilinear (quintic-faded) interpolation of wrapped lattice hashes.
static double layer(double x, double y, double period, int32_t seed) {
double x0 = std::floor(x), y0 = std::floor(y);
double u = fade(x - x0), v = fade(y - y0);
double xa = wrap(x0, period), xb = wrap(x0 + 1, period);
double ya = wrap(y0, period), yb = wrap(y0 + 1, period);
double top = lerp(hash2(static_cast<int32_t>(xa), static_cast<int32_t>(ya), seed), hash2(static_cast<int32_t>(xb), static_cast<int32_t>(ya), seed), u);
double bottom = lerp(hash2(static_cast<int32_t>(xa), static_cast<int32_t>(yb), seed), hash2(static_cast<int32_t>(xb), static_cast<int32_t>(yb), seed), u);
return lerp(top, bottom, v);
}
// Positive modulo — lattice indices wrap onto the torus of `period` cells.
static double wrap(double i, double period) {
return std::fmod(std::fmod(i, period) + period, period);
}
// Quintic smoothstep fade — C2-continuous interpolation curve.
static double fade(double t) {
return t * t * t * (t * (t * 6 - 15) + 10);
}
static double lerp(double a, double b, double t) {
return a + (b - a) * t;
}
// Mirrors the TS WorleyOptions: `points` is the number of hashed feature
// points scattered in the unit tile (>= 1; floored like the TS).
struct WorleyOptions {
int32_t seed;
double points;
};
// Mapped 0..1 distances to the nearest (d1) and second-nearest (d2) points.
struct WorleyResult {
double d1, d2;
};
// Wrapping (torus-metric) F1-F2 field: each axis distance wraps via
// min(d, 1-d), so the field tiles exactly with period 1 in x and y. Raw
// torus distances top out at the half-diagonal √½, which maps to 1. Fewer
// than one point returns the all-max {1, 1}; exactly one point leaves d2
// at the maximum.
WorleyResult worley(double x, double y, WorleyOptions opts) {
// Wrap the query onto the unit tile first so shifted queries are exact.
double wx = x - std::floor(x), wy = y - std::floor(y);
int32_t n = static_cast<int32_t>(std::floor(opts.points));
WorleyResult r{1, 1};
for (int32_t i = 0; i < n; i++) {
double px = hash2(i, 0, opts.seed);
double py = hash2(i, 1, opts.seed);
double dx = std::fabs(wx - px);
dx = std::fmin(dx, 1 - dx);
double dy = std::fabs(wy - py);
dy = std::fmin(dy, 1 - dy);
double d = std::sqrt(dx * dx + dy * dy) * 1.4142135623730951;
if (d < r.d1) {
r.d2 = r.d1;
r.d1 = d;
} else if (d < r.d2) {
r.d2 = d;
}
}
return r;
}
Also available in 13 other languages
Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →