Skip to content

Seamless Texture — Kotlin source

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

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

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

// 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.
fun blendWeight(x: Double, y: Double, w: Double, h: Double, feather: Double): Double {
    if (!(feather > 0)) return 1.0
    val fx = minOf(x, w - x) / feather
    val fy = minOf(y, h - y) / feather
    return minOf(1.0, maxOf(0.0, fx)) * minOf(1.0, maxOf(0.0, fy))
}

// imul is 32-bit wrapping multiplication — Kotlin's Int multiply already
// has Math.imul's exact truncating semantics.
private fun imul(a: Int, b: Int): Int = a * b

// The JS `h >>> n`: a logical right shift of the int32 bit pattern.
private fun shr(h: Int, n: Int): Int = h ushr n

// 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.
fun hash2(x: Int, y: Int, seed: Int): Double {
    var h = imul(x, 0x9e3779b1.toInt()) xor imul(y, 0x85ebca77.toInt()) xor imul(seed, 0xc2b2ae35.toInt())
    h = h xor shr(h, 15)
    h = imul(h, 0x2c1b3c6d.toInt())
    h = h xor shr(h, 12)
    h = imul(h, 0x297a2d39.toInt())
    h = h xor shr(h, 15)
    return (h.toLong() and 0xFFFFFFFFL) / 4294967296.0
}

// Mirrors the TS NoiseOptions: `scale` is the tile period in pixels;
// `octaves` < 1 snaps to 1 (the default included).
data class NoiseOptions(val seed: Int, val scale: Double, val octaves: Int = 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. Math.round is half-up like the JS
// Math.round.
fun valueNoise(x: Double, y: Double, opts: NoiseOptions): Double {
    val scale = maxOf(1.0, Math.round(opts.scale).toDouble())
    val octaves = maxOf(1, opts.octaves)
    var sum = 0.0
    var total = 0.0
    var amp = 1.0
    var freq = 1.0
    for (o in 0 until 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.
private fun layer(x: Double, y: Double, period: Double, seed: Int): Double {
    val x0 = Math.floor(x)
    val y0 = Math.floor(y)
    val u = fade(x - x0)
    val v = fade(y - y0)
    val xa = wrap(x0, period)
    val xb = wrap(x0 + 1, period)
    val ya = wrap(y0, period)
    val yb = wrap(y0 + 1, period)
    val top = lerp(hash2(xa.toInt(), ya.toInt(), seed), hash2(xb.toInt(), ya.toInt(), seed), u)
    val bottom = lerp(hash2(xa.toInt(), yb.toInt(), seed), hash2(xb.toInt(), yb.toInt(), seed), u)
    return lerp(top, bottom, v)
}

// Positive modulo — lattice indices wrap onto the torus of `period` cells.
private fun wrap(i: Double, period: Double): Double {
    return ((i % period) + period) % period
}

// Quintic smoothstep fade — C2-continuous interpolation curve.
private fun fade(t: Double): Double {
    return t * t * t * (t * (t * 6 - 15) + 10)
}

private fun lerp(a: Double, b: Double, t: Double): Double {
    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).
data class WorleyOptions(val seed: Int, val points: Double)

// Mapped 0..1 distances to the nearest (d1) and second-nearest (d2) points.
data class WorleyResult(val d1: Double, val 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.
fun worley(x: Double, y: Double, opts: WorleyOptions): WorleyResult {
    // Wrap the query onto the unit tile first so shifted queries are exact.
    val wx = x - Math.floor(x)
    val wy = y - Math.floor(y)
    val n = Math.floor(opts.points).toInt()
    var d1 = 1.0
    var d2 = 1.0
    for (i in 0 until n) {
        val px = hash2(i, 0, opts.seed)
        val py = hash2(i, 1, opts.seed)
        var dx = Math.abs(wx - px)
        dx = minOf(dx, 1 - dx)
        var dy = Math.abs(wy - py)
        dy = minOf(dy, 1 - dy)
        val d = Math.sqrt(dx * dx + dy * dy) * 1.4142135623730951
        if (d < d1) {
            d2 = d1
            d1 = d
        } else if (d < d2) {
            d2 = d
        }
    }
    return 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 →