Skip to content

3D engine that AI agents drive

Refuse an over-budget bakeGasSim and a sparse burstPosition (#2476)

Nguyen Xuan Aneaa7f7377368
diff --git a/changes/2476.md b/changes/2476.md
new file mode 100644
index 00000000..327c9718
--- /dev/null
+++ b/changes/2476.md
@@ -0,0 +1 @@
+- Refuse an over-budget `bakeGasSim` up front with the sequence limit `bakeGasSimToGrids` already used, and refuse a sparse `burstPosition` that baked NaN (#2476, #2477).
diff --git a/changes/2476.size.md b/changes/2476.size.md
new file mode 100644
index 00000000..86d66774
--- /dev/null
+++ b/changes/2476.size.md
@@ -0,0 +1 @@
+- scene:agent-scene+lazy: refuses a gas bake that would hold gigabytes and a sparse burstPosition that bakes NaN, about 150 bytes gzip.
diff --git a/docs/volumetric.md b/docs/volumetric.md
index acbad8f2..15e6b0fa 100644
--- a/docs/volumetric.md
+++ b/docs/volumetric.md
@@ -281,7 +281,7 @@ const frames = bakeGasSimToGrids({ resolution: 64, frames: 72, fps: 24, seed: 3
| `temperatureLift`, `densityWeight`, `ambientTemperature`, `cooling`, `dissipation`, `flameDecay`, `burnRate`, `ignitionTemperature`, `heatRelease`, `smokeYield` | the physics, each bounded; see `NUMBER_OPTIONS` in the source | tuned for a ground blast |
| `detail` | noise upres of the smoke density in every baked frame, 0 to 1: the density is multiplied by `1 + 4 * detail * noise` (two octaves of value noise about 5 and 2.5 cells wide, rising with the frame), so thin smoke breaks into puffs and gaps smaller than the grid resolves and empty cells stay empty. The solver never reads it back, and `0` leaves the bake byte for byte as it was. Flame, temperature and velocity are not touched, and `add-explosion` does not use it yet | `0` |
-Every option is checked, and an error names the field, its range and what to pass. The `temperature` channel holds the kelvin above the ambient air in units of `GAS_SIM_TEMPERATURE_UNIT` (1000 K): pass it as `temperatureScale`, and cold air reads 0 so the blackbody adds no glow there. `flame` is 0 to 1 per cell. A bake of 64 cubed with three channels is 1.5 MiB a frame, so 72 frames are 108 MiB of the 256 MiB sequence limit; `bakeGasSimToGrids` refuses a bake that would pass it before it starts. Smoke and fuel are conserved: each solver step scales the advected field back to its total before the step, less what the step carries out through the open sides and the top, so with no source and no dissipation the smoke mass stays within 2% over 72 frames at 32 cubed, and dissipation lowers it by exactly its own factor. Burning adds smoke and `dissipation` removes it as separate terms. Heat and flame are not mass and are advected without that correction.
+Every option is checked, and an error names the field, its range and what to pass. The `temperature` channel holds the kelvin above the ambient air in units of `GAS_SIM_TEMPERATURE_UNIT` (1000 K): pass it as `temperatureScale`, and cold air reads 0 so the blackbody adds no glow there. `flame` is 0 to 1 per cell. A bake of 64 cubed with three channels is 1.5 MiB a frame, so 72 frames are 108 MiB of the 256 MiB sequence limit; `bakeGasSim` and `bakeGasSimToGrids` refuse a bake that would pass it before it starts. Smoke and fuel are conserved: each solver step scales the advected field back to its total before the step, less what the step carries out through the open sides and the top, so with no source and no dissipation the smoke mass stays within 2% over 72 frames at 32 cubed, and dissipation lowers it by exactly its own factor. Burning adds smoke and `dissipation` removes it as separate terms. Heat and flame are not mass and are advected without that correction.
### Motion blur of a grid volume
diff --git a/src/gas-sim.js b/src/gas-sim.js
index f368d1f1..f038fe6c 100644
--- a/src/gas-sim.js
+++ b/src/gas-sim.js
@@ -76,7 +76,7 @@ function checkResolution(value) {
function checkPosition(value, resolution, cellSize) {
if (value === undefined) return [resolution[0] * cellSize / 2, 0, resolution[2] * cellSize / 2];
- if (!Array.isArray(value) || value.length !== 3 || !value.every(Number.isFinite)) {
+ if (!Array.isArray(value) || value.length !== 3 || !Array.from(value).every(Number.isFinite)) {
throw new TypeError(`bakeGasSim: burstPosition must be [x, y, z] in metres from the grid's corner (floor at y 0), got ${JSON.stringify(value)}; leave it out for the middle of the floor`);
}
const size = resolution.map(axis => axis * cellSize);
@@ -104,6 +104,18 @@ export function resolveGasSimOptions(options = {}) {
return resolved;
}
+// Bytes a bake takes as half-float grids, the figure the sequence limit counts.
+function sequenceBytes(resolved) {
+ return resolved.resolution[0] * resolved.resolution[1] * resolved.resolution[2] * 2 * (resolved.velocity ? 6 : 3) * resolved.frames;
+}
+
+function checkSequenceBytes(caller, resolved) {
+ const bytes = sequenceBytes(resolved);
+ if (bytes <= VOLUME_GRID_SEQUENCE_MAX_BYTES) return;
+ const fitting = Math.floor(resolved.frames * VOLUME_GRID_SEQUENCE_MAX_BYTES / bytes);
+ throw new RangeError(`${caller}: ${resolved.frames} frames of ${resolved.resolution.join('x')} take ${bytes} bytes, over the ${VOLUME_GRID_SEQUENCE_MAX_BYTES} byte sequence limit; pass frames up to ${fitting} or a smaller resolution`);
+}
+
function smoothstep(edge0, edge1, value) {
const t = Math.min(Math.max((value - edge0) / (edge1 - edge0), 0), 1);
return t * t * (3 - 2 * t);
@@ -538,6 +550,7 @@ function runGasSim(options, onFrame) {
*/
export function bakeGasSim(options = {}) {
const resolved = resolveGasSimOptions(options);
+ checkSequenceBytes('bakeGasSim', resolved);
const frames = [];
runGasSim(resolved, frame => frames.push(frame));
return { frames, resolution: resolved.resolution, size: resolved.size, fps: resolved.fps };
@@ -550,11 +563,7 @@ export function bakeGasSim(options = {}) {
export function bakeGasSimToGrids(options = {}, namePrefix = 'gas') {
if (typeof namePrefix !== 'string' || namePrefix === '') throw new TypeError('bakeGasSimToGrids: namePrefix must be a non-empty string, such as "blast"');
const resolved = resolveGasSimOptions(options);
- const frameBytes = resolved.resolution[0] * resolved.resolution[1] * resolved.resolution[2] * 2 * (resolved.velocity ? 6 : 3);
- if (frameBytes * resolved.frames > VOLUME_GRID_SEQUENCE_MAX_BYTES) {
- const fitting = Math.floor(VOLUME_GRID_SEQUENCE_MAX_BYTES / frameBytes);
- throw new RangeError(`bakeGasSimToGrids: ${resolved.frames} frames of ${resolved.resolution.join('x')} take ${frameBytes * resolved.frames} bytes, over the ${VOLUME_GRID_SEQUENCE_MAX_BYTES} byte sequence limit; pass frames up to ${fitting} or a smaller resolution`);
- }
+ checkSequenceBytes('bakeGasSimToGrids', resolved);
const names = [];
runGasSim(resolved, (frame, index) => {
const name = `${namePrefix}_${String(index).padStart(4, '0')}.67vg`;
diff --git a/tests/gas-sim.test.js b/tests/gas-sim.test.js
index 9ea54566..5c927562 100644
--- a/tests/gas-sim.test.js
+++ b/tests/gas-sim.test.js
@@ -238,3 +238,13 @@ test('detail is checked and the velocity channels are not warped', () => {
const plainVelocity = bakeGasSim({ resolution: 16, frames: 2, velocity: true });
assert.deepEqual(withVelocity.frames[1].velocityX, plainVelocity.frames[1].velocityX);
});
+
+test('bakeGasSim refuses an over-budget bake before it allocates anything (#2476)', () => {
+ assert.throws(() => bakeGasSim({ resolution: 96, frames: 240 }), /bakeGasSim: 240 frames of 96x96x96 take \d+ bytes, over the \d+ byte sequence limit; pass frames up to \d+/);
+ assert.throws(() => bakeGasSim({ resolution: 64, frames: 120, velocity: true }), /sequence limit/);
+});
+
+test('a sparse burstPosition is refused instead of baking NaN (#2477)', () => {
+ // eslint-disable-next-line no-sparse-arrays
+ assert.throws(() => bakeGasSim({ resolution: 8, frames: 1, burstPosition: [0.5, , 0.5] }), /burstPosition must be \[x, y, z\]/);
+});