Claude Opus 5.5max
render.js
// bun render.js [scale] [ss] [out.png] [x,y,w,h ...] — 並列で描いて PNG に書き出す(切り出しは原寸座標)
const { daichiCore } = require('./daichi.js');
const { deflateSync } = require('node:zlib');
const { writeFileSync } = require('node:fs');
const os = require('node:os');
const [scale = '1', ss = '3', out = '土谷大地.png', ...crops] = process.argv.slice(2);
const core = daichiCore({});
const { W, H } = core.setup(+scale, +ss);
const t0 = performance.now();
const shadow = core.buildShadow();
const hdr = new Float32Array(W * H * 3), depth = new Float32Array(W * H), mat = new Uint8Array(W * H);
await new Promise((resolve) => {
const n = Math.max(1, os.cpus().length - 2), CH = 4;
let next = 0, pending = 0, alive = n;
const give = (w) => {
if (next >= W) { w.terminate(); if (--alive === 0 && pending === 0) resolve(); return; }
const from = next, to = Math.min(W, next + CH); next = to; pending++;
w.postMessage({ type: 'cols', from, to });
};
for (let i = 0; i < n; i++) {
const w = new Worker(new URL('./worker.js', import.meta.url).href);
w.onmessage = (e) => {
const m = e.data;
if (m.type === 'done') {
const nc = m.to - m.from;
for (let y = 0; y < H; y++) {
hdr.set(m.rgb.subarray(y * nc * 3, (y + 1) * nc * 3), (y * W + m.from) * 3);
depth.set(m.depth.subarray(y * nc, (y + 1) * nc), y * W + m.from);
mat.set(m.mat.subarray(y * nc, (y + 1) * nc), y * W + m.from);
}
pending--;
}
give(w);
};
w.postMessage({ type: 'init', opt: {}, scale: +scale, ss: +ss, shadow });
}
});
console.log('terrain', Math.round(performance.now() - t0) + 'ms');
let t = performance.now();
if (core.paintDetails) { core.paintDetails(hdr, depth, mat); console.log('details', Math.round(performance.now() - t) + 'ms'); }
t = performance.now();
const rgba = new Uint8ClampedArray(W * H * 4);
core.develop(core.bloom(hdr, W, H), W, H, rgba);
if (core.paintTitle) core.paintTitle(rgba);
console.log('develop', Math.round(performance.now() - t) + 'ms', W + 'x' + H);
const CRC = new Uint32Array(256).map((_, n) => { let c = n; for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1; return c >>> 0; });
function chunk(type, data) {
const b = Buffer.alloc(12 + data.length);
b.writeUInt32BE(data.length, 0); b.write(type, 4, 'ascii'); data.copy(b, 8);
let c = 0xffffffff;
for (let i = 4; i < 8 + data.length; i++) c = CRC[(c ^ b[i]) & 255] ^ (c >>> 8);
b.writeUInt32BE((c ^ 0xffffffff) >>> 0, 8 + data.length);
return b;
}
function png(x0, y0, w, h) {
const raw = Buffer.alloc((w * 3 + 1) * h);
for (let y = 0, o = 0; y < h; y++) {
raw[o++] = 0;
for (let x = 0, i = ((y0 + y) * W + x0) * 4; x < w; x++, i += 4) { raw[o++] = rgba[i]; raw[o++] = rgba[i + 1]; raw[o++] = rgba[i + 2]; }
}
const ih = Buffer.alloc(13);
ih.writeUInt32BE(w, 0); ih.writeUInt32BE(h, 4); ih[8] = 8; ih[9] = 2;
return Buffer.concat([Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]), chunk('IHDR', ih), chunk('IDAT', deflateSync(raw, { level: 6 })), chunk('IEND', Buffer.alloc(0))]);
}
writeFileSync(out, png(0, 0, W, H));
crops.forEach((c, k) => {
const [x, y, w, h] = c.split(',').map((v) => Math.round(+v * +scale));
writeFileSync(out.replace(/\.png$/, `.crop${k}.png`), png(x, y, Math.min(w, W - x), Math.min(h, H - y)));
});
console.log('total', Math.round(performance.now() - t0) + 'ms');
process.exit(0);