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: Vector3.setFromMatrixScale, as three.js has it (#123)

Kevin Rajan9fcce121fdbb
diff --git a/packages/scene/src/math.ts b/packages/scene/src/math.ts
index 1016557..90ccd6c 100644
--- a/packages/scene/src/math.ts
+++ b/packages/scene/src/math.ts
@@ -98,6 +98,15 @@ export class Vector3 {
setFromMatrixPosition(m: Matrix4): this {
return this.set(m.elements[12], m.elements[13], m.elements[14]);
}
+ /** The magnitudes of a transformation matrix's three basis columns (three.js r187). */
+ setFromMatrixScale(m: Matrix4): this {
+ const e = m.elements;
+ return this.set(
+ Math.hypot(e[0], e[1], e[2]),
+ Math.hypot(e[4], e[5], e[6]),
+ Math.hypot(e[8], e[9], e[10]),
+ );
+ }
toArray(): [number, number, number] {
return [this.x, this.y, this.z];
}
diff --git a/packages/scene/test/three-compat-vector.test.ts b/packages/scene/test/three-compat-vector.test.ts
new file mode 100644
index 0000000..b0b09d0
--- /dev/null
+++ b/packages/scene/test/three-compat-vector.test.ts
@@ -0,0 +1,25 @@
+// three.js compatibility: Vector3.setFromMatrixScale, 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("Vector3: setFromMatrixScale reads transformed column lengths, as three.js does", () => {
+ const transformed = new THREE.Matrix4().set(
+ 0, -12, 0, 9,
+ 3, 0, 0, -4,
+ 0, 0, -5, 7,
+ 0, 0, 0, 1,
+ );
+ const result = new THREE.Vector3(99, 99, 99);
+ assert.equal(result.setFromMatrixScale(transformed), result, "mutates and returns the receiver");
+ assert.deepEqual(result.toArray(), [3, 12, 5], "column magnitudes, not rows or signed scales");
+ assert.deepEqual(result.setFromMatrixScale(new THREE.Matrix4()).toArray(), [1, 1, 1], "identity");
+
+ const shear = new THREE.Matrix4().set(
+ 3, 4, 0, 0,
+ 0, 0, 12, 0,
+ 0, 0, 5, 0,
+ 0, 0, 0, 1,
+ );
+ assert.deepEqual(result.setFromMatrixScale(shear).toArray(), [3, 4, 13], "column lengths even for shear");
+});