Skip to content

3D engine that AI agents drive

Name the bad field in camera sampler input errors (#2358)

Nguyen Xuan Anedade6f48340
diff --git a/changes/2358.md b/changes/2358.md
new file mode 100644
index 00000000..3605e150
--- /dev/null
+++ b/changes/2358.md
@@ -0,0 +1 @@
+- Camera samplers name a bad roll override, reject sparse frames and refuse a null, string or infinite sample time on both forms (#2358, #2359, #2360).
diff --git a/src/camera-path.js b/src/camera-path.js
index 860f3264..1a91a02e 100644
--- a/src/camera-path.js
+++ b/src/camera-path.js
@@ -149,6 +149,7 @@ export function createCameraPath({ duration, loop = false, eye, target = [0, 0,
const fieldOfView = overrides?.fov ?? tracks[2].output[0];
validateLens(fieldOfView, near, far);
const angle = overrides?.roll ?? tracks[3].output[0];
+ if (typeof angle !== 'number' || !Number.isFinite(angle)) throw new RangeError(`Camera roll must be a finite number of radians, got ${angle}.`);
const directionLength = Math.hypot(stagedTarget[0] - stagedEye[0], stagedTarget[1] - stagedEye[1], stagedTarget[2] - stagedEye[2]);
for (let axis = 0; axis < 3; axis++) direction[axis] = (stagedTarget[axis] - stagedEye[axis]) / directionLength;
const cosine = Math.cos(angle);
diff --git a/src/camera-track.js b/src/camera-track.js
index ec205742..931f541c 100644
--- a/src/camera-track.js
+++ b/src/camera-track.js
@@ -10,19 +10,26 @@ function validateFrameKeys(frames, framesPerSecond) {
}
if (frames.length === 0) throw new RangeError('Camera frames must hold at least one [eye, target] pair.');
const isFiniteTriple = vector => Array.isArray(vector) && vector.length === 3 && vector.every(Number.isFinite);
- frames.forEach((frame, index) => {
+ for (let index = 0; index < frames.length; index++) {
+ const frame = frames[index];
if (!Array.isArray(frame) || !isFiniteTriple(frame[0]) || !isFiniteTriple(frame[1])) {
throw new RangeError(`Camera frames[${index}] must be [eye, target], each three finite numbers.`);
}
if (Math.hypot(frame[1][0] - frame[0][0], frame[1][1] - frame[0][1], frame[1][2] - frame[0][2]) <= 1e-8) {
throw new RangeError(`Camera frames[${index}] eye and target are the same point, so there is no view direction; move the target away from the eye.`);
}
- });
+ }
if (!Number.isFinite(frames.length / framesPerSecond)) {
throw new RangeError(`Camera fps ${framesPerSecond} is too small for ${frames.length} frames to have a finite duration; pass a larger fps.`);
}
}
+function requireFiniteSeconds(seconds) {
+ if (typeof seconds !== 'number' || !Number.isFinite(seconds)) {
+ throw new RangeError(`Camera sample time must be a finite number of seconds, got ${seconds}.`);
+ }
+}
+
/**
* Turns camera keys into a function from seconds to a camera pose. Keys are either { fps, frames: [[eye, target], ...] },
* sampled with a linear blend between neighbouring frames, or the keys createCameraPath takes (duration, eye, target, fov...).
@@ -35,7 +42,7 @@ export function createCameraSampler(keys) {
return {
duration: frames.length / framesPerSecond,
sample(seconds) {
- if (!Number.isFinite(seconds)) throw new RangeError(`Camera sample time must be a finite number of seconds, got ${seconds}.`);
+ requireFiniteSeconds(seconds);
const position = Math.max(0, Math.min(frames.length - 1, seconds * framesPerSecond));
const before = Math.floor(position);
const after = Math.min(frames.length - 1, before + 1);
@@ -51,6 +58,7 @@ export function createCameraSampler(keys) {
return {
duration: keys.duration,
sample(seconds) {
+ requireFiniteSeconds(seconds);
const pose = path.sample(Math.min(seconds, keys.duration));
return { eye: Array.from(pose.eye), target: Array.from(pose.target), up: Array.from(pose.up), fov: pose.fov };
},
diff --git a/tests/camera-path.test.js b/tests/camera-path.test.js
index 3008f682..f7051616 100644
--- a/tests/camera-path.test.js
+++ b/tests/camera-path.test.js
@@ -99,3 +99,11 @@ test('looping and cubic lens overshoot obey camera domain constraints', () => {
assert.throws(() => createCameraPath({ duration: 1, eye: [0, 4, 0] }), /parallel/);
assert.throws(() => createCameraPath({ duration: 1, eye: [0, 0, 4], far: 0.05 }), /clipping/);
});
+
+test('a non-finite roll override names the roll, not the up vector (#2358)', () => {
+ const path = createCameraPath({ duration: 1, eye: [0, 0, 5] });
+ for (const roll of [NaN, Infinity, 'a']) {
+ assert.throws(() => path.sample(0, undefined, { overrides: { roll } }), /Camera roll must be a finite number of radians/);
+ }
+ assert.doesNotThrow(() => path.sample(0, undefined, { overrides: { roll: 0.5 } }));
+});
diff --git a/tests/camera-track.test.js b/tests/camera-track.test.js
index 6ef206b7..dea19662 100644
--- a/tests/camera-track.test.js
+++ b/tests/camera-track.test.js
@@ -26,3 +26,18 @@ test('createCameraSampler rejects frames whose eye equals the target and an fps
assert.throws(() => createCameraSampler({ frames: sameEyeAndTarget }), /frames\[1\] eye and target are the same point/);
assert.throws(() => createCameraSampler({ fps: 1e-320, frames: twoFrames }), /too small for 2 frames to have a finite duration/);
});
+
+test('createCameraSampler rejects a sparse frames array naming the empty index (#2359)', () => {
+ const sparse = [, [[0, 0, 1], [0, 0, 0]]];
+ assert.throws(() => createCameraSampler({ frames: sparse }), /frames\[0\] must be \[eye, target\]/);
+});
+
+test('both sampler forms reject a sample time that is not a finite number (#2360)', () => {
+ const pathKeys = { duration: 2, eye: [0, 0, 5], target: [0, 0, 0] };
+ for (const sampler of [createCameraSampler(pathKeys), createCameraSampler({ frames: twoFrames })]) {
+ for (const bad of [null, '1', undefined, Infinity, -Infinity, NaN]) {
+ assert.throws(() => sampler.sample(bad), /finite number of seconds/, `time ${String(bad)}`);
+ }
+ assert.doesNotThrow(() => sampler.sample(1));
+ }
+});