Skip to content

Seamless Texture — Java source

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

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

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

// Package-private: display snippets keep the repo filename (seamless-texture.java),
// which need not match the class name.
final class SeamlessTexture {

    // 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.
    static double blendWeight(double x, double y, double w, double h, double feather) {
        if (!(feather > 0)) return 1;
        double fx = Math.min(x, w - x) / feather;
        double 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 — Java's int multiply already
    // has Math.imul's exact truncating semantics.
    private static int imul(int a, int b) { return a * b; }

    // The JS `h >>> n`: a logical right shift of the int32 bit pattern.
    private static int shr(int h, int n) { return h >>> 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.
    static double hash2(int x, int y, int seed) {
        int 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 (h & 0xFFFFFFFFL) / 4294967296.0;
    }

    // Mirrors the TS NoiseOptions: `scale` is the tile period in pixels;
    // `octaves` < 1 snaps to 1 (the one-arg constructor included).
    static final class NoiseOptions {
        final int seed;
        final double scale;
        final int octaves;

        NoiseOptions(int seed, double scale) { this(seed, scale, 1); }

        NoiseOptions(int seed, double scale, int octaves) {
            this.seed = seed;
            this.scale = scale;
            this.octaves = octaves;
        }
    }

    // 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.
    static double valueNoise(double x, double y, NoiseOptions opts) {
        double scale = Math.max(1, Math.round(opts.scale));
        int octaves = Math.max(1, opts.octaves);
        double sum = 0, total = 0, amp = 1, freq = 1;
        for (int o = 0; o < octaves; o++) {
            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 static double layer(double x, double y, double period, int seed) {
        double x0 = Math.floor(x), y0 = Math.floor(y);
        double u = fade(x - x0), v = fade(y - y0);
        double xa = wrap(x0, period), xb = wrap(x0 + 1, period);
        double ya = wrap(y0, period), yb = wrap(y0 + 1, period);
        double top = lerp(hash2((int) xa, (int) ya, seed), hash2((int) xb, (int) ya, seed), u);
        double bottom = lerp(hash2((int) xa, (int) yb, seed), hash2((int) xb, (int) yb, seed), u);
        return lerp(top, bottom, v);
    }

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

    // Quintic smoothstep fade — C2-continuous interpolation curve.
    private static double fade(double t) {
        return t * t * t * (t * (t * 6 - 15) + 10);
    }

    private static double lerp(double a, double b, double t) {
        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).
    static final class WorleyOptions {
        final int seed;
        final double points;

        WorleyOptions(int seed, double points) {
            this.seed = seed;
            this.points = points;
        }
    }

    // Mapped 0..1 distances to the nearest (d1) and second-nearest (d2) points.
    static final class WorleyResult {
        final double d1;
        final double d2;

        WorleyResult(double d1, double d2) {
            this.d1 = d1;
            this.d2 = d2;
        }
    }

    // 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.
    static WorleyResult worley(double x, double y, WorleyOptions opts) {
        // Wrap the query onto the unit tile first so shifted queries are exact.
        double wx = x - Math.floor(x), wy = y - Math.floor(y);
        int n = (int) Math.floor(opts.points);
        double d1 = 1, d2 = 1;
        for (int i = 0; i < n; i++) {
            double px = hash2(i, 0, opts.seed);
            double py = hash2(i, 1, opts.seed);
            double dx = Math.abs(wx - px);
            dx = Math.min(dx, 1 - dx);
            double dy = Math.abs(wy - py);
            dy = Math.min(dy, 1 - dy);
            double d = Math.sqrt(dx * dx + dy * dy) * 1.4142135623730951;
            if (d < d1) {
                d2 = d1;
                d1 = d;
            } else if (d < d2) {
                d2 = d;
            }
        }
        return new 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 →