| 1 | /** |
| 2 | * World-space tile noise for subtle nature-ground tint (1 sample per tile). |
| 3 | * Uses chunk+local indices so adjacent chunks share a continuous field. |
| 4 | */ |
| 5 | |
| 6 | import { CHUNK_SIZE } from "../protocol/constants.js"; |
| 7 | |
| 8 | /** Locked look from playtest (scale / contrast / tint / darken). */ |
| 9 | export const NATURE_TINT = Object.freeze({ |
| 10 | scale: 0.17, |
| 11 | contrast: 1.65, |
| 12 | tint: 0.31, |
| 13 | darken: 0.16 |
| 14 | }); |
| 15 | |
| 16 | /** Shader uniforms for atlas nature tint (ground only). */ |
| 17 | export function natureTintUniforms () |
| 18 | { |
| 19 | return { |
| 20 | u_useNature: 1, |
| 21 | u_natureTint: NATURE_TINT.tint, |
| 22 | u_natureDark: NATURE_TINT.darken, |
| 23 | u_natureContrast: NATURE_TINT.contrast |
| 24 | }; |
| 25 | } |
| 26 | |
| 27 | /** Noise texture includes a 1-tile halo so soft blend crosses chunk edges. */ |
| 28 | export const NATURE_NOISE_PAD = 1; |
| 29 | export const NATURE_NOISE_SIZE = CHUNK_SIZE + NATURE_NOISE_PAD * 2; |
| 30 | |
| 31 | function fade (t) |
| 32 | { |
| 33 | return t * t * t * (t * (t * 6 - 15) + 10); |
| 34 | } |
| 35 | |
| 36 | function lerp (a, b, t) |
| 37 | { |
| 38 | return a + (b - a) * t; |
| 39 | } |
| 40 | |
| 41 | function hash2 (ix, iy) |
| 42 | { |
| 43 | const n = Math.sin(ix * 127.1 + iy * 311.7) * 43758.5453123; |
| 44 | return n - Math.floor(n); |
| 45 | } |
| 46 | |
| 47 | /** Gradient-style value at integer lattice. */ |
| 48 | function gradDot (ix, iy, x, y) |
| 49 | { |
| 50 | const angle = hash2(ix, iy) * Math.PI * 2; |
| 51 | return Math.cos(angle) * x + Math.sin(angle) * y; |
| 52 | } |
| 53 | |
| 54 | /** Classic 2D Perlin in roughly [-1, 1]. */ |
| 55 | export function perlin2 (x, y) |
| 56 | { |
| 57 | const x0 = Math.floor(x); |
| 58 | const y0 = Math.floor(y); |
| 59 | const fx = x - x0; |
| 60 | const fy = y - y0; |
| 61 | const u = fade(fx); |
| 62 | const v = fade(fy); |
| 63 | const n00 = gradDot(x0, y0, fx, fy); |
| 64 | const n10 = gradDot(x0 + 1, y0, fx - 1, fy); |
| 65 | const n01 = gradDot(x0, y0 + 1, fx, fy - 1); |
| 66 | const n11 = gradDot(x0 + 1, y0 + 1, fx - 1, fy - 1); |
| 67 | return lerp(lerp(n00, n10, u), lerp(n01, n11, u), v); |
| 68 | } |
| 69 | |
| 70 | /** 0..1 FBM of Perlin (2 octaves — cheap for 18×18 fills). */ |
| 71 | export function natureNoise01 (worldTileX, worldTileY, scale = NATURE_TINT.scale) |
| 72 | { |
| 73 | const s = Math.max(0.001, Number(scale) || NATURE_TINT.scale); |
| 74 | const x = worldTileX * s; |
| 75 | const y = worldTileY * s; |
| 76 | let sum = perlin2(x, y); |
| 77 | sum += 0.5 * perlin2(x * 2.0 + 19.1, y * 2.0 - 7.3); |
| 78 | return Math.min(1, Math.max(0, sum * 0.5 + 0.5)); |
| 79 | } |