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.setRotationFromMatrix, as three.js has it (#110)

Kevin Rajand40224f41781
diff --git a/packages/scene/src/core.ts b/packages/scene/src/core.ts
index 2678382..afbefdb 100644
--- a/packages/scene/src/core.ts
+++ b/packages/scene/src/core.ts
@@ -804,6 +804,11 @@ export class Object3D {
this.quaternion.setFromEuler(e);
return this;
}
+ /** Sets the rotation from the upper 3x3 of `m`, which is assumed unscaled; translation is ignored (three.js). */
+ setRotationFromMatrix(m: Matrix4): this {
+ this.quaternion.setFromRotationMatrix(m);
+ return this;
+ }
/** Applies `m` to the object's transform (three.js). */
applyMatrix4(m: Matrix4): this {
if (this.matrixAutoUpdate) this.updateMatrix();
diff --git a/packages/scene/test/three-compat.test.ts b/packages/scene/test/three-compat.test.ts
index f858648..aa298ce 100644
--- a/packages/scene/test/three-compat.test.ts
+++ b/packages/scene/test/three-compat.test.ts
@@ -290,3 +290,20 @@ test("Bugbot on #125 (2): setAutoExposure() with its defaults is set; a cloned c
const cam = ofType(calls, "object").map((c) => c[2] as ObjectDesc).find((d) => d.kind === "camera") as { camera: { exposure: { mode: string } } };
assert.equal(cam.camera.exposure.mode, "auto");
});
+
+test("Object3D.setRotationFromMatrix: replaces the rotation and ignores translation, as three.js does", () => {
+ const object = new THREE.Object3D();
+ object.position.set(2, 3, 4);
+ object.rotateX(Math.PI / 3);
+ const matrix = new THREE.Matrix4().makeRotationZ(Math.PI / 2).setPosition(10, 20, 30);
+
+ object.setRotationFromMatrix(matrix);
+ assert.ok(new THREE.Vector3(1, 0, 0).applyQuaternion(object.quaternion).distanceTo(new THREE.Vector3(0, 1, 0)) < 1e-9,
+ "Z +90 degrees replaces the previous X rotation");
+ assert.ok(Math.abs(object.rotation.z - Math.PI / 2) < 1e-9, "Euler rotation stays synchronized");
+ assert.deepEqual(object.position.toArray(), [2, 3, 4], "matrix translation does not move the object");
+
+ object.translateX(2);
+ assert.ok(object.position.distanceTo(new THREE.Vector3(2, 5, 4)) < 1e-9,
+ "subsequent local movement uses the extracted rotation");
+});