/** * World-space tile noise for subtle nature-ground tint (1 sample per tile). * Uses chunk+local indices so adjacent chunks share a continuous field. */ function fade (t) { return t * t * t * (t * (t * 6 - 15) + 10); } function lerp (a, b, t) { return a + (b - a) * t; } function hash2 (ix, iy) { const n = Math.sin(ix * 127.1 + iy * 311.7) * 43758.5453123; return n - Math.floor(n); } /** Gradient-style value at integer lattice. */ function gradDot (ix, iy, x, y) { const angle = hash2(ix, iy) * Math.PI * 2; return Math.cos(angle) * x + Math.sin(angle) * y; } /** Classic 2D Perlin in roughly [-1, 1]. */ export function perlin2 (x, y) { const x0 = Math.floor(x); const y0 = Math.floor(y); const fx = x - x0; const fy = y - y0; const u = fade(fx); const v = fade(fy); const n00 = gradDot(x0, y0, fx, fy); const n10 = gradDot(x0 + 1, y0, fx - 1, fy); const n01 = gradDot(x0, y0 + 1, fx, fy - 1); const n11 = gradDot(x0 + 1, y0 + 1, fx - 1, fy - 1); return lerp(lerp(n00, n10, u), lerp(n01, n11, u), v); } /** 0..1 FBM of Perlin (2 octaves — cheap for 16×16 fills). */ export function natureNoise01 (worldTileX, worldTileY, scale = 0.08) { const s = Math.max(0.001, Number(scale) || 0.08); const x = worldTileX * s; const y = worldTileY * s; let sum = perlin2(x, y); sum += 0.5 * perlin2(x * 2.0 + 19.1, y * 2.0 - 7.3); return Math.min(1, Math.max(0, sum * 0.5 + 0.5)); }