3D engine that AI agents drive
JavaScript 3d3d-engineagentic-aiai-agentsblenderclaude-codegame-enginegaussian-splattinggltfjavascriptmcpmcp-servermodel-context-protocolpath-tracerpath-tracingpbrrenderingthreejs-alternativewebgl2webgpu
Guard the HDR downsample size and the GLB texture recoder inputs (#2574, #2575)
Nguyen Xuan An6921171e3f1b
diff --git a/changes/2574.md b/changes/2574.mdnew file mode 100644index 00000000..8cdea735--- /dev/null+++ b/changes/2574.md@@ -0,0 +1 @@+- `scripts/downsample-hdr.mjs` refuses a target size above the source or not a whole number instead of writing a broken sky (#2574)diff --git a/changes/2575.md b/changes/2575.mdnew file mode 100644index 00000000..37bb2c93--- /dev/null+++ b/changes/2575.md@@ -0,0 +1 @@+- `scripts/recode-glb-textures.mjs` leaves URI images alone instead of crashing, and refuses a WebP quality above 100 or a texture cap that is not a whole number (#2575)diff --git a/scripts/downsample-hdr.mjs b/scripts/downsample-hdr.mjsindex ae9e449c..8d0cbae4 100644--- a/scripts/downsample-hdr.mjs+++ b/scripts/downsample-hdr.mjs@@ -1,38 +1,49 @@// Averages a Radiance .hdr down to a smaller flat RGBE file so a world's sky costs kilobytes, not megabytes.// node scripts/downsample-hdr.mjs <source.hdr> <target.hdr> <width> <height>import { readFileSync, writeFileSync } from 'node:fs';+import { resolve } from 'node:path';+import { pathToFileURL } from 'node:url';import { parseRadianceHdr } from '../src/hdri.js';+import { requireFiniteInRange } from '../src/modeling-limits.js';-const [sourcePath, targetPath, widthText, heightText] = process.argv.slice(2);-const targetWidth = Number(widthText);-const targetHeight = Number(heightText);-if (!sourcePath || !targetPath || !(targetWidth > 0) || !(targetHeight > 0)) throw new Error('Pass <source.hdr> <target.hdr> <width> <height>.');-const { width, height, data } = parseRadianceHdr(readFileSync(sourcePath));-const blockWidth = Math.max(1, Math.floor(width / targetWidth));-const blockHeight = Math.max(1, Math.floor(height / targetHeight));-const pixels = Buffer.alloc(targetWidth * targetHeight * 4);-for (let row = 0; row < targetHeight; row++) {- for (let column = 0; column < targetWidth; column++) {- const sum = [0, 0, 0];- for (let blockRow = 0; blockRow < blockHeight; blockRow++) {- for (let blockColumn = 0; blockColumn < blockWidth; blockColumn++) {- const texel = ((row * blockHeight + blockRow) * width + column * blockWidth + blockColumn) * 3;- for (let channel = 0; channel < 3; channel++) sum[channel] += data[texel + channel];+/** A flat RGBE file whose pixels are block averages of `sourceBuffer`; the target is no larger than the source. */+export function downsampleHdr(sourceBuffer, targetWidth, targetHeight) {+ const { width, height, data } = parseRadianceHdr(sourceBuffer);+ requireFiniteInRange('downsampleHdr', 'width', targetWidth, 1, width, true);+ requireFiniteInRange('downsampleHdr', 'height', targetHeight, 1, height, true);+ const blockWidth = Math.floor(width / targetWidth);+ const blockHeight = Math.floor(height / targetHeight);+ const pixels = Buffer.alloc(targetWidth * targetHeight * 4);+ for (let row = 0; row < targetHeight; row++) {+ for (let column = 0; column < targetWidth; column++) {+ const sum = [0, 0, 0];+ for (let blockRow = 0; blockRow < blockHeight; blockRow++) {+ for (let blockColumn = 0; blockColumn < blockWidth; blockColumn++) {+ const texel = ((row * blockHeight + blockRow) * width + column * blockWidth + blockColumn) * 3;+ for (let channel = 0; channel < 3; channel++) sum[channel] += data[texel + channel];+ }}+ const average = sum.map(value => value / (blockWidth * blockHeight));+ const brightest = Math.max(...average);+ if (brightest < 1e-32) continue;+ const exponent = Math.floor(Math.log2(brightest)) + 1;+ const scale = 256 / 2 ** exponent;+ const target = (row * targetWidth + column) * 4;+ for (let channel = 0; channel < 3; channel++) pixels[target + channel] = Math.min(255, Math.round(average[channel] * scale));+ pixels[target + 3] = exponent + 128;}- const average = sum.map(value => value / (blockWidth * blockHeight));- const brightest = Math.max(...average);- if (brightest < 1e-32) continue;- const exponent = Math.floor(Math.log2(brightest)) + 1;- const scale = 256 / 2 ** exponent;- const target = (row * targetWidth + column) * 4;- for (let channel = 0; channel < 3; channel++) pixels[target + channel] = Math.min(255, Math.round(average[channel] * scale));- pixels[target + 3] = exponent + 128;}+ const header = Buffer.from(`#?RADIANCE\nFORMAT=32-bit_rle_rgbe\n\n-Y ${targetHeight} +X ${targetWidth}\n`, 'latin1');+ const file = Buffer.concat([header, pixels]);+ const check = parseRadianceHdr(file);+ if (check.width !== targetWidth || check.height !== targetHeight) throw new Error('The sky does not parse back.');+ return file;+}++if (import.meta.url === pathToFileURL(resolve(process.argv[1] ?? '')).href) {+ const [sourcePath, targetPath, widthText, heightText] = process.argv.slice(2);+ if (!sourcePath || !targetPath || !widthText || !heightText) throw new Error('Pass <source.hdr> <target.hdr> <width> <height>.');+ const file = downsampleHdr(readFileSync(sourcePath), Number(widthText), Number(heightText));+ writeFileSync(targetPath, file);+ console.log(`${targetPath}: ${file.length} bytes`);}-const header = Buffer.from(`#?RADIANCE\nFORMAT=32-bit_rle_rgbe\n\n-Y ${targetHeight} +X ${targetWidth}\n`, 'latin1');-const file = Buffer.concat([header, pixels]);-writeFileSync(targetPath, file);-const check = parseRadianceHdr(file);-if (check.width !== targetWidth || check.height !== targetHeight) throw new Error('The sky does not parse back.');-console.log(`${targetPath}: ${file.length} bytes`);diff --git a/scripts/recode-glb-textures.mjs b/scripts/recode-glb-textures.mjsindex 5300f5e9..b9f1c7dc 100644--- a/scripts/recode-glb-textures.mjs+++ b/scripts/recode-glb-textures.mjs@@ -8,6 +8,7 @@ import { readFileSync, writeFileSync } from 'node:fs';import sharp from 'sharp';import { resolve } from 'node:path';import { pathToFileURL } from 'node:url';+import { requireFiniteInRange } from '../src/modeling-limits.js';const GLB_MAGIC = 0x46546c67;const JSON_CHUNK = 0x4e4f534a;@@ -27,6 +28,8 @@ export function parseArguments(argv) {if (!(key in options) || !Number.isFinite(value) || value <= 0) throw new Error(`Unknown or invalid option ${name}.`);options[key] = value;}+ requireFiniteInRange('recode-glb-textures', 'webpQuality', options.webpQuality, 1, 100, true);+ requireFiniteInRange('recode-glb-textures', 'maxTexture', options.maxTexture, 1, 16384, true);if (positional.length !== 2) throw new Error('Pass <input.glb> and <output.glb>.');return { input: positional[0], output: positional[1], options };}@@ -65,7 +68,7 @@ async function recodeImage(original, options) {let pipeline = sharp(original);if (longest > options.maxTexture) {const scale = options.maxTexture / longest;- pipeline = pipeline.resize({ width: Math.round(metadata.width * scale), height: Math.round(metadata.height * scale) });+ pipeline = pipeline.resize({ width: Math.max(1, Math.round(metadata.width * scale)), height: Math.max(1, Math.round(metadata.height * scale)) });}const converted = await pipeline.webp({ quality: options.webpQuality, effort: 6, alphaQuality: 90 }).toBuffer();return converted.length < original.length ? converted : null;@@ -100,6 +103,7 @@ export async function recodeGlbTextures(buffer, options) {const recodedByView = new Map();let imageBytesBefore = 0;for (const image of json.images ?? []) {+ if (image.bufferView === undefined) continue;const view = json.bufferViews[image.bufferView];const original = binary.subarray(view.byteOffset ?? 0, (view.byteOffset ?? 0) + view.byteLength);imageBytesBefore += original.length;diff --git a/tests/downsample-hdr.test.js b/tests/downsample-hdr.test.jsnew file mode 100644index 00000000..527cdc24--- /dev/null+++ b/tests/downsample-hdr.test.js@@ -0,0 +1,28 @@+import assert from 'node:assert/strict';+import { test } from 'node:test';+import { parseRadianceHdr } from '../src/hdri.js';++function flatHdr(width, height, rgbe) {+ const header = Buffer.from(`#?RADIANCE\nFORMAT=32-bit_rle_rgbe\n\n-Y ${height} +X ${width}\n`, 'latin1');+ const pixels = Buffer.alloc(width * height * 4);+ for (let index = 0; index < width * height; index++) Buffer.from(rgbe).copy(pixels, index * 4);+ return Buffer.concat([header, pixels]);+}++test('a block average keeps the sky colour', async () => {+ const { downsampleHdr } = await import('../scripts/downsample-hdr.mjs');+ const result = parseRadianceHdr(downsampleHdr(flatHdr(8, 4, [128, 64, 32, 129]), 4, 2));+ assert.equal(result.width, 4);+ assert.equal(result.height, 2);+ assert.ok(Math.abs(result.data[0] - 1) < 0.02);+ assert.ok(Math.abs(result.data[1] - 0.5) < 0.02);+});++test('a target larger than the source or not a whole number is refused', async () => {+ const { downsampleHdr } = await import('../scripts/downsample-hdr.mjs');+ const source = flatHdr(8, 4, [128, 64, 32, 129]);+ assert.throws(() => downsampleHdr(source, 16, 4), /width was 16; pass a whole number from 1 to 8/);+ assert.throws(() => downsampleHdr(source, 4, 8), /height was 8; pass a whole number from 1 to 4/);+ assert.throws(() => downsampleHdr(source, 2.5, 2), /width was 2.5/);+ assert.throws(() => downsampleHdr(source, 4, 0), /height was 0/);+});diff --git a/tests/recode-glb-textures.test.js b/tests/recode-glb-textures.test.jsindex ae3db016..5e5f248e 100644--- a/tests/recode-glb-textures.test.js+++ b/tests/recode-glb-textures.test.js@@ -85,3 +85,20 @@ test('declareWebpTextures moves only the WebP images into the extension and doesdeclareWebpTextures(untouched);assert.deepEqual(untouched, { images: [{ mimeType: 'image/png' }], textures: [{ source: 0 }] });});++test('parseArguments refuses a quality above 100 and a texture cap that is not a whole number', () => {+ assert.throws(() => parseArguments(['in.glb', 'out.glb', '--webp-quality', '150']), /webpQuality was 150; pass a whole number from 1 to 100/);+ assert.throws(() => parseArguments(['in.glb', 'out.glb', '--max-texture', '0.5']), /maxTexture was 0.5/);+});++test('an image that lives at a URI is left alone and the other images still convert', async () => {+ const { glb } = await buildGlb(256);+ const { json, binary } = readGlb(glb);+ json.images.unshift({ uri: 'external.png' });+ json.textures = [{ source: 1, sampler: 0 }, { source: 0 }];+ const result = await recodeGlbTextures(writeGlb(json, binary), { webpQuality: 60, maxTexture: 4096 });+ const recoded = readGlb(result.glb).json;+ assert.deepEqual(recoded.images[0], { uri: 'external.png' });+ assert.equal(recoded.images[1].mimeType, 'image/webp');+ assert.deepEqual(recoded.textures, [{ sampler: 0, extensions: { EXT_texture_webp: { source: 1 } } }, { source: 0 }]);+});