Skip to content

3D engine that AI agents drive

Refuse inputs that made export writers emit corrupt files (#2471)

Nguyen Xuan An91b824936381
diff --git a/changes/2471.md b/changes/2471.md
new file mode 100644
index 00000000..1fb0a30c
--- /dev/null
+++ b/changes/2471.md
@@ -0,0 +1 @@
+- Export writers refuse inputs that used to produce corrupt files: a null compressed mip level, a zero, fractional or too short PNG, and NaN or ragged GLB accessor data (#2471, #2472, #2473).
diff --git a/changes/2471.size.md b/changes/2471.size.md
new file mode 100644
index 00000000..15d2f50f
--- /dev/null
+++ b/changes/2471.size.md
@@ -0,0 +1,2 @@
+- entry:core: 119 bytes gzip for the refusals that stop a PNG or GLB writer from emitting a corrupt file (zero or short PNG input, NaN accessor bounds written as null).
+- goal:gltf-box: 35 bytes gzip, the same refusals plus the missing mip level check, on a scene already over its goal; each stops a silently invalid export.
diff --git a/docs/export-glb.md b/docs/export-glb.md
index 8ca8b820..f1443e56 100644
--- a/docs/export-glb.md
+++ b/docs/export-glb.md
@@ -198,3 +198,5 @@ Every draw reported zero WebGL errors.
The original exporter lost vertex channels and produced maximum channel differences of 31, 194 and 185 in those respective fixtures.
Each draw uses one renderer, which is disposed before the next draw.
These measurements establish fidelity for the fixtures, not identical rendering across tools or recognizable character quality.
+
+The writer refuses accessor data that would make an invalid file: NaN or Infinity where bounds are computed (positions, normals, colours, morph targets) and a length that is not a multiple of the component count. An empty accessor is written without `min` and `max`.
diff --git a/docs/gltf.md b/docs/gltf.md
index e1e8dc07..65475ce5 100644
--- a/docs/gltf.md
+++ b/docs/gltf.md
@@ -242,7 +242,7 @@ The renderer uploads these GPU-compressed formats as they are, with `texStorage2
| ETC2 | `WEBGL_compressed_texture_etc` | 0.5 or 1 |
A context with none of these, such as SwiftShader, gets RGBA8 (4 bytes a texel). Set `workerConfig` before the first load. A loader that you leave without a config throws.
-Color maps upload as sRGB and data maps as linear. A file with a partial mip chain keeps only its first level. A compressed texture cannot be exported to GLB or read by the path tracer, so transcode to RGBA8 for those.
+Color maps upload as sRGB and data maps as linear. A file with a partial mip chain keeps only its first level. A compressed texture cannot be exported to GLB or read by the path tracer, so transcode to RGBA8 for those. A mip level that is missing from a full chain fails the load, naming the level.
`asset.textureFormats` lists `{ format, width, height, colorSpace, bytes }` for each texture, with `format` set to `bc7`, `bc1-rgb`, `bc1-rgba`, `bc3`, `astc-4x4`, `etc2-rgb`, `etc2-rgba` or `rgba8`.
KTX2 dimensions count toward `maxTexturePixels` before decoding. KTX2 images are never scaled. When the loader's `workerConfig` allows a compressed format, a KTX2 image counts a quarter of its pixels, because it takes at most one byte a texel on the GPU. A KTX2-heavy file over the budget still fails.
diff --git a/src/compressed-texture.js b/src/compressed-texture.js
index f57fa977..4c7eeafc 100644
--- a/src/compressed-texture.js
+++ b/src/compressed-texture.js
@@ -58,6 +58,7 @@ export function describeCompressedImage(glFormat, levels, field, colorSpace) {
}
const usable = levels.length === levelCountFor(base.width, base.height) ? levels : [base];
usable.forEach((level, index) => {
+ if (!hasPositiveIntegerSize(level)) throw new Error(`${field} ${info.name} mip level ${index} is missing or has no whole width and height; pass { width, height, data } for every level.`);
const expectedWidth = Math.max(1, base.width >> index);
const expectedHeight = Math.max(1, base.height >> index);
if (level.width !== expectedWidth || level.height !== expectedHeight) {
diff --git a/src/export-png.js b/src/export-png.js
index e0688eec..0789d665 100644
--- a/src/export-png.js
+++ b/src/export-png.js
@@ -73,6 +73,8 @@ function hasTransparentPixel(pixels) {
* Pixels with full alpha everywhere are written as RGB, a quarter smaller.
*/
export function encodePng(pixels, width, height, { flipRows = false } = {}) {
+ const needed = width * height * 4;
+ if (!(width >= 1 && height >= 1 && Number.isInteger(width) && Number.isInteger(height) && pixels?.length >= needed)) throw new RangeError(`PNG needs whole width and height >= 1 and ${needed} pixel values.`);
const withAlpha = hasTransparentPixel(pixels);
const channels = withAlpha ? 4 : 3;
const rowBytes = width * channels;
diff --git a/src/export-writer.js b/src/export-writer.js
index 39753f69..2de95b29 100644
--- a/src/export-writer.js
+++ b/src/export-writer.js
@@ -12,6 +12,7 @@ function componentTypeOf(data) {
}
function componentBounds(data, componentCount) {
+ if (!data.length) return {};
const minimum = new Array(componentCount).fill(Infinity);
const maximum = new Array(componentCount).fill(-Infinity);
for (let index = 0; index < data.length; index++) {
@@ -19,6 +20,7 @@ function componentBounds(data, componentCount) {
minimum[component] = Math.min(minimum[component], data[index]);
maximum[component] = Math.max(maximum[component], data[index]);
}
+ if (![...minimum, ...maximum].every(Number.isFinite)) throw new RangeError('GLB data must be finite.');
return { min: minimum, max: maximum };
}
@@ -50,6 +52,7 @@ export function createGlbWriter() {
function addAccessor(data, type, target, { bounds = type === 'VEC3' } = {}) {
const bytes = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
const componentCount = COMPONENT_COUNTS[type];
+ if (data.length % componentCount) throw new RangeError(`${type} data needs a multiple of ${componentCount} values.`);
const entry = { bufferView: addBufferView(bytes, target), componentType: componentTypeOf(data), count: data.length / componentCount, type };
if (bounds) Object.assign(entry, componentBounds(data, componentCount));
document.accessors.push(entry);
diff --git a/tests/export-writer-guards.test.js b/tests/export-writer-guards.test.js
new file mode 100644
index 00000000..43626ed3
--- /dev/null
+++ b/tests/export-writer-guards.test.js
@@ -0,0 +1,43 @@
+import assert from 'node:assert/strict';
+import test from 'node:test';
+import { describeCompressedImage } from '../src/compressed-texture.js';
+import { createGlbWriter } from '../src/export-writer.js';
+import { encodePng } from '../src/export-png.js';
+
+const BC7 = 0x8e8c;
+
+function bc7Level(size) {
+ return { width: size, height: size, data: new Uint8Array(Math.ceil(size / 4) ** 2 * 16) };
+}
+
+test('a null mip level in a compressed chain names the field and level', () => {
+ const levels = [bc7Level(4), bc7Level(2), null];
+ assert.throws(() => describeCompressedImage(BC7, levels, 'baseColorTexture', 'srgb'), /baseColorTexture bc7 mip level 2 is missing/);
+});
+
+test('encodePng refuses a size that is not a whole number of at least 1', () => {
+ for (const [width, height] of [[0, 4], [4, 0], [2.5, 2], [-2, 2], [NaN, 2]]) {
+ assert.throws(() => encodePng(new Uint8Array(256), width, height), RangeError, `${width}x${height}`);
+ }
+});
+
+test('encodePng refuses pixels shorter than the size needs', () => {
+ assert.throws(() => encodePng(new Uint8Array(4), 4, 4), /64 pixel values/);
+ assert.equal(encodePng(new Uint8Array(16), 2, 2).length > 0, true);
+});
+
+test('GLB accessors refuse non-finite data and ragged lengths', () => {
+ const writer = createGlbWriter();
+ assert.throws(() => writer.addAccessor(new Float32Array([0, NaN, 1]), 'VEC3'), /must be finite/);
+ assert.throws(() => writer.addAccessor(new Float32Array([0, Infinity, 1]), 'VEC3'), /must be finite/);
+ assert.throws(() => writer.addAccessor(new Float32Array(4), 'VEC3'), /multiple of 3/);
+});
+
+test('an empty GLB accessor has no null bounds', () => {
+ const writer = createGlbWriter();
+ const index = writer.addAccessor(new Float32Array(0), 'VEC3');
+ const entry = writer.document.accessors[index];
+ assert.equal('min' in entry, false);
+ assert.equal('max' in entry, false);
+ assert.doesNotMatch(JSON.stringify(writer.document), /null/);
+});