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: Quaternion.setFromUnitVectors, as three.js has it (#144)

Kevin Rajan9d69a19911ad
diff --git a/packages/scene/src/math.ts b/packages/scene/src/math.ts
index ff61f6e..28a10f6 100644
--- a/packages/scene/src/math.ts
+++ b/packages/scene/src/math.ts
@@ -168,6 +168,28 @@ export class Quaternion {
const s = Math.sin(h);
return this.set(axis.x * s, axis.y * s, axis.z * s, Math.cos(h));
}
+ /** Rotation between normalized directions, including antiparallel vectors (three.js r187). */
+ setFromUnitVectors(vFrom: Vector3, vTo: Vector3): this {
+ let r = vFrom.dot(vTo) + 1;
+ if (r < 1e-8) {
+ r = 0;
+ if (Math.abs(vFrom.x) > Math.abs(vFrom.z)) {
+ this._x = -vFrom.y;
+ this._y = vFrom.x;
+ this._z = 0;
+ } else {
+ this._x = 0;
+ this._y = -vFrom.z;
+ this._z = vFrom.y;
+ }
+ } else {
+ this._x = vFrom.y * vTo.z - vFrom.z * vTo.y;
+ this._y = vFrom.z * vTo.x - vFrom.x * vTo.z;
+ this._z = vFrom.x * vTo.y - vFrom.y * vTo.x;
+ }
+ this._w = r;
+ return this.normalize();
+ }
setFromEuler(e: Euler, notify = true): this {
const c1 = Math.cos(e.x / 2), c2 = Math.cos(e.y / 2), c3 = Math.cos(e.z / 2);
const s1 = Math.sin(e.x / 2), s2 = Math.sin(e.y / 2), s3 = Math.sin(e.z / 2);
diff --git a/packages/scene/test/three-compat-quaternion-ops.test.ts b/packages/scene/test/three-compat-quaternion-ops.test.ts
new file mode 100644
index 0000000..9b76228
--- /dev/null
+++ b/packages/scene/test/three-compat-quaternion-ops.test.ts
@@ -0,0 +1,33 @@
+// three.js r187 parity for Quaternion.setFromUnitVectors, including antiparallel directions.
+import { test } from "node:test";
+import assert from "node:assert/strict";
+import * as THREE from "../src/index.ts";
+
+test("Quaternion.setFromUnitVectors: quarter-turn, opposite axes, and identity", () => {
+ const x = new THREE.Vector3(1, 0, 0);
+ const y = new THREE.Vector3(0, 1, 0);
+ const oppositeX = new THREE.Vector3(-1, 0, 0);
+ const z = new THREE.Vector3(0, 0, 1);
+ const oppositeZ = new THREE.Vector3(0, 0, -1);
+
+ const quarterTurn = new THREE.Quaternion();
+ assert.equal(quarterTurn.setFromUnitVectors(x, y), quarterTurn, "returns its receiver");
+ assert.ok(Math.abs(quarterTurn.z - Math.SQRT1_2) < 1e-12);
+ assert.ok(Math.abs(quarterTurn.w - Math.SQRT1_2) < 1e-12);
+ assert.ok(x.clone().applyQuaternion(quarterTurn).distanceTo(y) < 1e-12);
+
+ const xOpposite = new THREE.Quaternion().setFromUnitVectors(x, oppositeX);
+ assert.ok(Math.abs(xOpposite.y - 1) < 1e-12, "the X-axis case selects a Y rotation");
+ assert.ok(Math.abs(xOpposite.w) < 1e-12);
+ assert.ok(Math.abs(xOpposite.dot(xOpposite) - 1) < 1e-12);
+ assert.ok(x.clone().applyQuaternion(xOpposite).distanceTo(oppositeX) < 1e-12);
+
+ const zOpposite = new THREE.Quaternion().setFromUnitVectors(z, oppositeZ);
+ assert.ok(Math.abs(zOpposite.y + 1) < 1e-12, "the Z-axis case selects negative Y");
+ assert.ok(Math.abs(zOpposite.w) < 1e-12);
+ assert.ok(z.clone().applyQuaternion(zOpposite).distanceTo(oppositeZ) < 1e-12);
+
+ const unchanged = new THREE.Quaternion().setFromUnitVectors(x, x);
+ assert.ok(unchanged.angleTo(new THREE.Quaternion()) < 1e-12);
+ assert.ok(Math.abs(unchanged.dot(unchanged) - 1) < 1e-12);
+});