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: Object3D.lookAt aims ambient and hemisphere lights with -Z, as three.js does (#121)

Kevin Rajan8557c3ecd46e
diff --git a/packages/scene/src/core.ts b/packages/scene/src/core.ts
index fa9995a..c940b15 100644
--- a/packages/scene/src/core.ts
+++ b/packages/scene/src/core.ts
@@ -666,7 +666,8 @@ export class Object3D {
this.updateWorldMatrix(true, false);
const pos = new Vector3().setFromMatrixPosition(this.matrixWorld);
const m = new Matrix4();
- if (this.looksDownNegativeZ) m.lookAt(pos, target, this.up);
+ // Ambient and hemisphere lights are isLight objects without inheriting our Light base.
+ if (this.looksDownNegativeZ || ("isLight" in this && this.isLight === true)) m.lookAt(pos, target, this.up);
else m.lookAt(target, pos, this.up);
this.quaternion.setFromRotationMatrix(m);
if (this.parent) {
diff --git a/packages/scene/test/three-compat-lookat.test.ts b/packages/scene/test/three-compat-lookat.test.ts
new file mode 100644
index 0000000..f620876
--- /dev/null
+++ b/packages/scene/test/three-compat-lookat.test.ts
@@ -0,0 +1,22 @@
+// three.js compatibility: Object3D.lookAt on ambient and hemisphere lights, 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("Object3D.lookAt: ambient and hemisphere lights aim local -Z at the target", () => {
+ const target = new THREE.Vector3(0, 0, 5);
+ const forward = new THREE.Vector3(0, 0, 1);
+ const ambient = new THREE.AmbientLight();
+ const hemisphere = new THREE.HemisphereLight();
+ // three.js starts HemisphereLight at (0, 1, 0); put both at the origin so the expected direction is +Z in both.
+ for (const light of [ambient, hemisphere]) light.position.set(0, 0, 0);
+ ambient.lookAt(target);
+ hemisphere.lookAt(0, 0, 5);
+ for (const light of [ambient, hemisphere]) {
+ const direction = new THREE.Vector3(0, 0, -1).applyQuaternion(light.quaternion);
+ assert.ok(direction.distanceTo(forward) < 1e-12, `${light.type}: local -Z must face the target`);
+ }
+ const mesh = new THREE.Object3D();
+ mesh.lookAt(target);
+ assert.ok(new THREE.Vector3(0, 0, 1).applyQuaternion(mesh.quaternion).distanceTo(forward) < 1e-12, "ordinary objects still face +Z");
+});