/** * World-space tile noise for subtle nature-ground tint (1 sample per tile). * Uses chunk+local indices so adjacent chunks share a continuous field. */ import { CHUNK_SIZE } from "../protocol/constants.js"; /** Locked look from playtest (scale / contrast / tint / darken). */ export const NATURE_TINT = Object.freeze({ scale: 0.17, contrast: 1.65, tint: 0.31, darken: 0.16 }); /** Shader uniforms for atlas nature tint (ground only). */ export function natureTintUniforms () { return { u_useNature: 1, u_natureTint: NATURE_TINT.tint, u_natureDark: NATURE_TINT.darken, u_natureContrast: NATURE_TINT.contrast }; } /** Noise texture includes a 1-tile halo so soft blend crosses chunk edges. */ export const NATURE_NOISE_PAD = 1; export const NATURE_NOISE_SIZE = CHUNK_SIZE + NATURE_NOISE_PAD * 2; 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 18×18 fills). */ export function natureNoise01 (worldTileX, worldTileY, scale = NATURE_TINT.scale) { const s = Math.max(0.001, Number(scale) || NATURE_TINT.scale); 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)); }