Skip to content

3D engine that AI agents drive

Read motion page options like the film page and name the aspect on portrait film shots (#2414)

Nguyen Xuan An328b9e85c6cb
diff --git a/changes/2414.md b/changes/2414.md
new file mode 100644
index 00000000..be559859
--- /dev/null
+++ b/changes/2414.md
@@ -0,0 +1 @@
+- The motion page reads an empty `width`, `height`, `samples` or `memory` option as its default and rounds a fractional value, as the film page already did (#2414)
diff --git a/changes/2415.md b/changes/2415.md
new file mode 100644
index 00000000..65c70a7f
--- /dev/null
+++ b/changes/2415.md
@@ -0,0 +1 @@
+- `sampleFilm` names the aspect when the portrait shots cannot frame a very narrow one, like the other shots (#2415)
diff --git a/src/film-player.js b/src/film-player.js
index a7cb86ce..373bf13a 100644
--- a/src/film-player.js
+++ b/src/film-player.js
@@ -2,19 +2,15 @@ import './film-style.css';
import './brand.js';
import { CrucibleRenderer } from './renderer.js';
import './explosion-shader.js';
+import { readWholeNumberOption } from './query-number-option.js';
import { createFilmScenes, FILM_DURATION, sampleFilm } from './film.js';
const query = new URLSearchParams(window.location.search);
const exporting = query.get('export') === '1';
-function numberOption(name, fallback, minimum, maximum) {
- const raw = query.get(name);
- const value = raw === null || raw.trim() === '' ? fallback : Number(raw);
- return Number.isFinite(value) ? Math.round(Math.max(minimum, Math.min(maximum, value))) : fallback;
-}
-const width = numberOption('width', 1280, 320, 7680);
-const height = numberOption('height', 720, 240, 4320);
-const samples = numberOption('samples', 4, 1, 8);
-const memoryBudgetMiB = numberOption('memory', 384, 64, 1024);
+const width = readWholeNumberOption(query, 'width', 1280, 320, 7680);
+const height = readWholeNumberOption(query, 'height', 720, 240, 4320);
+const samples = readWholeNumberOption(query, 'samples', 4, 1, 8);
+const memoryBudgetMiB = readWholeNumberOption(query, 'memory', 384, 64, 1024);
const canvas = document.querySelector('#film-canvas');
const stage = document.querySelector('.film-stage');
const slider = document.querySelector('#film-time');
diff --git a/src/film.js b/src/film.js
index cae37163..c71d6c9e 100644
--- a/src/film.js
+++ b/src/film.js
@@ -998,7 +998,13 @@ export function sampleFilm(time, { aspect = 16 / 9 } = {}) {
vec3.scale(direction, direction, 1 / authoredDistance);
const center = bounds.minimum.map((value, axis) => (value + bounds.maximum[axis]) / 2);
const fittingView = { ...camera, target: center, eye: center.map((value, axis) => value + direction[axis] * authoredDistance) };
- const distance = Math.max(authoredDistance, fitPerspectiveDistance(bounds, fittingView, aspect, 1.1));
+ let fittedDistance;
+ try {
+ fittedDistance = fitPerspectiveDistance(bounds, fittingView, aspect, 1.1);
+ } catch {
+ throw new RangeError('Film aspect exceeds the supported lens range.');
+ }
+ const distance = Math.max(authoredDistance, fittedDistance);
const framingAmount = Math.min(1, (16 / 9 - aspect) / (16 / 9 - 1));
const framedDistance = authoredDistance + (distance - authoredDistance) * framingAmount;
camera.target = camera.target.map((value, axis) => value + (center[axis] - value) * framingAmount);
diff --git a/src/motion-player.js b/src/motion-player.js
index 65e4d486..07b78685 100644
--- a/src/motion-player.js
+++ b/src/motion-player.js
@@ -3,6 +3,7 @@ import './brand.js';
import { CrucibleRenderer } from './renderer.js';
import { createMotionScene, MOTION_DURATION } from './motion.js';
import { EFFECT_PRESETS } from './effects.js';
+import { readWholeNumberOption } from './query-number-option.js';
const query = new URLSearchParams(location.search);
const exporting = query.get('export') === '1';
@@ -18,9 +19,7 @@ let scheduledFrame = 0;
let playbackOrigin = 0;
function option(name, fallback, minimum, maximum) {
- const raw = query.get(name);
- const value = raw === null ? fallback : Number(raw);
- return Number.isFinite(value) ? Math.max(minimum, Math.min(maximum, value)) : fallback;
+ return readWholeNumberOption(query, name, fallback, minimum, maximum);
}
if (exporting) {
diff --git a/src/query-number-option.js b/src/query-number-option.js
new file mode 100644
index 00000000..5a8fc45c
--- /dev/null
+++ b/src/query-number-option.js
@@ -0,0 +1,10 @@
+/**
+ * Read a whole-number page option from URL search parameters. A missing or blank value, or one
+ * that is not a finite number, gives the fallback; any other value is rounded and kept in range.
+ */
+export function readWholeNumberOption(query, name, fallback, minimum, maximum) {
+ const raw = query.get(name);
+ const value = raw === null || raw.trim() === '' ? fallback : Number(raw);
+ if (!Number.isFinite(value)) return fallback;
+ return Math.round(Math.max(minimum, Math.min(maximum, value)));
+}
diff --git a/tests/film.test.js b/tests/film.test.js
index 23499166..c495a214 100644
--- a/tests/film.test.js
+++ b/tests/film.test.js
@@ -408,3 +408,8 @@ test('open and closed lids retain smooth surface normals throughout the brow ran
}
console.log(JSON.stringify({ eyelidNormalAgreement: minima }));
});
+
+test('a film aspect the portrait shots cannot frame is refused as a film aspect, not as a fit-bounds failure', () => {
+ const portraitShot = FILM_SHOTS.find(shot => shot.scene === 'portrait');
+ assert.throws(() => sampleFilm(portraitShot.start, { aspect: 0.0001 }), error => error instanceof RangeError && /Film aspect/.test(error.message));
+});
diff --git a/tests/query-number-option.test.js b/tests/query-number-option.test.js
new file mode 100644
index 00000000..b8e52275
--- /dev/null
+++ b/tests/query-number-option.test.js
@@ -0,0 +1,29 @@
+import assert from 'node:assert/strict';
+import test from 'node:test';
+
+async function readOption(search, fallback = 1280, minimum = 64, maximum = 4096) {
+ const { readWholeNumberOption } = await import('../src/query-number-option.js');
+ return readWholeNumberOption(new URLSearchParams(search), 'width', fallback, minimum, maximum);
+}
+
+test('a missing, empty or blank page option uses its default instead of the minimum', async () => {
+ assert.equal(await readOption(''), 1280);
+ assert.equal(await readOption('width='), 1280);
+ assert.equal(await readOption('width=%20%20'), 1280);
+});
+
+test('a value that is not a number uses the default', async () => {
+ assert.equal(await readOption('width=wide'), 1280);
+ assert.equal(await readOption('width=Infinity'), 1280);
+});
+
+test('a fractional page option is rounded to a whole number so the renderer accepts it', async () => {
+ assert.equal(await readOption('width=2.5', 4, 1, 8), 3);
+ assert.equal(await readOption('width=100.4'), 100);
+});
+
+test('a page option stays inside its range', async () => {
+ assert.equal(await readOption('width=1'), 64);
+ assert.equal(await readOption('width=99999'), 4096);
+ assert.equal(await readOption('width=800'), 800);
+});