3D engine that AI agents drive
JavaScript 3d3d-engineagentic-aiai-agentsblenderclaude-codegame-enginegaussian-splattinggltfjavascriptmcpmcp-servermodel-context-protocolpath-tracerpath-tracingpbrrenderingthreejs-alternativewebgl2webgpu
Write ID mattes as deflated 8-bit gray PNG (#1962)
Nguyen Xuan An7fa14eff8881
diff --git a/scripts/job-mattes.mjs b/scripts/job-mattes.mjsindex aa5a02f3..06442317 100644--- a/scripts/job-mattes.mjs+++ b/scripts/job-mattes.mjs@@ -1,10 +1,26 @@-import { encodePng } from '../src/export-png.js';-import { cryptomatteByte, cryptomatteMatte, floatPassToBytes, matteToGrayRgba, objectIdMatte } from '../src/pass-compositing.js';+import { deflateSync } from 'node:zlib';++import { PNG_SIGNATURE, pngChunk } from '../src/export-png.js';+import { cryptomatteByte, cryptomatteMatte, floatPassToBytes, objectIdMatte } from '../src/pass-compositing.js';// The ID mattes of a render job (settings.mattes): one gray PNG per matte, white where the named part or material is drawn.// Parts come from the exact 24-bit object ID pass, materials from the Cryptomatte pass (an 8-bit hash that two names can share).const NAMES_IN_ERROR = 20;+const PNG_GRAY_COLOR_TYPE = 0;++/** An 8-bit gray PNG of a 0/255 mask (one byte per pixel, top row first), deflated: a matte is mostly flat runs, so it stays small at 4K. */+export function encodeMattePng(mask, width, height) {+ if (mask.length !== width * height) throw new RangeError('The mask needs one byte per pixel at the frame size.');+ const rows = Buffer.alloc((width + 1) * height);+ for (let row = 0; row < height; row++) Buffer.from(mask.buffer, mask.byteOffset + row * width, width).copy(rows, row * (width + 1) + 1);+ const header = Buffer.alloc(13);+ header.writeUInt32BE(width, 0);+ header.writeUInt32BE(height, 4);+ header[8] = 8;+ header[9] = PNG_GRAY_COLOR_TYPE;+ return Buffer.concat([Buffer.from(PNG_SIGNATURE), pngChunk('IHDR', header), pngChunk('IDAT', deflateSync(rows, { level: 6 })), pngChunk('IEND', new Uint8Array(0))]);+}function nameList(names) {const shown = names.slice(0, NAMES_IN_ERROR).join(', ');@@ -60,7 +76,7 @@ export async function renderJobMattes({ mattes, renderPass }) {: materialMatte({ target: matte.material, index, cryptomattePass: await passBytes('cryptomatte') });const { width, height } = passes.get(matte.object !== undefined ? 'objectId' : 'cryptomatte');results.push({- bytes: encodePng(matteToGrayRgba(built.mask), width, height),+ bytes: encodeMattePng(built.mask, width, height),metric: { ...built.metric, coveragePixels: coverageOf(built.mask) },});}diff --git a/scripts/smoke-pass-mattes.mjs b/scripts/smoke-pass-mattes.mjsindex 36176b31..d6ced750 100644--- a/scripts/smoke-pass-mattes.mjs+++ b/scripts/smoke-pass-mattes.mjs@@ -56,7 +56,7 @@ function decodePng(bytes) {if (type === 'IDAT') chunks.push(data);offset += 12 + length;}- const channels = header.colorType === 6 ? 4 : 3;+ const channels = { 0: 1, 2: 3, 6: 4 }[header.colorType];const stride = header.width * channels;const raw = inflateSync(Buffer.concat(chunks));const pixels = Buffer.alloc(stride * header.height);diff --git a/tests/pass-compositing.test.js b/tests/pass-compositing.test.jsindex 8382e288..eea75827 100644--- a/tests/pass-compositing.test.js+++ b/tests/pass-compositing.test.js@@ -9,8 +9,8 @@ import { mkdtemp, rm, writeFile } from 'node:fs/promises';import { tmpdir } from 'node:os';import { join } from 'node:path';import { artifactNames, createAgentRenderJobs, normalizeJobSettings } from '../scripts/agent-render-jobs.mjs';-import { renderJobMattes } from '../scripts/job-mattes.mjs';-import { encodePng } from '../src/export-png.js';+import { encodeMattePng, renderJobMattes } from '../scripts/job-mattes.mjs';+import { inflateSync } from 'node:zlib';const rgba = (...pixels) => Uint8Array.from(pixels.flat());@@ -109,15 +109,28 @@ test('renderJobMattes builds one PNG per matte from the object and cryptomatte p};const results = await renderJobMattes({ mattes: [{ object: 'hero' }, { object: 'floor' }, { material: 'stone' }], renderPass });assert.deepEqual(seen, ['objectId', 'cryptomatte'], 'each pass renders once');- assert.deepEqual([...results[0].bytes], [...encodePng(matteToGrayRgba(Uint8Array.from([255, 0, 0, 0])), 2, 2)]);- assert.deepEqual([...results[1].bytes], [...encodePng(matteToGrayRgba(Uint8Array.from([0, 255, 255, 0])), 2, 2)]);- assert.deepEqual([...results[2].bytes], [...encodePng(matteToGrayRgba(Uint8Array.from([0, 255, 255, 0])), 2, 2)]);+ assert.deepEqual([...results[0].bytes], [...encodeMattePng(Uint8Array.from([255, 0, 0, 0]), 2, 2)]);+ assert.deepEqual([...results[1].bytes], [...encodeMattePng(Uint8Array.from([0, 255, 255, 0]), 2, 2)]);+ assert.deepEqual([...results[2].bytes], [...encodeMattePng(Uint8Array.from([0, 255, 255, 0]), 2, 2)]);assert.deepEqual(results.map(result => result.metric.coveragePixels), [1, 2, 2]);assert.deepEqual(results[0].metric.partIds, [1]);await assert.rejects(renderJobMattes({ mattes: [{ object: 'chair' }], renderPass }), /no part named "chair".*hero, floor/);await assert.rejects(renderJobMattes({ mattes: [{ material: 'glass' }], renderPass }), /no material named "glass".*steel, stone/);});+test('encodeMattePng writes an 8-bit gray PNG whose rows are the mask', () => {+ const png = encodeMattePng(Uint8Array.from([255, 0, 0, 255, 255, 0]), 3, 2);+ assert.deepEqual([...png.subarray(0, 8)], [137, 80, 78, 71, 13, 10, 26, 10]);+ assert.equal(png.readUInt32BE(16), 3);+ assert.equal(png.readUInt32BE(20), 2);+ assert.equal(png[24], 8, 'bit depth');+ assert.equal(png[25], 0, 'gray colour type');+ const dataLength = png.readUInt32BE(33);+ const rows = inflateSync(png.subarray(41, 41 + dataLength));+ assert.deepEqual([...rows], [0, 255, 0, 0, 0, 255, 255, 0]);+ assert.throws(() => encodeMattePng(Uint8Array.from([1, 2]), 3, 2), /one byte per pixel/);+});+test('settings.mattes is validated and adds one file per matte and frame', () => {const recipe = { format: 'fixture' };const base = { kind: 'image', width: 64, height: 64 };