Skip to content

The native runtime for three.js: three.js, down to the metal. 45× smaller than Electron, on a Sony PS3, and path-traced at 60 fps on ray-tracing GPUs.

Scene: PerspectiveCamera.setFocalLength and filmGauge, as three.js has them (#116)

Kevin Rajan84abb9b02906
diff --git a/packages/scene/src/core.ts b/packages/scene/src/core.ts
index 83f8695..fce557e 100644
--- a/packages/scene/src/core.ts
+++ b/packages/scene/src/core.ts
@@ -2102,6 +2102,8 @@ export class PerspectiveCamera extends Object3D {
override type = "PerspectiveCamera";
/** Vertical FOV in degrees. */
fov: number;
+ /** Film gauge in millimeters (three.js defaults to 35). */
+ filmGauge = 35;
aspect: number;
near: number;
far: number;
@@ -2136,6 +2138,12 @@ export class PerspectiveCamera extends Object3D {
this.exposure = { mode: "auto", compensation, minEv100, maxEv100 };
return this;
}
+ /** Set the vertical FOV from focal length, using the film height for this aspect ratio (three.js). */
+ setFocalLength(focalLength: number): void {
+ const filmHeight = this.filmGauge / Math.max(this.aspect, 1);
+ this.fov = (2 * Math.atan(0.5 * filmHeight / focalLength) * 180) / Math.PI;
+ this.updateProjectionMatrix();
+ }
/** three.js API; the engine reads fov / aspect / near / far as they are. */
updateProjectionMatrix(): void {}
/** As Object3D's, exposure and depth of field copied rather than shared (a default exposure stays the default). */
diff --git a/packages/scene/test/three-compat-camera.test.ts b/packages/scene/test/three-compat-camera.test.ts
new file mode 100644
index 0000000..dde67bf
--- /dev/null
+++ b/packages/scene/test/three-compat-camera.test.ts
@@ -0,0 +1,16 @@
+// three.js compatibility: PerspectiveCamera.setFocalLength, as three.js r187 has it (the snippet-level suite is three-compat.test.ts).
+import { test } from "node:test";
+import assert from "node:assert/strict";
+import * as THREE from "../src/index.ts";
+
+test("PerspectiveCamera.setFocalLength uses film height and aspect, as three.js does", () => {
+ const camera = new THREE.PerspectiveCamera(50, 2, 0.1, 100);
+ camera.setFocalLength(17.5);
+ assert.equal(camera.filmGauge, 35, "three.js defaults to a 35 mm film gauge");
+ assert.ok(Math.abs(camera.fov - 53.13010235415598) < 1e-10, "landscape: 17.5 mm film height at 17.5 mm focal length");
+
+ camera.aspect = 0.5;
+ camera.filmGauge = 70;
+ camera.setFocalLength(35);
+ assert.ok(Math.abs(camera.fov - 90) < 1e-10, "portrait: full 70 mm gauge at 35 mm focal length");
+});