Seamless Texture — Ruby source
Make images tile seamlessly or generate tileable noise textures — entirely in your browser. Nothing uploads.
This is the Ruby implementation — the same logic the interactive tool runs, in a shareable, citable form.
# seamless-texture — tileable-texture math, Ruby port: wrap-blend weights,
# integer lattice hash, wrapping value noise, torus Worley (stdlib only).
# Ruby 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. The 32-bit avalanche is emulated with masks — Ruby Integers
# are arbitrary precision.
module SeamlessTexture
module_function
# Mapped 0..1 distances to the nearest (d1) and second-nearest (d2) points.
WorleyResult = Struct.new(:d1, :d2)
# 32-bit wrapping multiplication — the Math.imul analogue.
def imul(a, b)
r = (a & 0xFFFFFFFF) * (b & 0xFFFFFFFF) & 0xFFFFFFFF
r >= 0x80000000 ? r - 0x100000000 : r
end
# The JS `h >>> n`: a logical right shift of the int32 bit pattern.
def shr(h, n)
(h & 0xFFFFFFFF) >> n
end
# JS Math.round (half up) — Ruby's Float#round is half away from zero.
def js_round(v)
(v + 0.5).floor
end
# 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` disables the ramp.
def blend_weight(x, y, w, h, feather)
return 1.0 unless feather > 0
fx = [x, w - x].min.to_f / feather
fy = [y, h - y].min.to_f / feather
[1.0, fx.clamp(0.0, 1.0)].min * [1.0, fy.clamp(0.0, 1.0)].min
end
# 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.
def hash2(x, y, seed)
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 & 0xFFFFFFFF) / 4294967296.0
end
# Wrapping value noise over a unit lattice; the field tiles exactly.
#
# Octaves >= 2 sum frequency-doubled, amplitude-halved layers normalized
# by total amplitude; scale < 1 snaps to 1; octaves < 1 snaps to 1.
def value_noise(x, y, seed:, scale:, octaves: 1)
scale = [1.0, js_round(scale).to_f].max
octaves = [1, octaves].max
sum = 0.0
total = 0.0
amp = 1.0
freq = 1.0
octaves.times do |o|
sum += amp * layer(x * freq, y * freq, scale * freq, seed + o * 101)
total += amp
amp /= 2
freq *= 2
end
sum / total
end
# One octave: bilinear (quintic-faded) interpolation of wrapped lattice hashes.
def layer(x, y, period, seed)
x0 = x.floor
y0 = y.floor
u = fade(x - x0)
v = fade(y - y0)
xa = wrap(x0, period)
xb = wrap(x0 + 1, period)
ya = wrap(y0, period)
yb = wrap(y0 + 1, period)
top = lerp(hash2(xa.to_i, ya.to_i, seed), hash2(xb.to_i, ya.to_i, seed), u)
bottom = lerp(hash2(xa.to_i, yb.to_i, seed), hash2(xb.to_i, yb.to_i, seed), u)
lerp(top, bottom, v)
end
# Positive modulo — lattice indices wrap onto the torus of `period` cells.
def wrap(i, period)
(i % period + period) % period
end
# Quintic smoothstep fade — C2-continuous interpolation curve.
def fade(t)
t * t * t * (t * (t * 6 - 15) + 10)
end
def lerp(a, b, t)
a + (b - a) * t
end
# Wrapping (torus-metric) F1-F2 field; tiles exactly with period 1.
#
# Each axis distance wraps via min(d, 1-d); 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.
def worley(x, y, seed:, points:)
# Wrap the query onto the unit tile first so shifted queries are exact.
wx = x - x.floor
wy = y - y.floor
n = points.floor
d1 = 1.0
d2 = 1.0
n.times do |i|
px = hash2(i, 0, seed)
py = hash2(i, 1, seed)
dx = (wx - px).abs
dx = [dx, 1 - dx].min
dy = (wy - py).abs
dy = [dy, 1 - dy].min
d = Math.sqrt(dx * dx + dy * dy) * Math.sqrt(2)
if d < d1
d2 = d1
d1 = d
elsif d < d2
d2 = d
end
end
WorleyResult.new(d1, d2)
end
end
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 →