Seamless Texture — C# source
Make images tile seamlessly or generate tileable noise textures — entirely in your browser. Nothing uploads.
This is the C# implementation — the same logic the interactive tool runs, in a shareable, citable form.
// seamless-texture — tileable-texture math, C# port: wrap-blend weights,
// integer lattice hash, wrapping value noise, torus Worley. .NET stdlib
// only. C# 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 — the default
// (unchecked) context wraps on overflow like the TS.
using System;
public static 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.
public 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 — the default unchecked int
/// multiply has Math.imul's exact truncating semantics.
static int Imul(int a, int b) { return a * b; }
/// The JS `h >>> n`: a logical right shift of the int32 bit pattern.
static int Shr(int h, int n) { return (int)((uint)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.
public static double Hash2(int x, int y, int seed)
{
int h = Imul(x, unchecked((int)0x9e3779b1)) ^ Imul(y, unchecked((int)0x85ebca77)) ^ Imul(seed, unchecked((int)0xc2b2ae35));
h ^= Shr(h, 15);
h = Imul(h, unchecked((int)0x2c1b3c6d));
h ^= Shr(h, 12);
h = Imul(h, unchecked((int)0x297a2d39));
h ^= Shr(h, 15);
return (uint)h / 4294967296.0;
}
/// Mirrors the TS NoiseOptions: `scale` is the tile period in pixels;
/// `octaves` < 1 snaps to 1 (the default included).
public struct NoiseOptions
{
public int Seed;
public double Scale;
public int Octaves;
public NoiseOptions(int seed, double scale, int octaves = 1)
{
Seed = seed;
Scale = scale;
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.
/// Floor(v + 0.5) is the JS Math.round (half up); Math.Round is
/// banker's rounding and must not be used here.
public static double ValueNoise(double x, double y, NoiseOptions opts)
{
double scale = Math.Max(1, Math.Floor(opts.Scale + 0.5));
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.
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.
static double Wrap(double i, double period)
{
return ((i % period) + period) % period;
}
/// Quintic smoothstep fade — C2-continuous interpolation curve.
static double Fade(double t)
{
return t * t * t * (t * (t * 6 - 15) + 10);
}
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).
public struct WorleyOptions
{
public int Seed;
public double Points;
public WorleyOptions(int seed, double points)
{
Seed = seed;
Points = points;
}
}
/// Mapped 0..1 distances to the nearest (D1) and second-nearest (D2) points.
public struct WorleyResult
{
public double D1;
public double 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.
public 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);
var r = new WorleyResult { 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 < r.D1)
{
r.D2 = r.D1;
r.D1 = d;
}
else if (d < r.D2)
{
r.D2 = d;
}
}
return r;
}
}
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 →