/** * Bake wall textures onto thick/thin greyscale face-id shape masks. * * Shapes (source of truth): * client/assets/blocks/_examples/stone-wall/{thick,thin}-stone-wall*.png * * Usage: * node client/scripts/texture-wall-sprites.mjs --variant=cobble|smooth|brick|wood|cliff * node client/scripts/texture-wall-sprites.mjs --variant=all * * cobble → thick-stone-wall / thin-stone-wall (named) * smooth → thick-smooth-stone-wall / thin-smooth-stone-wall (named) * brick → thick-brick-wall / thin-brick-wall (named) * wood → thick-wood-wall / thin-wood-wall (magenta masks 0–15) * cliff → thick-cliff / thin-cliff (dirt sides + magenta top, named) */ import { mkdirSync, writeFileSync, readdirSync, readFileSync, existsSync } from "node:fs"; import { deflateSync, inflateSync } from "node:zlib"; import { fileURLToPath } from "node:url"; import { dirname, join } from "node:path"; import { cardinalConnectionSuffix } from "../../shared/world/neighborConnect.js"; const SIZE = 16; const ROOT = join(dirname(fileURLToPath(import.meta.url)), "../assets/blocks"); const EXAMPLES = join(ROOT, "_examples/stone-wall"); const GROUNDS = join(dirname(fileURLToPath(import.meta.url)), "../assets/grounds"); const THICK_ROLE = { 0xaf: "N", 0x95: "W", 0x80: "T", 0x6c: "E", 0x54: "S", 0x40: "O" }; const THIN_ROLE = { 0xb6: "W", 0x95: "N", 0x80: "E", 0x73: "S", 0x40: "O" }; function roleOf (prefix, g) { const map = prefix.startsWith("thin") ? THIN_ROLE : THICK_ROLE; return map[g] ?? null; } function isTopRole (role) { return role === "T" || role === "N"; } function crcTable () { const table = new Uint32Array(256); for (let n = 0; n < 256; n++) { let c = n; for (let k = 0; k < 8; k++) { c = (c & 1) ? (0xedb88320 ^ (c >>> 1)) : (c >>> 1); } table[n] = c >>> 0; } return table; } const CRC_TABLE = crcTable(); function crc32 (buf) { let c = 0xffffffff; for (let i = 0; i < buf.length; i++) { c = CRC_TABLE[(c ^ buf[i]) & 0xff] ^ (c >>> 8); } return (c ^ 0xffffffff) >>> 0; } function chunk (type, data) { const len = Buffer.alloc(4); len.writeUInt32BE(data.length); const typeBuf = Buffer.from(type, "ascii"); const crcBuf = Buffer.alloc(4); crcBuf.writeUInt32BE(crc32(Buffer.concat([typeBuf, data]))); return Buffer.concat([len, typeBuf, data, crcBuf]); } function encodePng (rgba) { const raw = Buffer.alloc((SIZE * 4 + 1) * SIZE); for (let y = 0; y < SIZE; y++) { const row = y * (SIZE * 4 + 1); raw[row] = 0; for (let x = 0; x < SIZE; x++) { const i = (y * SIZE + x) * 4; const o = row + 1 + x * 4; raw[o] = rgba[i]; raw[o + 1] = rgba[i + 1]; raw[o + 2] = rgba[i + 2]; raw[o + 3] = rgba[i + 3]; } } const ihdr = Buffer.alloc(13); ihdr.writeUInt32BE(SIZE, 0); ihdr.writeUInt32BE(SIZE, 4); ihdr[8] = 8; ihdr[9] = 6; return Buffer.concat([ Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]), chunk("IHDR", ihdr), chunk("IDAT", deflateSync(raw, { level: 9 })), chunk("IEND", Buffer.alloc(0)) ]); } function decodePngRgba (path) { const buf = readFileSync(path); let off = 8; let w = 0; let h = 0; const idat = []; while (off < buf.length) { const len = buf.readUInt32BE(off); const type = buf.slice(off + 4, off + 8).toString("ascii"); const data = buf.slice(off + 8, off + 8 + len); if (type === "IHDR") { w = data.readUInt32BE(0); h = data.readUInt32BE(4); } else if (type === "IDAT") { idat.push(data); } else if (type === "IEND") { break; } off += 12 + len; } const inflated = inflateSync(Buffer.concat(idat)); const rgba = new Uint8Array(w * h * 4); const bpp = 4; let src = 0; for (let y = 0; y < h; y++) { const filter = inflated[src++]; const row = inflated.subarray(src, src + w * bpp); src += w * bpp; const prev = y > 0 ? rgba.subarray((y - 1) * w * bpp, y * w * bpp) : null; const out = rgba.subarray(y * w * bpp, (y + 1) * w * bpp); for (let i = 0; i < row.length; i++) { let v = row[i]; if (filter === 1) { v = (v + (i >= bpp ? out[i - bpp] : 0)) & 255; } else if (filter === 2) { v = (v + (prev ? prev[i] : 0)) & 255; } else if (filter === 3) { const a = i >= bpp ? out[i - bpp] : 0; const b = prev ? prev[i] : 0; v = (v + Math.floor((a + b) / 2)) & 255; } else if (filter === 4) { const a = i >= bpp ? out[i - bpp] : 0; const b = prev ? prev[i] : 0; const c = prev && i >= bpp ? prev[i - bpp] : 0; const p = a + b - c; const pa = Math.abs(p - a); const pb = Math.abs(p - b); const pc = Math.abs(p - c); v = (v + (pa <= pb && pa <= pc ? a : pb <= pc ? b : c)) & 255; } out[i] = v; } } if (w !== SIZE || h !== SIZE) { throw new Error(`${path}: expected ${SIZE}×${SIZE}, got ${w}×${h}`); } return rgba; } function hash2 (x, y) { let n = (x * 374761393 + y * 668265263) | 0; n = (n ^ (n >>> 13)) * 1274126177; return (n ^ (n >>> 16)) >>> 0; } function rgba (r, g, b, a = 255) { return [r, g, b, a]; } function mulRgb (c, factor) { return rgba( Math.min(255, Math.round(c[0] * factor)), Math.min(255, Math.round(c[1] * factor)), Math.min(255, Math.round(c[2] * factor)), c[3] ?? 255 ); } function mixRgb (a, b, t) { return rgba( Math.round(a[0] + (b[0] - a[0]) * t), Math.round(a[1] + (b[1] - a[1]) * t), Math.round(a[2] + (b[2] - a[2]) * t), 255 ); } /** Face role → lighting factor (NW light). */ const ROLE_LIT = { N: 1.15, W: 1.0, T: 1.05, E: 0.82, S: 0.62, O: 0.45 }; function faceLit (role) { return ROLE_LIT[role] ?? 0.9; } function loadDirt () { const path = join(GROUNDS, "dirt/dirt.png"); if (!existsSync(path)) { throw new Error(`Missing dirt texture: ${path}`); } return decodePngRgba(path); } let dirtRgba = null; function dirtAt (x, y) { if (!dirtRgba) { dirtRgba = loadDirt(); } const i = ((y & 15) * SIZE + (x & 15)) * 4; return rgba(dirtRgba[i], dirtRgba[i + 1], dirtRgba[i + 2]); } /* ---- palettes ---- */ const COBBLE = { N: rgba(198, 198, 202), W: rgba(132, 132, 136), T: rgba(168, 168, 172), E: rgba(112, 112, 116), S: rgba(88, 88, 92), O: rgba(70, 70, 74), mortar: rgba(78, 78, 82), hiN: rgba(210, 210, 214), loS: rgba(78, 78, 82) }; const BRICK = { N: rgba(196, 92, 72), W: rgba(168, 72, 56), T: rgba(178, 78, 60), E: rgba(148, 58, 46), S: rgba(118, 48, 38), O: rgba(72, 36, 30), mortar: rgba(92, 78, 68), hi: rgba(210, 110, 88), lo: rgba(100, 42, 34) }; function cobbleLike (basePal, x, y, role, gritMask) { if (role === "O") { const h = hash2(x, y); return (h & 15) === 0 ? basePal.mortar : basePal.O; } const base = basePal[role] ?? basePal.T; const h = hash2(x, y); const speck = h & gritMask; if (speck === 0) { return basePal.mortar; } if (gritMask === 7) { if (speck === 1 || speck === 2) { return role === "N" || role === "T" ? (basePal.hiN ?? basePal.hi ?? mulRgb(base, 1.12)) : (basePal.hi ?? mulRgb(base, 1.1)); } if (speck === 3) { return role === "S" ? (basePal.loS ?? basePal.lo ?? mulRgb(base, 0.88)) : (basePal.lo ?? mulRgb(base, 0.9)); } } else if (gritMask === 31) { // Smooth: rare soft variation only. if (speck === 1) { return mulRgb(base, 1.06); } if (speck === 2) { return mulRgb(base, 0.94); } } return base; } function brickShade (x, y, role) { if (role === "O") { return BRICK.O; } const row = Math.floor(y / 4); const brickX = x + (row & 1) * 3; const onHMortar = (y % 4) === 0; const onVMortar = (brickX % 6) === 0; if (onHMortar || onVMortar) { return BRICK.mortar; } const h = hash2(x, y); if ((h & 15) === 0) { return BRICK.hi; } if ((h & 15) === 1) { return BRICK.lo; } return BRICK[role] ?? BRICK.T; } function woodMagenta (role) { // R≈B, G name.startsWith(shapeKey) && name.endsWith(".png") && !name.includes("ref-")) .sort(); } function namedOutName (outPrefix, shapeName, shapeKey) { // thick-stone-wall-esw.png → thick-smooth-stone-wall-esw.png const suffix = shapeName.slice(shapeKey.length); // "" or "-esw" return `${outPrefix}${suffix}`; } function bakeVariant (variant) { const cfg = VARIANT_OUT[variant]; if (!cfg) { throw new Error(`Unknown variant: ${variant}`); } let wrote = 0; for (const kind of ["thick", "thin"]) { const shapeKey = `${kind}-stone-wall`; const out = cfg[kind]; const outDir = join(ROOT, out.dir); mkdirSync(outDir, { recursive: true }); const files = shapeFiles(shapeKey); if (files.length !== 16) { throw new Error(`Expected 16 ${shapeKey} masks, found ${files.length}`); } if (out.named) { for (const name of files) { const shaped = decodePngRgba(join(EXAMPLES, name)); const textured = textureMask(shaped, variant, shapeKey); const outName = namedOutName(out.prefix, name, shapeKey); writeFileSync(join(outDir, outName), encodePng(textured)); wrote++; } } else { // Material-connect: write 0.png … 15.png from matching named shapes. for (let mask = 0; mask < 16; mask++) { const suffix = cardinalConnectionSuffix(mask); const shapeName = suffix ? `${shapeKey}-${suffix}.png` : `${shapeKey}.png`; const shaped = decodePngRgba(join(EXAMPLES, shapeName)); const textured = textureMask(shaped, variant, shapeKey); writeFileSync(join(outDir, `${mask}.png`), encodePng(textured)); wrote++; } } console.log(` ${variant} ${kind} → ${outDir} (${out.named ? "named" : "0–15"})`); } return wrote; } const arg = process.argv.find((a) => a.startsWith("--variant=")); const variant = arg ? arg.slice("--variant=".length) : "cobble"; const list = variant === "all" ? ["cobble", "smooth", "brick", "wood", "cliff"] : [variant]; if (!existsSync(EXAMPLES)) { throw new Error(`Missing ${EXAMPLES}`); } let total = 0; for (const v of list) { console.log(`Baking ${v}…`); total += bakeVariant(v); } console.log(`Done (${total} sprites).`);