BlueGit

timfa questward

Files

questward / client / scripts / texture-stone-wall-sprites.mjs

texture-stone-wall-sprites.mjs

wall texture
timfa Tim Falken · pushed by timfa · 2 months ago
0896e8da History
texture-stone-wall-sprites.mjs 7.4 KiB Download Raw
1/**
2 * Bake grey cobblestone texture onto thick/thin stone-wall shape masks.
3 *
4 * Shape source of truth (greyscale face-IDs):
5 * client/assets/blocks/_examples/stone-wall/{thick,thin}-stone-wall*.png
6 *
7 * Textured output (gameplay sprites):
8 * client/assets/blocks/thick-stone-wall/
9 * client/assets/blocks/thin-stone-wall/
10 *
11 * Face greys (see docs/STONE_WALL_SHAPES.md) pick a cobble base luminance;
12 * a stable hash adds mortar/highlight speckles inside each face.
13 *
14 * node client/scripts/texture-stone-wall-sprites.mjs
15 */
16import { mkdirSync, writeFileSync, readdirSync, readFileSync, existsSync } from "node:fs";
17import { deflateSync, inflateSync } from "node:zlib";
18import { fileURLToPath } from "node:url";
19import { dirname, join } from "node:path";
20
21const SIZE = 16;
22const ROOT = join(dirname(fileURLToPath(import.meta.url)), "../assets/blocks");
23const EXAMPLES = join(ROOT, "_examples/stone-wall");
24const OUT = {
25 "thick-stone-wall": join(ROOT, "thick-stone-wall"),
26 "thin-stone-wall": join(ROOT, "thin-stone-wall")
27};
28
29/** Gray cobble palette (cool stone; no magenta). */
30const TOP = [168, 168, 172, 255];
31const TOP_HI = [198, 198, 202, 255];
32const TOP_LO = [140, 140, 144, 255];
33const FACE = [112, 112, 116, 255];
34const FACE_HI = [132, 132, 136, 255];
35const FACE_LO = [88, 88, 92, 255];
36const EDGE = [70, 70, 74, 255];
37const MORTAR = [78, 78, 82, 255];
38
39/**
40 * Face-id grey → cobble base (directional light from NW).
41 * Thick and thin use slightly different vocabularies (thin frustum).
42 */
43const THICK_ROLE_BASE = new Map([
44 [0xaf, TOP_HI], // north bevel
45 [0x95, FACE_HI], // west face
46 [0x80, TOP], // top plateau
47 [0x6c, FACE], // east face
48 [0x54, FACE_LO], // south face
49 [0x40, EDGE] // outline / crease
50]);
51
52const THIN_ROLE_BASE = new Map([
53 [0xb6, FACE_HI], // west face (bright)
54 [0x95, TOP_HI], // north rim / ridge top
55 [0x80, FACE], // east face
56 [0x73, FACE_LO], // south / front body
57 [0x40, EDGE]
58]);
59
60function roleBaseFor (prefix)
61{
62 return prefix === "thin-stone-wall" ? THIN_ROLE_BASE : THICK_ROLE_BASE;
63}
64
65/** Fallback: map arbitrary grey luminance into the cobble range. */
66function baseFromLuminance (g)
67{
68 if (g <= 0x48)
69 { return EDGE; }
70 if (g <= 0x60)
71 { return FACE_LO; }
72 if (g <= 0x78)
73 { return FACE; }
74 if (g <= 0x90)
75 { return FACE_HI; }
76 if (g <= 0xa8)
77 { return TOP; }
78 return TOP_HI;
79}
80
81function crcTable ()
82{
83 const table = new Uint32Array(256);
84 for (let n = 0; n < 256; n++)
85 {
86 let c = n;
87 for (let k = 0; k < 8; k++)
88 { c = (c & 1) ? (0xedb88320 ^ (c >>> 1)) : (c >>> 1); }
89 table[n] = c >>> 0;
90 }
91 return table;
92}
93
94const CRC_TABLE = crcTable();
95
96function crc32 (buf)
97{
98 let c = 0xffffffff;
99 for (let i = 0; i < buf.length; i++)
100 { c = CRC_TABLE[(c ^ buf[i]) & 0xff] ^ (c >>> 8); }
101 return (c ^ 0xffffffff) >>> 0;
102}
103
104function chunk (type, data)
105{
106 const len = Buffer.alloc(4);
107 len.writeUInt32BE(data.length);
108 const typeBuf = Buffer.from(type, "ascii");
109 const crcBuf = Buffer.alloc(4);
110 crcBuf.writeUInt32BE(crc32(Buffer.concat([typeBuf, data])));
111 return Buffer.concat([len, typeBuf, data, crcBuf]);
112}
113
114function encodePng (rgba)
115{
116 const raw = Buffer.alloc((SIZE * 4 + 1) * SIZE);
117 for (let y = 0; y < SIZE; y++)
118 {
119 const row = y * (SIZE * 4 + 1);
120 raw[row] = 0;
121 for (let x = 0; x < SIZE; x++)
122 {
123 const i = (y * SIZE + x) * 4;
124 const o = row + 1 + x * 4;
125 raw[o] = rgba[i];
126 raw[o + 1] = rgba[i + 1];
127 raw[o + 2] = rgba[i + 2];
128 raw[o + 3] = rgba[i + 3];
129 }
130 }
131 const ihdr = Buffer.alloc(13);
132 ihdr.writeUInt32BE(SIZE, 0);
133 ihdr.writeUInt32BE(SIZE, 4);
134 ihdr[8] = 8;
135 ihdr[9] = 6;
136 return Buffer.concat([
137 Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]),
138 chunk("IHDR", ihdr),
139 chunk("IDAT", deflateSync(raw, { level: 9 })),
140 chunk("IEND", Buffer.alloc(0))
141 ]);
142}
143
144function decodePngRgba (path)
145{
146 const buf = readFileSync(path);
147 let off = 8;
148 let w = 0;
149 let h = 0;
150 const idat = [];
151 while (off < buf.length)
152 {
153 const len = buf.readUInt32BE(off);
154 const type = buf.slice(off + 4, off + 8).toString("ascii");
155 const data = buf.slice(off + 8, off + 8 + len);
156 if (type === "IHDR")
157 {
158 w = data.readUInt32BE(0);
159 h = data.readUInt32BE(4);
160 }
161 else if (type === "IDAT")
162 { idat.push(data); }
163 else if (type === "IEND")
164 { break; }
165 off += 12 + len;
166 }
167 const inflated = inflateSync(Buffer.concat(idat));
168 const rgba = new Uint8Array(w * h * 4);
169 const bpp = 4;
170 let src = 0;
171 for (let y = 0; y < h; y++)
172 {
173 const filter = inflated[src++];
174 const row = inflated.subarray(src, src + w * bpp);
175 src += w * bpp;
176 const prev = y > 0 ? rgba.subarray((y - 1) * w * bpp, y * w * bpp) : null;
177 const out = rgba.subarray(y * w * bpp, (y + 1) * w * bpp);
178 for (let i = 0; i < row.length; i++)
179 {
180 let v = row[i];
181 if (filter === 1)
182 { v = (v + (i >= bpp ? out[i - bpp] : 0)) & 255; }
183 else if (filter === 2)
184 { v = (v + (prev ? prev[i] : 0)) & 255; }
185 else if (filter === 3)
186 {
187 const a = i >= bpp ? out[i - bpp] : 0;
188 const b = prev ? prev[i] : 0;
189 v = (v + Math.floor((a + b) / 2)) & 255;
190 }
191 else if (filter === 4)
192 {
193 const a = i >= bpp ? out[i - bpp] : 0;
194 const b = prev ? prev[i] : 0;
195 const c = prev && i >= bpp ? prev[i - bpp] : 0;
196 const p = a + b - c;
197 const pa = Math.abs(p - a);
198 const pb = Math.abs(p - b);
199 const pc = Math.abs(p - c);
200 v = (v + (pa <= pb && pa <= pc ? a : pb <= pc ? b : c)) & 255;
201 }
202 out[i] = v;
203 }
204 }
205 if (w !== SIZE || h !== SIZE)
206 { throw new Error(`${path}: expected ${SIZE}×${SIZE}, got ${w}×${h}`); }
207 return rgba;
208}
209
210function hash2 (x, y)
211{
212 let n = (x * 374761393 + y * 668265263) | 0;
213 n = (n ^ (n >>> 13)) * 1274126177;
214 return (n ^ (n >>> 16)) >>> 0;
215}
216
217function cobbleShade (base, x, y, isOutline)
218{
219 if (isOutline)
220 {
221 // Keep creases mostly solid so silhouettes stay crisp.
222 const h = hash2(x, y);
223 return (h & 15) === 0 ? MORTAR : EDGE;
224 }
225 const h = hash2(x, y);
226 const speck = h & 7;
227 if (speck === 0)
228 { return MORTAR; }
229 if (speck === 1 || speck === 2)
230 {
231 return base === TOP || base === TOP_HI || base === TOP_LO
232 ? TOP_HI
233 : FACE_HI;
234 }
235 if (speck === 3)
236 {
237 return base === TOP || base === TOP_HI || base === TOP_LO
238 ? TOP_LO
239 : FACE_LO;
240 }
241 return base;
242}
243
244function textureMask (rgba, roleBase)
245{
246 const out = new Uint8Array(SIZE * SIZE * 4);
247 for (let y = 0; y < SIZE; y++)
248 {
249 for (let x = 0; x < SIZE; x++)
250 {
251 const i = (y * SIZE + x) * 4;
252 const a = rgba[i + 3];
253 if (a === 0)
254 { continue; }
255 const g = rgba[i];
256 const base = roleBase.get(g) ?? baseFromLuminance(g);
257 const color = cobbleShade(base, x, y, g === 0x40);
258 out[i] = color[0];
259 out[i + 1] = color[1];
260 out[i + 2] = color[2];
261 out[i + 3] = 255;
262 }
263 }
264 return out;
265}
266
267function shapeFiles (prefix)
268{
269 if (!existsSync(EXAMPLES))
270 { throw new Error(`Missing examples folder: ${EXAMPLES}`); }
271 return readdirSync(EXAMPLES)
272 .filter((name) => name.startsWith(prefix) && name.endsWith(".png"))
273 .sort();
274}
275
276let wrote = 0;
277for (const [prefix, outDir] of Object.entries(OUT))
278{
279 mkdirSync(outDir, { recursive: true });
280 const files = shapeFiles(prefix);
281 if (files.length !== 16)
282 {
283 throw new Error(
284 `Expected 16 ${prefix} masks in ${EXAMPLES}, found ${files.length}`
285 );
286 }
287 const roleBase = roleBaseFor(prefix);
288 for (const name of files)
289 {
290 const shaped = decodePngRgba(join(EXAMPLES, name));
291 const textured = textureMask(shaped, roleBase);
292 writeFileSync(join(outDir, name), encodePng(textured));
293 wrote++;
294 }
295 console.log(`Textured ${files.length} → ${outDir}`);
296}
297console.log(`Done (${wrote} sprites). Shapes left greyscale in ${EXAMPLES}`);