Seamless Texture — Swift source
Make images tile seamlessly or generate tileable noise textures — entirely in your browser. Nothing uploads.
This is the Swift implementation — the same logic the interactive tool runs, in a shareable, citable form.
// seamless-texture — tileable-texture math, Swift port: wrap-blend weights,
// integer lattice hash, wrapping value noise, torus Worley. Swift stdlib
// only. Swift 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. Int32 &* wrapping multiplication reproduces
// 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.
func blendWeight(_ x: Double, _ y: Double, _ w: Double, _ h: Double, _ feather: Double) -> Double {
if !(feather > 0) { return 1 }
let fx = min(x, w - x) / feather
let fy = min(y, h - y) / feather
return min(1, max(0, fx)) * min(1, max(0, fy))
}
/// 32-bit wrapping multiplication — Int32 `&*` has Math.imul's exact
/// truncating semantics.
func imul(_ a: Int32, _ b: Int32) -> Int32 {
a &* b
}
/// The JS `h >>> n`: a logical right shift of the int32 bit pattern.
func shr(_ h: Int32, _ n: UInt32) -> Int32 {
Int32(bitPattern: UInt32(bitPattern: h) >> n)
}
/// JS Math.round (half up) — `.rounded()` is half away from zero.
func jsRound(_ v: Double) -> Double {
(v + 0.5).rounded(.down)
}
/// 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.
func hash2(_ x: Int32, _ y: Int32, _ seed: Int32) -> Double {
var h = imul(x, Int32(bitPattern: 0x9e3779b1)) ^ imul(y, Int32(bitPattern: 0x85ebca77)) ^ imul(seed, Int32(bitPattern: 0xc2b2ae35))
h ^= shr(h, 15)
h = imul(h, Int32(bitPattern: 0x2c1b3c6d))
h ^= shr(h, 12)
h = imul(h, Int32(bitPattern: 0x297a2d39))
h ^= shr(h, 15)
return Double(UInt32(bitPattern: h)) / 4294967296.0
}
/// Mirrors the TS NoiseOptions: `scale` is the tile period in pixels;
/// `octaves` < 1 snaps to 1 (the default included).
struct NoiseOptions {
var seed: Int32
var scale: Double
var octaves: Int32 = 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.
func valueNoise(_ x: Double, _ y: Double, _ opts: NoiseOptions) -> Double {
let scale = max(1, jsRound(opts.scale))
let octaves = max(1, opts.octaves)
var sum = 0.0
var total = 0.0
var amp = 1.0
var freq = 1.0
for o in 0..<octaves {
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.
func layer(_ x: Double, _ y: Double, _ period: Double, _ seed: Int32) -> Double {
let x0 = x.rounded(.down)
let y0 = y.rounded(.down)
let u = fade(x - x0)
let v = fade(y - y0)
let xa = wrap(x0, period)
let xb = wrap(x0 + 1, period)
let ya = wrap(y0, period)
let yb = wrap(y0 + 1, period)
let top = lerp(hash2(Int32(xa), Int32(ya), seed), hash2(Int32(xb), Int32(ya), seed), u)
let bottom = lerp(hash2(Int32(xa), Int32(yb), seed), hash2(Int32(xb), Int32(yb), seed), u)
return lerp(top, bottom, v)
}
/// Positive modulo — lattice indices wrap onto the torus of `period` cells.
func wrap(_ i: Double, _ period: Double) -> Double {
(i.truncatingRemainder(dividingBy: period) + period).truncatingRemainder(dividingBy: period)
}
/// Quintic smoothstep fade — C2-continuous interpolation curve.
func fade(_ t: Double) -> Double {
t * t * t * (t * (t * 6 - 15) + 10)
}
func lerp(_ a: Double, _ b: Double, _ t: Double) -> Double {
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 {
var seed: Int32
var points: Double
}
/// Mapped 0..1 distances to the nearest (d1) and second-nearest (d2) points.
struct WorleyResult {
var d1: Double
var d2: Double
}
/// 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.
func worley(_ x: Double, _ y: Double, _ opts: WorleyOptions) -> WorleyResult {
// Wrap the query onto the unit tile first so shifted queries are exact.
let wx = x - x.rounded(.down)
let wy = y - y.rounded(.down)
let n = Int32(opts.points.rounded(.down))
var d1 = 1.0
var d2 = 1.0
if n >= 1 {
for i in 0..<n {
let px = hash2(i, 0, opts.seed)
let py = hash2(i, 1, opts.seed)
var dx = abs(wx - px)
dx = min(dx, 1 - dx)
var dy = abs(wy - py)
dy = min(dy, 1 - dy)
let d = (dx * dx + dy * dy).squareRoot() * 2.0.squareRoot()
if d < d1 {
d2 = d1
d1 = d
} else if d < d2 {
d2 = d
}
}
}
return WorleyResult(d1: d1, d2: 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 →