/** * Bake 16×16 editor palette icons for procedural fluids. * Renders a 3×3 tile neighborhood and crops the center tile so noise is continuous. * * node client/scripts/write-fluid-preview-icons.mjs */ import { join } from "node:path"; import { makeRgba, writeRgbaPng } from "../../server/src/assets/placeholderPng.js"; import { DEFAULT_WATER_DEBUG, DEFAULT_LAVA_DEBUG } from "../src/play/waterDebug.js"; const TILE = 16; const GRID = 3; const BAKE = TILE * GRID; const TIME = 2.4; const root = join(import.meta.dirname, "..", "assets", "grounds"); function clamp (v, lo, hi) { return Math.min(hi, Math.max(lo, v)); } function mix (a, b, t) { return a + (b - a) * t; } function smoothstep (edge0, edge1, x) { const t = clamp((x - edge0) / Math.max(1e-6, edge1 - edge0), 0, 1); return t * t * (3 - 2 * t); } /** Compact value-noise FBM (preview-only stand-in for simplex). */ function hash3 (x, y, z) { const n = Math.sin(x * 127.1 + y * 311.7 + z * 74.7) * 43758.5453123; return n - Math.floor(n); } function valueNoise3 (x, y, z) { const x0 = Math.floor(x); const y0 = Math.floor(y); const z0 = Math.floor(z); const fx = x - x0; const fy = y - y0; const fz = z - z0; const ux = fx * fx * (3 - 2 * fx); const uy = fy * fy * (3 - 2 * fy); const uz = fz * fz * (3 - 2 * fz); const n000 = hash3(x0, y0, z0); const n100 = hash3(x0 + 1, y0, z0); const n010 = hash3(x0, y0 + 1, z0); const n110 = hash3(x0 + 1, y0 + 1, z0); const n001 = hash3(x0, y0, z0 + 1); const n101 = hash3(x0 + 1, y0, z0 + 1); const n011 = hash3(x0, y0 + 1, z0 + 1); const n111 = hash3(x0 + 1, y0 + 1, z0 + 1); const nx00 = mix(n000, n100, ux); const nx10 = mix(n010, n110, ux); const nx01 = mix(n001, n101, ux); const nx11 = mix(n011, n111, ux); const nxy0 = mix(nx00, nx10, uy); const nxy1 = mix(nx01, nx11, uy); return mix(nxy0, nxy1, uz) * 2 - 1; } function fbm3 (x, y, z) { let sum = 0; let amp = 0.5; let px = x; let py = y; let pz = z; for (let i = 0; i < 4; i++) { sum += amp * valueNoise3(px, py, pz); px *= 2; py *= 2; pz *= 2; amp *= 0.5; } return sum; } function sampleHeight (worldX, worldY, p, timeSec) { const flowX = timeSec * p.flowX; const flowY = timeSec * p.flowY; const baseX = worldX * p.waveScale + flowX; const baseY = worldY * p.waveScale + flowY; const baseZ = timeSec * p.flowZ; const eps = Math.max(p.normalEps, 0.001); const nC = fbm3(baseX, baseY, baseZ); const nL = fbm3(baseX - eps, baseY, baseZ); const nR = fbm3(baseX + eps, baseY, baseZ); const nD = fbm3(baseX, baseY - eps, baseZ); const nU = fbm3(baseX, baseY + eps, baseZ); const h = nC * p.fbmAmp; const ndotl = clamp( (-(nL - nR) * p.lightDirX + -(nD - nU) * p.lightDirY + p.normalZ * p.lightDirZ) / Math.hypot(nL - nR, nD - nU, p.normalZ), 0, 1 ); const wave = clamp(h * p.waveGain + 0.5, 0, 1); const crest = smoothstep(p.crestLo, Math.max(p.crestHi, p.crestLo + 0.01), wave) ** Math.max(p.crestPow, 0.01); let glitter = (ndotl ** Math.max(p.glitterPow, 0.01)) * crest; glitter = Math.max(glitter, (crest ** 2.5) * p.crestGlitter); return { wave, glitter, ndotl }; } function rgbBytes (r, g, b) { return [ Math.round(clamp(r, 0, 1) * 255), Math.round(clamp(g, 0, 1) * 255), Math.round(clamp(b, 0, 1) * 255), 255 ]; } function paintWaterPixel (worldX, worldY, deepAmt, p) { const { wave, glitter, ndotl } = sampleHeight(worldX, worldY, p, TIME); const darkW = clamp(p.deepDark, 0, 1); const deepCol = [ mix(0.12, mix(0.05, 0.015, darkW), deepAmt), mix(0.34, mix(0.16, 0.055, darkW), deepAmt), mix(0.54, mix(0.34, 0.14, darkW), deepAmt) ]; const midCol = [ mix(0.24, mix(0.10, 0.04, darkW), deepAmt), mix(0.52, mix(0.30, 0.14, darkW), deepAmt), mix(0.70, mix(0.50, 0.28, darkW), deepAmt) ]; const litCol = [ mix(0.52, mix(0.26, 0.12, darkW), deepAmt), mix(0.78, mix(0.46, 0.28, darkW), deepAmt), mix(0.90, mix(0.66, 0.46, darkW), deepAmt) ]; const sparkCol = [ mix(0.98, mix(0.72, 0.48, darkW), deepAmt), mix(1.0, mix(0.88, 0.68, darkW), deepAmt), mix(1.0, mix(0.98, 0.86, darkW), deepAmt) ]; const body = smoothstep(p.bodyLo, Math.max(p.bodyHi, p.bodyLo + 0.01), wave); const litAmt = smoothstep(p.litLo, Math.max(p.litHi, p.litLo + 0.01), wave) * p.litMix; let r = mix(deepCol[0], midCol[0], body); let g = mix(deepCol[1], midCol[1], body); let b = mix(deepCol[2], midCol[2], body); r = mix(r, litCol[0], litAmt); g = mix(g, litCol[1], litAmt); b = mix(b, litCol[2], litAmt); const shade = mix(p.shadeLo, p.shadeHi, ndotl); r *= shade; g *= shade; b *= shade; const spark = Math.min(glitter, p.glitterCap) * p.glitterMix; r = mix(r, sparkCol[0], spark); g = mix(g, sparkCol[1], spark); b = mix(b, sparkCol[2], spark); return rgbBytes(r, g, b); } function paintLavaPixel (worldX, worldY, p) { const { wave, glitter, ndotl } = sampleHeight(worldX, worldY, p, TIME); let heat = smoothstep(p.bodyLo, Math.max(p.bodyHi, p.bodyLo + 0.01), wave); heat = mix(heat, 1, smoothstep(p.litLo, Math.max(p.litHi, p.litLo + 0.01), wave) * p.litMix * 0.35); heat = clamp(heat + Math.min(glitter, p.glitterCap) * p.glitterMix * 0.45, 0, 1); let r = mix(0.16, 0.78, smoothstep(0, 0.38, heat)); let g = mix(0.015, 0.08, smoothstep(0, 0.38, heat)); let b = mix(0.01, 0.02, smoothstep(0, 0.38, heat)); r = mix(r, 0.98, smoothstep(0.28, 0.68, heat)); g = mix(g, 0.38, smoothstep(0.28, 0.68, heat)); b = mix(b, 0.04, smoothstep(0.28, 0.68, heat)); const yellowAmt = smoothstep(0.55, 0.98, heat); r = mix(r, 1.0, yellowAmt); g = mix(g, 0.9, yellowAmt); b = mix(b, 0.28, yellowAmt); const shade = mix(p.shadeLo, p.shadeHi, ndotl); return rgbBytes(r * shade, g * shade, b * shade); } function bakeCenterTile (paintPixel) { const full = makeRgba(BAKE, BAKE, [0, 0, 0, 255]); const origin = 1; for (let py = 0; py < BAKE; py++) { for (let px = 0; px < BAKE; px++) { const worldX = origin + px / TILE; const worldY = origin + py / TILE; full.pixels.set(paintPixel(worldX, worldY), (py * BAKE + px) * 4); } } const out = makeRgba(TILE, TILE, [0, 0, 0, 255]); for (let y = 0; y < TILE; y++) { for (let x = 0; x < TILE; x++) { const sx = TILE + x; const sy = TILE + y; const src = (sy * BAKE + sx) * 4; out.pixels.set(full.pixels.subarray(src, src + 4), (y * TILE + x) * 4); } } return out; } const waterP = { ...DEFAULT_WATER_DEBUG }; const lavaP = { ...DEFAULT_LAVA_DEBUG }; const waterIcon = bakeCenterTile((x, y) => paintWaterPixel(x, y, 0, waterP)); writeRgbaPng(join(root, "water", "water-preview.png"), TILE, TILE, waterIcon.pixels); const deepIcon = bakeCenterTile((x, y) => paintWaterPixel(x, y, Math.pow(1, waterP.deepAbrupt), waterP)); writeRgbaPng(join(root, "deep-water", "deep-water-preview.png"), TILE, TILE, deepIcon.pixels); const lavaIcon = bakeCenterTile((x, y) => paintLavaPixel(x, y, lavaP)); writeRgbaPng(join(root, "lava", "lava-preview.png"), TILE, TILE, lavaIcon.pixels); console.log("Wrote water / deep-water / lava preview icons (3×3 center crop).");