/** * Bake seamless water noise packs: * R/G = soft Perlin body layers * B/A = ridged “caustic vein” sprites for crossed glitter overlays */ 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 normalizeLayer (values) { let min = Infinity; let max = -Infinity; for (let i = 0; i < values.length; i++) { if (values[i] < min) { min = values[i]; } if (values[i] > max) { max = values[i]; } } const span = Math.max(1e-6, max - min); const out = new Uint8Array(values.length); for (let i = 0; i < values.length; i++) { out[i] = Math.round(((values[i] - min) / span) * 255); } return out; } function bakeSoft (seed, cells) { const values = new Float32Array(SIZE * SIZE); for (let y = 0; y < SIZE; y++) { for (let x = 0; x < SIZE; x++) { values[y * SIZE + x] = fbm((x / SIZE) * cells, (y / SIZE) * cells, cells, seed); } } return normalizeLayer(values); } /** Ridged / vein caustic: bright thin networks on dark. */ function bakeCaustic (seed, cells) { const values = new Float32Array(SIZE * SIZE); for (let y = 0; y < SIZE; y++) { for (let x = 0; x < SIZE; x++) { const nx = (x / SIZE) * cells; const ny = (y / SIZE) * cells; const n1 = fbm(nx, ny, cells, seed, 5); const n2 = fbm(nx + 3.1, ny - 1.7, cells, seed + 17.3, 4); const ridge = 1 - Math.abs(n1 * 2 - 1); const ridge2 = 1 - Math.abs(n2 * 2 - 1); const veins = Math.pow(ridge, 3.2) * 0.75 + Math.pow(ridge * ridge2, 2.4) * 0.55; values[y * SIZE + x] = veins; } } return normalizeLayer(values); } function writePackedNoise (file, softSeeds, causticSeeds) { const r = bakeSoft(softSeeds[0], 4); const g = bakeSoft(softSeeds[1], 5); const b = bakeCaustic(causticSeeds[0], 7); const a = bakeCaustic(causticSeeds[1], 8); 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] = a[i]; } writeRgbaPng(file, SIZE, SIZE, image.pixels); } writePackedNoise( join(root, "grounds", "water", "water.png"), [11.3, 27.9], [101.4, 133.8] ); writePackedNoise( join(root, "grounds", "deep-water", "deep-water.png"), [53.7, 67.1], [157.2, 191.6] ); console.log(`Wrote ${SIZE}x${SIZE} RGBA soft+caustic packs for water + deep-water.`);