/** * Bake seamless multi-octave value-noise layers into water ground PNGs. * R/G/B = three independent tiling fields for sliding additive caustics. */ import { join } from "node:path"; import { makeRgba, writeRgbaPng } from "../../server/src/assets/placeholderPng.js"; const SIZE = 64; const root = join(import.meta.dirname, "..", "assets"); function wrapInt (v, period) { const m = ((v % period) + period) % period; return m | 0; } function hash2 (ix, iy, seed) { const n = Math.sin(ix * 127.1 + iy * 311.7 + seed * 74.7) * 43758.5453123; return n - Math.floor(n); } function valueNoise (x, y, period, seed) { const x0 = Math.floor(x); const y0 = Math.floor(y); const fx = x - x0; const fy = y - y0; const ux = fx * fx * (3 - 2 * fx); const uy = fy * fy * (3 - 2 * fy); const a = hash2(wrapInt(x0, period), wrapInt(y0, period), seed); const b = hash2(wrapInt(x0 + 1, period), wrapInt(y0, period), seed); const c = hash2(wrapInt(x0, period), wrapInt(y0 + 1, period), seed); const d = hash2(wrapInt(x0 + 1, period), wrapInt(y0 + 1, period), seed); return a + (b - a) * ux + (c - a) * uy + (a - b - c + d) * ux * uy; } function fbm (x, y, period, seed, octaves = 4) { let amp = 0.5; let freq = 1; let sum = 0; let norm = 0; for (let i = 0; i < octaves; i++) { const p = Math.max(2, Math.round(period * freq)); sum += valueNoise(x * freq, y * freq, p, seed + i * 19.1) * amp; norm += amp; amp *= 0.5; freq *= 2; } return sum / norm; } function bakeLayer (seed, cells) { const values = new Float32Array(SIZE * SIZE); let min = Infinity; let max = -Infinity; for (let y = 0; y < SIZE; y++) { for (let x = 0; x < SIZE; x++) { const v = fbm((x / SIZE) * cells, (y / SIZE) * cells, cells, seed); values[y * SIZE + x] = v; if (v < min) { min = v; } if (v > max) { max = v; } } } const span = Math.max(1e-6, max - min); const out = new Uint8Array(SIZE * SIZE); for (let i = 0; i < values.length; i++) { out[i] = Math.round(((values[i] - min) / span) * 255); } return out; } function writePackedNoise (file, seeds) { const r = bakeLayer(seeds[0], 4); const g = bakeLayer(seeds[1], 5); const b = bakeLayer(seeds[2], 6); const image = makeRgba(SIZE, SIZE, [0, 0, 0, 255]); for (let i = 0; i < SIZE * SIZE; i++) { const o = i * 4; image.pixels[o] = r[i]; image.pixels[o + 1] = g[i]; image.pixels[o + 2] = b[i]; image.pixels[o + 3] = 255; } writeRgbaPng(file, SIZE, SIZE, image.pixels); } writePackedNoise(join(root, "grounds", "water", "water.png"), [11.3, 27.9, 41.2]); writePackedNoise(join(root, "grounds", "deep-water", "deep-water.png"), [53.7, 67.1, 89.4]); console.log(`Wrote ${SIZE}x${SIZE} RGB tiling Perlin packs for water + deep-water.`);