/** * Bake grey cobblestone texture onto thick/thin stone-wall shape masks. * * Shape source of truth (greyscale face-IDs): * client/assets/blocks/_examples/stone-wall/{thick,thin}-stone-wall*.png * * Textured output (gameplay sprites): * client/assets/blocks/thick-stone-wall/ * client/assets/blocks/thin-stone-wall/ * * Face greys (see docs/STONE_WALL_SHAPES.md) pick a cobble base luminance; * a stable hash adds mortar/highlight speckles inside each face. * * node client/scripts/texture-stone-wall-sprites.mjs */ 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"; const SIZE = 16; const ROOT = join(dirname(fileURLToPath(import.meta.url)), "../assets/blocks"); const EXAMPLES = join(ROOT, "_examples/stone-wall"); const OUT = { "thick-stone-wall": join(ROOT, "thick-stone-wall"), "thin-stone-wall": join(ROOT, "thin-stone-wall") }; /** Gray cobble palette (cool stone; no magenta). */ const TOP = [168, 168, 172, 255]; const TOP_HI = [198, 198, 202, 255]; const TOP_LO = [140, 140, 144, 255]; const FACE = [112, 112, 116, 255]; const FACE_HI = [132, 132, 136, 255]; const FACE_LO = [88, 88, 92, 255]; const EDGE = [70, 70, 74, 255]; const MORTAR = [78, 78, 82, 255]; /** * Face-id grey → cobble base (directional light from NW). * Thick and thin use slightly different vocabularies (thin frustum). */ const THICK_ROLE_BASE = new Map([ [0xaf, TOP_HI], // north bevel [0x95, FACE_HI], // west face [0x80, TOP], // top plateau [0x6c, FACE], // east face [0x54, FACE_LO], // south face [0x40, EDGE] // outline / crease ]); const THIN_ROLE_BASE = new Map([ [0xb6, FACE_HI], // west face (bright) [0x95, TOP_HI], // north rim / ridge top [0x80, FACE], // east face [0x73, FACE_LO], // south / front body [0x40, EDGE] ]); function roleBaseFor (prefix) { return prefix === "thin-stone-wall" ? THIN_ROLE_BASE : THICK_ROLE_BASE; } /** Fallback: map arbitrary grey luminance into the cobble range. */ function baseFromLuminance (g) { if (g <= 0x48) { return EDGE; } if (g <= 0x60) { return FACE_LO; } if (g <= 0x78) { return FACE; } if (g <= 0x90) { return FACE_HI; } if (g <= 0xa8) { return TOP; } return TOP_HI; } 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 cobbleShade (base, x, y, isOutline) { if (isOutline) { // Keep creases mostly solid so silhouettes stay crisp. const h = hash2(x, y); return (h & 15) === 0 ? MORTAR : EDGE; } const h = hash2(x, y); const speck = h & 7; if (speck === 0) { return MORTAR; } if (speck === 1 || speck === 2) { return base === TOP || base === TOP_HI || base === TOP_LO ? TOP_HI : FACE_HI; } if (speck === 3) { return base === TOP || base === TOP_HI || base === TOP_LO ? TOP_LO : FACE_LO; } return base; } function textureMask (rgba, roleBase) { const out = new Uint8Array(SIZE * SIZE * 4); for (let y = 0; y < SIZE; y++) { for (let x = 0; x < SIZE; x++) { const i = (y * SIZE + x) * 4; const a = rgba[i + 3]; if (a === 0) { continue; } const g = rgba[i]; const base = roleBase.get(g) ?? baseFromLuminance(g); const color = cobbleShade(base, x, y, g === 0x40); out[i] = color[0]; out[i + 1] = color[1]; out[i + 2] = color[2]; out[i + 3] = 255; } } return out; } function shapeFiles (prefix) { if (!existsSync(EXAMPLES)) { throw new Error(`Missing examples folder: ${EXAMPLES}`); } return readdirSync(EXAMPLES) .filter((name) => name.startsWith(prefix) && name.endsWith(".png")) .sort(); } let wrote = 0; for (const [prefix, outDir] of Object.entries(OUT)) { mkdirSync(outDir, { recursive: true }); const files = shapeFiles(prefix); if (files.length !== 16) { throw new Error( `Expected 16 ${prefix} masks in ${EXAMPLES}, found ${files.length}` ); } const roleBase = roleBaseFor(prefix); for (const name of files) { const shaped = decodePngRgba(join(EXAMPLES, name)); const textured = textureMask(shaped, roleBase); writeFileSync(join(outDir, name), encodePng(textured)); wrote++; } console.log(`Textured ${files.length} → ${outDir}`); } console.log(`Done (${wrote} sprites). Shapes left greyscale in ${EXAMPLES}`);