Seamless Texture — Rust source
Make images tile seamlessly or generate tileable noise textures — entirely in your browser. Nothing uploads.
This is the Rust implementation — the same logic the interactive tool runs, in a shareable, citable form.
//! seamless-texture — tileable-texture math, Rust port: wrap-blend weights,
//! integer lattice hash, wrapping value noise, torus Worley.
//! Rust 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. i32 wrapping_mul / u32 shifts reproduce Math.imul exactly.
/// Cross-fade weight for wrap-blending a texture's seams: 1 in the interior,
/// falling linearly to 0 across `feather` pixels approaching each wrapped
/// edge (axis weights multiplied, clamped 0..1). `feather <= 0` (or NaN)
/// disables the ramp.
pub fn blend_weight(x: f64, y: f64, w: f64, h: f64, feather: f64) -> f64 {
if !(feather > 0.0) {
return 1.0;
}
let fx = (x.min(w - x)) / feather;
let fy = (y.min(h - y)) / feather;
fx.clamp(0.0, 1.0) * fy.clamp(0.0, 1.0)
}
/// 32-bit wrapping multiplication — i32::wrapping_mul has Math.imul's exact
/// truncating semantics.
fn imul(a: i32, b: i32) -> i32 {
a.wrapping_mul(b)
}
/// The JS `h >>> n`: a logical right shift of the int32 bit pattern.
fn shr(h: i32, n: u32) -> i32 {
((h as u32) >> n) as i32
}
/// Deterministic 0..1 hash of an integer lattice point: 32-bit integer
/// avalanche — h = x·0x9E3779B1 ^ y·0x85EBCA77 ^ seed·0xC2B2AE35 (truncated
/// to int32), then three xor-shift/multiply rounds; the unsigned result is
/// scaled by 1/2³². Same (x, y, seed) → same value on every platform and run.
pub fn hash2(x: i32, y: i32, seed: i32) -> f64 {
let mut h = imul(x, 0x9e3779b1) ^ imul(y, 0x85ebca77) ^ imul(seed, 0xc2b2ae35);
h ^= shr(h, 15);
h = imul(h, 0x2c1b3c6d);
h ^= shr(h, 12);
h = imul(h, 0x297a2d39);
h ^= shr(h, 15);
(h as u32) as f64 / 4294967296.0
}
/// Mirrors the TS NoiseOptions: `scale` is the tile period in pixels;
/// `octaves` < 1 snaps to 1.
#[derive(Debug, Clone, Copy)]
pub struct NoiseOptions {
pub seed: i32,
pub scale: f64,
pub octaves: u32,
}
/// 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.
pub fn value_noise(x: f64, y: f64, opts: &NoiseOptions) -> f64 {
let scale = opts.scale.round().max(1.0);
let octaves = (opts.octaves as f64).floor().max(1.0);
let mut sum = 0.0;
let mut total = 0.0;
let mut amp = 1.0;
let mut freq = 1.0;
let mut o = 0.0;
while o < octaves {
sum += amp * layer(x * freq, y * freq, scale * freq, opts.seed.wrapping_add((o as i32).wrapping_mul(101)));
total += amp;
amp /= 2.0;
freq *= 2.0;
o += 1.0;
}
sum / total
}
/// One octave: bilinear (quintic-faded) interpolation of wrapped lattice hashes.
fn layer(x: f64, y: f64, period: f64, seed: i32) -> f64 {
let x0 = x.floor();
let y0 = y.floor();
let u = fade(x - x0);
let v = fade(y - y0);
let xa = wrap(x0, period);
let xb = wrap(x0 + 1.0, period);
let ya = wrap(y0, period);
let yb = wrap(y0 + 1.0, period);
let top = lerp(hash2(xa as i32, ya as i32, seed), hash2(xb as i32, ya as i32, seed), u);
let bottom = lerp(hash2(xa as i32, yb as i32, seed), hash2(xb as i32, yb as i32, seed), u);
lerp(top, bottom, v)
}
/// Positive modulo — lattice indices wrap onto the torus of `period` cells.
fn wrap(i: f64, period: f64) -> f64 {
(i % period + period) % period
}
/// Quintic smoothstep fade — C2-continuous interpolation curve.
fn fade(t: f64) -> f64 {
t * t * t * (t * (t * 6.0 - 15.0) + 10.0)
}
fn lerp(a: f64, b: f64, t: f64) -> f64 {
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).
#[derive(Debug, Clone, Copy)]
pub struct WorleyOptions {
pub seed: i32,
pub points: f64,
}
/// Mapped 0..1 distances to the nearest (d1) and second-nearest (d2) points.
#[derive(Debug, Clone, Copy)]
pub struct WorleyResult {
pub d1: f64,
pub d2: f64,
}
/// 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.
pub fn worley(x: f64, y: f64, opts: &WorleyOptions) -> WorleyResult {
// Wrap the query onto the unit tile first so shifted queries are exact.
let wx = x - x.floor();
let wy = y - y.floor();
let n = opts.points.floor() as i64;
let mut d1 = 1.0;
let mut d2 = 1.0;
for i in 0..n {
let px = hash2(i as i32, 0, opts.seed);
let py = hash2(i as i32, 1, opts.seed);
let mut dx = (wx - px).abs();
dx = dx.min(1.0 - dx);
let mut dy = (wy - py).abs();
dy = dy.min(1.0 - dy);
let d = (dx * dx + dy * dy).sqrt() * std::f64::consts::SQRT_2;
if d < d1 {
d2 = d1;
d1 = d;
} else if d < d2 {
d2 = d;
}
}
WorleyResult { d1, d2 }
}
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 →