Skip to content

Seamless Texture — Go source

Make images tile seamlessly or generate tileable noise textures — entirely in your browser. Nothing uploads.

This is the Go implementation — the same logic the interactive tool runs, in a shareable, citable form.

// seamless-texture — tileable-texture math, Go port: wrap-blend weights,
// integer lattice hash, wrapping value noise, torus Worley.
// Go 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 arithmetic reproduces Math.imul exactly — Go defines
// signed overflow as two's-complement wraparound.
package seamless

import "math"

// BlendWeight is the 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, y, w, h, feather float64) float64 {
	if !(feather > 0) {
		return 1
	}
	fx := math.Min(x, w-x) / feather
	fy := math.Min(y, h-y) / feather
	return math.Min(1, math.Max(0, fx)) * math.Min(1, math.Max(0, fy))
}

// imul is 32-bit wrapping multiplication — Go's int32 multiply already has
// Math.imul's exact truncating semantics.
func imul(a, b int32) int32 { return a * b }

// shr is the JS `h >>> n`: a logical right shift of the int32 bit pattern.
func shr(h int32, n uint) int32 { return int32(uint32(h) >> n) }

// Hash2 is the 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 everywhere.
func Hash2(x, y, seed int32) float64 {
	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)
	return float64(uint32(h)) / 4294967296.0
}

// NoiseOptions mirrors the TS NoiseOptions: Scale is the tile period in
// pixels; Octaves < 1 snaps to 1 (the Go zero value included).
type NoiseOptions struct {
	Seed    int32
	Scale   float64
	Octaves int
}

// ValueNoise is 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, y float64, opts NoiseOptions) float64 {
	scale := math.Max(1, math.Round(opts.Scale))
	octaves := math.Max(1, math.Floor(float64(opts.Octaves)))
	sum, total, amp, freq := 0.0, 0.0, 1.0, 1.0
	for o := 0; float64(o) < octaves; o++ {
		sum += amp * layer(x*freq, y*freq, scale*freq, opts.Seed+int32(o)*101)
		total += amp
		amp /= 2
		freq *= 2
	}
	return sum / total
}

// layer is one octave: bilinear (quintic-faded) interpolation of wrapped
// lattice hashes.
func layer(x, y, period float64, seed int32) float64 {
	x0, y0 := math.Floor(x), math.Floor(y)
	u, v := fade(x-x0), fade(y-y0)
	xa, xb := wrap(x0, period), wrap(x0+1, period)
	ya, yb := wrap(y0, period), wrap(y0+1, period)
	top := lerp(Hash2(int32(xa), int32(ya), seed), Hash2(int32(xb), int32(ya), seed), u)
	bottom := lerp(Hash2(int32(xa), int32(yb), seed), Hash2(int32(xb), int32(yb), seed), u)
	return lerp(top, bottom, v)
}

// wrap is positive modulo — lattice indices wrap onto the torus of `period` cells.
func wrap(i, period float64) float64 {
	return math.Mod(math.Mod(i, period)+period, period)
}

// fade is the quintic smoothstep fade — C2-continuous interpolation curve.
func fade(t float64) float64 {
	return t * t * t * (t*(t*6-15) + 10)
}

func lerp(a, b, t float64) float64 {
	return a + (b-a)*t
}

// WorleyOptions mirrors the TS WorleyOptions: Points is the number of hashed
// feature points scattered in the unit tile (≥1; floored like the TS).
type WorleyOptions struct {
	Seed   int32
	Points float64
}

// WorleyResult holds the mapped 0..1 distances to the nearest (D1) and
// second-nearest (D2) feature points.
type WorleyResult struct {
	D1, D2 float64
}

// Worley is the 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, y float64, opts WorleyOptions) WorleyResult {
	// Wrap the query onto the unit tile first so shifted queries are exact.
	wx, wy := x-math.Floor(x), y-math.Floor(y)
	n := int(math.Floor(opts.Points))
	d1, d2 := 1.0, 1.0
	for i := 0; i < n; i++ {
		px := Hash2(int32(i), 0, opts.Seed)
		py := Hash2(int32(i), 1, opts.Seed)
		dx := math.Abs(wx - px)
		dx = math.Min(dx, 1-dx)
		dy := math.Abs(wy - py)
		dy = math.Min(dy, 1-dy)
		d := math.Sqrt(dx*dx+dy*dy) * math.Sqrt2
		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 →